From e4cae6faeaf3e0446f835232645cca16da0efe29 Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Fri, 9 Aug 2024 09:16:59 -0700 Subject: [PATCH] Add AutoHistory(false) option (default remains true) to not automatically add to the History (#4) * Add AutoHistory(false) option (default remains true) to not automatically add to the History and let the caller of ReadLine() decide, for instance to only add validated commands --- terminal.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/terminal.go b/terminal.go index 74ce989..0b2e0c3 100644 --- a/terminal.go +++ b/terminal.go @@ -94,6 +94,10 @@ type Terminal struct { // the incomplete, initial line. That value is stored in // historyPending. historyPending string + // autoHistory, if true, causes lines to be automatically added to the history. + // If false, call AddToHistory to add lines to the history for instance only adding + // successful commands. Defaults to true. This is controlled through AutoHistory(bool). + autoHistory bool } // NewTerminal runs a VT100 terminal on the given ReadWriter. If the ReadWriter is @@ -110,6 +114,7 @@ func NewTerminal(c io.ReadWriter, prompt string) *Terminal { echo: true, historyIndex: -1, history: NewHistory(defaultNumEntries), + autoHistory: true, } } @@ -772,8 +777,10 @@ func (t *Terminal) readLine() (line string, err error) { t.outBuf = t.outBuf[:0] if lineOk { if t.echo { - t.historyIndex = -1 - t.history.Add(line) + t.historyIndex = -1 // Resets the key up behavior/historyPending. + if t.autoHistory { + t.history.Add(line) + } } if lineIsPasted { err = ErrPasteIndicator @@ -829,6 +836,14 @@ func (t *Terminal) AddToHistory(entry ...string) { } } +// AutoHistory sets whether lines are automatically added to the history +// before being returned by ReadLine() or not. Defaults to true. +func (t *Terminal) AutoHistory(onOff bool) { + t.lock.Lock() + t.autoHistory = onOff + t.lock.Unlock() +} + // SetPrompt sets the prompt to be used when reading subsequent lines. func (t *Terminal) SetPrompt(prompt string) { t.lock.Lock()