feat: add method for deleting history

history-delete
sammyette 2023-03-25 17:37:34 -04:00
parent 3d0fffd49a
commit 72d7c4b8ab
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
3 changed files with 36 additions and 0 deletions

View File

@ -12,6 +12,14 @@ import (
type luaHistory struct {} 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) { func (h *luaHistory) Write(line string) (int, error) {
histWrite := hshMod.Get(rt.StringValue("history")).AsTable().Get(rt.StringValue("add")) histWrite := hshMod.Get(rt.StringValue("history")).AsTable().Get(rt.StringValue("add"))
ln, err := rt.Call1(l.MainThread(), histWrite, rt.StringValue(line)) 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 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) { func (h *fileHistory) GetLine(idx int) (string, error) {
if len(h.items) == 0 { if len(h.items) == 0 {
return "", nil return "", nil

View File

@ -13,6 +13,9 @@ type History interface {
// Append takes the line and returns an updated number of lines or an error // Append takes the line and returns an updated number of lines or an error
Write(string) (int, 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 takes the historic line number and returns the line or an error
GetLine(int) (string, error) GetLine(int) (string, error)
@ -61,6 +64,10 @@ func (h *ExampleHistory) Write(s string) (int, error) {
return len(h.items), nil return len(h.items), nil
} }
func (h *ExampleHistory) Delete(idx int) error {
return nil
}
// GetLine returns a line from history // GetLine returns a line from history
func (h *ExampleHistory) GetLine(i int) (string, error) { func (h *ExampleHistory) GetLine(i int) (string, error) {
return h.items[i], nil return h.items[i], nil
@ -85,6 +92,10 @@ func (h *NullHistory) Write(s string) (int, error) {
return 0, nil return 0, nil
} }
func (h *NullHistory) Delete(idx int) error {
return nil
}
// GetLine returns a line from history // GetLine returns a line from history
func (h *NullHistory) GetLine(i int) (string, error) { func (h *NullHistory) GetLine(i int) (string, error) {
return "", nil return "", nil

View File

@ -837,6 +837,15 @@ func (rl *Instance) escapeSeq(r []rune) {
rl.updateHelpers() rl.updateHelpers()
case seqCtrlDelete, seqCtrlDelete2, seqAltD: 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 { if rl.modeTabCompletion {
rl.resetVirtualComp(false) rl.resetVirtualComp(false)
} }