Skip to content

Commit 586198c

Browse files
sorinfjl
authored andcommitted
console: add admin.clearHistory command (#15614)
1 parent d95962c commit 586198c

3 files changed

Lines changed: 20 additions & 0 deletions

File tree

‎console/console.go‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ func (c *Console) init(preload []string) error {
192192
if obj := admin.Object(); obj != nil { // make sure the admin api is enabled over the interface
193193
obj.Set("sleepBlocks", bridge.SleepBlocks)
194194
obj.Set("sleep", bridge.Sleep)
195+
obj.Set("clearHistory", c.clearHistory)
195196
}
196197
// Preload any JavaScript files before starting the console
197198
for _, path := range preload {
@@ -216,6 +217,16 @@ func (c *Console) init(preload []string) error {
216217
return nil
217218
}
218219

220+
func (c *Console) clearHistory() {
221+
c.history = nil
222+
c.prompter.ClearHistory()
223+
if err := os.Remove(c.histPath); err != nil {
224+
fmt.Fprintln(c.printer, "can't delete history file:", err)
225+
} else {
226+
fmt.Fprintln(c.printer, "history file deleted")
227+
}
228+
}
229+
219230
// consoleOutput is an override for the console.log and console.error methods to
220231
// stream the output into the configured output stream instead of stdout.
221232
func (c *Console) consoleOutput(call otto.FunctionCall) otto.Value {

‎console/console_test.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func (p *hookedPrompter) PromptConfirm(prompt string) (bool, error) {
6868
}
6969
func (p *hookedPrompter) SetHistory(history []string) {}
7070
func (p *hookedPrompter) AppendHistory(command string) {}
71+
func (p *hookedPrompter) ClearHistory() {}
7172
func (p *hookedPrompter) SetWordCompleter(completer WordCompleter) {}
7273

7374
// tester is a console test environment for the console tests to operate on.

‎console/prompter.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ type UserPrompter interface {
5151
// if and only if the prompt to append was a valid command.
5252
AppendHistory(command string)
5353

54+
// ClearHistory clears the entire history
55+
ClearHistory()
56+
5457
// SetWordCompleter sets the completion function that the prompter will call to
5558
// fetch completion candidates when the user presses tab.
5659
SetWordCompleter(completer WordCompleter)
@@ -158,6 +161,11 @@ func (p *terminalPrompter) AppendHistory(command string) {
158161
p.State.AppendHistory(command)
159162
}
160163

164+
// ClearHistory clears the entire history
165+
func (p *terminalPrompter) ClearHistory() {
166+
p.State.ClearHistory()
167+
}
168+
161169
// SetWordCompleter sets the completion function that the prompter will call to
162170
// fetch completion candidates when the user presses tab.
163171
func (p *terminalPrompter) SetWordCompleter(completer WordCompleter) {

0 commit comments

Comments
 (0)