Skip to content

Commit

Permalink
perf(textinput): use uniseg.StringWidth
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani committed Jan 22, 2024
1 parent 6fe92f9 commit 6399027
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions textinput/textinput.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
rw "github.com/mattn/go-runewidth"
"github.com/rivo/uniseg"
)

// Internal messages for clipboard operations.
Expand Down Expand Up @@ -332,7 +333,7 @@ func (m *Model) insertRunesFromUserInput(v []rune) {
// If a max width is defined, perform some logic to treat the visible area
// as a horizontally scrolling viewport.
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.offsetRight = len(m.value)
return
Expand Down Expand Up @@ -541,7 +542,7 @@ func (m *Model) wordForward() {
func (m Model) echoTransform(v string) string {
switch m.EchoMode {
case EchoPassword:
return strings.Repeat(string(m.EchoCharacter), rw.StringWidth(v))
return strings.Repeat(string(m.EchoCharacter), uniseg.StringWidth(v))
case EchoNone:
return ""
case EchoNormal:
Expand Down Expand Up @@ -688,7 +689,7 @@ func (m Model) View() string {

// If a max width and background color were set fill the empty spaces with
// the background color.
valWidth := rw.StringWidth(string(value))
valWidth := uniseg.StringWidth(string(value))
if m.Width > 0 && valWidth <= m.Width {
padding := max(0, m.Width-valWidth)
if valWidth+padding <= m.Width && pos < len(value) {
Expand Down

0 comments on commit 6399027

Please sign in to comment.