Support the repeat prev char escape code when calculating the width of a string

This commit is contained in:
Kovid Goyal 2022-11-16 16:25:16 +05:30
parent 4b05822474
commit dd1fcf6855
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 19 additions and 0 deletions

View File

@ -4,6 +4,7 @@ package wcswidth
import ( import (
"fmt" "fmt"
"strconv"
"kitty/tools/utils" "kitty/tools/utils"
) )
@ -31,6 +32,7 @@ type WCWidthIterator struct {
func CreateWCWidthIterator() *WCWidthIterator { func CreateWCWidthIterator() *WCWidthIterator {
var ans WCWidthIterator var ans WCWidthIterator
ans.parser.HandleRune = ans.handle_rune ans.parser.HandleRune = ans.handle_rune
ans.parser.HandleCSI = ans.handle_csi
return &ans return &ans
} }
@ -42,6 +44,22 @@ func (self *WCWidthIterator) Reset() {
self.parser.Reset() self.parser.Reset()
} }
func (self *WCWidthIterator) handle_csi(csi []byte) error {
if len(csi) > 1 && csi[len(csi)-1] == 'b' {
num_string := utils.UnsafeBytesToString(csi[:len(csi)-1])
n, err := strconv.Atoi(num_string)
if err == nil && n > 0 {
for i := 0; i < n; i++ {
err = self.handle_rune(self.prev_ch)
if err != nil {
return err
}
}
}
}
return nil
}
func (self *WCWidthIterator) handle_rune(ch rune) error { func (self *WCWidthIterator) handle_rune(ch rune) error {
self.rune_count += 1 self.rune_count += 1
const ( const (

View File

@ -36,6 +36,7 @@ func TestWCSWidth(t *testing.T) {
wcswidth("\U0001F1E6\U0001F1E8a", 3) wcswidth("\U0001F1E6\U0001F1E8a", 3)
wcswidth("\U0001F1E6\U0001F1E8\U0001F1E6", 4) wcswidth("\U0001F1E6\U0001F1E8\U0001F1E6", 4)
wcswidth("a\u00adb", 2) wcswidth("a\u00adb", 2)
wcswidth("a\x1b[22b", 23)
// Flags individually and together // Flags individually and together
wcwidth("\U0001f1ee\U0001f1f3", 2, 2) wcwidth("\U0001f1ee\U0001f1f3", 2, 2)
wcswidth("\U0001f1ee\U0001f1f3", 2) wcswidth("\U0001f1ee\U0001f1f3", 2)