1
1
mirror of https://github.com/walles/moar.git synced 2024-11-22 03:14:56 +03:00

Add a rune width test

This commit is contained in:
Johan Walles 2024-09-15 14:13:16 +02:00
parent e0e9ee6610
commit 9c858a7ecb
2 changed files with 21 additions and 10 deletions

View File

@ -20,16 +20,22 @@ func NewStyledRune(rune rune, style Style) StyledRune {
}
}
func (cell StyledRune) String() string {
return fmt.Sprint("rune='", string(cell.Rune), "' ", cell.Style)
func (styledRune StyledRune) String() string {
return fmt.Sprint("rune='", string(styledRune.Rune), "' ", styledRune.Style)
}
// How many screen cells will this rune cover? Most runes cover one, but some
// like '午' will cover two.
func (styledRune StyledRune) Width() int {
return 1
}
// Returns a slice of cells with trailing whitespace cells removed
func TrimSpaceRight(cells []StyledRune) []StyledRune {
for i := len(cells) - 1; i >= 0; i-- {
cell := cells[i]
func TrimSpaceRight(runes []StyledRune) []StyledRune {
for i := len(runes) - 1; i >= 0; i-- {
cell := runes[i]
if !unicode.IsSpace(cell.Rune) {
return cells[0 : i+1]
return runes[0 : i+1]
}
// That was a space, keep looking
@ -40,11 +46,11 @@ func TrimSpaceRight(cells []StyledRune) []StyledRune {
}
// Returns a slice of cells with leading whitespace cells removed
func TrimSpaceLeft(cells []StyledRune) []StyledRune {
for i := 0; i < len(cells); i++ {
cell := cells[i]
func TrimSpaceLeft(runes []StyledRune) []StyledRune {
for i := 0; i < len(runes); i++ {
cell := runes[i]
if !unicode.IsSpace(cell.Rune) {
return cells[i:]
return runes[i:]
}
// That was a space, keep looking

View File

@ -36,3 +36,8 @@ func TestTrimSpaceRight(t *testing.T) {
),
[]StyledRune{{Rune: 'x'}}))
}
func TestRuneWidth(t *testing.T) {
assert.Equal(t, NewStyledRune('x', Style{}).Width(), 1)
assert.Equal(t, NewStyledRune('午', Style{}).Width(), 2)
}