aerc

Working clone of aerc-mail.org
git clone git://git.danielmoch.com/aerc.git
Log | Files | Refs | README | LICENSE

exline.go (1002B)


      1 package widgets
      2 
      3 import (
      4 	"github.com/gdamore/tcell"
      5 
      6 	"git.sr.ht/~sircmpwn/aerc/lib/ui"
      7 )
      8 
      9 type ExLine struct {
     10 	ui.Invalidatable
     11 	cancel func()
     12 	commit func(cmd string)
     13 	input  *ui.TextInput
     14 }
     15 
     16 func NewExLine(commit func(cmd string), cancel func()) *ExLine {
     17 	input := ui.NewTextInput("").Prompt(":")
     18 	exline := &ExLine{
     19 		cancel: cancel,
     20 		commit: commit,
     21 		input:  input,
     22 	}
     23 	input.OnInvalidate(func(d ui.Drawable) {
     24 		exline.Invalidate()
     25 	})
     26 	return exline
     27 }
     28 
     29 func (ex *ExLine) Invalidate() {
     30 	ex.DoInvalidate(ex)
     31 }
     32 
     33 func (ex *ExLine) Draw(ctx *ui.Context) {
     34 	ex.input.Draw(ctx)
     35 }
     36 
     37 func (ex *ExLine) Focus(focus bool) {
     38 	ex.input.Focus(focus)
     39 }
     40 
     41 func (ex *ExLine) Event(event tcell.Event) bool {
     42 	switch event := event.(type) {
     43 	case *tcell.EventKey:
     44 		switch event.Key() {
     45 		case tcell.KeyEnter:
     46 			ex.input.Focus(false)
     47 			ex.commit(ex.input.String())
     48 		case tcell.KeyEsc, tcell.KeyCtrlC:
     49 			ex.input.Focus(false)
     50 			ex.cancel()
     51 		default:
     52 			return ex.input.Event(event)
     53 		}
     54 	}
     55 	return true
     56 }