summaryrefslogtreecommitdiff
path: root/world/ui.scm
blob: 73bfcc85732230e806b571373bc5d28a153400f8 (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
;; TODO make this the only platform specific thing
;; Should remember the latest screen to be able to only reprint differences

(define terminal-size #f)
(define current-port #f)
(define scroll 0)

(define (scroll-down)
  (set! scroll (+ 3 scroll)))
(define (scroll-up)
  (set! scroll (- scroll 3))
  (when (> 0 scroll) (set! scroll 0)))

(define (get-terminal-size)
  (get-terminal-dimensions current-port))

(define (control c)
  (integer->char (bitwise-and #b00011111 (char->integer c))))


(define (redisplay! table)
  ; TODO: This is currently utter slow!
  "I need to remove the arguments from it... Probably call-with-window later"
  (erase-screen!)
  (for-each
    (lambda (line-number)
      (let ((line (piece-table-line table (+ line-number scroll))))
        (move-cursor! 0 line-number)
        (cond
          ((eof-object? line)
           (write-char #\~))
          ((string? line)
           (write-string line)))))
    (iota (car terminal-size))))

(define (tui-loop)
  (define table (make-piece-table "hola\nhola"))
  (redisplay! table)
  (call/cc
    (lambda (exit)
      (let loop ((char (read-char)))
        (cond
          ; TODO: Assign characters and combinations symbolic names and
          ; pass that to the core?
          ((char=? (control #\q) char) (exit))
          ((char=? (control #\d) char) (scroll-down))
          ((char=? (control #\u) char) (scroll-up))
          (else (piece-table-insert! table
                                     (piece-table-text-length table)
                                     (string char)
                                     'normal)))
        (redisplay! table)
        (loop (read-char))))))

(define (call-with-tui thunk)
  (dynamic-wind
    tui-initialize!
    (lambda ()
      (call-with-input-file "/dev/tty"
        (lambda (p)
          (set! current-port p)
          (set! terminal-size (get-terminal-size))
          (with-raw-io p thunk))))
    tui-deinitialize!))

(define (run arguments)
  (call-with-tui tui-loop))