From 65880693d22b58ad9598ea2fb5117997bd13ed17 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Thu, 5 Dec 2024 15:38:23 -0500 Subject: [PATCH] refactor(cellbuf): change Cell.Content to Cell.String --- cellbuf/buffer.go | 2 +- cellbuf/cell.go | 12 ++++-------- cellbuf/go.mod | 5 ++--- cellbuf/go.sum | 2 -- cellbuf/options.go | 2 +- cellbuf/screen.go | 4 ++-- cellbuf/window.go | 4 ++-- 7 files changed, 12 insertions(+), 19 deletions(-) diff --git a/cellbuf/buffer.go b/cellbuf/buffer.go index be20f0e9..462e0a73 100644 --- a/cellbuf/buffer.go +++ b/cellbuf/buffer.go @@ -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, " ") diff --git a/cellbuf/cell.go b/cellbuf/cell.go index 7244390f..7b78d8bf 100644 --- a/cellbuf/cell.go +++ b/cellbuf/cell.go @@ -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 @@ -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) } @@ -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, } diff --git a/cellbuf/go.mod b/cellbuf/go.mod index e646b294..bc06df08 100644 --- a/cellbuf/go.mod +++ b/cellbuf/go.mod @@ -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 diff --git a/cellbuf/go.sum b/cellbuf/go.sum index 9362b2c0..e74b0f5a 100644 --- a/cellbuf/go.sum +++ b/cellbuf/go.sum @@ -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= diff --git a/cellbuf/options.go b/cellbuf/options.go index bb0c934a..3b3e56c2 100644 --- a/cellbuf/options.go +++ b/cellbuf/options.go @@ -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 } diff --git a/cellbuf/screen.go b/cellbuf/screen.go index 978af3bb..b77077cf 100644 --- a/cellbuf/screen.go +++ b/cellbuf/screen.go @@ -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 } } diff --git a/cellbuf/window.go b/cellbuf/window.go index 40f61448..c21deaa9 100644 --- a/cellbuf/window.go +++ b/cellbuf/window.go @@ -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 += " " @@ -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