aerc/widgets/exline.go

143 lines
2.9 KiB
Go
Raw Normal View History

2018-02-27 04:54:39 +01:00
package widgets
import (
"github.com/gdamore/tcell"
2018-06-12 02:04:21 +02:00
"github.com/mattn/go-runewidth"
2018-02-27 04:54:39 +01:00
"git.sr.ht/~sircmpwn/aerc2/lib/ui"
)
// TODO: history
// TODO: tab completion
// TODO: scrolling
type ExLine struct {
ui.Invalidatable
2018-02-27 22:46:14 +01:00
command []rune
2018-02-28 03:02:56 +01:00
commit func(cmd string)
2019-01-14 14:07:24 +01:00
ctx *ui.Context
2018-02-28 03:02:56 +01:00
cancel func()
2019-01-14 14:07:24 +01:00
cells int
2019-03-17 19:02:33 +01:00
focus bool
index int
scroll int
2018-02-27 04:54:39 +01:00
onInvalidate func(d ui.Drawable)
}
2018-06-12 02:04:21 +02:00
func NewExLine(commit func(cmd string), cancel func()) *ExLine {
2018-02-28 03:02:56 +01:00
return &ExLine{
cancel: cancel,
2019-01-14 14:07:24 +01:00
cells: -1,
2018-02-28 03:02:56 +01:00
commit: commit,
command: []rune{},
}
}
func (ex *ExLine) Invalidate() {
ex.DoInvalidate(ex)
}
2018-02-27 04:54:39 +01:00
func (ex *ExLine) Draw(ctx *ui.Context) {
2019-01-14 14:07:24 +01:00
ex.ctx = ctx // gross
ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', tcell.StyleDefault)
ctx.Printf(0, 0, tcell.StyleDefault, ":%s", string(ex.command))
2018-02-27 22:46:14 +01:00
cells := runewidth.StringWidth(string(ex.command[:ex.index]))
2019-01-14 14:07:24 +01:00
if cells != ex.cells {
ctx.SetCursor(cells+1, 0)
}
}
2019-03-17 19:02:33 +01:00
func (ex *ExLine) Focus(focus bool) {
ex.focus = focus
if focus && ex.ctx != nil {
cells := runewidth.StringWidth(string(ex.command[:ex.index]))
ex.ctx.SetCursor(cells+1, 0)
}
}
func (ex *ExLine) insert(ch rune) {
2018-02-27 22:46:14 +01:00
left := ex.command[:ex.index]
right := ex.command[ex.index:]
ex.command = append(left, append([]rune{ch}, right...)...)
ex.index++
ex.Invalidate()
}
func (ex *ExLine) deleteWord() {
// TODO: Break on any of / " '
2018-02-27 22:46:14 +01:00
if len(ex.command) == 0 {
return
}
i := ex.index - 1
2018-02-27 22:46:14 +01:00
if ex.command[i] == ' ' {
i--
}
for ; i >= 0; i-- {
2018-02-27 22:46:14 +01:00
if ex.command[i] == ' ' {
break
}
}
2018-02-27 22:46:14 +01:00
ex.command = append(ex.command[:i+1], ex.command[ex.index:]...)
ex.index = i + 1
ex.Invalidate()
}
func (ex *ExLine) deleteChar() {
2018-02-27 22:46:14 +01:00
if len(ex.command) > 0 && ex.index != len(ex.command) {
ex.command = append(ex.command[:ex.index], ex.command[ex.index+1:]...)
ex.Invalidate()
}
}
func (ex *ExLine) backspace() {
2018-02-27 22:46:14 +01:00
if len(ex.command) > 0 && ex.index != 0 {
ex.command = append(ex.command[:ex.index-1], ex.command[ex.index:]...)
ex.index--
ex.Invalidate()
}
}
func (ex *ExLine) Event(event tcell.Event) bool {
switch event := event.(type) {
case *tcell.EventKey:
switch event.Key() {
case tcell.KeyBackspace, tcell.KeyBackspace2:
ex.backspace()
case tcell.KeyCtrlD, tcell.KeyDelete:
ex.deleteChar()
case tcell.KeyCtrlB, tcell.KeyLeft:
if ex.index > 0 {
ex.index--
ex.Invalidate()
}
case tcell.KeyCtrlF, tcell.KeyRight:
2018-02-27 22:46:14 +01:00
if ex.index < len(ex.command) {
ex.index++
ex.Invalidate()
}
case tcell.KeyCtrlA, tcell.KeyHome:
ex.index = 0
ex.Invalidate()
case tcell.KeyCtrlE, tcell.KeyEnd:
2018-02-27 22:46:14 +01:00
ex.index = len(ex.command)
ex.Invalidate()
case tcell.KeyCtrlW:
ex.deleteWord()
case tcell.KeyEnter:
2019-03-15 06:46:14 +01:00
if ex.ctx != nil {
ex.ctx.HideCursor()
}
2018-02-28 03:02:56 +01:00
ex.commit(string(ex.command))
case tcell.KeyEsc, tcell.KeyCtrlC:
2019-03-15 06:46:14 +01:00
if ex.ctx != nil {
ex.ctx.HideCursor()
}
2018-02-28 03:02:56 +01:00
ex.cancel()
case tcell.KeyRune:
ex.insert(event.Rune())
}
}
return true
}