summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--commands/prompt.go33
-rw-r--r--doc/aerc.1.scd6
-rw-r--r--widgets/aerc.go34
-rw-r--r--widgets/exline.go41
4 files changed, 108 insertions, 6 deletions
diff --git a/commands/prompt.go b/commands/prompt.go
new file mode 100644
index 0000000..3734881
--- /dev/null
+++ b/commands/prompt.go
@@ -0,0 +1,33 @@
+package commands
+
+import (
+	"errors"
+	"fmt"
+
+	"git.sr.ht/~sircmpwn/aerc/widgets"
+)
+
+type Prompt struct{}
+
+func init() {
+	register(Prompt{})
+}
+
+func (_ Prompt) Aliases() []string {
+	return []string{"prompt"}
+}
+
+func (_ Prompt) Complete(aerc *widgets.Aerc, args []string) []string {
+	return nil // TODO: add completions
+}
+
+func (_ Prompt) Execute(aerc *widgets.Aerc, args []string) error {
+	if len(args) < 3 {
+		return errors.New(fmt.Sprintf("Usage: %s <prompt> <cmd>", args[0]))
+	}
+
+	prompt := args[1]
+	cmd := args[2:]
+	aerc.RegisterPrompt(prompt, cmd)
+	return nil
+}
diff --git a/doc/aerc.1.scd b/doc/aerc.1.scd
index 4aa777c..225ded7 100644
--- a/doc/aerc.1.scd
+++ b/doc/aerc.1.scd
@@ -67,6 +67,12 @@ These commands work in any context.
 	Cycles to the previous or next tab in the list, repeating n times
 	(default: 1).
 
+*prompt* <prompt> <command...>
+	Displays the prompt on the status bar, waits for user input, then appends
+	that input as the last argument to the command and executes it. The input is
+	passed as one argument to the command, unless it is empty, in which case no
+	extra argument is added.
+
 *quit*
 	Exits aerc.
 
diff --git a/widgets/aerc.go b/widgets/aerc.go
index 5a7914a..345f3ea 100644
--- a/widgets/aerc.go
+++ b/widgets/aerc.go
@@ -30,6 +30,7 @@ type Aerc struct {
 	statusbar   *libui.Stack
 	statusline  *StatusLine
 	pendingKeys []config.KeyStroke
+	prompts     *libui.Stack
 	tabs        *libui.Tabs
 	beep        func() error
 }
@@ -65,6 +66,7 @@ func NewAerc(conf *config.AercConfig, logger *log.Logger,
 		logger:     logger,
 		statusbar:  statusbar,
 		statusline: statusline,
+		prompts:    libui.NewStack(),
 		tabs:       tabs,
 	}
 
@@ -105,6 +107,20 @@ func (aerc *Aerc) Tick() bool {
 	for _, acct := range aerc.accounts {
 		more = acct.Tick() || more
 	}
+
+	if len(aerc.prompts.Children()) > 0 {
+		more = true
+		previous := aerc.focused
+		prompt := aerc.prompts.Pop().(*ExLine)
+		prompt.finish = func() {
+			aerc.statusbar.Pop()
+			aerc.focus(previous)
+		}
+
+		aerc.statusbar.Push(prompt)
+		aerc.focus(prompt)
+	}
+
 	return more
 }
 
@@ -358,8 +374,6 @@ func (aerc *Aerc) BeginExCommand() {
 		if aerc.simulating == 0 {
 			aerc.cmdHistory.Add(cmd)
 		}
-		aerc.statusbar.Pop()
-		aerc.focus(previous)
 	}, func() {
 		aerc.statusbar.Pop()
 		aerc.focus(previous)
@@ -370,6 +384,22 @@ func (aerc *Aerc) BeginExCommand() {
 	aerc.focus(exline)
 }
 
+func (aerc *Aerc) RegisterPrompt(prompt string, cmd []string) {
+	p := NewPrompt(prompt, func(text string) {
+		if text != "" {
+			cmd = append(cmd, text)
+		}
+		err := aerc.cmd(cmd)
+		if err != nil {
+			aerc.PushStatus(" "+err.Error(), 10*time.Second).
+				Color(tcell.ColorDefault, tcell.ColorRed)
+		}
+	}, func(cmd string) []string {
+		return nil // TODO: completions
+	})
+	aerc.prompts.Push(p)
+}
+
 func (aerc *Aerc) Mailto(addr *url.URL) error {
 	acct := aerc.SelectedAccount()
 	if acct == nil {
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() {}
25:03 +0200 committer Andrew Yu <andrew@andrewyu.org> 2022-04-15 20:25:03 +0200 refer matrix' href='/runxiyu/www/commit/pragmatics.txt?id=c9cef811d0097eed76bb8b11d1157cf235ee9e58'>c9cef81 ^
c51e74b ^
91fd35d ^


91fd35d ^






























9b03a75 ^
91fd35d ^



c9cef81 ^
c9cef81 ^
1578212 ^


c9cef81 ^
91fd35d ^
853ba9a ^


1578212 ^

0578710 ^
1578212 ^





















9b03a75 ^








1578212 ^
91fd35d ^
9b03a75 ^


566ce45 ^



9b03a75 ^








91fd35d ^
1578212 ^
91fd35d ^
1578212 ^
91fd35d ^
1578212 ^
c9cef81 ^


1578212 ^
36ff6b2 ^
566ce45 ^



f5ba891 ^
36ff6b2 ^

681124a ^

681124a ^







c6afda6 ^
681124a ^
681124a ^










91fd35d ^










1578212 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280