Merge pull request #451 from peco/topic/set-cursor

Call SetCursor when drawing the prompt
This commit is contained in:
lestrrat 2018-03-31 05:23:53 +09:00 committed by GitHub
commit 9385f4ec68
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 3 deletions

View file

@ -6,14 +6,18 @@ func (c *Caret) Pos() int {
return c.pos
}
func (c *Caret) setPos_nolock(p int) {
c.pos = p
}
func (c *Caret) SetPos(p int) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.pos = p
c.setPos_nolock(p)
}
func (c *Caret) Move(diff int) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.pos = c.pos + diff
c.setPos_nolock(c.pos + diff)
}

View file

@ -157,6 +157,7 @@ type Screen interface {
Print(PrintArgs) int
Resume()
SetCell(int, int, rune, termbox.Attribute, termbox.Attribute)
SetCursor(int, int)
Size() (int, int)
SendEvent(termbox.Event)
Suspend()

View file

@ -111,6 +111,10 @@ func (u UserPrompt) Draw(state *Peco) {
fg := u.styles.Query.fg
bg := u.styles.Query.bg
// Used to notify termbox where our cursor is
var posX int
switch ql {
case 0:
u.screen.Print(PrintArgs{
@ -120,6 +124,7 @@ func (u UserPrompt) Draw(state *Peco) {
Bg: bg,
Fill: true,
})
posX = u.promptLen + 1
u.screen.Print(PrintArgs{
X: u.promptLen + 1,
Y: location,
@ -145,8 +150,9 @@ func (u UserPrompt) Draw(state *Peco) {
Msg: qs,
Fill: false,
})
posX = u.promptLen + 1 + int(runewidth.StringWidth(qs))
u.screen.Print(PrintArgs{
X: u.promptLen + 1 + int(runewidth.StringWidth(qs)),
X: posX,
Y: location,
Fg: fg | termbox.AttrReverse,
Bg: bg | termbox.AttrReverse,
@ -154,6 +160,7 @@ func (u UserPrompt) Draw(state *Peco) {
Fill: false,
})
default:
posX = c.Pos()
// the caret is in the middle of the string
prev := int(0)
var i int
@ -179,6 +186,8 @@ func (u UserPrompt) Draw(state *Peco) {
})
}
u.screen.SetCursor(posX, location)
width, _ := u.screen.Size()
loc := state.Location()

View file

@ -101,6 +101,9 @@ func NewDummyScreen() *dummyScreen {
}
}
func (d dummyScreen) SetCursor(_, _ int) {
}
func (d dummyScreen) Init() error {
return nil
}

View file

@ -34,6 +34,10 @@ func (t *Termbox) Close() error {
return nil
}
func (t *Termbox) SetCursor(x, y int) {
termbox.SetCursor(x, y)
}
// SendEvent is used to allow programmers generate random
// events, but it's only useful for testing purposes.
// When interactiving with termbox-go, this method is a noop