Repository: JanDeDobbeleer/oh-my-posh
Pull request #5409 (2024-08-04 18:30:24)

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

- [x] I have read and understood the [contributing guide][CONTRIBUTING.md].
- [x] The commit message follows the [conventional commits][cc] guidelines.
- [x] Tests for the changes have been added (for bug fixes / features).
- [x] Docs have been added/updated (for bug fixes / features).

<!---

Tips:

If you're not comfortable with working with Git,
we're working a guide (https://ohmyposh.dev/docs/contributing/git) to help you out.
Oh My Posh advises GitKraken (https://www.gitkraken.com/invite/nQmDPR9D) as your preferred cross platform Git GUI power tool.

-->

[CONTRIBUTING.md]: https://github.com/JanDeDobbeleer/oh-my-posh/blob/main/CONTRIBUTING.md
[cc]: https://www.conventionalcommits.org/en/v1.0.0/#summary



Patch:
diff --git a/src/go.mod b/src/go.mod
index 06fdaf1e..58620295 100644
--- a/src/go.mod
+++ b/src/go.mod
@@ -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
diff --git a/src/terminal/writer.go b/src/terminal/writer.go
index 7bafc1f6..977a34ff 100644
--- a/src/terminal/writer.go
+++ b/src/terminal/writer.go
@@ -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)
 }
diff --git a/website/docs/configuration/block.mdx b/website/docs/configuration/block.mdx
index 6b6e3cfd..9cde78cc 100644
--- a/website/docs/configuration/block.mdx
+++ b/website/docs/configuration/block.mdx
@@ -51,8 +51,6 @@ starts (1st prompt). To enable the same behavior for `bash` and `zsh`, set `enab
 This can have the side effect of swallowing commands you type while the prompt loads, or while the current
 command is still ongoing. This is a limitation of the shell and not Oh My Posh.
 
-```json
-
 ### Alignment
 
 - `left`


Test patch:
diff --git a/src/terminal/writer_test.go b/src/terminal/writer_test.go
index a69b8972..a50a64c1 100644
--- a/src/terminal/writer_test.go
+++ b/src/terminal/writer_test.go
@@ -246,7 +246,7 @@ func TestWriteLength(t *testing.T) {
 		{
 			Case:     "Emoji",
 			Input:    " ⏰  ",
-			Expected: 4,
+			Expected: 5,
 			Colors:   &color.Set{Foreground: "black", Background: color.ParentBackground},
 		},
 		{