Skip to content

Commit

Permalink
Correct cursor movement when drawing wide characters in statusbar (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
kimtore committed Dec 24, 2023
1 parent 6f0282a commit 628d497
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions widgets/multibar.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/ambientsound/pms/style"
"github.com/ambientsound/pms/tabcomplete"
"github.com/ambientsound/pms/utils"
`github.com/mattn/go-runewidth`

"github.com/gdamore/tcell/v2"
"github.com/gdamore/tcell/v2/views"
Expand Down Expand Up @@ -181,9 +182,14 @@ func (m *MultibarWidget) RuneLen() int {
return len(m.runes)
}

// Cursor returns the cursor position.
// Cursor returns the physical cursor position.
// The `cursor` member points to the actual character index, which may be a wide character.
// Thus, we must calculate the actual physical width of the content up to the point where the cursor is.
func (m *MultibarWidget) Cursor() int {
return m.cursor
if m.cursor == 0 {
return m.cursor
}
return runewidth.StringWidth(string(m.runes[:m.cursor]))
}

// validateCursor makes sure the cursor stays within boundaries.
Expand Down Expand Up @@ -437,7 +443,7 @@ func (m *MultibarWidget) handleTextInputEvent(ev *tcell.EventKey) bool {

// handleNormalEvent is called when an input event is received during command mode.
func (m *MultibarWidget) handleNormalEvent(ev *tcell.EventKey) bool {
//console.Log("Input event in command mode: %s %s", ke.Key, string(ke.Rune))
// console.Log("Input event in command mode: %s %s", ke.Key, string(ke.Rune))
m.events <- ev
return true
}
Expand Down

0 comments on commit 628d497

Please sign in to comment.