Skip to content

Commit

Permalink
feat: add method for deleting history
Browse files Browse the repository at this point in the history
  • Loading branch information
TorchedSammy committed Mar 25, 2023
1 parent 3d0fffd commit 72d7c4b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
16 changes: 16 additions & 0 deletions history.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions readline/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions readline/readline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 72d7c4b

Please sign in to comment.