aboutsummaryrefslogtreecommitdiff
path: root/module/output/terminal.scm
blob: 5abb5f571432ddfc168f083db7bb590df81b26fa (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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
(define-module (output terminal)
  #:use-module (output general)
  #:use-module (srfi srfi-1)
  #:use-module (datetime)
  #:use-module (srfi srfi-26)
  #:use-module (srfi srfi-41)
  #:use-module (srfi srfi-41 util)
  #:use-module (util)
  #:use-module ((util app) :prefix app/)
  #:use-module (vulgar)
  #:use-module (vulgar info)
  #:use-module (vulgar color)
  #:use-module (vulgar components)
  #:use-module (vcomponent group)

  #:use-module (vcomponent)
  #:use-module (vcomponent datetime)

  #:use-module (text util)
  #:use-module (text flow)

  #:use-module (ice-9 format)
  #:use-module (ice-9 readline)
  #:use-module (ice-9 sandbox)
  #:use-module (ice-9 match)

  #:use-module (vulgar termios)

  #:use-module (oop goops)
  #:use-module (oop goops describe)

  #:export (main-loop))

(define-values (height width) (get-terminal-size))

(define (open-in-editor fname)
  (system (string-append (getenv "EDITOR") " " fname)))


(define (box-top intersection line . lengths)
  (reduce (lambda (str done) (string-append done (string intersection) str))
          "" (map (cut make-string <> line) lengths)))

(define* (display-event-table events #:key
                              (cur-event -1)
                              (summary-width 30)
                              (location-width 20))
  (for-each
   (lambda (ev i)
     (display
      (string-append
       (if (datetime? (prop ev 'DTSTART))
           (datetime->string (prop ev 'DTSTART) "~Y-~m-~d ~H:~M:~S")
           ((@ (texinfo string-utils) center-string)
            (date->string (prop ev 'DTSTART))
            19))
       " │ "
       (if (= i cur-event) "\x1b[7m" "")
       (color-escape (prop (parent ev) 'COLOR))
       ;; Summary filter is a hook for the user
       (let ((dirty (prop ev '-X-HNH-DIRTY)))
         (string-append
          (if dirty "* " "")
          ;; TODO reintroduce summary-filter
          (trim-to-width (prop ev 'SUMMARY) (- summary-width
                                               (if dirty 2 0)))))
       STR-RESET
       " │ "
       (if (prop ev 'LOCATION) "" "\x1b[1;30m")
       (trim-to-width
        (or (prop ev 'LOCATION) "INGEN LOKAL") location-width)
       STR-RESET
       "\n")))
   events
   (iota (length events))))

(define (displayln a)
  (display a) (newline))


(define-class <day-view> ()
  (event-set getter: get-event-set
             init-keyword: event-set:)
  (date accessor: view-date
        init-keyword: date:)
  (cur-event accessor: cur-event
             init-value: 0))


(define-method (output (this <day-view>))
  (define date (view-date this))
  (define event-set (get-event-set this))
  (define groups (group-stream event-set))
  (define group (get-groups-between groups date date))
  (define events (group->event-list (stream-car group)))

  (cls)

  (display "== Day View ==\n")

  (display-calendar-header! date)

  ;; display event list
  (let* ((date-width 20)
         (location-width 15)
         (summary-width (- width date-width location-width 6)))
    (displayln
     (box-top #\┬ #\─ date-width (+ summary-width 2) (1+ location-width)))
    (display-event-table
     events
     #:cur-event (cur-event this)
     #:location-width location-width
     #:summary-width summary-width)
    (displayln
     (box-top #\┴ #\─ date-width (+ summary-width 2) (1+ location-width))))

  ;; display highlighted event
  (unless (null? events)
    (let ((ev (list-ref events (cur-event this))))
      (format #t "~a~%~%  ~a~%~%~a\x1b[1mStart:\x1b[m ~a	\x1b[1mSlut:\x1b[m ~a~%~%~a~%"
              (prop ev '-X-HNH-FILENAME)
              (prop ev 'SUMMARY)
              (or (and=> (prop ev 'LOCATION)
                         (cut string-append "\x1b[1mPlats:\x1b[m " <> "\n")) "")
              ;; NOTE RFC 5545 says that DTSTART and DTEND MUST
              ;; have the same type. However we believe that is
              ;; another story.
              (let ((start (prop ev 'DTSTART)))
                (if (datetime? start)
                    (datetime->string (prop ev 'DTSTART) "~Y-~m-~d ~H:~M:~S")
                    (date->string start)))
              (let ((end (prop ev 'DTEND)))
                (if (datetime? end)
                    (datetime->string (prop ev 'DTEND) "~Y-~m-~d ~H:~M:~S")
                    (date->string end)))
              (unlines (take-to (flow-text (or (prop ev 'DESCRIPTION) "")
                                           #:width (min 70 width))
                                (- height 8 5 (length events) 5)))))))

(define-method (input (this <day-view>) char)
  (define date (view-date this))
  (define event-set (get-event-set this))
  (define groups (group-stream event-set))
  (define group (get-groups-between groups date date))
  (define events (group->event-list (stream-car group)))

  (case char
    ((#\L #\l)
     (set! (view-date this) (add-day date)
           (cur-event this) 0))
    ((#\h #\H)
     (set! (view-date this) (remove-day date)
           (cur-event this) 0))
    ((#\t)
     ;; TODO this should be local time
     ;; currently it's UTC (maybe?)
     (set! (view-date this) (current-date)
           (cur-event this) 0))
    ((#\j #\J) (unless (= (cur-event this) (1- (length events)))
                 (set! (cur-event this) = (+ 1))))
    ((#\k #\K) (unless (= (cur-event this) 0)
                 (set! (cur-event this) = (- 1))))
    ((#\g) (set! (cur-event this) 0))
    ((#\G) (set! (cur-event this) (1- (length events))))
    ((#\q) '(pop))
    ((#\() (set-cursor-pos 0 (1- height))
     (let* ((attr (make-termios))
            (search-term #f))
       (tcgetattr! attr)
       (set! (lflag attr) (logior ECHO (lflag attr)))
       (tcsetattr! attr)
       (set! search-term (readline "search: "))
       (set! (lflag attr) (logand (lognot ECHO) (lflag attr)))
       (tcsetattr! attr)
       `(push ,(search-view search-term (event-set this)))
       ))))

(define (day-view event-set date)
  (make <day-view> event-set: event-set date: date))

(define-class <search-view> ()
  (event-set getter: event-set init-keyword: event-set)
  (search-result getter: search-result)
  (search-term getter: search-term
               init-keyword: search-term:))

(define (search-view search-term event-set)
  (make <search-view> search-term: search-term event-set: event-set))

(define (prepare-string str)

  (define missing-parenthesis-count
   (string-fold (lambda (char count)
                  (case char
                    ((#\() (1+ count))
                    ((#\)) (1- count))
                    (else count)))
                0 str))

  (string-append str (make-string missing-parenthesis-count #\))))

(define-method (initialize (this <search-view>) args)
  (next-method)
  ;; (display (search-term this)) (newline)
  (slot-set! this 'search-result
             (stream-paginate
              (stream-filter
               (eval (print-and-return
                      `(lambda (event) ,(call-with-input-string
                                       (prepare-string (search-term this))
                                     read)))
                     (make-sandbox-module
                      `(
                        ((vcomponent base) prop)
                        ((ice-9 regex) string-match)
                        #;
                        ((datetime) ,@(module-map (lambda (a . _) a) ; ;
                        (resolve-module '(datetime))))
                        ,@all-pure-bindings)
                      ))
               (event-set this))))
  ;; (define current-page 0)
  ;; (define current-entry 0)
  )

(define-method (output (this <search-view>))
  (define page
    (catch #t
      (lambda ()
       (call-with-time-limit
        1
        (lambda () (stream->list (stream-ref (search-result this) 0)))
        (lambda _ (throw 'timed-out))))
      (lambda (err . args)
        (display (cons err args) (current-error-port))
        (newline  (current-error-port))
        'timed-out ; when search took to long
        'unbound-variable ; when search term has unbound variables
        '()

        )
      ))

  (cls)

  (display "== Search View ==\n")
  (for entry in page
       (let ((start (prop entry 'DTSTART)))
         (display
          (if (date? start)
              (date->string start "~Y-~m-~d ")
              (datetime->string start "~Y-~m-~d ~H:~M "))))
       (display (prop entry 'SUMMARY))
       (newline)))

(define-method (input (this <search-view>) char)
  (case char
    ((#\q) '(pop))))

(app/define-method (main-loop date)
  (define state (list (day-view (app/getf 'event-set) date)))

  (while #t
    (output (car state))

    (let ((char (read-char)))

      (when (eof-object? char)
        (break))

      (match (input (car state) char)
        (('push new-state) (set! state (cons new-state state)))
        (('pop)
         (set! state (cdr state))
         (when (null? state) (break)))
        (('break) (break))
        (else
         'continue)))))