From 72d7c4b8ab652bb455bffdeab12ccc6e97ad663d Mon Sep 17 00:00:00 2001 From: sammyette Date: Sat, 25 Mar 2023 17:37:34 -0400 Subject: [PATCH] feat: add method for deleting history --- history.go | 16 ++++++++++++++++ readline/history.go | 11 +++++++++++ readline/readline.go | 9 +++++++++ 3 files changed, 36 insertions(+) diff --git a/history.go b/history.go index 51ccf27..bd547cf 100644 --- a/history.go +++ b/history.go @@ -12,6 +12,14 @@ import ( type luaHistory struct {} + +func (h *luaHistory) Delete(idx int) error { + histWrite := hshMod.Get(rt.StringValue("history")).AsTable().Get(rt.StringValue("delete")) + _, err := rt.Call1(l.MainThread(), histWrite, rt.IntValue(int64(idx))) + + return err +} + func (h *luaHistory) Write(line string) (int, error) { histWrite := hshMod.Get(rt.StringValue("history")).AsTable().Get(rt.StringValue("add")) ln, err := rt.Call1(l.MainThread(), histWrite, rt.StringValue(line)) @@ -109,6 +117,14 @@ func (h *fileHistory) Write(line string) (int, error) { return len(h.items), nil } +func (h *fileHistory) Delete(idx int) error { + h.items[idx] = "" + h.f.WriteAt([]byte(strings.Join(h.items, "\n")), 0) + h.f.Sync() + + return nil +} + func (h *fileHistory) GetLine(idx int) (string, error) { if len(h.items) == 0 { return "", nil diff --git a/readline/history.go b/readline/history.go index f772813..91dccb9 100644 --- a/readline/history.go +++ b/readline/history.go @@ -13,6 +13,9 @@ type History interface { // Append takes the line and returns an updated number of lines or an error Write(string) (int, error) + // Delete removes an entry from the history based on the index. + Delete(int) error + // GetLine takes the historic line number and returns the line or an error GetLine(int) (string, error) @@ -61,6 +64,10 @@ func (h *ExampleHistory) Write(s string) (int, error) { return len(h.items), nil } +func (h *ExampleHistory) Delete(idx int) error { + return nil +} + // GetLine returns a line from history func (h *ExampleHistory) GetLine(i int) (string, error) { return h.items[i], nil @@ -85,6 +92,10 @@ func (h *NullHistory) Write(s string) (int, error) { return 0, nil } +func (h *NullHistory) Delete(idx int) error { + return nil +} + // GetLine returns a line from history func (h *NullHistory) GetLine(i int) (string, error) { return "", nil diff --git a/readline/readline.go b/readline/readline.go index f1d6c96..54713a6 100644 --- a/readline/readline.go +++ b/readline/readline.go @@ -837,6 +837,15 @@ func (rl *Instance) escapeSeq(r []rune) { rl.updateHelpers() case seqCtrlDelete, seqCtrlDelete2, seqAltD: + if rl.searchMode == HistoryFind { + fmt.Println("\nCURRENT COMP\n", string(rl.lineComp), "\n\n\n") + rl.resetVirtualComp(true) + + rl.renderHelpers() + rl.viUndoSkipAppend = true + return + } + if rl.modeTabCompletion { rl.resetVirtualComp(false) }