From 1a5ce192092879036363f28b0222d4aa67121e9a Mon Sep 17 00:00:00 2001 From: Dragos Vingarzan Date: Thu, 6 Jun 2024 15:19:28 +0200 Subject: [PATCH 1/6] added the basic code to get and set the history --- terminal.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/terminal.go b/terminal.go index f636667..f5fa339 100644 --- a/terminal.go +++ b/terminal.go @@ -879,6 +879,26 @@ func (t *Terminal) SetSize(width, height int) error { return err } +func (t *Terminal) GetHistory() []string { + t.lock.Lock() + defer t.lock.Unlock() + + history := make([]string, t.history.size) + for i := 0; i < t.history.size; i++ { + history[i], _ = t.history.NthPreviousEntry(i) + } + return history +} + +func (t *Terminal) SettHistory(history []string) { + t.lock.Lock() + defer t.lock.Unlock() + + for _, entry := range history { + t.history.Add(entry) + } +} + type pasteIndicatorError struct{} func (pasteIndicatorError) Error() string { From 74ace86fada4b66f31a2e56cb34adac0e37720a8 Mon Sep 17 00:00:00 2001 From: Dragos Vingarzan Date: Thu, 6 Jun 2024 15:20:12 +0200 Subject: [PATCH 2/6] fixed typo in function name --- terminal.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/terminal.go b/terminal.go index f5fa339..77c95ee 100644 --- a/terminal.go +++ b/terminal.go @@ -890,7 +890,7 @@ func (t *Terminal) GetHistory() []string { return history } -func (t *Terminal) SettHistory(history []string) { +func (t *Terminal) SetHistory(history []string) { t.lock.Lock() defer t.lock.Unlock() From 7c91ac4cd649c83f64ab2f73e0623e89137c1490 Mon Sep 17 00:00:00 2001 From: Dragos Vingarzan Date: Thu, 6 Jun 2024 16:06:26 +0200 Subject: [PATCH 3/6] fixed the order on get and added a test --- terminal.go | 2 +- terminal_test.go | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/terminal.go b/terminal.go index 77c95ee..63d8dd0 100644 --- a/terminal.go +++ b/terminal.go @@ -885,7 +885,7 @@ func (t *Terminal) GetHistory() []string { history := make([]string, t.history.size) for i := 0; i < t.history.size; i++ { - history[i], _ = t.history.NthPreviousEntry(i) + history[i], _ = t.history.NthPreviousEntry(t.history.size - i - 1) } return history } diff --git a/terminal_test.go b/terminal_test.go index d5c1794..86247be 100644 --- a/terminal_test.go +++ b/terminal_test.go @@ -435,3 +435,12 @@ func TestOutputNewlines(t *testing.T) { t.Errorf("incorrect output: was %q, expected %q", output, expected) } } + +func TestGetSetHistory(t *testing.T) { + term := NewTerminal(nil, ">") + term.SetHistory([]string{"a", "b", "c"}) + history := term.GetHistory() + if len(history) != 3 || history[0] != "a" || history[1] != "b" || history[2] != "c" { + t.Errorf("history not set correctly: %v", history) + } +} From 0a0203d71046a8f9e8a62eb96a3613031e812bae Mon Sep 17 00:00:00 2001 From: Dragos Vingarzan Date: Thu, 6 Jun 2024 16:22:39 +0200 Subject: [PATCH 4/6] added history clear --- terminal.go | 15 +++++++++++++++ terminal_test.go | 10 ++++++++++ 2 files changed, 25 insertions(+) diff --git a/terminal.go b/terminal.go index 63d8dd0..7f86f90 100644 --- a/terminal.go +++ b/terminal.go @@ -899,6 +899,13 @@ func (t *Terminal) SetHistory(history []string) { } } +func (t *Terminal) ClearHistory() { + t.lock.Lock() + defer t.lock.Unlock() + + t.history.Clear() +} + type pasteIndicatorError struct{} func (pasteIndicatorError) Error() string { @@ -964,6 +971,14 @@ func (s *stRingBuffer) NthPreviousEntry(n int) (value string, ok bool) { return s.entries[index], true } +// Clear removes all elements from the ring buffer. +func (s *stRingBuffer) Clear() { + for i := range s.entries { + s.entries[i] = "" + } + s.size = 0 +} + // readPasswordLine reads from reader until it finds \n or io.EOF. // The slice returned does not include the \n. // readPasswordLine also ignores any \r it finds. diff --git a/terminal_test.go b/terminal_test.go index 86247be..9a41f00 100644 --- a/terminal_test.go +++ b/terminal_test.go @@ -444,3 +444,13 @@ func TestGetSetHistory(t *testing.T) { t.Errorf("history not set correctly: %v", history) } } + +func TestClearHistory(t *testing.T) { + term := NewTerminal(nil, ">") + term.SetHistory([]string{"a", "b", "c"}) + term.ClearHistory() + history := term.GetHistory() + if len(history) != 0 { + t.Errorf("history not cleared: %v", history) + } +} From 64e304c30cc210b132ac6e2b565f5d404b222708 Mon Sep 17 00:00:00 2001 From: Dragos Vingarzan Date: Thu, 6 Jun 2024 20:47:55 +0200 Subject: [PATCH 5/6] added a pop history function --- terminal.go | 23 +++++++++++++++++++++++ terminal_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/terminal.go b/terminal.go index 7f86f90..de2010b 100644 --- a/terminal.go +++ b/terminal.go @@ -906,6 +906,13 @@ func (t *Terminal) ClearHistory() { t.history.Clear() } +func (t *Terminal) PopHistory() (string, bool) { + t.lock.Lock() + defer t.lock.Unlock() + + return t.history.Pop() +} + type pasteIndicatorError struct{} func (pasteIndicatorError) Error() string { @@ -979,6 +986,22 @@ func (s *stRingBuffer) Clear() { s.size = 0 } +// Pop the last element from the history. +func (s *stRingBuffer) Pop() (string, bool) { + if s.size == 0 { + return "", false + } + value := s.entries[s.head] + s.head-- + if s.head < 0 { + s.head += s.max + } + if s.size > 0 { + s.size-- + } + return value, true +} + // readPasswordLine reads from reader until it finds \n or io.EOF. // The slice returned does not include the \n. // readPasswordLine also ignores any \r it finds. diff --git a/terminal_test.go b/terminal_test.go index 9a41f00..c31134d 100644 --- a/terminal_test.go +++ b/terminal_test.go @@ -454,3 +454,31 @@ func TestClearHistory(t *testing.T) { t.Errorf("history not cleared: %v", history) } } + +func TestPopHistory(t *testing.T) { + term := NewTerminal(nil, ">") + term.SetHistory([]string{"a", "b", "c"}) + history := term.GetHistory() + if len(history) != 3 || history[0] != "a" || history[1] != "b" || history[2] != "c" { + t.Errorf("history not set correctly: %v", history) + } + h, ok := term.PopHistory() + if !ok { + t.Errorf("history not popped correctly: %v", h) + } + if h != "c" { + t.Errorf("history not popped correctly: %v", h) + } + history = term.GetHistory() + if len(history) != 2 || history[0] != "a" || history[1] != "b" { + t.Errorf("history not popped correctly: %v", history) + } + term.ClearHistory() + h, ok = term.PopHistory() + if ok { + t.Errorf("history not popped correctly: %v", h) + } + if h != "" { + t.Errorf("history not popped correctly: %v", h) + } +} From d1d0a3df60dde94bf8820cf27ce7d4fdf1f9ba2e Mon Sep 17 00:00:00 2001 From: Dragos Vingarzan Date: Sat, 8 Jun 2024 13:48:43 +0200 Subject: [PATCH 6/6] disabling auto-completion around GetPassword() --- terminal.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/terminal.go b/terminal.go index de2010b..9ac1ddd 100644 --- a/terminal.go +++ b/terminal.go @@ -699,11 +699,14 @@ func (t *Terminal) ReadPassword(prompt string) (line string, err error) { oldPrompt := t.prompt t.prompt = []rune(prompt) t.echo = false + oldAutoComplete := t.AutoCompleteCallback + t.AutoCompleteCallback = nil line, err = t.readLine() t.prompt = oldPrompt t.echo = true + t.AutoCompleteCallback = oldAutoComplete return }