Add support for the repeat escape code to TruncatetoVisualLength()

This commit is contained in:
Kovid Goyal 2022-11-17 08:17:11 +05:30
parent 9db1d581f7
commit a2f022d166
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 19 additions and 2 deletions

View File

@ -5,6 +5,8 @@ package wcswidth
import (
"errors"
"fmt"
"strconv"
"kitty/tools/utils"
)
@ -25,8 +27,21 @@ type truncate_iterator struct {
limit_exceeded_at *truncate_error
}
func (self *truncate_iterator) handle_csi(body []byte) error {
self.pos += len(body) + 2
func (self *truncate_iterator) handle_csi(csi []byte) error {
if len(csi) > 1 && csi[len(csi)-1] == 'b' { // repeat previous char escape code
num_string := utils.UnsafeBytesToString(csi[:len(csi)-1])
n, err := strconv.Atoi(num_string)
if err == nil && n > 0 {
width_before_repeat := self.w.current_width
for ; n > 0; n-- {
self.w.handle_rune(self.w.prev_ch)
if self.w.current_width > self.limit {
return &truncate_error{pos: self.pos, width: width_before_repeat}
}
}
}
}
self.pos += len(csi) + 2
return nil
}

View File

@ -61,6 +61,8 @@ func TestWCSWidth(t *testing.T) {
truncate("a🌷\ufe0e", 2, "a🌷\ufe0e", 2)
truncate("a🌷\ufe0eb", 3, "a🌷\ufe0eb", 3)
truncate("a\x1b[31mb", 2, "a\x1b[31mb", 2)
truncate("a\x1b[7bb", 2, "a", 1)
truncate("a\x1b[3bbc", 5, "a\x1b[3bb", 5)
}
func TestCellIterator(t *testing.T) {