aboutsummaryrefslogtreecommitdiffstats
path: root/bin/habit
blob: 5afb4cedce73590f9078be6c853e4854c2bf7776 (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
#!/usr/bin/guile \
-e main-rofi -s
!#

(add-to-load-path "/home/titan/dev/dotfiles/lib/guile/")

(use-modules (ice-9 rdelim)
             (ice-9 popen)
             (ice-9 ftw)
             (ice-9 format)
             (ice-9 and-let-star)
             (utils))

(define habits-dir (expand-file "~/org/habits/"))

(define (get-habit-files dir)
  (scandir dir (lambda (file) (string-suffix? ".dat" file))))

(define (prepare-options files)
  (string-join (map (lambda (name) (string-drop-right name 4)) files) "\n"))

(define (rofi-capture-option options)
  (let* ((cmd (format #f "echo -e ~s | rofi -dmenu" options))
         (port (open-input-pipe cmd))
         (option (read-line port)))
    (close-pipe port)
    (if (eof-object? option) #f option)))

(define (rofi-capture-habit-quantity habit)
  (let* ((cmd (format #f "echo 1 | rofi -dmenu -p 'Add to ~a'" habit))
         (port (open-input-pipe cmd))
         (quantity (read-line port)))
    (close-pipe port)
    (if (eof-object? quantity) #f quantity)))

(define (main-rofi args)
  (and-let* ((habit (rofi-capture-option (prepare-options (get-habit-files habits-dir))))
             (quantity (rofi-capture-habit-quantity habit))
             (file-out (open-file (string-append habits-dir habit ".dat") "a")))
    (format file-out "~d:~a\n" (current-time) quantity)
    (close-port file-out)))