summaryrefslogtreecommitdiff
path: root/world
diff options
context:
space:
mode:
authorEkaitz Zarraga <ekaitz@elenq.tech>2024-02-13 22:15:11 +0100
committerEkaitz Zarraga <ekaitz@elenq.tech>2024-02-13 22:15:14 +0100
commit6de260026b4de51021b9a31969558955b3b95d62 (patch)
tree2a8646c34f1486942987bcb0343d2d69e04321c4 /world
parentd088ae8cb747ad3a12b243f7ea731cd87fdffaed (diff)
world: ui: make a minimally working example
- It doesn't have a cursor - It's very slow
Diffstat (limited to 'world')
-rw-r--r--world/ui.scm68
-rw-r--r--world/ui.sld3
2 files changed, 61 insertions, 10 deletions
diff --git a/world/ui.scm b/world/ui.scm
index 757e7dd..73bfcc8 100644
--- a/world/ui.scm
+++ b/world/ui.scm
@@ -1,18 +1,66 @@
+;; 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"))
- (let loop ((char (read-char)))
- (move-cursor! 0 0)
- (cond
- ((char=? #\q char) #f)
- (else (piece-table-insert! table 4 "hola" 'normal)
- (erase-screen!)
- (write-string (piece-table->string table))
- (loop (read-char))))))
+ (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 () (with-raw-io (current-input-port) thunk))
+ (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)
diff --git a/world/ui.sld b/world/ui.sld
index b59401e..e9d4e11 100644
--- a/world/ui.sld
+++ b/world/ui.sld
@@ -1,5 +1,8 @@
(define-library (world ui)
(import (scheme base)
+ (scheme file)
+ (srfi 1)
+ (srfi 151)
(world tty-commands)
(par piece-table)) ;; Remove the piece table from here later, and move to main
(cond-expand