Make ex line fully unicode aware

This commit is contained in:
Drew DeVault 2018-02-27 16:46:14 -05:00
parent 1418e1b9dc
commit 384fe0d826
1 changed files with 19 additions and 20 deletions

View File

@ -1,6 +1,7 @@
package widgets package widgets
import ( import (
"github.com/mattn/go-runewidth"
tb "github.com/nsf/termbox-go" tb "github.com/nsf/termbox-go"
"git.sr.ht/~sircmpwn/aerc2/lib/ui" "git.sr.ht/~sircmpwn/aerc2/lib/ui"
@ -13,7 +14,7 @@ import (
// TODO: scrolling // TODO: scrolling
type ExLine struct { type ExLine struct {
command *string command []rune
commit func(cmd *string) commit func(cmd *string)
index int index int
scroll int scroll int
@ -22,8 +23,7 @@ type ExLine struct {
} }
func NewExLine() *ExLine { func NewExLine() *ExLine {
cmd := "" return &ExLine{command: []rune{}}
return &ExLine{command: &cmd}
} }
func (ex *ExLine) OnInvalidate(onInvalidate func(d ui.Drawable)) { func (ex *ExLine) OnInvalidate(onInvalidate func(d ui.Drawable)) {
@ -43,49 +43,48 @@ func (ex *ExLine) Draw(ctx *ui.Context) {
Ch: ' ', Ch: ' ',
} }
ctx.Fill(0, 0, ctx.Width(), ctx.Height(), cell) ctx.Fill(0, 0, ctx.Width(), ctx.Height(), cell)
ctx.Printf(0, 0, cell, ":%s", *ex.command) ctx.Printf(0, 0, cell, ":%s", string(ex.command))
tb.SetCursor(ctx.X()+ex.index-ex.scroll+1, ctx.Y()) cells := runewidth.StringWidth(string(ex.command[:ex.index]))
tb.SetCursor(ctx.X()+cells+1, ctx.Y())
} }
func (ex *ExLine) insert(ch rune) { func (ex *ExLine) insert(ch rune) {
newCmd := (*ex.command)[:ex.index] + string(ch) + (*ex.command)[ex.index:] left := ex.command[:ex.index]
ex.command = &newCmd right := ex.command[ex.index:]
ex.command = append(left, append([]rune{ch}, right...)...)
ex.index++ ex.index++
ex.Invalidate() ex.Invalidate()
} }
func (ex *ExLine) deleteWord() { func (ex *ExLine) deleteWord() {
// TODO: Break on any of / " ' // TODO: Break on any of / " '
if len(*ex.command) == 0 { if len(ex.command) == 0 {
return return
} }
i := ex.index - 1 i := ex.index - 1
if (*ex.command)[i] == ' ' { if ex.command[i] == ' ' {
i-- i--
} }
for ; i >= 0; i-- { for ; i >= 0; i-- {
if (*ex.command)[i] == ' ' { if ex.command[i] == ' ' {
break break
} }
} }
newCmd := (*ex.command)[:i+1] + (*ex.command)[ex.index:] ex.command = append(ex.command[:i+1], ex.command[ex.index:]...)
ex.command = &newCmd
ex.index = i + 1 ex.index = i + 1
ex.Invalidate() ex.Invalidate()
} }
func (ex *ExLine) deleteChar() { func (ex *ExLine) deleteChar() {
if len(*ex.command) > 0 && ex.index != len(*ex.command) { if len(ex.command) > 0 && ex.index != len(ex.command) {
newCmd := (*ex.command)[:ex.index] + (*ex.command)[ex.index+1:] ex.command = append(ex.command[:ex.index], ex.command[ex.index+1:]...)
ex.command = &newCmd
ex.Invalidate() ex.Invalidate()
} }
} }
func (ex *ExLine) backspace() { func (ex *ExLine) backspace() {
if len(*ex.command) > 0 && ex.index != 0 { if len(ex.command) > 0 && ex.index != 0 {
newCmd := (*ex.command)[:ex.index-1] + (*ex.command)[ex.index:] ex.command = append(ex.command[:ex.index-1], ex.command[ex.index:]...)
ex.command = &newCmd
ex.index-- ex.index--
ex.Invalidate() ex.Invalidate()
} }
@ -107,7 +106,7 @@ func (ex *ExLine) Event(event tb.Event) bool {
ex.Invalidate() ex.Invalidate()
} }
case tb.KeyCtrlF, tb.KeyArrowRight: case tb.KeyCtrlF, tb.KeyArrowRight:
if ex.index < len(*ex.command) { if ex.index < len(ex.command) {
ex.index++ ex.index++
ex.Invalidate() ex.Invalidate()
} }
@ -115,7 +114,7 @@ func (ex *ExLine) Event(event tb.Event) bool {
ex.index = 0 ex.index = 0
ex.Invalidate() ex.Invalidate()
case tb.KeyCtrlE, tb.KeyEnd: case tb.KeyCtrlE, tb.KeyEnd:
ex.index = len(*ex.command) ex.index = len(ex.command)
ex.Invalidate() ex.Invalidate()
case tb.KeyCtrlW: case tb.KeyCtrlW:
ex.deleteWord() ex.deleteWord()