From 6ba6eed90a247bc02d6c4b9667439fa1ea1e4217 Mon Sep 17 00:00:00 2001 From: Caleb Foust Date: Wed, 3 Jul 2024 06:33:47 +0800 Subject: [PATCH] fix: lots of runewidth bugs --- pkg/emu/module.go | 9 +++++---- pkg/emu/wrap.go | 12 +++--------- pkg/geom/tty/render.go | 2 +- pkg/replay/motion/jump.go | 8 ++------ pkg/replay/motion/physical.go | 4 +--- pkg/replay/motion/screen.go | 6 +----- 6 files changed, 13 insertions(+), 28 deletions(-) diff --git a/pkg/emu/module.go b/pkg/emu/module.go index be80fbe3..376ebe2e 100644 --- a/pkg/emu/module.go +++ b/pkg/emu/module.go @@ -83,7 +83,9 @@ func (g Glyph) IsDefault() bool { } func (g Glyph) Width() int { - return runewidth.RuneWidth(g.Char) + // runewidth can be 0, but we strictly want visible glyphs to be at + // least one cell wide. + return geom.Max(runewidth.RuneWidth(g.Char), 1) } func (g Glyph) Equal(other Glyph) bool { @@ -103,7 +105,7 @@ type Line []Glyph func (l Line) String() (str string) { for i := 0; i < len(l); i++ { str += string(l[i].Char) - i += runewidth.RuneWidth(l[i].Char) - 1 + i += l[i].Width() - 1 } return str @@ -138,8 +140,7 @@ func (l Line) Occupancy() []bool { } // handle wide runes - r := l[i].Char - w := runewidth.RuneWidth(r) + w := l[i].Width() for j := 0; j < w; j++ { occupancy[i+j] = true } diff --git a/pkg/emu/wrap.go b/pkg/emu/wrap.go index 655e0106..d96d4226 100644 --- a/pkg/emu/wrap.go +++ b/pkg/emu/wrap.go @@ -2,8 +2,6 @@ package emu import ( "github.com/cfoust/cy/pkg/geom" - - "github.com/mattn/go-runewidth" ) type ScreenLine struct { @@ -143,7 +141,7 @@ func getLineLength(line Line) int { continue } - length = i + runewidth.RuneWidth(glyph.Char) + length = i + glyph.Width() break } return length @@ -180,9 +178,7 @@ findOld: for row, line := range oldLine { numChars := line.C1 - line.C0 for col := 0; col < numChars; col++ { - width := runewidth.RuneWidth( - oldLines[line.R][line.C0+col].Char, - ) + width := oldLines[line.R][line.C0+col].Width() if oldCursor.R == row && oldCursor.C >= col && oldCursor.C < col+width { didFind = true @@ -230,9 +226,7 @@ findNew: break findNew } - width := runewidth.RuneWidth( - newLines[line.R][line.C0+col].Char, - ) + width := newLines[line.R][line.C0+col].Width() isLast = (col + width) == numCols newOffset++ col += width - 1 diff --git a/pkg/geom/tty/render.go b/pkg/geom/tty/render.go index 5b341951..636be3fb 100644 --- a/pkg/geom/tty/render.go +++ b/pkg/geom/tty/render.go @@ -102,7 +102,7 @@ func swapImage( info.Fprintf(data, terminfo.ExitAttributeMode) // CJK characters - col += geom.Max(srcCell.Width(), 1) - 1 + col += srcCell.Width() - 1 } } diff --git a/pkg/replay/motion/jump.go b/pkg/replay/motion/jump.go index 3301e32e..1c9cff1d 100644 --- a/pkg/replay/motion/jump.go +++ b/pkg/replay/motion/jump.go @@ -3,8 +3,6 @@ package motion import ( "github.com/cfoust/cy/pkg/emu" "github.com/cfoust/cy/pkg/geom" - - "github.com/mattn/go-runewidth" ) func calculateJump(line emu.Line, needle string, isForward bool, isTo bool, oldPos int) int { @@ -18,8 +16,7 @@ func calculateJump(line emu.Line, needle string, isForward bool, isTo bool, oldP break } - w := runewidth.RuneWidth(char) - for i := 1; i < w; i++ { + for i := 1; i < line[i].Width(); i++ { i++ } } @@ -35,8 +32,7 @@ func calculateJump(line emu.Line, needle string, isForward bool, isTo bool, oldP continue } - w := runewidth.RuneWidth(char) - for i := 1; i < w; i++ { + for i := 1; i < line[i].Width(); i++ { i++ } } diff --git a/pkg/replay/motion/physical.go b/pkg/replay/motion/physical.go index 595c2ea9..1fb98213 100644 --- a/pkg/replay/motion/physical.go +++ b/pkg/replay/motion/physical.go @@ -3,8 +3,6 @@ package motion import ( "github.com/cfoust/cy/pkg/emu" "github.com/cfoust/cy/pkg/geom" - - "github.com/mattn/go-runewidth" ) func lineMotion(getIndex func(line emu.Line) int) Motion { @@ -22,7 +20,7 @@ func lineMotion(getIndex func(line emu.Line) int) Motion { ) // Need to check for CJK - if index > 0 && runewidth.RuneWidth(line[index-1].Char) == 2 { + if index > 0 && line[index-1].Width() == 2 { index-- } diff --git a/pkg/replay/motion/screen.go b/pkg/replay/motion/screen.go index 3543f3c9..a32df84a 100644 --- a/pkg/replay/motion/screen.go +++ b/pkg/replay/motion/screen.go @@ -3,8 +3,6 @@ package motion import ( "github.com/cfoust/cy/pkg/emu" "github.com/cfoust/cy/pkg/geom" - - "github.com/mattn/go-runewidth" ) func getCurrentLine(m Movable) (line emu.ScreenLine, ok bool) { @@ -36,9 +34,7 @@ func screenLineMotion(getIndex func( ) // Need to check for CJK - if index > 0 && runewidth.RuneWidth( - line.Chars[index-1].Char, - ) == 2 { + if index > 0 && line.Chars[index-1].Width() == 2 { index-- }