2018-02-27 04:54:39 +01:00
|
|
|
package widgets
|
2018-02-27 04:41:54 +01:00
|
|
|
|
|
|
|
import (
|
2018-06-01 09:58:00 +02:00
|
|
|
"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"
|
2018-02-27 04:41:54 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// TODO: history
|
|
|
|
// TODO: tab completion
|
|
|
|
// TODO: scrolling
|
|
|
|
|
|
|
|
type ExLine struct {
|
2018-02-27 22:46:14 +01:00
|
|
|
command []rune
|
2018-02-28 03:02:56 +01:00
|
|
|
commit func(cmd string)
|
|
|
|
cancel func()
|
2018-02-27 04:41:54 +01:00
|
|
|
index int
|
|
|
|
scroll int
|
|
|
|
|
2018-02-27 04:54:39 +01:00
|
|
|
onInvalidate func(d ui.Drawable)
|
2018-02-27 04:41:54 +01:00
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
commit: commit,
|
|
|
|
command: []rune{},
|
|
|
|
}
|
2018-02-27 04:41:54 +01:00
|
|
|
}
|
|
|
|
|
2018-02-27 04:54:39 +01:00
|
|
|
func (ex *ExLine) OnInvalidate(onInvalidate func(d ui.Drawable)) {
|
2018-02-27 04:41:54 +01:00
|
|
|
ex.onInvalidate = onInvalidate
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ex *ExLine) Invalidate() {
|
|
|
|
if ex.onInvalidate != nil {
|
|
|
|
ex.onInvalidate(ex)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-27 04:54:39 +01:00
|
|
|
func (ex *ExLine) Draw(ctx *ui.Context) {
|
2018-06-01 09:58:00 +02:00
|
|
|
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]))
|
2018-06-12 02:04:21 +02:00
|
|
|
ctx.SetCursor(cells+1, 0)
|
2018-02-27 04:41:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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...)...)
|
2018-02-27 04:41:54 +01:00
|
|
|
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 {
|
2018-02-27 04:41:54 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
i := ex.index - 1
|
2018-02-27 22:46:14 +01:00
|
|
|
if ex.command[i] == ' ' {
|
2018-02-27 04:41:54 +01:00
|
|
|
i--
|
|
|
|
}
|
|
|
|
for ; i >= 0; i-- {
|
2018-02-27 22:46:14 +01:00
|
|
|
if ex.command[i] == ' ' {
|
2018-02-27 04:41:54 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2018-02-27 22:46:14 +01:00
|
|
|
ex.command = append(ex.command[:i+1], ex.command[ex.index:]...)
|
2018-02-27 04:41:54 +01:00
|
|
|
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:]...)
|
2018-02-27 04:41:54 +01:00
|
|
|
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:]...)
|
2018-02-27 04:41:54 +01:00
|
|
|
ex.index--
|
|
|
|
ex.Invalidate()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-01 09:58:00 +02:00
|
|
|
func (ex *ExLine) Event(event tcell.Event) bool {
|
|
|
|
switch event := event.(type) {
|
|
|
|
case *tcell.EventKey:
|
|
|
|
switch event.Key() {
|
|
|
|
case tcell.KeyBackspace, tcell.KeyBackspace2:
|
2018-02-27 04:41:54 +01:00
|
|
|
ex.backspace()
|
2018-06-01 09:58:00 +02:00
|
|
|
case tcell.KeyCtrlD, tcell.KeyDelete:
|
2018-02-27 04:41:54 +01:00
|
|
|
ex.deleteChar()
|
2018-06-01 09:58:00 +02:00
|
|
|
case tcell.KeyCtrlB, tcell.KeyLeft:
|
2018-02-27 04:41:54 +01:00
|
|
|
if ex.index > 0 {
|
|
|
|
ex.index--
|
|
|
|
ex.Invalidate()
|
|
|
|
}
|
2018-06-01 09:58:00 +02:00
|
|
|
case tcell.KeyCtrlF, tcell.KeyRight:
|
2018-02-27 22:46:14 +01:00
|
|
|
if ex.index < len(ex.command) {
|
2018-02-27 04:41:54 +01:00
|
|
|
ex.index++
|
|
|
|
ex.Invalidate()
|
|
|
|
}
|
2018-06-01 09:58:00 +02:00
|
|
|
case tcell.KeyCtrlA, tcell.KeyHome:
|
2018-02-27 04:41:54 +01:00
|
|
|
ex.index = 0
|
|
|
|
ex.Invalidate()
|
2018-06-01 09:58:00 +02:00
|
|
|
case tcell.KeyCtrlE, tcell.KeyEnd:
|
2018-02-27 22:46:14 +01:00
|
|
|
ex.index = len(ex.command)
|
2018-02-27 04:41:54 +01:00
|
|
|
ex.Invalidate()
|
2018-06-01 09:58:00 +02:00
|
|
|
case tcell.KeyCtrlW:
|
2018-02-27 04:41:54 +01:00
|
|
|
ex.deleteWord()
|
2018-06-01 09:58:00 +02:00
|
|
|
case tcell.KeyEnter:
|
|
|
|
//ex.ctx.Screen().HideCursor()
|
2018-02-28 03:02:56 +01:00
|
|
|
ex.commit(string(ex.command))
|
2018-06-01 09:58:00 +02:00
|
|
|
case tcell.KeyEsc, tcell.KeyCtrlC:
|
|
|
|
//ex.ctx.Screen().HideCursor()
|
2018-02-28 03:02:56 +01:00
|
|
|
ex.cancel()
|
2018-02-27 04:41:54 +01:00
|
|
|
default:
|
2018-06-01 09:58:00 +02:00
|
|
|
if event.Rune() != 0 {
|
|
|
|
ex.insert(event.Rune())
|
2018-02-27 04:41:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|