Debian Codegraph @

app/exline.go

raw
1 package app
2
3 import (
4 "context"
5
6 "git.sr.ht/~rjarry/aerc/config"
7 "git.sr.ht/~rjarry/aerc/lib"
8 "git.sr.ht/~rjarry/aerc/lib/ui"
9 "git.sr.ht/~rjarry/go-opt/v2"
10 "go.rockorager.dev/vaxis"
11 )
12
13 type ExLine struct {
14 commit func(cmd string)
15 finish func()
16 tabcomplete func(ctx context.Context, cmd string) ([]opt.Completion, string)
17 cmdHistory lib.History
18 input *ui.TextInput
19 }
20
21 func NewExLine(cmd string, commit func(cmd string), finish func(),
22 tabcomplete func(ctx context.Context, cmd string) ([]opt.Completion, string),
23 cmdHistory lib.History,
24 ) *ExLine {
25 input := ui.NewTextInput("", config.Ui()).Prompt(":").Set(cmd)
26 if config.Ui().CompletionPopovers {
27 input.TabComplete(
28 tabcomplete,
29 config.Ui().CompletionDelay,
30 config.Ui().CompletionMinChars,
31 &config.Binds().Global.CompleteKey,
32 )
33 }
34 exline := &ExLine{
35 commit: commit,
36 finish: finish,
37 tabcomplete: tabcomplete,
38 cmdHistory: cmdHistory,
39 input: input,
40 }
41 return exline
42 }
43
44 func (x *ExLine) TabComplete(tabComplete func(context.Context, string) ([]opt.Completion, string)) {
45 x.input.TabComplete(
46 tabComplete,
47 config.Ui().CompletionDelay,
48 config.Ui().CompletionMinChars,
49 &config.Binds().Global.CompleteKey,
50 )
51 }
52
53 func NewPrompt(prompt string, commit func(text string),
54 tabcomplete func(ctx context.Context, cmd string) ([]opt.Completion, string),
55 ) *ExLine {
56 input := ui.NewTextInput("", config.Ui()).Prompt(prompt)
57 if config.Ui().CompletionPopovers {
58 input.TabComplete(
59 tabcomplete,
60 config.Ui().CompletionDelay,
61 config.Ui().CompletionMinChars,
62 &config.Binds().Global.CompleteKey,
63 )
64 }
65 exline := &ExLine{
66 commit: commit,
67 tabcomplete: tabcomplete,
68 cmdHistory: &nullHistory{input: input},
69 input: input,
70 }
71 return exline
72 }
73
74 func (ex *ExLine) Invalidate() {
75 ui.Invalidate()
76 }
77
78 func (ex *ExLine) Draw(ctx *ui.Context) {
79 ex.input.Draw(ctx)
80 }
81
82 func (ex *ExLine) Focus(focus bool) {
83 ex.input.Focus(focus)
84 }
85
86 func (ex *ExLine) Event(event vaxis.Event) bool {
87 if key, ok := event.(vaxis.Key); ok {
88 switch {
89 case key.Matches(vaxis.KeyEnter), key.Matches('j', vaxis.ModCtrl):
90 cmd := ex.input.String()
91 ex.input.Focus(false)
92 ex.commit(cmd)
93 ex.finish()
94 case key.Matches(vaxis.KeyUp):
95 ex.input.Set(ex.cmdHistory.Prev())
96 ex.Invalidate()
97 case key.Matches(vaxis.KeyDown):
98 ex.input.Set(ex.cmdHistory.Next())
99 ex.Invalidate()
100 case key.Matches(vaxis.KeyEsc), key.Matches('c', vaxis.ModCtrl):
101 ex.input.Focus(false)
102 ex.cmdHistory.Reset()
103 ex.finish()
104 default:
105 return ex.input.Event(event)
106 }
107 }
108 return true
109 }
110
111 type nullHistory struct {
112 input *ui.TextInput
113 }
114
115 func (*nullHistory) Add(string) {}
116
117 func (h *nullHistory) Next() string {
118 return h.input.String()
119 }
120
121 func (h *nullHistory) Prev() string {
122 return h.input.String()
123 }
124
125 func (h *nullHistory) Last() string {
126 return h.input.String()
127 }
128
129 func (*nullHistory) Reset() {}