perf(textinput): use uniseg.StringWidth

This commit is contained in:
Maas Lalani
2024-01-22 15:37:12 -05:00
parent 6fe92f94b5
commit 6399027ab7
+4 -3
View File
@@ -13,6 +13,7 @@ import (
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
rw "github.com/mattn/go-runewidth" rw "github.com/mattn/go-runewidth"
"github.com/rivo/uniseg"
) )
// Internal messages for clipboard operations. // Internal messages for clipboard operations.
@@ -332,7 +333,7 @@ func (m *Model) insertRunesFromUserInput(v []rune) {
// If a max width is defined, perform some logic to treat the visible area // If a max width is defined, perform some logic to treat the visible area
// as a horizontally scrolling viewport. // as a horizontally scrolling viewport.
func (m *Model) handleOverflow() { func (m *Model) handleOverflow() {
if m.Width <= 0 || rw.StringWidth(string(m.value)) <= m.Width { if m.Width <= 0 || uniseg.StringWidth(string(m.value)) <= m.Width {
m.offset = 0 m.offset = 0
m.offsetRight = len(m.value) m.offsetRight = len(m.value)
return return
@@ -541,7 +542,7 @@ func (m *Model) wordForward() {
func (m Model) echoTransform(v string) string { func (m Model) echoTransform(v string) string {
switch m.EchoMode { switch m.EchoMode {
case EchoPassword: case EchoPassword:
return strings.Repeat(string(m.EchoCharacter), rw.StringWidth(v)) return strings.Repeat(string(m.EchoCharacter), uniseg.StringWidth(v))
case EchoNone: case EchoNone:
return "" return ""
case EchoNormal: case EchoNormal:
@@ -688,7 +689,7 @@ func (m Model) View() string {
// If a max width and background color were set fill the empty spaces with // If a max width and background color were set fill the empty spaces with
// the background color. // the background color.
valWidth := rw.StringWidth(string(value)) valWidth := uniseg.StringWidth(string(value))
if m.Width > 0 && valWidth <= m.Width { if m.Width > 0 && valWidth <= m.Width {
padding := max(0, m.Width-valWidth) padding := max(0, m.Width-valWidth)
if valWidth+padding <= m.Width && pos < len(value) { if valWidth+padding <= m.Width && pos < len(value) {