Skip to content

Commit

Permalink
feat(cellbuf): add Bubblezone support
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Nov 5, 2024
1 parent ba42ab7 commit 318a466
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cellbuf/cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ type Cell struct {

// Width is the mono-space width of the grapheme cluster.
Width int

// Zone is the zone id of the cell.
Zone int
}

// Equal returns whether the cell is equal to the other cell.
func (c Cell) Equal(o Cell) bool {
return c.Width == o.Width &&
c.Content == o.Content &&
c.Zone == o.Zone &&
c.Style.Equal(o.Style) &&
c.Link.Equal(o.Link)
}
Expand All @@ -39,6 +43,7 @@ func (c Cell) Equal(o Cell) bool {
func (c Cell) Empty() bool {
return c.Content == "" &&
c.Width == 0 &&
c.Zone == 0 &&
c.Style.Empty() &&
c.Link.Empty()
}
Expand All @@ -47,6 +52,7 @@ func (c Cell) Empty() bool {
func (c *Cell) Reset() {
c.Content = ""
c.Width = 0
c.Zone = 0
c.Style.Reset()
c.Link.Reset()
}
12 changes: 12 additions & 0 deletions cellbuf/screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func RenderLine(d Screen, n int) (w int, line string) {
func RenderLineWithProfile(d Screen, n int, p colorprofile.Profile) (w int, line string) {
var pen Style
var link Link
var zone int
var buf bytes.Buffer
var pendingLine string
var pendingWidth int // this ignores space cells until we hit a non-space cell
Expand Down Expand Up @@ -113,6 +114,17 @@ func RenderLineWithProfile(d Screen, n int, p colorprofile.Profile) (w int, line
link = cellLink
}

// Write the Bubblezone escape sequence
if cell.Zone != zone {
writePending()
if cell.Zone == 0 {
buf.WriteString(ansi.SetZone(zone)) //nolint:errcheck
} else {
buf.WriteString(ansi.SetZone(cell.Zone)) //nolint:errcheck
}
zone = cell.Zone
}

// 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(spaceCell) {
Expand Down
18 changes: 18 additions & 0 deletions cellbuf/screen_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func setContent(
var cell Cell
var pen Style
var link Link
var zone int
var x, y int

p := ansi.GetParser()
Expand Down Expand Up @@ -56,6 +57,7 @@ func setContent(
cell.Width = width
cell.Style = pen
cell.Link = link
cell.Zone = zone

dis.SetCell(x, y, cell) //nolint:errcheck

Expand All @@ -76,6 +78,8 @@ func setContent(
switch p.Cmd {
case 'm': // SGR - Select Graphic Rendition
handleSgr(p, &pen)
case 'z': // Bubblezone
handleZone(p, &zone)
}
case ansi.HasOscPrefix(seq) && p.Cmd != 0:
switch p.Cmd {
Expand Down Expand Up @@ -218,3 +222,17 @@ func handleHyperlinks(p *ansi.Parser, link *Link) {
}
link.URL = string(params[2])
}

// handleZone handles Bubblezone escape sequences.
func handleZone(p *ansi.Parser, zone *int) {
if p.ParamsLen == 0 {
return
}

z := ansi.Param(p.Params[0]).Param()
if *zone == z {
*zone = 0
} else {
*zone = z
}
}

0 comments on commit 318a466

Please sign in to comment.