fix(text): improve effective text width measuring

This commit is contained in:
L. Yeung 2022-07-03 20:36:49 +08:00 committed by Jan De Dobbeleer
parent 9f9b91e566
commit 65192cb065
2 changed files with 9 additions and 4 deletions

View file

@ -3,14 +3,19 @@ package color
import (
"oh-my-posh/regex"
"strings"
"unicode/utf8"
"github.com/mattn/go-runewidth"
)
func init() { // nolint:gochecknoinits
runewidth.DefaultCondition.EastAsianWidth = false
}
func (ansi *Ansi) MeasureText(text string) int {
// skip strings with ANSI
if !strings.Contains(text, "\x1b") {
text = ansi.TrimEscapeSequences(text)
length := utf8.RuneCountInString(text)
length := runewidth.StringWidth(text)
return length
}
if strings.Contains(text, "\x1b]8;;") {
@ -21,7 +26,7 @@ func (ansi *Ansi) MeasureText(text string) int {
}
text = ansi.TrimAnsi(text)
text = ansi.TrimEscapeSequences(text)
length := utf8.RuneCountInString(text)
length := runewidth.StringWidth(text)
return length
}

View file

@ -38,6 +38,7 @@ require (
github.com/charmbracelet/bubbletea v0.22.0
github.com/charmbracelet/lipgloss v0.5.0
github.com/hashicorp/hcl/v2 v2.13.0
github.com/mattn/go-runewidth v0.0.13
github.com/spf13/cobra v1.5.0
golang.org/x/mod v0.5.1
gopkg.in/yaml.v3 v3.0.1
@ -74,7 +75,6 @@ require (
github.com/google/go-cmp v0.5.8 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/muesli/ansi v0.0.0-20211031195517-c9f0611b6c70 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect