diff options
| author | Joel Martin <github@martintribe.org> | 2015-01-02 23:55:11 -0600 |
|---|---|---|
| committer | Joel Martin <github@martintribe.org> | 2015-01-09 16:16:56 -0600 |
| commit | 0f352c382c7291b12da7f1800f54b9a27a27ad64 (patch) | |
| tree | d45089a9a498052fb73f34bbf86bb5777020c827 | |
| parent | 96f1845afdb816515bba83ad4bb8fee6bc466557 (diff) | |
| download | mal-0f352c382c7291b12da7f1800f54b9a27a27ad64.tar.gz mal-0f352c382c7291b12da7f1800f54b9a27a27ad64.zip | |
Racket: add readline/line edit support.
| -rw-r--r-- | racket/readline.rkt | 29 |
1 files changed, 23 insertions, 6 deletions
diff --git a/racket/readline.rkt b/racket/readline.rkt index 0a60c17..7f92169 100644 --- a/racket/readline.rkt +++ b/racket/readline.rkt @@ -2,14 +2,31 @@ (provide readline) +(require (prefix-in readline: readline/readline)) + (require "types.rkt") +(define history-loaded #f) +(define HISTORY-FILE (format "~a/.mal-history" (find-system-path 'home-dir))) + +(define (load-history path) + (map + (lambda (line) (readline:add-history line)) + (string-split + (port->string (open-input-file path)) + #px"\n"))) + (define (readline prompt) - (_printf "~a" prompt) - (let ([line (read-line (current-input-port) 'any)]) + (when (not history-loaded) + (set! history-loaded #t) + (load-history HISTORY-FILE)) + (let ([line (readline:readline prompt)]) (if (eq? eof line) nil - line))) - - - + (begin + (readline:add-history line) + (with-output-to-file + HISTORY-FILE + (lambda () (printf "~a~n" line)) + #:exists 'append) + line)))) |
