summary refs log tree commit diff stats
path: root/widgets
diff options
context:
space:
mode:
authorDrew DeVault <sir@cmpwn.com>2019-03-17 16:19:15 -0400
committerDrew DeVault <sir@cmpwn.com>2019-03-17 16:19:15 -0400
commit589db742cb2af4b29607ceba62ceca38ec982f62 (patch)
tree0975f74796cb809cd1bd8d04ee0c7a052a45f3f5 /widgets
parent9e28a02f6a4345ec7b5fdee68864610186f34e91 (diff)
downloadaerc-589db742cb2af4b29607ceba62ceca38ec982f62.tar.gz
Move exline handling up to aerc, add :term
Diffstat (limited to 'widgets')
-rw-r--r--widgets/account.go120
-rw-r--r--widgets/aerc.go129
-rw-r--r--widgets/tabhost.go11
3 files changed, 148 insertions, 112 deletions
diff --git a/widgets/account.go b/widgets/account.go
index b6ba595..8a3b989 100644
--- a/widgets/account.go
+++ b/widgets/account.go
@@ -3,7 +3,6 @@ package widgets
 import (
 	"fmt"
 	"log"
-	"time"
 
 	"github.com/gdamore/tcell"
 
@@ -19,63 +18,51 @@ type AccountView struct {
 	conf         *config.AercConfig
 	dirlist      *DirectoryList
 	grid         *ui.Grid
+	host         TabHost
 	logger       *log.Logger
-	interactive  []ui.Interactive
 	onInvalidate func(d ui.Drawable)
-	runCmd       func(cmd string) error
 	msglist      *MessageList
 	msgStores    map[string]*lib.MessageStore
-	pendingKeys  []config.KeyStroke
-	statusline   *StatusLine
-	statusbar    *ui.Stack
 	worker       *types.Worker
 }
 
 func NewAccountView(conf *config.AercConfig, acct *config.AccountConfig,
-	logger *log.Logger, runCmd func(cmd string) error) *AccountView {
-
-	statusbar := ui.NewStack()
-	statusline := NewStatusLine()
-	statusbar.Push(statusline)
+	logger *log.Logger, host TabHost) *AccountView {
 
 	grid := ui.NewGrid().Rows([]ui.GridSpec{
 		{ui.SIZE_WEIGHT, 1},
-		{ui.SIZE_EXACT, 1},
 	}).Columns([]ui.GridSpec{
 		{ui.SIZE_EXACT, conf.Ui.SidebarWidth},
 		{ui.SIZE_WEIGHT, 1},
 	})
-	grid.AddChild(statusbar).At(1, 1)
 
 	worker, err := worker.NewWorker(acct.Source, logger)
 	if err != nil {
-		statusline.Set(fmt.Sprintf("%s", err))
+		host.SetStatus(fmt.Sprintf("%s: %s", acct.Name, err))
 		return &AccountView{
-			acct:       acct,
-			grid:       grid,
-			logger:     logger,
-			statusline: statusline,
+			acct:   acct,
+			grid:   grid,
+			host:   host,
+			logger: logger,
 		}
 	}
 
 	dirlist := NewDirectoryList(acct, logger, worker)
-	grid.AddChild(ui.NewBordered(dirlist, ui.BORDER_RIGHT)).Span(2, 1)
+	grid.AddChild(ui.NewBordered(dirlist, ui.BORDER_RIGHT))
 
 	msglist := NewMessageList(logger)
 	grid.AddChild(msglist).At(0, 1)
 
 	view := &AccountView{
-		acct:       acct,
-		conf:       conf,
-		dirlist:    dirlist,
-		grid:       grid,
-		logger:     logger,
-		msglist:    msglist,
-		msgStores:  make(map[string]*lib.MessageStore),
-		runCmd:     runCmd,
-		statusbar:  statusbar,
-		statusline: statusline,
-		worker:     worker,
+		acct:      acct,
+		conf:      conf,
+		dirlist:   dirlist,
+		grid:      grid,
+		host:      host,
+		logger:    logger,
+		msglist:   msglist,
+		msgStores: make(map[string]*lib.MessageStore),
+		worker:    worker,
 	}
 
 	go worker.Backend.Run()
@@ -89,7 +76,7 @@ func NewAccountView(conf *config.AercConfig, acct *config.AccountConfig,
 
 	worker.PostAction(&types.Configure{Config: acct}, nil)
 	worker.PostAction(&types.Connect{}, view.connected)
-	statusline.Set("Connecting...")
+	host.SetStatus("Connecting...")
 
 	return view
 }
@@ -116,75 +103,14 @@ func (acct *AccountView) Draw(ctx *ui.Context) {
 	acct.grid.Draw(ctx)
 }
 
-func (acct *AccountView) popInteractive() {
-	acct.interactive = acct.interactive[:len(acct.interactive)-1]
-	if len(acct.interactive) != 0 {
-		acct.interactive[len(acct.interactive)-1].Focus(true)
-	}
-}
-
-func (acct *AccountView) pushInteractive(item ui.Interactive) {
-	if len(acct.interactive) != 0 {
-		acct.interactive[len(acct.interactive)-1].Focus(false)
-	}
-	acct.interactive = append(acct.interactive, item)
-	item.Focus(true)
-}
-
-func (acct *AccountView) beginExCommand() {
-	exline := NewExLine(func(command string) {
-		err := acct.runCmd(command)
-		if err != nil {
-			acct.statusline.Push(" "+err.Error(), 10*time.Second).
-				Color(tcell.ColorRed, tcell.ColorWhite)
-		}
-		acct.statusbar.Pop()
-		acct.popInteractive()
-	}, func() {
-		acct.statusbar.Pop()
-		acct.popInteractive()
-	})
-	acct.pushInteractive(exline)
-	acct.statusbar.Push(exline)
-}
-
-func (acct *AccountView) Event(event tcell.Event) bool {
-	if len(acct.interactive) != 0 {
-		return acct.interactive[len(acct.interactive)-1].Event(event)
-	}
-
-	switch event := event.(type) {
-	case *tcell.EventKey:
-		acct.pendingKeys = append(acct.pendingKeys, config.KeyStroke{
-			Key:  event.Key(),
-			Rune: event.Rune(),
-		})
-		result, output := acct.conf.Lbinds.GetBinding(acct.pendingKeys)
-		switch result {
-		case config.BINDING_FOUND:
-			acct.pendingKeys = []config.KeyStroke{}
-			for _, stroke := range output {
-				simulated := tcell.NewEventKey(
-					stroke.Key, stroke.Rune, tcell.ModNone)
-				acct.Event(simulated)
-			}
-		case config.BINDING_INCOMPLETE:
-			return false
-		case config.BINDING_NOT_FOUND:
-			acct.pendingKeys = []config.KeyStroke{}
-			if event.Rune() == ':' {
-				acct.beginExCommand()
-				return true
-			}
-		}
-	}
-	return false
+func (acct *AccountView) Focus(focus bool) {
+	// TODO: Unfocus children I guess
 }
 
 func (acct *AccountView) connected(msg types.WorkerMessage) {
 	switch msg := msg.(type) {
 	case *types.Done:
-		acct.statusline.Set("Listing mailboxes...")
+		acct.host.SetStatus("Listing mailboxes...")
 		acct.logger.Println("Listing mailboxes...")
 		acct.dirlist.UpdateList(func(dirs []string) {
 			var dir string
@@ -199,7 +125,7 @@ func (acct *AccountView) connected(msg types.WorkerMessage) {
 			}
 			acct.dirlist.Select(dir)
 			acct.logger.Println("Connected.")
-			acct.statusline.Set("Connected.")
+			acct.host.SetStatus("Connected.")
 		})
 	case *types.CertificateApprovalRequest:
 		// TODO: Ask the user
@@ -252,7 +178,7 @@ func (acct *AccountView) onMessage(msg types.WorkerMessage) {
 		store.Update(msg)
 	case *types.Error:
 		acct.logger.Printf("%v", msg.Error)
-		acct.statusline.Set(fmt.Sprintf("%v", msg.Error)).
+		acct.host.SetStatus(fmt.Sprintf("%v", msg.Error)).
 			Color(tcell.ColorRed, tcell.ColorDefault)
 	}
 }
diff --git a/widgets/aerc.go b/widgets/aerc.go
index 3537897..5841876 100644
--- a/widgets/aerc.go
+++ b/widgets/aerc.go
@@ -2,6 +2,7 @@ package widgets
 
 import (
 	"log"
+	"time"
 
 	"github.com/gdamore/tcell"
 
@@ -11,10 +12,16 @@ import (
 )
 
 type Aerc struct {
-	accounts map[string]*AccountView
-	cmd      func(cmd string) error
-	grid     *libui.Grid
-	tabs     *libui.Tabs
+	accounts    map[string]*AccountView
+	cmd         func(cmd string) error
+	conf        *config.AercConfig
+	focused     libui.Interactive
+	grid        *libui.Grid
+	logger      *log.Logger
+	statusbar   *libui.Stack
+	statusline  *StatusLine
+	pendingKeys []config.KeyStroke
+	tabs        *libui.Tabs
 }
 
 func NewAerc(conf *config.AercConfig, logger *log.Logger,
@@ -22,29 +29,42 @@ func NewAerc(conf *config.AercConfig, logger *log.Logger,
 
 	tabs := libui.NewTabs()
 
-	mainGrid := libui.NewGrid().Rows([]libui.GridSpec{
+	statusbar := ui.NewStack()
+	statusline := NewStatusLine()
+	statusbar.Push(statusline)
+
+	grid := libui.NewGrid().Rows([]libui.GridSpec{
 		{libui.SIZE_EXACT, 1},
 		{libui.SIZE_WEIGHT, 1},
+		{libui.SIZE_EXACT, 1},
 	}).Columns([]libui.GridSpec{
 		{libui.SIZE_EXACT, conf.Ui.SidebarWidth},
 		{libui.SIZE_WEIGHT, 1},
 	})
+	grid.AddChild(statusbar).At(2, 1)
+	// Minor hack
+	grid.AddChild(libui.NewBordered(
+		libui.NewFill(' '), libui.BORDER_RIGHT)).At(2, 0)
 
-	mainGrid.AddChild(libui.NewText("aerc").
+	grid.AddChild(libui.NewText("aerc").
 		Strategy(libui.TEXT_CENTER).
 		Color(tcell.ColorBlack, tcell.ColorWhite))
-	mainGrid.AddChild(tabs.TabStrip).At(0, 1)
-	mainGrid.AddChild(tabs.TabContent).At(1, 0).Span(1, 2)
+	grid.AddChild(tabs.TabStrip).At(0, 1)
+	grid.AddChild(tabs.TabContent).At(1, 0).Span(1, 2)
 
 	aerc := &Aerc{
-		accounts: make(map[string]*AccountView),
-		cmd:      cmd,
-		grid:     mainGrid,
-		tabs:     tabs,
+		accounts:   make(map[string]*AccountView),
+		conf:       conf,
+		cmd:        cmd,
+		grid:       grid,
+		logger:     logger,
+		statusbar:  statusbar,
+		statusline: statusline,
+		tabs:       tabs,
 	}
 
 	for _, acct := range conf.Accounts {
-		view := NewAccountView(conf, &acct, logger, cmd)
+		view := NewAccountView(conf, &acct, logger, aerc)
 		aerc.accounts[acct.Name] = view
 		tabs.Add(view, acct.Name)
 	}
@@ -75,8 +95,41 @@ func (aerc *Aerc) Draw(ctx *libui.Context) {
 }
 
 func (aerc *Aerc) Event(event tcell.Event) bool {
-	acct, _ := aerc.tabs.Tabs[aerc.tabs.Selected].Content.(*AccountView)
-	return acct.Event(event)
+	if aerc.focused != nil {
+		aerc.logger.Println("sending event to focused child")
+		return aerc.focused.Event(event)
+	}
+
+	switch event := event.(type) {
+	case *tcell.EventKey:
+		aerc.pendingKeys = append(aerc.pendingKeys, config.KeyStroke{
+			Key:  event.Key(),
+			Rune: event.Rune(),
+		})
+		result, output := aerc.conf.Lbinds.GetBinding(aerc.pendingKeys)
+		switch result {
+		case config.BINDING_FOUND:
+			aerc.pendingKeys = []config.KeyStroke{}
+			for _, stroke := range output {
+				simulated := tcell.NewEventKey(
+					stroke.Key, stroke.Rune, tcell.ModNone)
+				aerc.Event(simulated)
+			}
+		case config.BINDING_INCOMPLETE:
+			return false
+		case config.BINDING_NOT_FOUND:
+			aerc.pendingKeys = []config.KeyStroke{}
+			if event.Rune() == ':' {
+				aerc.BeginExCommand()
+				return true
+			}
+		}
+	}
+	return false
+}
+
+func (aerc *Aerc) Config() *config.AercConfig {
+	return aerc.conf
 }
 
 func (aerc *Aerc) SelectedAccount() *AccountView {
@@ -86,3 +139,49 @@ func (aerc *Aerc) SelectedAccount() *AccountView {
 	}
 	return acct
 }
+
+func (aerc *Aerc) NewTab(drawable ui.Drawable, name string) *ui.Tab {
+	tab := aerc.tabs.Add(drawable, name)
+	aerc.tabs.Select(len(aerc.tabs.Tabs) - 1)
+	return tab
+}
+
+// TODO: Use per-account status lines, but a global ex line
+func (aerc *Aerc) SetStatus(status string) *StatusMessage {
+	return aerc.statusline.Set(status)
+}
+
+func (aerc *Aerc) PushStatus(text string, expiry time.Duration) *StatusMessage {
+	return aerc.statusline.Push(text, expiry)
+}
+
+func (aerc *Aerc) focus(item libui.Interactive) {
+	if aerc.focused == item {
+		return
+	}
+	if aerc.focused != nil {
+		aerc.focused.Focus(false)
+	}
+	aerc.focused = item
+	if item != nil {
+		item.Focus(true)
+	}
+}
+
+func (aerc *Aerc) BeginExCommand() {
+	previous := aerc.focused
+	exline := NewExLine(func(cmd string) {
+		err := aerc.cmd(cmd)
+		if err != nil {
+			aerc.PushStatus(" "+err.Error(), 10*time.Second).
+				Color(tcell.ColorRed, tcell.ColorWhite)
+		}
+		aerc.statusbar.Pop()
+		aerc.focus(previous)
+	}, func() {
+		aerc.statusbar.Pop()
+		aerc.focus(previous)
+	})
+	aerc.statusbar.Push(exline)
+	aerc.focus(exline)
+}
diff --git a/widgets/tabhost.go b/widgets/tabhost.go
new file mode 100644
index 0000000..7c502cb
--- /dev/null
+++ b/widgets/tabhost.go
@@ -0,0 +1,11 @@
+package widgets
+
+import (
+	"time"
+)
+
+type TabHost interface {
+	BeginExCommand()
+	SetStatus(status string) *StatusMessage
+	PushStatus(text string, expiry time.Duration) *StatusMessage
+}
35:06 -0700 committer Kartik K. Agaram <vc@akkartik.com> 2015-07-29 01:35:06 -0700 1882' href='/akkartik/mu/commit/032array.cc?h=main&id=10ffce9815b0242259730629668215b01dccbda1'>10ffce98 ^
7b085072 ^
7b085072 ^




9edabeaa ^

c6520d96 ^
9edabeaa ^


086acf39 ^
9edabeaa ^


7e9c6925 ^
c91caafd ^
795f5244 ^
93b7d983 ^
795f5244 ^
c6034af3 ^
5f98a10c ^
49659e72 ^
3076bab4 ^
49659e72 ^
795f5244 ^
49659e72 ^
a55bbd06 ^
7284d503 ^

88be3dbc ^
7e9c6925 ^
c6520d96 ^
bc643692 ^


c6520d96 ^
7e9c6925 ^
7e9c6925 ^

88be3dbc ^
07d35c4a ^
c6520d96 ^
bc643692 ^



c6520d96 ^
07d35c4a ^
07d35c4a ^

1848b18f ^

a55bbd06 ^
795f5244 ^
b8515e8a ^
a55bbd06 ^
b8515e8a ^
795f5244 ^
e4630643 ^

b8515e8a ^

1ad3fe9e ^
795f5244 ^
18e626df ^

fb85b3b4 ^



c6034af3 ^
bb7142db ^
b954ac38 ^
fb85b3b4 ^

b8515e8a ^



c6034af3 ^

18e626df ^

5f98a10c ^
46026f62 ^

c6034af3 ^

fca0ebbe ^
c6034af3 ^
795f5244 ^
5f98a10c ^
551d155c ^

ac0e9db5 ^
cdd6fd09 ^
795f5244 ^
e853b94e ^
827898fc ^
c6034af3 ^
cfb142b9 ^
a55bbd06 ^


7e9c6925 ^
c6034af3 ^

7e9c6925 ^

551d155c ^
a55bbd06 ^
c6520d96 ^
bc643692 ^


7136ddd5 ^
10ffce98 ^
a55bbd06 ^
551d155c ^


5f98a10c ^
551d155c ^
c6520d96 ^
bc643692 ^





b94ea5c8 ^
10ffce98 ^
551d155c ^
5f98a10c ^
551d155c ^
051c4738 ^
5f98a10c ^
551d155c ^
c6520d96 ^
bc643692 ^





7136ddd5 ^
10ffce98 ^
551d155c ^
5f98a10c ^
a55bbd06 ^
fb85b3b4 ^
5f98a10c ^
fb85b3b4 ^







7136ddd5 ^
fb85b3b4 ^

5f98a10c ^
fb85b3b4 ^
5c6840b8 ^











7284d503 ^
1848b18f ^
551d155c ^
1848b18f ^
c6520d96 ^
bc643692 ^


fb85b3b4 ^
1848b18f ^
551d155c ^
1848b18f ^


a55bbd06 ^
795f5244 ^
b8515e8a ^
a55bbd06 ^
b8515e8a ^
795f5244 ^
e4630643 ^

b8515e8a ^

1ad3fe9e ^
795f5244 ^
46026f62 ^

fb85b3b4 ^



c6034af3 ^
795f5244 ^
bb7142db ^
b954ac38 ^
fb85b3b4 ^

b8515e8a ^



c6034af3 ^

18e626df ^

5f98a10c ^
18e626df ^

c6034af3 ^

fca0ebbe ^
c6034af3 ^
795f5244 ^
5f98a10c ^
551d155c ^

ac0e9db5 ^
cfb142b9 ^
827898fc ^
a55bbd06 ^

c8a58cdc ^
551d155c ^
5f98a10c ^
551d155c ^
c6520d96 ^
bc643692 ^





b94ea5c8 ^
10ffce98 ^
551d155c ^
5f98a10c ^
551d155c ^
051c4738 ^
5f98a10c ^
551d155c ^
c6520d96 ^
bc643692 ^





7136ddd5 ^
10ffce98 ^
551d155c ^
5f98a10c ^
551d155c ^
fb85b3b4 ^
5f98a10c ^
fb85b3b4 ^







7136ddd5 ^
fb85b3b4 ^

6808ff7d ^
fb85b3b4 ^
c8a58cdc ^



c6520d96 ^
bc643692 ^


c6520d96 ^
c8a58cdc ^





795f5244 ^
b8515e8a ^
c8a58cdc ^
b8515e8a ^
795f5244 ^
e4630643 ^

b8515e8a ^

1ad3fe9e ^
5f98a10c ^
c8a58cdc ^

b8515e8a ^



c6034af3 ^

18e626df ^
5f98a10c ^
18e626df ^

cfb142b9 ^
795f5244 ^
c8a58cdc ^

e74a2940 ^






59b6a42a ^











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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437