Move wcswidth into its own package as it is very slow to build

This commit is contained in:
Kovid Goyal 2022-08-24 22:05:51 +05:30
parent 5dca2a1a25
commit 5703a3370e
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
9 changed files with 25 additions and 23 deletions

2
.gitattributes vendored
View File

@ -15,7 +15,7 @@ kittens/diff/options/parse.py linguist-generated=true
glfw/*.c linguist-vendored=true
glfw/*.h linguist-vendored=true
kittens/unicode_input/names.h linguist-generated=true
tools/tui/wcwidth-std.go linguist-generated=true
tools/wcswidth/std.go linguist-generated=true
*.py text diff=python
*.m text diff=objc

View File

@ -21,7 +21,7 @@ kitty/options/parse.py
kitty/options/to-c-generated.h
kittens/diff/options/types.py
kittens/diff/options/parse.py
tools/tui/wcwidth-std.go
tools/wcswidth/std.go
'''
ignored = []

View File

@ -560,10 +560,10 @@ def add_all(p: Callable[..., None], for_go: bool = False) -> None:
else:
p('\treturn 1;\n}')
with create_header('kitty/wcwidth-std.h') as p, open('tools/tui/wcwidth-std.go', 'w') as gof:
with create_header('kitty/wcwidth-std.h') as p, open('tools/wcswidth/std.go', 'w') as gof:
gop = partial(print, file=gof)
gop('package tui\n\n')
gop('func Wcwidth(code rune) int {')
gop('package wcswidth\n\n')
gop('func Runewidth(code rune) int {')
p('static inline int\nwcwidth_std(int32_t code) {')
p('\tif (LIKELY(0x20 <= code && code <= 0x7e)) { return 1; }')
p('\tswitch(code) {')

View File

@ -16,8 +16,8 @@ import (
"kitty"
"kitty/tools/tty"
"kitty/tools/tui"
"kitty/tools/utils"
"kitty/tools/wcswidth"
)
var RootCmd *cobra.Command
@ -120,7 +120,7 @@ func format_line_with_indent(output io.Writer, text string, indent string, scree
var escapes strings.Builder
print_word := func(r rune) {
w := tui.Wcswidth(current_word.String())
w := wcswidth.Stringwidth(current_word.String())
if x+w > screen_width {
fmt.Fprintln(output)
fmt.Fprint(output, indent)

View File

@ -18,6 +18,7 @@ import (
"kitty/tools/cli"
"kitty/tools/crypto"
"kitty/tools/tty"
"kitty/tools/tui"
"kitty/tools/utils"
)
@ -255,10 +256,9 @@ func get_password(password string, password_file string, password_env string, us
if ans == "" && password_file != "" {
if password_file == "-" {
if tty.IsTerminal(os.Stdin.Fd()) {
var q string
q, err = tty.ReadPassword("Password: ")
if err == nil {
ans = string(q)
ans, err = tui.ReadPassword("Password: ", false)
if err != nil {
return
}
} else {
var q []byte

View File

@ -4,6 +4,8 @@ import (
"errors"
"fmt"
"strings"
"kitty/tools/wcswidth"
)
type KilledBySignal struct {
@ -25,9 +27,9 @@ func ReadPassword(prompt string, kill_if_signaled bool) (password string, err er
loop.OnInitialize = func(loop *Loop) string { return "\r\n" }
loop.OnText = func(loop *Loop, text string, from_key_event bool, in_bracketed_paste bool) error {
old_width := Wcswidth(password)
old_width := wcswidth.Stringwidth(password)
password += text
new_width := Wcswidth(password)
new_width := wcswidth.Stringwidth(password)
if new_width > old_width {
extra := strings.Repeat("*", new_width-old_width)
loop.QueueWriteString(extra)
@ -40,9 +42,9 @@ func ReadPassword(prompt string, kill_if_signaled bool) (password string, err er
if event.MatchesPressOrRepeat("backscape") || event.MatchesPressOrRepeat("delete") {
event.Handled = true
if len(password) > 0 {
old_width := Wcswidth(password)
old_width := wcswidth.Stringwidth(password)
password = password[:len(password)-1]
new_width := Wcswidth(password)
new_width := wcswidth.Stringwidth(password)
delta := new_width - old_width
if delta > 0 {
if delta > len(shadow) {

View File

@ -1,6 +1,6 @@
package tui
package wcswidth
func Wcwidth(code rune) int {
func Runewidth(code rune) int {
switch code {
// Flags (26 codepoints) {{{
case 0x1f1e6, 0x1f1e7, 0x1f1e8, 0x1f1e9, 0x1f1ea, 0x1f1eb, 0x1f1ec, 0x1f1ed, 0x1f1ee, 0x1f1ef, 0x1f1f0, 0x1f1f1, 0x1f1f2, 0x1f1f3, 0x1f1f4, 0x1f1f5, 0x1f1f6, 0x1f1f7, 0x1f1f8, 0x1f1f9, 0x1f1fa, 0x1f1fb, 0x1f1fc, 0x1f1fd, 0x1f1fe, 0x1f1ff:

View File

@ -1,4 +1,4 @@
package tui
package wcswidth
func IsFlagCodepoint(ch rune) bool {
return 0x1F1E6 <= ch && ch <= 0x1F1FF
@ -71,7 +71,7 @@ func (self *WCWidthIterator) Step(ch rune) int {
if IsFlagCodepoint(ch) {
self.state = flag_pair_started
}
w := Wcwidth(ch)
w := Runewidth(ch)
switch w {
case -1:
case 0:
@ -102,7 +102,7 @@ func (self *WCWidthIterator) Step(ch rune) int {
return ans
}
func Wcswidth(text string) int {
func Stringwidth(text string) int {
var w WCWidthIterator
ans := 0
for _, ch := range []rune(text) {

View File

@ -1,4 +1,4 @@
package tui
package wcswidth
import (
"testing"
@ -7,13 +7,13 @@ import (
func TestWCSWidth(t *testing.T) {
wcswidth := func(text string, expected int) {
if w := Wcswidth(text); w != expected {
if w := Stringwidth(text); w != expected {
t.Fatalf("The width for %#v was %d instead of %d", text, w, expected)
}
}
wcwidth := func(text string, widths ...int) {
for i, q := range []rune(text) {
if w := Wcwidth(q); w != widths[i] {
if w := Runewidth(q); w != widths[i] {
t.Fatalf("The width of the char: U+%x was %d instead of %d", q, w, widths[i])
}
}