Skip to content

Commit 0f352c3

Browse files
committed
Racket: add readline/line edit support.
1 parent 96f1845 commit 0f352c3

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

racket/readline.rkt

+23-6
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,31 @@
22

33
(provide readline)
44

5+
(require (prefix-in readline: readline/readline))
6+
57
(require "types.rkt")
68

9+
(define history-loaded #f)
10+
(define HISTORY-FILE (format "~a/.mal-history" (find-system-path 'home-dir)))
11+
12+
(define (load-history path)
13+
(map
14+
(lambda (line) (readline:add-history line))
15+
(string-split
16+
(port->string (open-input-file path))
17+
#px"\n")))
18+
719
(define (readline prompt)
8-
(_printf "~a" prompt)
9-
(let ([line (read-line (current-input-port) 'any)])
20+
(when (not history-loaded)
21+
(set! history-loaded #t)
22+
(load-history HISTORY-FILE))
23+
(let ([line (readline:readline prompt)])
1024
(if (eq? eof line)
1125
nil
12-
line)))
13-
14-
15-
26+
(begin
27+
(readline:add-history line)
28+
(with-output-to-file
29+
HISTORY-FILE
30+
(lambda () (printf "~a~n" line))
31+
#:exists 'append)
32+
line))))

0 commit comments

Comments
 (0)