2018-02-27 04:54:39 +01:00
|
|
|
package widgets
|
2018-02-27 04:41:54 +01:00
|
|
|
|
|
|
|
import (
|
2020-11-30 23:07:03 +01:00
|
|
|
"github.com/gdamore/tcell/v2"
|
2018-02-27 04:54:39 +01:00
|
|
|
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/config"
|
|
|
|
"git.sr.ht/~rjarry/aerc/lib"
|
|
|
|
"git.sr.ht/~rjarry/aerc/lib/ui"
|
2018-02-27 04:41:54 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type ExLine struct {
|
2019-04-27 18:47:59 +02:00
|
|
|
ui.Invalidatable
|
2019-06-27 19:33:11 +02:00
|
|
|
commit func(cmd string)
|
2019-08-20 03:56:12 +02:00
|
|
|
finish func()
|
2019-06-27 19:33:11 +02:00
|
|
|
tabcomplete func(cmd string) []string
|
2019-07-23 18:52:33 +02:00
|
|
|
cmdHistory lib.History
|
2019-06-27 19:33:11 +02:00
|
|
|
input *ui.TextInput
|
2020-07-27 10:03:55 +02:00
|
|
|
conf *config.AercConfig
|
2018-02-27 04:41:54 +01:00
|
|
|
}
|
|
|
|
|
2019-12-20 19:21:33 +01:00
|
|
|
func NewExLine(conf *config.AercConfig, cmd string, commit func(cmd string), finish func(),
|
2019-07-23 18:52:33 +02:00
|
|
|
tabcomplete func(cmd string) []string,
|
|
|
|
cmdHistory lib.History) *ExLine {
|
2019-06-27 19:33:11 +02:00
|
|
|
|
2020-07-27 10:03:55 +02:00
|
|
|
input := ui.NewTextInput("", conf.Ui).Prompt(":").Set(cmd)
|
2019-12-20 19:21:33 +01:00
|
|
|
if conf.Ui.CompletionPopovers {
|
|
|
|
input.TabComplete(tabcomplete, conf.Ui.CompletionDelay)
|
|
|
|
}
|
2019-05-11 19:12:44 +02:00
|
|
|
exline := &ExLine{
|
2019-06-27 19:33:11 +02:00
|
|
|
commit: commit,
|
2019-08-20 03:56:12 +02:00
|
|
|
finish: finish,
|
2019-06-27 19:33:11 +02:00
|
|
|
tabcomplete: tabcomplete,
|
2019-07-23 18:52:33 +02:00
|
|
|
cmdHistory: cmdHistory,
|
2019-06-27 19:33:11 +02:00
|
|
|
input: input,
|
2020-07-27 10:03:55 +02:00
|
|
|
conf: conf,
|
2018-02-28 03:02:56 +01:00
|
|
|
}
|
2019-05-11 19:12:44 +02:00
|
|
|
input.OnInvalidate(func(d ui.Drawable) {
|
|
|
|
exline.Invalidate()
|
|
|
|
})
|
|
|
|
return exline
|
2018-02-27 04:41:54 +01:00
|
|
|
}
|
|
|
|
|
2019-12-20 19:21:33 +01:00
|
|
|
func NewPrompt(conf *config.AercConfig, prompt string, commit func(text string),
|
2019-08-20 03:56:12 +02:00
|
|
|
tabcomplete func(cmd string) []string) *ExLine {
|
|
|
|
|
2020-07-27 10:03:55 +02:00
|
|
|
input := ui.NewTextInput("", conf.Ui).Prompt(prompt)
|
2019-12-20 19:21:33 +01:00
|
|
|
if conf.Ui.CompletionPopovers {
|
|
|
|
input.TabComplete(tabcomplete, conf.Ui.CompletionDelay)
|
|
|
|
}
|
2019-08-20 03:56:12 +02:00
|
|
|
exline := &ExLine{
|
|
|
|
commit: commit,
|
|
|
|
tabcomplete: tabcomplete,
|
|
|
|
cmdHistory: &nullHistory{input: input},
|
|
|
|
input: input,
|
|
|
|
}
|
|
|
|
input.OnInvalidate(func(d ui.Drawable) {
|
|
|
|
exline.Invalidate()
|
|
|
|
})
|
|
|
|
return exline
|
|
|
|
}
|
|
|
|
|
2018-02-27 04:41:54 +01:00
|
|
|
func (ex *ExLine) Invalidate() {
|
2019-04-27 18:47:59 +02:00
|
|
|
ex.DoInvalidate(ex)
|
2018-02-27 04:41:54 +01:00
|
|
|
}
|
|
|
|
|
2018-02-27 04:54:39 +01:00
|
|
|
func (ex *ExLine) Draw(ctx *ui.Context) {
|
2019-05-11 19:12:44 +02:00
|
|
|
ex.input.Draw(ctx)
|
2018-02-27 04:41:54 +01:00
|
|
|
}
|
|
|
|
|
2019-03-17 19:02:33 +01:00
|
|
|
func (ex *ExLine) Focus(focus bool) {
|
2019-05-11 19:12:44 +02:00
|
|
|
ex.input.Focus(focus)
|
2018-02-27 04:41:54 +01:00
|
|
|
}
|
|
|
|
|
2018-06-01 09:58:00 +02:00
|
|
|
func (ex *ExLine) Event(event tcell.Event) bool {
|
|
|
|
switch event := event.(type) {
|
|
|
|
case *tcell.EventKey:
|
|
|
|
switch event.Key() {
|
2019-08-18 07:37:49 +02:00
|
|
|
case tcell.KeyEnter, tcell.KeyCtrlJ:
|
2019-07-23 18:52:33 +02:00
|
|
|
cmd := ex.input.String()
|
2019-05-11 19:20:29 +02:00
|
|
|
ex.input.Focus(false)
|
2019-07-23 18:52:33 +02:00
|
|
|
ex.commit(cmd)
|
2019-08-20 03:56:12 +02:00
|
|
|
ex.finish()
|
2019-07-23 18:52:33 +02:00
|
|
|
case tcell.KeyUp:
|
|
|
|
ex.input.Set(ex.cmdHistory.Prev())
|
|
|
|
ex.Invalidate()
|
|
|
|
case tcell.KeyDown:
|
|
|
|
ex.input.Set(ex.cmdHistory.Next())
|
|
|
|
ex.Invalidate()
|
2018-06-01 09:58:00 +02:00
|
|
|
case tcell.KeyEsc, tcell.KeyCtrlC:
|
2019-05-11 19:20:29 +02:00
|
|
|
ex.input.Focus(false)
|
2019-07-23 18:52:33 +02:00
|
|
|
ex.cmdHistory.Reset()
|
2019-08-20 03:56:12 +02:00
|
|
|
ex.finish()
|
2019-05-11 19:12:44 +02:00
|
|
|
default:
|
|
|
|
return ex.input.Event(event)
|
2018-02-27 04:41:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2019-08-20 03:56:12 +02:00
|
|
|
|
|
|
|
type nullHistory struct {
|
|
|
|
input *ui.TextInput
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (*nullHistory) Add(string) {}
|
2019-08-20 03:56:12 +02:00
|
|
|
|
|
|
|
func (h *nullHistory) Next() string {
|
|
|
|
return h.input.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *nullHistory) Prev() string {
|
|
|
|
return h.input.String()
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (*nullHistory) Reset() {}
|