aboutsummaryrefslogtreecommitdiffstats
path: root/oni/home/services/rofi.scm
blob: b3fa65fa6fa9e9877eb693c9bdaf8a4f9f340618 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
(define-module (oni home services rofi)
  #:use-module (gnu services configuration)
  #:use-module (gnu packages xdisorg)
  #:use-module (gnu home services)
  #:use-module (gnu home services utils)
  #:use-module (guix packages)
  #:use-module (guix gexp)
  #:use-module (guix records)
  #:use-module (guix i18n)
  #:use-module (guix modules)
  #:use-module (guix diagnostics)
  #:use-module (oni home services xbindkeys)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-26)
  #:use-module (ice-9 match)
  #:use-module (ice-9 regex)
  #:use-module (ice-9 common-list)

  #:export (home-rofi-service-type
            home-rofi-configuration

            home-rofi-default-service))

;;; This module has been copied from
;;; https://github.com/nouun/jayu/blob/92ff6629da686f0ddc96a4d6cb2a657c76795bc6/jayu/home/services/xdisorg.scm

(define-configuration/no-serialization home-rofi-configuration
  (package
   (package rofi)
   "Package to use for setting Rofi")
  (config (alist '())
          "rofi config")
  (theme (alist '())
         "theme config"))

(define color-regexp
  (make-regexp
   (string-append
    "("
    ;; #RGB
    "#[a-zA-Z0-9]{3}" "|"
    ;; #RGBA
    "#[a-zA-Z0-9]{4}" "|"
    ;; #RRGGBB
    "#[a-zA-Z0-9]{6}" "|"
    ;; #RRGGBBAA
    "#[a-zA-Z0-9]{8}" "|"
    ;; rgb(123, 123, 123)
    "rgba?. *[0-9]{1,3} *%? *, *[0-9]{1,3} *%? *, *[0-9]{1,3} *%? *" "|"
    ;; rgba(123, 123, 123, 0.1)
    "rgba?. *[0-9]{1,3} *%? *, *[0-9]{1,3} *%? *, *[0-9]{1,3} *%? *, [0-9]\\.[0-9]*" "|"
    ;; rgba(123, 123, 123, 100%)
    "rgba?. *[0-9]{1,3} *%? *, *[0-9]{1,3} *%? *, *[0-9]{1,3} *%? *, [0-9]{1,3} *% *"
    ;;children
    ")")))

(define (format-rofi-value val)
  (cond
   ((list? val)
    (string-join (map format-rofi-value val) " "))
   ((boolean? val)
    (if val "true" "false"))
   ((number? val)
    (string-append
     (object->string val)
     (if (not (eq? 0 val))
         "px" "")))
   ((symbol? val)
    (object->string val))
   ((string? val)
    (let ((color-match (regexp-exec color-regexp val)))
      (if (or (string-prefix? "@" val)
              (regexp-match? color-match))
          val
          (string-append "\"" val "\""))))
   (else val)))

(define (format-rofi-config config)
  (let ((key (object->string (car config)))
        (value (cdr config)))
    (define (format-children children)
      (string-append
       "[" (string-join (map object->string children) ",") "]"))
    (string-append key ": "
                   (if (eq? 'children (car config))
                       (format-children value)
                       (format-rofi-value value))
                   ";")))

(define (serialize-rofi-config config)
 (list
  (string-append
   "configuration {\n  "
   (string-join (map format-rofi-config config) "\n  ")
   "\n}\n")))

(define (format-rofi-theme theme)
  (let ((keys (map (λ (s)
                     (if (symbol? s)
                         (symbol->string s)
                         (object->string s)))
                   (butlast theme 1)))
        (values (car (list-tail theme (- (length theme) 1)))))
    (string-append (string-join keys ", ")
                   " {\n  "
                   (string-join
                    (map format-rofi-config values)
                    "\n  ")
                   "\n}\n")))

(define (serialize-rofi-theme theme)
  (string-join (map format-rofi-theme theme) "\n"))

(define (home-rofi-files-service conf)
  `(("rofi/config.rasi"
     ,(apply mixed-text-file
        "config.rasi"
        (append
         ;; Main config
         (serialize-rofi-config
          (home-rofi-configuration-config conf))
         ;; Apply theme
         (if (not (eq? (home-rofi-configuration-theme conf) '()))
             (list "\n@import \"guix\"\n") '()))))
    ("rofi/guix.rasi"
     ,(mixed-text-file
        "guix.rasi"
        (serialize-rofi-theme
         (home-rofi-configuration-theme conf))))))

(define (home-rofi-profile-service config)
  (list (home-rofi-configuration-package config)))

(define (home-rofi-extension old-config extension-configs)
  (match old-config
    (($ <home-rofi-configuration> package* config* theme*)
     (home-rofi-configuration
      (package package*)
      (config (append config*
                      (append-map home-rofi-configuration-config
                                  extension-configs)))
      (theme (append theme*
                     (append-map home-rofi-configuration-theme
                                 extension-configs)))))))

(define (add-rofi-xbindkeys-keybindings config)
  (home-xbindkeys-extension
   (keybindings
    '(((mod4 r) . "herbstclient spawn rofi -modi drun -show drun")
      ((mod4 w) . "herbstclient spawn rofi -show window -window-command \"/home/chelys/usr/bin/hlwm-switch-to-window {window}\"")))))

(define home-rofi-service-type
  (service-type (name 'home-rofi)
                (extensions
                 (list (service-extension
                        home-xdg-configuration-files-service-type
                        home-rofi-files-service)
                       (service-extension
                        home-profile-service-type
                        home-rofi-profile-service)
                       (service-extension
                        home-xbindkeys-service-type
                        add-rofi-xbindkeys-keybindings)))
                (compose concatenate)
                (extend home-rofi-extension)
                (default-value (home-rofi-configuration))
                (description "Configure Rofi")))

(define (generate-home-rofi-documentation)
  (generate-documentation
   `((home-rofi-configuration
      ,home-rofi-configuration-fields))
   'home-rofi-configuration))

(define-public home-rofi-default-service
  (service home-rofi-service-type
           (home-rofi-configuration
            (config
             '((kb-cancel . "Escape,Control+g")
               (window-format . "{c}  {t}")))
            (theme
             '((* ((text-color . "#bfbfbf")
                   (background-color . "#3f4242")
                   (lightbg . "#5b6161")
                   (red . "#a85454")
                   (orange . "#faa41a")
                   (blue . "#1f2c3f")

                   (selected-normal-foreground . "@text-color")
                   (normal-foreground . "@text-color")
                   (alternate-normal-background . "@background-color")
                   (selected-urgent-foreground . "@text-color")
                   (urgent-foreground . "@text-color")
                   (alternate-urgent-background . "@background-color")
                   (active-foreground . "@text-color")
                   (selected-active-foreground . "@text-color")
                   (alternate-normal-foreground . "@text-color")
                   (alternate-active-background . "@blue")
                   (bordercolor . "@text-color")
                   (normal-background . "@background-color")
                   (selected-normal-background . "@blue")
                   (separatorcolor . "@orange")
                   (spacing . 2)
                   (urgent-background . "@red")
                   (alternate-urgent-foreground . "@text-color")
                   (selected-urgent-background . "@red")
                   (alternate-active-foreground . "@text-color")
                   (selected-active-background . "@blue")
                   (active-background . "@red")

                   (font . "Fantasque Sans Mono 14")))
               (window ((border . 0)
                        (text-color . "@text-color")
                        (background-color . "rgba(0, 0, 0, 0%)")
                        (padding . 5)
                        (text-color . "@bordercolor")
                        (background-color . "@background-color")))
               (mainbox ((border . 0)
                         (padding . 0)))
               (message ((border . "1px dash 0px 0px")
                         (text-color . "@separatorcolor")
                         (padding . "2px 0px 0px")))
               (textbox ((text-color . "@text-color")))
               (listview ((fixed-height . 0)
                          (border . "2px 0px 0px")
                          (padding . "2px 0px 0px")
                          (text-color . "@separatorcolor")))
               (element ((border . 0)
                         (children "element-icon" "element-text")
                         (spacing . "5px")))
               (element.normal.normal ((text-color . "@normal-foreground")
                                       (background-color . "@normal-background")))
               (element.normal.urgent ((text-color . "@urgent-foreground")
                                       (background-color . "@urgent-background")))
               (element.normal.active ((text-color . "@active-foreground")
                                       (background-color . "@active-background")))
               (element.selected.urgent ((text-color . "@selected-urgent-foreground")
                                         (background-color . "@selected-urgent-background")))
               (element.selected.active ((text-color . "@selected-active-foreground")
                                         (background-color . "@selected-active-foreground")))
               (element.alternate.normal ((text-color . "@alternate-normal-foreground")
                                          (background-color . "@alternate-normal-background")))
               (element.alternate.urgent ((text-color . "@alternate-urgent-foreground")
                                          (background-color . "@alternate-urgent-background")))
               (element.alternate.active ((text-color . "@alternate-active-foreground")
                                          (background-color . "@alternate-active-background")))
               (mode-switcher ((border . "1px dash 0px 0px")))
               (#{button selected}# ((text-color . "@selected-normal-foreground")
                                     (background-color . "@selected-normal-background")))
               (inputbar ((spacing . 0)
                          (border . "0px")
                          (children "prompt" "textbox-prompt-colon" "entry" "case-indicator")))
               (#{button normal}# ((text-color . "@text-color")))
               (text-prompt-color ((expand . #f)
                                   (str . ":")
                                   (margin . "0px 0.3em 0em 0em")
                                   (text-color . "@normal-foreground"))))))))