Skip to content

Commit

Permalink
fix: lots of runewidth bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
cfoust committed Jul 2, 2024
1 parent bdcbe7b commit 6ba6eed
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 28 deletions.
9 changes: 5 additions & 4 deletions pkg/emu/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
12 changes: 3 additions & 9 deletions pkg/emu/wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package emu

import (
"github.com/cfoust/cy/pkg/geom"

"github.com/mattn/go-runewidth"
)

type ScreenLine struct {
Expand Down Expand Up @@ -143,7 +141,7 @@ func getLineLength(line Line) int {
continue
}

length = i + runewidth.RuneWidth(glyph.Char)
length = i + glyph.Width()
break
}
return length
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/geom/tty/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func swapImage(
info.Fprintf(data, terminfo.ExitAttributeMode)

// CJK characters
col += geom.Max(srcCell.Width(), 1) - 1
col += srcCell.Width() - 1
}
}

Expand Down
8 changes: 2 additions & 6 deletions pkg/replay/motion/jump.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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++
}
}
Expand All @@ -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++
}
}
Expand Down
4 changes: 1 addition & 3 deletions pkg/replay/motion/physical.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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--
}

Expand Down
6 changes: 1 addition & 5 deletions pkg/replay/motion/screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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--
}

Expand Down

0 comments on commit 6ba6eed

Please sign in to comment.