Add cursor handling in ex line

This commit is contained in:
Drew DeVault 2019-01-14 08:07:24 -05:00
parent 501c95d852
commit d35213eaab
2 changed files with 18 additions and 11 deletions

View File

@ -10,7 +10,9 @@ import (
// A context allows you to draw in a sub-region of the terminal // A context allows you to draw in a sub-region of the terminal
type Context struct { type Context struct {
screen tcell.Screen
viewport *views.ViewPort viewport *views.ViewPort
x, y int
} }
func (ctx *Context) X() int { func (ctx *Context) X() int {
@ -35,7 +37,7 @@ func (ctx *Context) Height() int {
func NewContext(width, height int, screen tcell.Screen) *Context { func NewContext(width, height int, screen tcell.Screen) *Context {
vp := views.NewViewPort(screen, 0, 0, width, height) vp := views.NewViewPort(screen, 0, 0, width, height)
return &Context{vp} return &Context{screen, vp, 0, 0}
} }
func (ctx *Context) Subcontext(x, y, width, height int) *Context { func (ctx *Context) Subcontext(x, y, width, height int) *Context {
@ -47,7 +49,7 @@ func (ctx *Context) Subcontext(x, y, width, height int) *Context {
panic(fmt.Errorf("Attempted to create context larger than parent")) panic(fmt.Errorf("Attempted to create context larger than parent"))
} }
vp := views.NewViewPort(ctx.viewport, x, y, width, height) vp := views.NewViewPort(ctx.viewport, x, y, width, height)
return &Context{vp} return &Context{ctx.screen, vp, ctx.x + x, ctx.y + y}
} }
func (ctx *Context) SetCell(x, y int, ch rune, style tcell.Style) { func (ctx *Context) SetCell(x, y int, ch rune, style tcell.Style) {
@ -105,10 +107,9 @@ func (ctx *Context) Fill(x, y, width, height int, rune rune, style tcell.Style)
} }
func (ctx *Context) SetCursor(x, y int) { func (ctx *Context) SetCursor(x, y int) {
// FIXME: Cursor needs to be set on tcell.Screen, or layout has to ctx.screen.ShowCursor(ctx.x+x, ctx.y+y)
// provide a CellModel }
// cv := views.NewCellView()
// cv.Init() func (ctx *Context) HideCursor() {
// cv.SetView(ctx.viewport) ctx.screen.HideCursor()
// cv.SetCursor(x, y)
} }

View File

@ -14,7 +14,9 @@ import (
type ExLine struct { type ExLine struct {
command []rune command []rune
commit func(cmd string) commit func(cmd string)
ctx *ui.Context
cancel func() cancel func()
cells int
index int index int
scroll int scroll int
@ -24,6 +26,7 @@ type ExLine struct {
func NewExLine(commit func(cmd string), cancel func()) *ExLine { func NewExLine(commit func(cmd string), cancel func()) *ExLine {
return &ExLine{ return &ExLine{
cancel: cancel, cancel: cancel,
cells: -1,
commit: commit, commit: commit,
command: []rune{}, command: []rune{},
} }
@ -40,10 +43,13 @@ func (ex *ExLine) Invalidate() {
} }
func (ex *ExLine) Draw(ctx *ui.Context) { func (ex *ExLine) Draw(ctx *ui.Context) {
ex.ctx = ctx // gross
ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', tcell.StyleDefault) ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', tcell.StyleDefault)
ctx.Printf(0, 0, tcell.StyleDefault, ":%s", string(ex.command)) ctx.Printf(0, 0, tcell.StyleDefault, ":%s", string(ex.command))
cells := runewidth.StringWidth(string(ex.command[:ex.index])) cells := runewidth.StringWidth(string(ex.command[:ex.index]))
ctx.SetCursor(cells+1, 0) if cells != ex.cells {
ctx.SetCursor(cells+1, 0)
}
} }
func (ex *ExLine) insert(ch rune) { func (ex *ExLine) insert(ch rune) {
@ -115,10 +121,10 @@ func (ex *ExLine) Event(event tcell.Event) bool {
case tcell.KeyCtrlW: case tcell.KeyCtrlW:
ex.deleteWord() ex.deleteWord()
case tcell.KeyEnter: case tcell.KeyEnter:
//ex.ctx.Screen().HideCursor() ex.ctx.HideCursor()
ex.commit(string(ex.command)) ex.commit(string(ex.command))
case tcell.KeyEsc, tcell.KeyCtrlC: case tcell.KeyEsc, tcell.KeyCtrlC:
//ex.ctx.Screen().HideCursor() ex.ctx.HideCursor()
ex.cancel() ex.cancel()
default: default:
if event.Rune() != 0 { if event.Rune() != 0 {