fix(writer): correct width calculation for emoji's

This commit is contained in:
Jan De Dobbeleer 2024-08-04 20:29:43 +02:00 committed by Jan De Dobbeleer
parent 9e238adbb9
commit 0f66661f6e
3 changed files with 9 additions and 4 deletions

View file

@ -35,6 +35,7 @@ require (
github.com/goccy/go-yaml v1.11.3
github.com/gookit/goutil v0.6.16
github.com/hashicorp/hcl/v2 v2.21.0
github.com/mattn/go-runewidth v0.0.16
github.com/pelletier/go-toml/v2 v2.2.2
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
@ -78,7 +79,6 @@ require (
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect

View file

@ -4,14 +4,18 @@ import (
"fmt"
"os"
"strings"
"unicode/utf8"
"github.com/jandedobbeleer/oh-my-posh/src/color"
"github.com/jandedobbeleer/oh-my-posh/src/log"
"github.com/jandedobbeleer/oh-my-posh/src/regex"
"github.com/jandedobbeleer/oh-my-posh/src/shell"
"github.com/mattn/go-runewidth"
)
func init() {
runewidth.DefaultCondition.EastAsianWidth = false
}
type style struct {
AnchorStart string
AnchorEnd string
@ -396,7 +400,8 @@ func write(s rune) {
}
}
length += utf8.RuneCountInString(string(s))
// length += utf8.RuneCountInString(string(s))
length += runewidth.RuneWidth(s)
lastRune = s
builder.WriteRune(s)
}

View file

@ -246,7 +246,7 @@ func TestWriteLength(t *testing.T) {
{
Case: "Emoji",
Input: " ⏰ ",
Expected: 4,
Expected: 5,
Colors: &color.Set{Foreground: "black", Background: color.ParentBackground},
},
{