table

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cell

type Cell struct {
	// Char is the character of the cell.
	Char rune

	// Style is the Style of the cell.
	Style tcell.Style
}

Cell is a table cell.

func LineToCells

func LineToCells(line string, style tcell.Style) []*Cell

LineToCells converts a string to a slice of DrawCells with the given style.

Parameters:

  • line: The string to convert.
  • style: The style to use.

Returns:

  • []*DrawCell: The slice of DrawCells.

func NewCell

func NewCell(char rune, style tcell.Style) *Cell

NewCell creates a new table cell.

Parameters:

  • char: The character of the cell.
  • style: The style of the cell.

Returns:

  • *Cell: The new table cell. Never returns nil.

type Displayer

type Displayer interface {
	// Draw is a method of cdd.TableDrawer that draws the unit to the table at the given x and y
	// coordinates.
	//
	// Parameters:
	//   - table: The table to draw the unit to.
	//   - x: The x coordinate to draw the unit at.
	//   - y: The y coordinate to draw the unit at.
	//
	// Returns:
	//   - error: An error of type *ers.ErrInvalidParameter if the table is nil.
	//
	// Behaviors:
	//   - Any value that would be drawn outside of the table is not drawn.
	//   - Assumes that the table is not nil.
	Draw(table *Table, x, y *int) error
}

type Table

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

Table represents a table of cells that can be drawn to the screen.

func NewTable

func NewTable(width, height int) (*Table, error)

NewTable creates a new table of type DrawCell with the given width and height. Negative parameters are treated as absolute values.

Parameters:

  • width: The width of the table.
  • height: The height of the table.

Returns:

  • *Table: The new table.
  • error: An error if the table could not be created.

Errors:

  • *gcers.ErrInvalidParameter: If the width or height is less than 0.

func (*Table) Cell

func (t *Table) Cell() iter.Seq[*Cell]

Cell returns an iterator that is a pull-model iterator that scans the table row by row as it was an array of elements of type DrawCell.

Example:

[ a b c ]
[ d e f ]

Cell() -> [ a ] -> [ b ] -> [ c ] -> [ d ] -> [ e ] -> [ f ]

func (*Table) CellAt

func (t *Table) CellAt(x, y int) *Cell

CellAt returns the cell at the given coordinates in the table. However, out-of-bounds coordinates return nil.

Parameters:

  • x: The x-coordinate of the cell.
  • y: The y-coordinate of the cell.

Returns:

  • *DrawCell: The cell at the given coordinates.

func (*Table) Cleanup

func (t *Table) Cleanup()

Cleanup is a method that cleans up the table.

It sets all cells in the table to the zero value of type int.

func (*Table) FullTable

func (t *Table) FullTable() [][]*Cell

FullTable returns the full table as a 2D slice of elements of type DrawCell.

Returns:

  • [][]*DrawCell: The full table.

func (*Table) GetLines

func (t *Table) GetLines() []string

GetLines returns each line of the drawTable as a string.

Parameters:

  • table: The drawTable.

Returns:

  • []string: The lines of the drawTable.

Behaviors:

  • Any nil cells in the drawTable are represented by a space character.
  • The last line may not be full width.

func (*Table) Height

func (t *Table) Height() int

Height returns the height of the table.

Returns:

  • int: The height of the table. Never negative.

func (*Table) IsXInBounds

func (t *Table) IsXInBounds(x int) error

IsXInBounds checks if the given x-coordinate is within the bounds of the table.

Parameters:

  • x: The x-coordinate to check.

Returns:

  • error: An error of type *ints.ErrOutOfBounds if the x-coordinate is out of bounds.

func (*Table) IsYInBounds

func (t *Table) IsYInBounds(y int) error

IsYInBounds checks if the given y-coordinate is within the bounds of the table.

Parameters:

  • y: The y-coordinate to check.

Returns:

  • error: An error of type *ints.ErrOutOfBounds if the y-coordinate is out of bounds.

func (*Table) ResizeHeight

func (t *Table) ResizeHeight(new_height int) error

ResizeHeight resizes the table to the given height.

Parameters:

  • new_height: The new height of the table.

Returns:

  • error: An error if the table could not be resized.

Errors:

  • *gcers.ErrInvalidParameter: If the new height is less than 0.
  • gcers.NilReceiver: If the table is nil.

func (*Table) ResizeWidth

func (t *Table) ResizeWidth(new_width int) error

ResizeWidth resizes the table to the given width.

Parameters:

  • new_width: The new width of the table.

Returns:

  • error: An error if the table could not be resized.

Errors:

  • *gcers.ErrInvalidParameter: If the new width is less than 0.
  • gcers.NilReceiver: If the table is nil.

func (*Table) Row

func (t *Table) Row() iter.Seq[[]*Cell]

Row returns an iterator that is a pull-model iterator that scans the table row by row as it was an array of elements of type DrawCell.

Example:

[ a b c ]
[ d e f ]

Row(0) -> [ a b c ]
Row(1) -> [ d e f ]

func (*Table) Width

func (t *Table) Width() int

Width returns the width of the table.

Returns:

  • int: The width of the table. Never negative.

func (*Table) WriteAt

func (t *Table) WriteAt(x, y int, cell *Cell)

WriteAt writes a cell to the table at the given coordinates. However, out-of-bounds coordinates do nothing.

Parameters:

  • x: The x-coordinate of the cell.
  • y: The y-coordinate of the cell.
  • cell: The cell to write to the table.

func (*Table) WriteHorizontalSequence

func (t *Table) WriteHorizontalSequence(x, y *int, sequence []*Cell)

WriteHorizontalSequence is the equivalent of WriteVerticalSequence but for horizontal sequences.

See WriteVerticalSequence for more information.

Parameters:

  • x: The x-coordinate of the starting cell.
  • y: The y-coordinate of the starting cell.
  • sequence: The sequence of cells to write to the table.

func (*Table) WriteLineAt

func (t *Table) WriteLineAt(x, y *int, line string, style tcell.Style, isHorizontal bool)

WriteLineAt writes a string to the drawTable at the given coordinates.

Parameters:

  • x: The x-coordinate of the starting cell.
  • y: The y-coordinate of the starting cell.
  • line: The string to write to the drawTable.
  • style: The style of the string.
  • isHorizontal: A boolean that determines if the string should be written horizontally or vertically.

Behaviors:

  • This is just a convenience function that converts the string to a sequence of cells and calls WriteHorizontalSequence or WriteVerticalSequence.
  • x and y are updated to the next available cell after the line is written.

func (*Table) WriteTableAt

func (t *Table) WriteTableAt(table *Table, x, y *int)

WriteTableAt is a convenience function that copies the values from the given table to the table starting at the given coordinates in a more efficient way than using any other methods.

While it acts in the same way as both WriteVerticalSequence and WriteHorizontalSequence combined, it is more efficient than calling those two functions separately.

See WriteVerticalSequence for more information.

Parameters:

  • table: The table to write to the table.
  • x: The x-coordinate to write the table at.
  • y: The y-coordinate to write the table at.

If the table is nil, x or y are nil, nothing happens.

func (*Table) WriteVerticalSequence

func (t *Table) WriteVerticalSequence(x, y *int, sequence []*Cell)

WriteVerticalSequence is a function that writes the specified values to the table starting from the specified coordinates (top = 0, 0) and continuing down the table in the vertical direction until either the sequence is exhausted or the end of the table is reached; at which point any remaining values in the sequence are ignored.

Due to implementation details, any value that would be written outside are ignored. As such, if x is out-of-bounds, the function does nothing and, if y is out-of-bounds, only out-of-bounds values are not written.

Parameters:

  • x: The x-coordinate of the starting cell. (Never changes)
  • y: The y-coordinate of the starting cell.
  • sequence: The sequence of cells to write to the table.

At the end of the function, the y coordinate points to the cell right below the last cell in the sequence that was written.

Example:

// [ a b c ]
// [ d e f ]
//
// seq := [ g h i ], x = 0, y = -1

WriteVerticalSequence(x, y, seq)

// [ h b c ]
// [ i e f ]
//
// x = 0, y = 2

As you can see, the 'g' value was ignored as it would be out-of-bounds. Finally, if either x or y is nil, the function does nothing.

Jump to

Keyboard shortcuts

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