about summary refs log tree commit diff stats
path: root/widgets/exline.go
diff options
context:
space:
mode:
Diffstat (limited to 'widgets/exline.go')
-rw-r--r--widgets/exline.go41
1 files changed, 37 insertions, 4 deletions
diff --git a/widgets/exline.go b/widgets/exline.go
index be1cde1..8ec69d6 100644
--- a/widgets/exline.go
+++ b/widgets/exline.go
@@ -9,21 +9,21 @@ import (
 
 type ExLine struct {
 	ui.Invalidatable
-	cancel      func()
 	commit      func(cmd string)
+	finish      func()
 	tabcomplete func(cmd string) []string
 	cmdHistory  lib.History
 	input       *ui.TextInput
 }
 
-func NewExLine(commit func(cmd string), cancel func(),
+func NewExLine(commit func(cmd string), finish func(),
 	tabcomplete func(cmd string) []string,
 	cmdHistory lib.History) *ExLine {
 
 	input := ui.NewTextInput("").Prompt(":").TabComplete(tabcomplete)
 	exline := &ExLine{
-		cancel:      cancel,
 		commit:      commit,
+		finish:      finish,
 		tabcomplete: tabcomplete,
 		cmdHistory:  cmdHistory,
 		input:       input,
@@ -34,6 +34,22 @@ func NewExLine(commit func(cmd string), cancel func(),
 	return exline
 }
 
+func NewPrompt(prompt string, commit func(text string),
+	tabcomplete func(cmd string) []string) *ExLine {
+
+	input := ui.NewTextInput("").Prompt(prompt).TabComplete(tabcomplete)
+	exline := &ExLine{
+		commit:      commit,
+		tabcomplete: tabcomplete,
+		cmdHistory:  &nullHistory{input: input},
+		input:       input,
+	}
+	input.OnInvalidate(func(d ui.Drawable) {
+		exline.Invalidate()
+	})
+	return exline
+}
+
 func (ex *ExLine) Invalidate() {
 	ex.DoInvalidate(ex)
 }
@@ -54,6 +70,7 @@ func (ex *ExLine) Event(event tcell.Event) bool {
 			cmd := ex.input.String()
 			ex.input.Focus(false)
 			ex.commit(cmd)
+			ex.finish()
 		case tcell.KeyUp:
 			ex.input.Set(ex.cmdHistory.Prev())
 			ex.Invalidate()
@@ -63,10 +80,26 @@ func (ex *ExLine) Event(event tcell.Event) bool {
 		case tcell.KeyEsc, tcell.KeyCtrlC:
 			ex.input.Focus(false)
 			ex.cmdHistory.Reset()
-			ex.cancel()
+			ex.finish()
 		default:
 			return ex.input.Event(event)
 		}
 	}
 	return true
 }
+
+type nullHistory struct {
+	input *ui.TextInput
+}
+
+func (_ *nullHistory) Add(string) {}
+
+func (h *nullHistory) Next() string {
+	return h.input.String()
+}
+
+func (h *nullHistory) Prev() string {
+	return h.input.String()
+}
+
+func (_ *nullHistory) Reset() {}