aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/selecter.go
diff options
context:
space:
mode:
authorGreg Anders <greg@gpanders.com>2019-11-15 13:28:34 -0700
committerDrew DeVault <sir@cmpwn.com>2019-11-17 13:19:13 -0500
commit4bdef7d8609aa2d382fa74018e28ccb176276615 (patch)
treeef22e1ef17c3247de6531715c7502a3a3d313867 /widgets/selecter.go
parent809083f8438401e2ee1d2d14712677f55eb4e4ef (diff)
downloadaerc-4bdef7d8609aa2d382fa74018e28ccb176276615.tar.gz
Add UI options to save/pipe messages with unsupported mimetypes
Adds a message indicating the user's ability to :save or :pipe a message with an unsupported mimetype and also adds a selector widget (similar to the tutorial). The selector widget was previously defined in the account wizard module, so this commit breaks it out into its own module to allow for re-use. Further, modify the BeginExLine() function to take an argument that pre-populates the command line, allowing functions to initiate an ex command without executing it. Closes #95.
Diffstat (limited to 'widgets/selecter.go')
-rw-r--r--widgets/selecter.go103
1 files changed, 103 insertions, 0 deletions
diff --git a/widgets/selecter.go b/widgets/selecter.go
new file mode 100644
index 00000000..7fae9cda
--- /dev/null
+++ b/widgets/selecter.go
@@ -0,0 +1,103 @@
+package widgets
+
+import (
+ "github.com/gdamore/tcell"
+
+ "git.sr.ht/~sircmpwn/aerc/lib/ui"
+)
+
+type Selecter struct {
+ ui.Invalidatable
+ chooser bool
+ focused bool
+ focus int
+ options []string
+
+ onChoose func(option string)
+ onSelect func(option string)
+}
+
+func NewSelecter(options []string, focus int) *Selecter {
+ return &Selecter{
+ focus: focus,
+ options: options,
+ }
+}
+
+func (sel *Selecter) Chooser(chooser bool) *Selecter {
+ sel.chooser = chooser
+ return sel
+}
+
+func (sel *Selecter) Invalidate() {
+ sel.DoInvalidate(sel)
+}
+
+func (sel *Selecter) Draw(ctx *ui.Context) {
+ ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', tcell.StyleDefault)
+ x := 2
+ for i, option := range sel.options {
+ style := tcell.StyleDefault
+ if sel.focus == i {
+ if sel.focused {
+ style = style.Reverse(true)
+ } else if sel.chooser {
+ style = style.Bold(true)
+ }
+ }
+ x += ctx.Printf(x, 1, style, "[%s]", option)
+ x += 5
+ }
+}
+
+func (sel *Selecter) OnChoose(fn func(option string)) *Selecter {
+ sel.onChoose = fn
+ return sel
+}
+
+func (sel *Selecter) OnSelect(fn func(option string)) *Selecter {
+ sel.onSelect = fn
+ return sel
+}
+
+func (sel *Selecter) Selected() string {
+ return sel.options[sel.focus]
+}
+
+func (sel *Selecter) Focus(focus bool) {
+ sel.focused = focus
+ sel.Invalidate()
+}
+
+func (sel *Selecter) Event(event tcell.Event) bool {
+ switch event := event.(type) {
+ case *tcell.EventKey:
+ switch event.Key() {
+ case tcell.KeyCtrlH:
+ fallthrough
+ case tcell.KeyLeft:
+ if sel.focus > 0 {
+ sel.focus--
+ sel.Invalidate()
+ }
+ if sel.onSelect != nil {
+ sel.onSelect(sel.Selected())
+ }
+ case tcell.KeyCtrlL:
+ fallthrough
+ case tcell.KeyRight:
+ if sel.focus < len(sel.options)-1 {
+ sel.focus++
+ sel.Invalidate()
+ }
+ if sel.onSelect != nil {
+ sel.onSelect(sel.Selected())
+ }
+ case tcell.KeyEnter:
+ if sel.onChoose != nil {
+ sel.onChoose(sel.Selected())
+ }
+ }
+ }
+ return false
+}