-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This package implements an interface on top of golang.org/x/text/width to calculate the width of runes and strings similar to `wcwidth` and `wcswidth` methods. It's a drop-in replacement to go-runewidth without multi-rune emojis support i.e. ZWJ (zero-width joiner) is ignored.
- Loading branch information
1 parent
ec9fda2
commit 0336e39
Showing
6 changed files
with
163 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ use ( | |
./sshkey | ||
./term | ||
./termios | ||
./wcwidth | ||
./windows | ||
./xpty | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,16 @@ | ||
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d/go.mod h1:asat636LX7Bqt5lYEZ27JNDcqxfjdBQuJ/MM4CN/Lzo= | ||
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4= | ||
github.com/charmbracelet/bubbletea v0.26.1 h1:xujcQeF73rh4jwu3+zhfQsvV18x+7zIjlw7/CYbzGJ0= | ||
github.com/charmbracelet/bubbletea v0.26.1/go.mod h1:FzKr7sKoO8iFVcdIBM9J0sJOcQv5nDQaYwsee3kpbgo= | ||
github.com/charmbracelet/bubbletea v0.26.5/go.mod h1:dz8CWPlfCCGLFbBlTY4N7bjLiyOGDJEnd2Muu7pOWhk= | ||
github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE= | ||
github.com/charmbracelet/harmonica v0.2.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao= | ||
github.com/charmbracelet/lipgloss v0.11.0/go.mod h1:1UdRTH9gYgpcdNN5oBtjbu/IzNKtzVtb7sqN1t9LNn8= | ||
github.com/charmbracelet/lipgloss v0.12.0/go.mod h1:beLlcmkF7MWA+5UrKKIRo/VJ21xGXr7YJ9miWfdMRIU= | ||
github.com/charmbracelet/ssh v0.0.0-20240515141028-546b2ee33a4d/go.mod h1:8/Ve8iGRRIGFM1kepYfRF2pEOF5Y3TEZYoJaA54228U= | ||
github.com/charmbracelet/x/errors v0.0.0-20240117030013-d31dba354651/go.mod h1:2P0UgXMEa6TsToMSuFqKFQR+fZTO9CNGUNokkPatT/0= | ||
github.com/charmbracelet/x/exp/term v0.0.0-20240524151031-ff83003bf67a/go.mod h1:YBotIGhfoWhHDlnUpJMkjebGV2pdGRCn1Y4/Nk/vVcU= | ||
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk= | ||
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= | ||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= | ||
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= | ||
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= | ||
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= | ||
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8= | ||
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y= | ||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= | ||
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= | ||
github.com/sahilm/fuzzy v0.1.1/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y= | ||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= | ||
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg= | ||
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE= | ||
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 h1:yixxcjnhBmY0nkL253HFVIm0JsFHwrHdT3Yh6szTnfY= | ||
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8/go.mod h1:jj3sYF3dwk5D+ghuXyeI3r5MFf+NT2An6/9dOA95KSI= | ||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= | ||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= | ||
golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= | ||
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= | ||
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= | ||
golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= | ||
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= | ||
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= | ||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= | ||
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= | ||
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= | ||
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= | ||
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= | ||
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE= | ||
golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE= | ||
golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY= | ||
golang.org/x/term v0.19.0/go.mod h1:2CuTdWZ7KHSQwUzKva0cbMg6q2DMI3Mmxp+gKJbskEk= | ||
golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0= | ||
golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4= | ||
golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk= | ||
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= | ||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= | ||
golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU= | ||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= | ||
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= | ||
golang.org/x/tools v0.14.0/go.mod h1:uYBEerGOWcJyEORxN+Ek8+TT266gXkNlHdJBwexUsBg= | ||
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk= | ||
golang.org/x/tools v0.22.0/go.mod h1:aCwcsjqvq7Yqt6TNyX7QMU2enbQ/Gt0bo6krSeEri+c= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/charmbracelet/x/wcwidth | ||
|
||
go 1.18 | ||
|
||
require golang.org/x/text v0.17.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= | ||
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// wcwidth is a Go implementation of wcwidth(3) that uses | ||
// golang.org/x/text/width. It is a drop-in replacement for | ||
// github.com/mattn/go-runewidth. | ||
// | ||
// Unlike go-runewidth, wcwidth treats East Asian ambiguous characters as | ||
// single-width characters. This is consistent with the behavior of wcwidth(3). | ||
|
||
package wcwidth | ||
|
||
import ( | ||
"unicode" | ||
|
||
"golang.org/x/text/width" | ||
) | ||
|
||
// IsComb returns true if r is a Unicode combining character. Alias of: | ||
// | ||
// unicode.Is(unicode.Mn, r) | ||
func IsComb(r rune) bool { return unicode.Is(unicode.Mn, r) } | ||
|
||
// RuneWidth returns fixed-width width of rune. | ||
// https://en.wikipedia.org/wiki/Halfwidth_and_fullwidth_forms#In_Unicode | ||
func RuneWidth(r rune) int { | ||
if r == 0 || !unicode.IsPrint(r) || IsComb(r) { | ||
return 0 | ||
} | ||
k := width.LookupRune(r) | ||
switch k.Kind() { | ||
case width.EastAsianWide, width.EastAsianFullwidth: | ||
return 2 | ||
case width.EastAsianNarrow, width.EastAsianHalfwidth, width.EastAsianAmbiguous, width.Neutral: | ||
return 1 | ||
default: | ||
return 0 | ||
} | ||
} | ||
|
||
// StringWidth returns fixed-width width of string. | ||
// https://en.wikipedia.org/wiki/Halfwidth_and_fullwidth_forms#In_Unicode | ||
func StringWidth(s string) (n int) { | ||
for _, r := range s { | ||
n += RuneWidth(r) | ||
} | ||
return n | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package wcwidth | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
// test cases copied from https://github.com/mattn/go-runewidth/raw/master/runewidth_test.go | ||
|
||
var stringwidthtests = []struct { | ||
in string | ||
out int | ||
eaout int | ||
}{ | ||
{"■㈱の世界①", 10, 12}, | ||
{"スター☆", 7, 8}, | ||
{"つのだ☆HIRO", 11, 12}, | ||
} | ||
|
||
func BenchmarkStringWidth(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
StringWidth(stringwidthtests[i%len(stringwidthtests)].in) | ||
} | ||
} | ||
|
||
func TestStringWidth(t *testing.T) { | ||
for _, tt := range stringwidthtests { | ||
if out := StringWidth(tt.in); out != tt.out { | ||
t.Errorf("StringWidth(%q) = %d, want %d", tt.in, out, tt.out) | ||
} | ||
} | ||
} | ||
|
||
var runewidthtests = []struct { | ||
in rune | ||
out int | ||
eaout int | ||
nseout int | ||
}{ | ||
{'世', 2, 2, 2}, | ||
{'界', 2, 2, 2}, | ||
{'セ', 1, 1, 1}, | ||
{'カ', 1, 1, 1}, | ||
{'イ', 1, 1, 1}, | ||
{'☆', 1, 2, 2}, // double width in ambiguous | ||
{'☺', 1, 1, 2}, | ||
{'☻', 1, 1, 2}, | ||
{'♥', 1, 2, 2}, | ||
{'♦', 1, 1, 2}, | ||
{'♣', 1, 2, 2}, | ||
{'♠', 1, 2, 2}, | ||
{'♂', 1, 2, 2}, | ||
{'♀', 1, 2, 2}, | ||
{'♪', 1, 2, 2}, | ||
{'♫', 1, 1, 2}, | ||
{'☼', 1, 1, 2}, | ||
{'↕', 1, 2, 2}, | ||
{'‼', 1, 1, 2}, | ||
{'↔', 1, 2, 2}, | ||
{'\x00', 0, 0, 0}, | ||
{'\x01', 0, 0, 0}, | ||
{'\u0300', 0, 0, 0}, | ||
{'\u2028', 0, 0, 0}, | ||
{'\u2029', 0, 0, 0}, | ||
{'a', 1, 1, 1}, // ASCII classified as "na" (narrow) | ||
{'⟦', 1, 1, 1}, // non-ASCII classified as "na" (narrow) | ||
{'👁', 1, 1, 2}, | ||
} | ||
|
||
func BenchmarkRuneWidth(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
RuneWidth(runewidthtests[i%len(runewidthtests)].in) | ||
} | ||
} | ||
|
||
func TestRuneWidth(t *testing.T) { | ||
for i, tt := range runewidthtests { | ||
if out := RuneWidth(tt.in); out != tt.out { | ||
t.Errorf("case %d: RuneWidth(%q) = %d, want %d", i, tt.in, out, tt.out) | ||
} | ||
} | ||
} | ||
|
||
func TestZeroWidthJoiner(t *testing.T) { | ||
tests := []struct { | ||
in string | ||
want int | ||
}{ | ||
{"👩", 2}, | ||
{"👩\u200d", 2}, | ||
{"👩\u200d🍳", 4}, | ||
{"\u200d🍳", 2}, | ||
{"👨\u200d👨", 4}, | ||
{"👨\u200d👨\u200d👧", 6}, | ||
{"🏳️\u200d🌈", 3}, | ||
{"あ👩\u200d🍳い", 8}, | ||
{"あ\u200d🍳い", 6}, | ||
{"あ\u200dい", 4}, | ||
} | ||
|
||
for _, tt := range tests { | ||
if got := StringWidth(tt.in); got != tt.want { | ||
t.Errorf("StringWidth(%q) = %d, want %d", tt.in, got, tt.want) | ||
} | ||
} | ||
} |