Skip to content
This repository was archived by the owner on Oct 13, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions grid.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tui

import (
"errors"
"image"
)

Expand Down Expand Up @@ -98,6 +99,20 @@ func (g *Grid) Draw(p *Painter) {
}
}

// GetCells returns all cells in the grid.
func (g *Grid) GetCells() map[image.Point]Widget {
return g.cells
}

// GetCell returns the cell at the specified location in the grid.s
func (g *Grid) GetCell(location image.Point) (Widget, error) {
value, ok := g.cells[location]
if !ok {
return nil, errors.New("Location does not exist")
}
return value, nil
}

// MinSizeHint returns the minimum size hint for the grid.
func (g *Grid) MinSizeHint() image.Point {
if g.cols == 0 || g.rows == 0 {
Expand Down
34 changes: 34 additions & 0 deletions grid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,37 @@ func TestGrid_Draw(t *testing.T) {
})
}
}

func TestGrid_GetCells(t *testing.T) {
grid := NewGrid(0, 0)
foo := NewLabel("foo")
bar := NewLabel("bar")
grid.AppendRow(
foo,
bar,
)

cells := grid.GetCells()
if cells[image.Point{0, 0}] != foo {
t.Error("Foo not at expected position")
}
if cells[image.Point{1, 0}] != bar {
t.Error("Bar not at expected position")
}
}

func TestGrid_GetCell(t *testing.T) {
grid := NewGrid(0, 0)
foo := NewLabel("foo")
grid.AppendRow(foo)

fooCell, err := grid.GetCell(image.Point{0, 0})
if err != nil || fooCell != foo {
t.Error("Could not retreive Foo cell")
}

nonExistentCell, err := grid.GetCell(image.Point{1, 0})
if err == nil || nonExistentCell != nil {
t.Error("Nonexistent cell exists")
}
}