Skip to content

Commit

Permalink
repl: add readline-persistent-history option
Browse files Browse the repository at this point in the history
  • Loading branch information
willghatch committed Apr 21, 2022
1 parent c85dda5 commit 1165297
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions rash/repl.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@

(define (main)
(define use-readline? (not (equal? (system-type 'os) 'windows)))
(define readline-persistent-history? #t)
(define (cmdline-bool->bool b)
(match (string-downcase b)
["true" #t]
Expand All @@ -143,6 +144,10 @@
["--readline" readline?
"use readline (true or false)"
(set! use-readline? (cmdline-bool->bool readline?))]
["--readline-persistent-history" persistent-history?
"persist history for readline (true or false)"
(set! readline-persistent-history?
(cmdline-bool->bool persistent-history?))]
)

(define input-port-for-repl (current-input-port))
Expand Down Expand Up @@ -177,20 +182,21 @@
(define history-add (dynamic-require 'readline/readline 'add-history))
(define get-preference (dynamic-require 'racket/file 'get-preference))
(define put-preferences (dynamic-require 'racket/file 'put-preferences))
(for ([i (in-range (history-length))])
(history-delete 0))
(define rash-readline-input-history
(get-preference 'rash:repl:readline-input-history (λ () null)))
(for ([h rash-readline-input-history])
(history-add h))
(set! save-readline-history!
(λ ()
;; TODO - I should trim this to a maximum length.
(define rash-history
(for/list ([i (in-range (history-length))])
(history-get i)))
(put-preferences '(rash:repl:readline-input-history)
(list rash-history))))))
(when readline-persistent-history?
(for ([i (in-range (history-length))])
(history-delete 0))
(define rash-readline-input-history
(get-preference 'rash:repl:readline-input-history (λ () null)))
(for ([h rash-readline-input-history])
(history-add h))
(set! save-readline-history!
(λ ()
;; TODO - I should trim this to a maximum length.
(define rash-history
(for/list ([i (in-range (history-length))])
(history-get i)))
(put-preferences '(rash:repl:readline-input-history)
(list rash-history)))))))

(port-count-lines! (current-input-port))
(putenv "SHELL" "rash-repl")
Expand Down

0 comments on commit 1165297

Please sign in to comment.