grid

package
v0.0.0-...-0780db2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2024 License: MIT Imports: 4 Imported by: 1

Documentation

Overview

Package grid contains common utilities for dealing with 2d arrays / grids

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AnsiFormatter

type AnsiFormatter func(string) string

AnsiFormatter is a function applying ansi code formats ( e.g. ansi.WhitBG )

type AnsiRenderer

type AnsiRenderer struct {
	// contains filtered or unexported fields
}

AnsiRenderer is text based renderer for Grid Ansi codes are used to color and format the cells It technically can render any grid size, but it is more sutuble small grids that can fit in a typical terminal window

func NewAnsiRenderer

func NewAnsiRenderer() *AnsiRenderer

NewAnsiRenderer creates a new ansi renderer

func (*AnsiRenderer) FmtFn

func (r *AnsiRenderer) FmtFn(f AnsiFormatter, fn func(Cell[string]) bool) *AnsiRenderer

FmtFn sets a conditional formatter for a cell All cells within the grid that fit the specified condition would be formated with the specified formatter

Example
g := Grid[string]{Values: [][]string{
	{"1", "2", "3"},
	{"4", "5", "6"},
	{"7", "8", "9"},
}}
r := NewAnsiRenderer().SetDefaultFmt(square)
r.FmtFn(triangle, func(c Cell[string]) bool {
	return (c.Point.Row*g.Size().Columns+c.Point.Column+1)%2 == 0
})
fmt.Println(r.Render(&g))
Output:

[ 1 ]< 2 >[ 3 ]
< 4 >[ 5 ]< 6 >
[ 7 ]< 8 >[ 9 ]

func (*AnsiRenderer) FmtPoints

func (r *AnsiRenderer) FmtPoints(f AnsiFormatter, p ...Point) *AnsiRenderer

FmtPoints sets a different format for the specified points useful for marking points on a grid with different colors

Example
g := Grid[string]{Values: [][]string{
	{"1", "2", "3"},
	{"4", "5", "6"},
	{"7", "8", "9"},
}}
r := NewAnsiRenderer().SetDefaultFmt(square)
r.FmtPoints(triangle, p(0, 0), p(1, 1), p(2, 2))
fmt.Println(r.Render(&g))
Output:

< 1 >[ 2 ][ 3 ]
[ 4 ]< 5 >[ 6 ]
[ 7 ][ 8 ]< 9 >

func (*AnsiRenderer) Render

func (r *AnsiRenderer) Render(g *Grid[string]) string

Render returns the full ansi string render of the Grid

Example
g := Grid[string]{Values: [][]string{
	{"1", "2", "3"},
	{"4", "5", "6"},
	{"7", "8", "9"},
}}
r := NewAnsiRenderer().SetDefaultFmt(square)
fmt.Println(r.Render(&g))
Output:

[ 1 ][ 2 ][ 3 ]
[ 4 ][ 5 ][ 6 ]
[ 7 ][ 8 ][ 9 ]

func (*AnsiRenderer) SetDefaultFmt

func (r *AnsiRenderer) SetDefaultFmt(f AnsiFormatter) *AnsiRenderer

SetDefaultFmt changes the default formatter

type Cell

type Cell[T any] struct {
	Value T
	Point Point
}

Cell represents a cell in the grid, it includes both the coordinates Point and the Value

type Grid

type Grid[T any] struct {
	Values [][]T
}

Grid represents a 2d array or a grid internally it wrappers a two-dimensional slice of any type and provides common helper functions to manipulate grids

The grid stats from the top left

[0,0] [0,1] [0,2]
[1,0] [1,1] [1,2]
[2,0] [2,1] [2,2]
[3,0] [3,1] [3,2]

func IntToStr

func IntToStr(g *Grid[int]) Grid[string]

IntToStr converts Grid[int] to a Grid[string] that can be used for rendering

Example
g := Grid[int]{Values: [][]int{
	{1, 2, 3},
	{4, 5, 6},
	{7, 8, 9},
}}

ig := IntToStr(&g)

for c := range ig.Cells() {
	fmt.Println(c.Value)

}
Output:

1
2
3
4
5
6
7
8
9

func (*Grid[T]) AppendRow

func (g *Grid[T]) AppendRow(values []T)

AppendRow appends a row to the grid

func (*Grid[T]) Cell

func (g *Grid[T]) Cell(p Point) Cell[T]

Cell returns the cell at a specific Point will panic (index out of range) if the position is out of bounds

Example
g := Grid[int]{Values: [][]int{
	{1, 2, 3},
	{4, 5, 6},
	{7, 8, 9},
}}

fmt.Println(g.Cell(Point{Row: 0, Column: 0}))
fmt.Println(g.Cell(Point{Row: 1, Column: 1}))
fmt.Println(g.Cell(Point{Row: 2, Column: 2}))
Output:

{1 {0 0}}
{5 {1 1}}
{9 {2 2}}

func (*Grid[T]) Cells

func (g *Grid[T]) Cells() iter.Seq[Cell[T]]

Cells returns an iterator iter.Seq over the grid cells it iterates the grid left to right from top to bottom.

Example
g := Grid[int]{Values: [][]int{
	{1, 2, 3},
	{4, 5, 6},
	{7, 8, 9},
}}

for c := range g.Cells() {
	fmt.Println(c.Value)

}
Output:

1
2
3
4
5
6
7
8
9

func (*Grid[T]) Includes

func (g *Grid[T]) Includes(p Point) bool

Includes indicates if a Point is within the cell position in the grid

Example
g := Grid[int]{Values: [][]int{
	{1, 2, 3},
	{4, 5, 6},
	{7, 8, 9},
}}

fmt.Println(g.Includes(Point{Row: 0, Column: 0}))
fmt.Println(g.Includes(Point{Row: -1, Column: 0}))
Output:

true
false

func (*Grid[T]) Rows

func (g *Grid[T]) Rows() [][]T

Rows Returns the underlying two-dimensional array In many cases Iterator might be a better option

Example
g := Grid[int]{Values: [][]int{
	{1, 2, 3},
	{4, 5, 6},
	{7, 8, 9},
}}

// In many cases g.Iterator would fit better
fmt.Println(g.Rows())
Output:

[[1 2 3] [4 5 6] [7 8 9]]

func (*Grid[T]) Size

func (g *Grid[T]) Size() Size

Size returns the size of the grid

Example
g := Grid[int]{Values: [][]int{
	{1, 2, 3},
	{4, 5, 6},
	{7, 8, 9},
}}

// Number of columns is indicated by the number of columns in first row only
nonuniform := Grid[int]{Values: [][]int{
	{1, 2, 3},
	{4, 5, 6, 7},
	{8, 9},
}}

empty := Grid[int]{}

fmt.Println(g.Size())
fmt.Println(nonuniform.Size())
fmt.Println(empty.Size())
Output:

{3 3}
{3 3}
{0 0}

func (*Grid[T]) Value

func (g *Grid[T]) Value(p Point) T

Value returns the cell value of a specific position will panic (index out of range) if the position is out of bounds

Example
g := Grid[int]{Values: [][]int{
	{1, 2, 3},
	{4, 5, 6},
	{7, 8, 9},
}}

fmt.Println(g.Value(Point{Row: 0, Column: 0}))
fmt.Println(g.Value(Point{Row: 1, Column: 1}))
fmt.Println(g.Value(Point{Row: 2, Column: 2}))
Output:

1
5
9

type Point

type Point struct {
	Row    int
	Column int
}

Point represents a point in the grid

func (*Point) FirstInRow

func (p *Point) FirstInRow() bool

FirstInRow is true if the point is the first column in a given row

Example
g := Grid[int]{Values: [][]int{
	{1, 2, 3},
	{4, 5, 6},
	{7, 8, 9},
}}

c1 := g.Cell(Point{Row: 0, Column: 0})
c2 := g.Cell(Point{Row: 2, Column: 0})
c3 := g.Cell(Point{Row: 1, Column: 1})

fmt.Println(c1.Point.FirstInRow())
fmt.Println(c2.Point.FirstInRow())
fmt.Println(c3.Point.FirstInRow())
Output:

true
true
false

type Region

type Region struct {
	Start Point
	End   Point
}

Region represents an area in a grid

type Size

type Size struct {
	Rows    int
	Columns int
}

Size represents the size of the grid

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL