Skip to content

Commit b263a87

Browse files
committed
repl: save history to $HOME/.lsc_history
repl: write to .lsc_history only if it exists repl: retain 500 lines of history
1 parent cc05b37 commit b263a87

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

lib/command.js

Lines changed: 29 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/command.ls

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ require! {
33
path
44
fs
55
util
6-
'prelude-ls': {each, break-list}:prelude
6+
'prelude-ls': {each, break-list, lines, unlines, take}:prelude
77
'./options': {parse: parse-options, generate-help}
88
'./util': {name-from-path}
99
'source-map': {SourceNode}
@@ -24,6 +24,10 @@ p = (...args) !->
2424
pp = (x, show-hidden, depth) !->
2525
say util.inspect x, show-hidden, depth, !process.env.NODE_DISABLE_COLORS
2626
ppp = !-> pp it, true, null
27+
file-exists = (path) ->
28+
try
29+
fs.stat-sync path
30+
true
2731

2832
try
2933
o = parse-options args
@@ -237,6 +241,8 @@ switch
237241
# - __^C__: Cancel input if any. Quit otherwise.
238242
# - __??__: <https://github.com/joyent/node/blob/master/lib/readline.js>
239243
!function repl
244+
MAX-HISTORY-SIZE = 500
245+
history-file = path.join process.env.HOME, '/.lsc_history'
240246
code = if repl.infunc then ' ' else ''
241247
cont = 0
242248
rl = require 'readline' .create-interface process.stdin, process.stdout
@@ -251,6 +257,7 @@ switch
251257
_tty-write ...
252258
prompt = 'ls'
253259
prompt += " -#that" if 'b' * !!o.bare + 'c' * !!o.compile
260+
try rl.history = lines <| fs.read-file-sync history-file, 'utf-8' .trim!
254261
LiveScript.history = rl.history if LiveScript?
255262
unless o.compile
256263
module.paths = module.constructor._node-module-paths \
@@ -309,7 +316,11 @@ switch
309316
catch then say e
310317
reset!
311318
process.on 'uncaughtException' !-> say "\n#{ it?stack or it }"
312-
process.on 'exit' !-> rl._tty-write '\r' if code and rl.output.is-TTY
319+
process.on 'exit' !->
320+
rl._tty-write '\r' if code and rl.output.is-TTY
321+
if file-exists history-file
322+
(unlines . take MAX-HISTORY-SIZE) rl.history
323+
|> fs.write-file-sync history-file, _
313324
rl.set-prompt "#prompt> "
314325
rl.prompt!
315326

0 commit comments

Comments
 (0)