Skip to content

Commit efe64c8

Browse files
authored
prompt: ClearHistory() to clear commands in history (#12)
1 parent 24384d0 commit efe64c8

File tree

9 files changed

+45
-12
lines changed

9 files changed

+45
-12
lines changed

examples/prompt/sql/main.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,9 @@ func main() {
109109

110110
// Prompt the user and handle each input in a loop until we are done for any
111111
// reason (user wants to quit, etc.).
112-
cmdNum := len(p.History())
113112
for {
114113
// Update the # of the command we are handling on the title bar
115-
cmdNum++
116-
segmentCmdNum.SetContent(fmt.Sprintf("#%d", cmdNum))
114+
segmentCmdNum.SetContent(fmt.Sprint(len(p.History()) + 1))
117115

118116
// Prompt
119117
input, err := p.Prompt(ctx)
@@ -127,6 +125,9 @@ func main() {
127125
switch strings.ToLower(cmd) {
128126
case "/?", "/help":
129127
printHelp()
128+
case "/clear":
129+
p.ClearHistory()
130+
fmt.Println("Cleared history.")
130131
case "/quit":
131132
fmt.Println("Bye!")
132133
os.Exit(0)
@@ -186,6 +187,7 @@ func printHelp() {
186187
fmt.Println(`SQL Prompt demo using github.com/jedib0t/go-prompter.
187188
188189
* /?, /help Prints this help text.
190+
* /clear Clears History.
189191
* /quit Exits the prompt.`)
190192
}
191193

mocks/prompt/mock_prompter.go

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

prompt/demo.gif

83.5 KB
Loading

prompt/history.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,10 @@ func (h *History) Render(numItems int, dispWidth int) string {
105105
type historyCommandType string
106106

107107
const (
108-
historyCommandNone historyCommandType = ""
109-
historyCommandList historyCommandType = "list"
110-
historyCommandExec historyCommandType = "exec"
108+
historyCommandNone historyCommandType = ""
109+
historyCommandClear historyCommandType = "clear"
110+
historyCommandExec historyCommandType = "exec"
111+
historyCommandList historyCommandType = "list"
111112
)
112113

113114
type historyCommand struct {
@@ -135,10 +136,9 @@ func (p *prompt) processHistoryCommand(input string) *historyCommand {
135136
input = strings.TrimSpace(input)
136137
itemNum, err := strconv.Atoi(input)
137138
if err != nil {
139+
itemNum = 0
138140
if input == p.historyExecPrefix { // prefix=!; input.original=!!; input=!
139141
itemNum = len(p.History())
140-
} else {
141-
itemNum = 0
142142
}
143143
}
144144
return &historyCommand{Type: historyCommandExec, Value: itemNum}

prompt/history_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,5 +139,5 @@ func TestPrompt_processHistoryCommand(t *testing.T) {
139139
hc = p.processHistoryCommand("!!")
140140
assert.NotNil(t, hc)
141141
assert.Equal(t, historyCommandExec, hc.Type)
142-
assert.Equal(t, 3, hc.Value)
142+
assert.Equal(t, len(p.history.Commands), hc.Value)
143143
}

prompt/prompt.go

+6
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ type prompt struct {
8383
timeSyntaxGen time.Duration
8484
}
8585

86+
// ClearHistory clears all record of previously executed commands.
87+
func (p *prompt) ClearHistory() {
88+
p.SetHistory(nil)
89+
}
90+
8691
// CursorLocation returns the current location of the cursor on the prompt.
8792
func (p *prompt) CursorLocation() CursorLocation {
8893
if p.buffer != nil {
@@ -268,6 +273,7 @@ func (p *prompt) SetHistory(commands []HistoryCommand) {
268273
// SetHistoryExecPrefix sets up the pattern used to exec command from history.
269274
// Example (prefix="!"):
270275
// - !10 == execute 10th command
276+
// - ! == execute last command in history
271277
func (p *prompt) SetHistoryExecPrefix(prefix string) {
272278
p.historyExecPrefix = prefix
273279
}

prompt/prompt_handle.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package prompt
22

33
import (
4-
"fmt"
54
"strings"
65

76
tea "github.com/charmbracelet/bubbletea"
@@ -17,8 +16,9 @@ func (p *prompt) handleHistoryExec(output *termenv.Output, cmdNum int) {
1716

1817
cmd := p.history.Get(cmdNum - 1)
1918
if cmd == "" {
20-
errMsg := fmt.Sprintf("ERROR: invalid command number: %v.\n\n", cmdNum)
21-
_, _ = output.WriteString(p.style.Colors.Error.Sprintf(errMsg))
19+
_, _ = output.WriteString(p.style.Colors.Error.Sprintf("ERROR: invalid command number: %v.\n", cmdNum))
20+
_, _ = output.WriteString("\n")
21+
p.linesRendered = make([]string, 0)
2222
p.buffer.Reset()
2323
return
2424
}

prompt/prompt_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ var (
2020
errFoo = errors.New("test-error-foo")
2121
)
2222

23+
func TestPrompt_ClearHistory(t *testing.T) {
24+
p := &prompt{}
25+
p.SetHistory(testHistoryCommands)
26+
assert.Len(t, p.History(), len(testHistoryCommands))
27+
28+
p.ClearHistory()
29+
assert.Len(t, p.History(), 0)
30+
}
31+
2332
func TestPrompt_CursorLocation(t *testing.T) {
2433
p := &prompt{}
2534
assert.Equal(t, CursorLocation{}, p.CursorLocation())

prompt/prompter.go

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import (
1010
// Prompter in the interface to create and manage a shell-like interactive
1111
// command prompt.
1212
type Prompter interface {
13+
// ClearHistory clears all record of previously executed commands.
14+
ClearHistory()
15+
1316
// CursorLocation returns the current location of the cursor on the prompt.
1417
CursorLocation() CursorLocation
1518

@@ -86,6 +89,7 @@ type Prompter interface {
8689
// SetHistoryExecPrefix sets up the pattern used to exec command from
8790
// history. Example (prefix="!"):
8891
// - !10 == execute 10th command
92+
// - ! == execute last command in history
8993
SetHistoryExecPrefix(prefix string)
9094

9195
// SetHistoryListPrefix sets up the prefix used to list commands from

0 commit comments

Comments
 (0)