-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix column alignment and truncation for Eastern Asian languages
- Ensure that text is never truncated mid-character, which would result in garbled text - Ensure that columns in `issue/pr list` output align properly
- Loading branch information
Showing
7 changed files
with
148 additions
and
24 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
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
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
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
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,63 @@ | ||
package text | ||
|
||
import ( | ||
"golang.org/x/text/width" | ||
) | ||
|
||
// DisplayWidth calculates what the rendered width of a string may be | ||
func DisplayWidth(s string) int { | ||
w := 0 | ||
for _, r := range s { | ||
w += runeDisplayWidth(r) | ||
} | ||
return w | ||
} | ||
|
||
const ( | ||
ellipsisWidth = 3 | ||
minWidthForEllipsis = 5 | ||
) | ||
|
||
// Truncate shortens a string to fit the maximum display width | ||
func Truncate(max int, s string) string { | ||
w := DisplayWidth(s) | ||
if w <= max { | ||
return s | ||
} | ||
|
||
useEllipsis := false | ||
if max >= minWidthForEllipsis { | ||
useEllipsis = true | ||
max -= 3 | ||
} | ||
|
||
cw := 0 | ||
ri := 0 | ||
for _, r := range s { | ||
rw := runeDisplayWidth(r) | ||
if cw+rw > max { | ||
break | ||
} | ||
cw += rw | ||
ri++ | ||
} | ||
|
||
res := string([]rune(s)[:ri]) | ||
if useEllipsis { | ||
res += "..." | ||
} | ||
if cw < max { | ||
// compensate if truncating a wide character left an odd space | ||
res += " " | ||
} | ||
return res | ||
} | ||
|
||
func runeDisplayWidth(r rune) int { | ||
switch width.LookupRune(r).Kind() { | ||
case width.EastAsianWide, width.EastAsianAmbiguous, width.EastAsianFullwidth: | ||
return 2 | ||
default: | ||
return 1 | ||
} | ||
} |
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,71 @@ | ||
package text | ||
|
||
import "testing" | ||
|
||
func TestTruncate(t *testing.T) { | ||
type args struct { | ||
max int | ||
s string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
}{ | ||
{ | ||
name: "Japanese", | ||
args: args{ | ||
max: 11, | ||
s: "テストテストテストテスト", | ||
}, | ||
want: "テストテ...", | ||
}, | ||
{ | ||
name: "Japanese filled", | ||
args: args{ | ||
max: 11, | ||
s: "aテストテストテストテスト", | ||
}, | ||
want: "aテスト... ", | ||
}, | ||
{ | ||
name: "Chinese", | ||
args: args{ | ||
max: 11, | ||
s: "幫新舉報違章工廠新增編號", | ||
}, | ||
want: "幫新舉報...", | ||
}, | ||
{ | ||
name: "Chinese filled", | ||
args: args{ | ||
max: 11, | ||
s: "a幫新舉報違章工廠新增編號", | ||
}, | ||
want: "a幫新舉... ", | ||
}, | ||
{ | ||
name: "Korean", | ||
args: args{ | ||
max: 11, | ||
s: "프로젝트 내의", | ||
}, | ||
want: "프로젝트...", | ||
}, | ||
{ | ||
name: "Korean filled", | ||
args: args{ | ||
max: 11, | ||
s: "a프로젝트 내의", | ||
}, | ||
want: "a프로젝... ", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := Truncate(tt.args.max, tt.args.s); got != tt.want { | ||
t.Errorf("Truncate() = %q, want %q", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
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