Skip to content

Commit

Permalink
Add --no-repl-history option.
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Bataev committed Apr 5, 2020
1 parent 9de9f94 commit e84b1d8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,8 @@ func usage(out io.Writer) {
fmt.Fprintln(out, " After failure processing --eval or --file, drop into repl instead of exiting.")
fmt.Fprintln(out, " --no-readline")
fmt.Fprintln(out, " Disable readline functionality in the repl. Useful when using rlwrap.")
fmt.Fprintln(out, " --no-repl-history")
fmt.Fprintln(out, " Do not read or save repl command history to a file.")
fmt.Fprintln(out, " --working-dir <directory>")
fmt.Fprintln(out, " Specify directory to lint or working directory for lint configuration if linting single file (requires --lint).")
fmt.Fprintln(out, " --report-globally-unused")
Expand Down Expand Up @@ -429,6 +431,7 @@ var (
cpuProfileRateFlag bool
memProfileName string
noReadline bool
noReplHistory bool
exitToRepl bool
errorToRepl bool
)
Expand Down Expand Up @@ -576,6 +579,8 @@ func parseArgs(args []string) {
}
case "--no-readline":
noReadline = true
case "--no-repl-history":
noReplHistory = true
case "--exit-to-repl":
exitToRepl = true
if i < length-1 && notOption(args[i+1]) {
Expand Down Expand Up @@ -708,6 +713,7 @@ func main() {
fmt.Fprintf(debugOut, "replSocket=%v\n", replSocket)
fmt.Fprintf(debugOut, "classPath=%v\n", classPath)
fmt.Fprintf(debugOut, "noReadline=%v\n", noReadline)
fmt.Fprintf(debugOut, "noReplHistory=%v\n", noReplHistory)
fmt.Fprintf(debugOut, "filename=%v\n", filename)
fmt.Fprintf(debugOut, "remainingArgs=%v\n", remainingArgs)
fmt.Fprintf(debugOut, "exitToRepl=%v\n", exitToRepl)
Expand Down
35 changes: 20 additions & 15 deletions repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ func completer(line string, pos int) (head string, c []string, tail string) {
return
}

func saveReplHistory(rl *liner.State, filename string) {
if filename == "" {
return
}
if f, err := os.Create(filename); err == nil {
rl.WriteHistory(f)
f.Close()
}
}

func repl(phase Phase) {
ProcessReplData()
GLOBAL_ENV.FindNamespace(MakeSymbol("user")).ReferAll(GLOBAL_ENV.FindNamespace(MakeSymbol("joker.repl")))
Expand All @@ -80,22 +90,22 @@ func repl(phase Phase) {
fmt.Fprintf(Stderr, "WARNING: could not create %s \n", jokerd)
}
}
historyFilename = filepath.Join(jokerd, ".repl_history")
if !noReplHistory {
historyFilename = filepath.Join(jokerd, ".repl_history")
}
rl = liner.NewLiner()
OnExit(func() {
if f, err := os.Create(historyFilename); err == nil {
rl.WriteHistory(f)
f.Close()
}
rl.Close()
saveReplHistory(rl, historyFilename)
})
defer rl.Close()
rl.SetCtrlCAborts(true)
rl.SetWordCompleter(completer)

if f, err := os.Open(historyFilename); err == nil {
rl.ReadHistory(f)
f.Close()
if !noReplHistory {
if f, err := os.Open(historyFilename); err == nil {
rl.ReadHistory(f)
f.Close()
}
}

runeReader = NewLineRuneReader(rl)
Expand All @@ -117,12 +127,7 @@ func repl(phase Phase) {
runeReader.(*LineRuneReader).Prompt = (GLOBAL_ENV.CurrentNamespace().Name.ToString(false) + "=> ")
}
if processReplCommand(reader, phase, parseContext, replContext) {
if !noReadline {
if f, err := os.Create(historyFilename); err == nil {
rl.WriteHistory(f)
f.Close()
}
}
saveReplHistory(rl, historyFilename)
return
}
}
Expand Down

0 comments on commit e84b1d8

Please sign in to comment.