Skip to content

Commit

Permalink
refactor(cellbuf): rename grid to screen and clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Oct 28, 2024
1 parent ae502d7 commit 1c2bac8
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 231 deletions.
211 changes: 0 additions & 211 deletions cellbuf/grid_write.go

This file was deleted.

8 changes: 4 additions & 4 deletions cellbuf/method.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package cellbuf

// WidthMethod is a type that represents the how the renderer should calculate
// the display width of cells.
type WidthMethod uint8
// Method is a type that represents the how the renderer should calculate the
// display width of cells.
type Method uint8

// Display width modes.
const (
WcWidth WidthMethod = iota
WcWidth Method = iota
GraphemeWidth
)
32 changes: 16 additions & 16 deletions cellbuf/grid.go → cellbuf/screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
// attributes and hyperlink.
type Segment = Cell

// Grid represents an interface for a grid of cells that can be written to and
// read from.
type Grid interface {
// Screen represents an interface for a grid of cells that can be written to
// and read from.
type Screen interface {
// Width returns the width of the grid.
Width() int

Expand All @@ -32,17 +32,17 @@ type Grid interface {
}

// SetContent writes the given data to the grid starting from the first cell.
func (m WidthMethod) SetContent(g Grid, content string) []int {
return setContent(g, content, m)
func SetContent(d Screen, m Method, content string) []int {
return setContent(d, content, m)
}

// Render returns a string representation of the grid with ANSI escape sequences.
// Use [ansi.Strip] to remove them.
func Render(g Grid) string {
func Render(d Screen) string {
var buf bytes.Buffer
height := g.Height()
height := d.Height()
for y := 0; y < height; y++ {
_, line := RenderLine(g, y)
_, line := RenderLine(d, y)
buf.WriteString(line)
if y < height-1 {
buf.WriteString("\r\n")
Expand All @@ -53,7 +53,7 @@ func Render(g Grid) string {

// RenderLine returns a string representation of the yth line of the grid along
// with the width of the line.
func RenderLine(g Grid, n int) (w int, line string) {
func RenderLine(d Screen, n int) (w int, line string) {
var pen Style
var link Link
var buf bytes.Buffer
Expand All @@ -71,8 +71,8 @@ func RenderLine(g Grid, n int) (w int, line string) {
pendingLine = ""
}

for x := 0; x < g.Width(); x++ {
if cell, ok := g.Cell(x, n); ok && cell.Width > 0 {
for x := 0; x < d.Width(); x++ {
if cell, ok := d.Cell(x, n); ok && cell.Width > 0 {
if cell.Style.Empty() && !pen.Empty() {
writePending()
buf.WriteString(ansi.ResetStyle) //nolint:errcheck
Expand Down Expand Up @@ -119,16 +119,16 @@ func RenderLine(g Grid, n int) (w int, line string) {
}

// Fill fills the grid with the given cell.
func Fill(g Grid, c Cell) {
for y := 0; y < g.Height(); y++ {
for x := 0; x < g.Width(); x++ {
g.SetCell(x, y, c) //nolint:errcheck
func Fill(d Screen, c Cell) {
for y := 0; y < d.Height(); y++ {
for x := 0; x < d.Width(); x++ {
d.SetCell(x, y, c) //nolint:errcheck
}
}
}

// Equal returns whether two grids are equal.
func Equal(a, b Grid) bool {
func Equal(a, b Screen) bool {
if a.Width() != b.Width() || a.Height() != b.Height() {
return false
}
Expand Down
Loading

0 comments on commit 1c2bac8

Please sign in to comment.