5685a17674
Many Drawable implementations have their own Invalidate and OnInvalidate functions, with an unexported onInvalidate field. However OnInvalidate and Invalidate are usually not called in the same goroutine. This results in a race on this field, e.g.: Read at 0x00c000094748 by goroutine 7: git.sr.ht/~sircmpwn/aerc2/widgets.NewDirectoryList.func1() /home/simon/src/aerc2/widgets/dirlist.go:85 +0x56 git.sr.ht/~sircmpwn/aerc2/widgets.(*Spinner).Start.func1() /home/simon/src/aerc2/widgets/spinner.go:93 +0x1bb Previous write at 0x00c000094748 by main goroutine: [failed to restore the stack] Goroutine 7 (running) created at: git.sr.ht/~sircmpwn/aerc2/widgets.(*Spinner).Start() /home/simon/src/aerc2/widgets/spinner.go:46 +0x8f git.sr.ht/~sircmpwn/aerc2/widgets.NewDirectoryList() /home/simon/src/aerc2/widgets/dirlist.go:37 +0x286 git.sr.ht/~sircmpwn/aerc2/widgets.NewAccountView() /home/simon/src/aerc2/widgets/account.go:50 +0x5ca git.sr.ht/~sircmpwn/aerc2/widgets.NewAerc() /home/simon/src/aerc2/widgets/aerc.go:60 +0x800 main.main() /home/simon/src/aerc2/aerc.go:65 +0x33e To fix this, introduce a new type, Invalidatable, which protects the field. Unfortunately the Drawable must be passed to the callback function in Invalidate, so we still need to re-implement this in each Invalidatable user.
142 lines
2.9 KiB
Go
142 lines
2.9 KiB
Go
package widgets
|
|
|
|
import (
|
|
"github.com/gdamore/tcell"
|
|
"github.com/mattn/go-runewidth"
|
|
|
|
"git.sr.ht/~sircmpwn/aerc2/lib/ui"
|
|
)
|
|
|
|
// TODO: history
|
|
// TODO: tab completion
|
|
// TODO: scrolling
|
|
|
|
type ExLine struct {
|
|
ui.Invalidatable
|
|
command []rune
|
|
commit func(cmd string)
|
|
ctx *ui.Context
|
|
cancel func()
|
|
cells int
|
|
focus bool
|
|
index int
|
|
scroll int
|
|
|
|
onInvalidate func(d ui.Drawable)
|
|
}
|
|
|
|
func NewExLine(commit func(cmd string), cancel func()) *ExLine {
|
|
return &ExLine{
|
|
cancel: cancel,
|
|
cells: -1,
|
|
commit: commit,
|
|
command: []rune{},
|
|
}
|
|
}
|
|
|
|
func (ex *ExLine) Invalidate() {
|
|
ex.DoInvalidate(ex)
|
|
}
|
|
|
|
func (ex *ExLine) Draw(ctx *ui.Context) {
|
|
ex.ctx = ctx // gross
|
|
ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', tcell.StyleDefault)
|
|
ctx.Printf(0, 0, tcell.StyleDefault, ":%s", string(ex.command))
|
|
cells := runewidth.StringWidth(string(ex.command[:ex.index]))
|
|
if cells != ex.cells {
|
|
ctx.SetCursor(cells+1, 0)
|
|
}
|
|
}
|
|
|
|
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) {
|
|
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 / " '
|
|
if len(ex.command) == 0 {
|
|
return
|
|
}
|
|
i := ex.index - 1
|
|
if ex.command[i] == ' ' {
|
|
i--
|
|
}
|
|
for ; i >= 0; i-- {
|
|
if ex.command[i] == ' ' {
|
|
break
|
|
}
|
|
}
|
|
ex.command = append(ex.command[:i+1], ex.command[ex.index:]...)
|
|
ex.index = i + 1
|
|
ex.Invalidate()
|
|
}
|
|
|
|
func (ex *ExLine) deleteChar() {
|
|
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() {
|
|
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:
|
|
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:
|
|
ex.index = len(ex.command)
|
|
ex.Invalidate()
|
|
case tcell.KeyCtrlW:
|
|
ex.deleteWord()
|
|
case tcell.KeyEnter:
|
|
if ex.ctx != nil {
|
|
ex.ctx.HideCursor()
|
|
}
|
|
ex.commit(string(ex.command))
|
|
case tcell.KeyEsc, tcell.KeyCtrlC:
|
|
if ex.ctx != nil {
|
|
ex.ctx.HideCursor()
|
|
}
|
|
ex.cancel()
|
|
case tcell.KeyRune:
|
|
ex.insert(event.Rune())
|
|
}
|
|
}
|
|
return true
|
|
}
|