aboutsummaryrefslogtreecommitdiff
path: root/racket/readline.rkt
blob: 7f92169f3f57e2219b5c9debb691e104bc64096f (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
#lang racket

(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)
  (when (not history-loaded)
    (set! history-loaded #t)
    (load-history HISTORY-FILE))
  (let ([line (readline:readline prompt)])
    (if (eq? eof line)
      nil
      (begin
        (readline:add-history line)
        (with-output-to-file
          HISTORY-FILE
          (lambda () (printf "~a~n" line))
          #:exists 'append)
        line))))