totally redo the line interface, go and pagination commands
This commit is contained in:
55
gem300.rkt
55
gem300.rkt
@@ -3,22 +3,51 @@
|
|||||||
(require (prefix-in net: "net.rkt")
|
(require (prefix-in net: "net.rkt")
|
||||||
(prefix-in gmi: "gmi.rkt"))
|
(prefix-in gmi: "gmi.rkt"))
|
||||||
|
|
||||||
(define commands
|
;; global state variable which will be an input port containing the
|
||||||
(list
|
;; rendered out gemtext document as it shall be shown to the user
|
||||||
(cons "default" (lambda (line) (void)))))
|
(define document-buffer null)
|
||||||
|
|
||||||
(define (dispatch-command line)
|
(define (go-cmd url)
|
||||||
(let ([split (string-split line " ")])
|
(if (non-empty-string? url)
|
||||||
|
(let-values ([(status meta c-in) (net:get url)])
|
||||||
|
(let-values ([(document) (gmi:parse (port->lines c-in))]
|
||||||
|
[(db-in db-out) (make-pipe #f)])
|
||||||
|
|
||||||
(let ([cmd (assoc (cond [(null? split) "default"]
|
(set! document-buffer db-in)
|
||||||
[else (first split)])
|
(parameterize ([current-output-port db-out])
|
||||||
commands)])
|
(gmi:render document))
|
||||||
(if cmd
|
|
||||||
((cdr cmd) (string-join (cdr split)))
|
|
||||||
(displayln "no such command")))))
|
|
||||||
|
|
||||||
|
(let ([remaining (pipe-content-length db-in)])
|
||||||
|
(printf "document retrieved. ~a bytes\n" remaining))
|
||||||
|
|
||||||
|
(next-cmd)))
|
||||||
|
|
||||||
|
(displayln "go where?")))
|
||||||
|
|
||||||
|
(define (next-cmd)
|
||||||
|
(define (iter depth)
|
||||||
|
(when (> depth 0)
|
||||||
|
(let ()
|
||||||
|
(displayln (read-line document-buffer))
|
||||||
|
(iter (sub1 depth)))))
|
||||||
|
|
||||||
|
(iter 10)
|
||||||
|
(newline)
|
||||||
|
(let ([remaining (pipe-content-length document-buffer)])
|
||||||
|
(printf "~a bytes remaining\n" remaining)))
|
||||||
|
|
||||||
(define (repl)
|
(define (repl)
|
||||||
(display "G300> ")
|
(display "G-300 > ")
|
||||||
(dispatch-command (read-line))
|
|
||||||
|
(let ([matches (regexp-match #px"(\\w+)\\s*(.*)" (read-line))])
|
||||||
|
(cond
|
||||||
|
;; nothing was given. default command
|
||||||
|
[(not matches)
|
||||||
|
(next-cmd)]
|
||||||
|
|
||||||
|
;; go command
|
||||||
|
[(string=? (cadr matches) "go")
|
||||||
|
(go-cmd (caddr matches))]))
|
||||||
|
|
||||||
|
|
||||||
(repl))
|
(repl))
|
||||||
|
Reference in New Issue
Block a user