Skip to content

Commit

Permalink
refactor(cellbuf): change Cell.Content to Cell.String
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Dec 5, 2024
1 parent 5d7933a commit 6588069
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 19 deletions.
2 changes: 1 addition & 1 deletion cellbuf/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (l Line) String() (s string) {
} else if c.Empty() {
continue
} else {
s += c.Content()
s += c.String()
}
}
s = strings.TrimRight(s, " ")
Expand Down
12 changes: 4 additions & 8 deletions cellbuf/cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ type Cell struct {
// Link is the hyperlink of the cell.
Link Link

// TODO: Is it worth it changing this to a single rune with combining
// runes? Most of the time, we're only dealing with single runes anyway.
// It's more efficient to use a single rune and combining runes when
// necessary than allocating a new string for each cell.

// Comb is the combining runes of the cell. This is nil if the cell is a
// single rune or if it's a zero width cell that is part of a wider cell.
Comb []rune
Expand All @@ -39,8 +34,9 @@ type Cell struct {
Width int
}

// Content returns the content of the cell as a string.
func (c Cell) Content() string {
// String returns the string content of the cell excluding any styles, links,
// and escape sequences.
func (c Cell) String() string {
if len(c.Comb) == 0 {
return string(c.Rune)
}
Expand Down Expand Up @@ -100,7 +96,7 @@ func (c *Cell) Blank() *Cell {
// Segment returns a segment of the cell.
func (c *Cell) Segment() Segment {
return Segment{
Content: c.Content(),
Content: c.String(),
Style: c.Style,
Link: c.Link,
}
Expand Down
5 changes: 2 additions & 3 deletions cellbuf/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ go 1.18
require (
github.com/charmbracelet/colorprofile v0.1.9
github.com/charmbracelet/x/ansi v0.5.2
github.com/charmbracelet/x/vt v0.0.0-20241113152101-0af7d04e9f32
github.com/charmbracelet/x/term v0.2.1
github.com/charmbracelet/x/wcwidth v0.0.0-20241011142426-46044092ad91
github.com/rivo/uniseg v0.4.7
)

require (
github.com/charmbracelet/x/term v0.2.1 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/text v0.20.0 // indirect
Expand Down
2 changes: 0 additions & 2 deletions cellbuf/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ github.com/charmbracelet/x/ansi v0.5.2 h1:dEa1x2qdOZXD/6439s+wF7xjV+kZLu/iN00GuX
github.com/charmbracelet/x/ansi v0.5.2/go.mod h1:KBUFw1la39nl0dLl10l5ORDAqGXaeurTQmwyyVKse/Q=
github.com/charmbracelet/x/term v0.2.1 h1:AQeHeLZ1OqSXhrAWpYUtZyX1T3zVxfpZuEQMIQaGIAQ=
github.com/charmbracelet/x/term v0.2.1/go.mod h1:oQ4enTYFV7QN4m0i9mzHrViD7TQKvNEEkHUMCmsxdUg=
github.com/charmbracelet/x/vt v0.0.0-20241113152101-0af7d04e9f32 h1:F6G/LwhlSj/oQgnNKkELI934e/oao0MM67rst7MExDY=
github.com/charmbracelet/x/vt v0.0.0-20241113152101-0af7d04e9f32/go.mod h1:+CYC0tzYqYMtIryA0lcGQgCUaAiRLaS7Rxi9R+PFii8=
github.com/charmbracelet/x/wcwidth v0.0.0-20241011142426-46044092ad91 h1:D5OO0lVavz7A+Swdhp62F9gbkibxmz9B2hZ/jVdMPf0=
github.com/charmbracelet/x/wcwidth v0.0.0-20241011142426-46044092ad91/go.mod h1:Ey8PFmYwH+/td9bpiEx07Fdx9ZVkxfIjWXxBluxF4Nw=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
Expand Down
2 changes: 1 addition & 1 deletion cellbuf/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func (opt Options) Diff(b, prev *Buffer) (diff []Span) {
if span.X+span.Width == x &&
span.Style.Equal(cellB.Style) &&
span.Link == cellB.Link {
span.Content += cellB.Content()
span.Content += cellB.String()
span.Width += cellB.Width
continue
}
Expand Down
4 changes: 2 additions & 2 deletions cellbuf/screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ func renderLine(d *Buffer, n int, opt Options) (w int, line string) {
// We only write the cell content if it's not empty. If it is, we
// append it to the pending line and width to be evaluated later.
if cell.Equal(&BlankCell) {
pendingLine += cell.Content()
pendingLine += cell.String()
pendingWidth += cell.Width
} else {
writePending()
buf.WriteString(cell.Content())
buf.WriteString(cell.String())
w += cell.Width
}
}
Expand Down
4 changes: 2 additions & 2 deletions cellbuf/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func relativeCursorMove(s *Screen, fx, fy, tx, ty int, overwrite bool) (seq stri
for i := 0; i < n; i++ {
cell := s.newbuf.Cell(fx+i, ty)
if cell != nil {
ovw += cell.Content()
ovw += cell.String()
i += cell.Width - 1
} else {
ovw += " "
Expand Down Expand Up @@ -443,7 +443,7 @@ func (s *Screen) putCell(w *bytes.Buffer, cell *Cell) {
}

s.updatePen(w, cell)
w.WriteString(cell.Content())
w.WriteString(cell.String())
s.cur.X += cell.Width
s.lastChar = cell.Rune

Expand Down

0 comments on commit 6588069

Please sign in to comment.