grid

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2023 License: Apache-2.0 Imports: 3 Imported by: 40

Documentation

Overview

Package grid helps to build grid layouts.

Example

Shows how to create a simple 4x4 grid with four widgets. All the cells in the grid contain the same widget in this example.

t, err := tcell.New()
if err != nil {
	panic(err)
}
defer t.Close()

bc, err := barchart.New()
if err != nil {
	panic(err)
}

builder := New()
builder.Add(
	RowHeightPerc(
		50,
		ColWidthPerc(50, Widget(bc)),
		ColWidthPerc(50, Widget(bc)),
	),
	RowHeightPerc(
		50,
		ColWidthPerc(50, Widget(bc)),
		ColWidthPerc(50, Widget(bc)),
	),
)
gridOpts, err := builder.Build()
if err != nil {
	panic(err)
}

cont, err := container.New(t, gridOpts...)
if err != nil {
	panic(err)
}

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := termdash.Run(ctx, t, cont); err != nil {
	panic(err)
}
Output:

Example (Iterative)

Shows how to create rows iteratively. Each row contains two columns and each column contains the same widget.

t, err := tcell.New()
if err != nil {
	panic(err)
}
defer t.Close()

bc, err := barchart.New()
if err != nil {
	panic(err)
}

builder := New()
for i := 0; i < 5; i++ {
	builder.Add(
		RowHeightPerc(
			20,
			ColWidthPerc(50, Widget(bc)),
			ColWidthPerc(50, Widget(bc)),
		),
	)
}
gridOpts, err := builder.Build()
if err != nil {
	panic(err)
}

cont, err := container.New(t, gridOpts...)
if err != nil {
	panic(err)
}

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := termdash.Run(ctx, t, cont); err != nil {
	panic(err)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

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

Builder builds grid layouts.

func New

func New() *Builder

New returns a new grid builder.

func (*Builder) Add

func (b *Builder) Add(subElements ...Element)

Add adds the specified elements. The subElements can be either a single Widget or any combination of Rows and Columns. Rows are created using functions with the RowHeight prefix and Columns are created using functions with the ColWidth prefix Can be called repeatedly, e.g. to add multiple Rows or Columns.

func (*Builder) Build

func (b *Builder) Build() ([]container.Option, error)

Build builds the grid layout and returns the corresponding container options.

type Element

type Element interface {
	// contains filtered or unexported methods
}

Element is an element that can be added to the grid.

func ColWidthFixed added in v0.10.0

func ColWidthFixed(widthCells int, subElements ...Element) Element

ColWidthFixed creates a column of the specified fixed width. The width is supplied as a number of cells on the terminal. If the actual terminal size leaves the container with less than the specified amount of cells, the container will be created with zero cells and won't be drawn until the terminal size increases. If the sum of all the widths is less than 100% of the screen width, the last element stretches to the edge of the screen. The subElements can be either a single Widget or any combination of Rows and Columns. A column with fixed width cannot contain any sub-elements with relative size.

func ColWidthFixedWithOpts added in v0.10.0

func ColWidthFixedWithOpts(widthCells int, cOpts []container.Option, subElements ...Element) Element

ColWidthFixedWithOpts is like ColWidthFixed, but also allows to apply additional options to the container that represents the column.

func ColWidthPerc

func ColWidthPerc(widthPerc int, subElements ...Element) Element

ColWidthPerc creates a column of the specified relative width. The width is supplied as width percentage of the parent element. The sum of all widths at the same level cannot be larger than 100%. If it is less that 100%, the last element stretches to the edge of the screen. The subElements can be either a single Widget or any combination of Rows and Columns.

func ColWidthPercWithOpts added in v0.9.0

func ColWidthPercWithOpts(widthPerc int, cOpts []container.Option, subElements ...Element) Element

ColWidthPercWithOpts is like ColWidthPerc, but also allows to apply additional options to the container that represents the column.

func RowHeightFixed added in v0.10.0

func RowHeightFixed(heightCells int, subElements ...Element) Element

RowHeightFixed creates a row of the specified fixed height. The height is supplied as a number of cells on the terminal. If the actual terminal size leaves the container with less than the specified amount of cells, the container will be created with zero cells and won't be drawn until the terminal size increases. If the sum of all the heights is less than 100% of the screen height, the last element stretches to the edge of the screen. The subElements can be either a single Widget or any combination of Rows and Columns. A row with fixed height cannot contain any sub-elements with relative size.

func RowHeightFixedWithOpts added in v0.10.0

func RowHeightFixedWithOpts(heightCells int, cOpts []container.Option, subElements ...Element) Element

RowHeightFixedWithOpts is like RowHeightFixed, but also allows to apply additional options to the container that represents the row.

func RowHeightPerc

func RowHeightPerc(heightPerc int, subElements ...Element) Element

RowHeightPerc creates a row of the specified relative height. The height is supplied as height percentage of the parent element. The sum of all heights at the same level cannot be larger than 100%. If it is less that 100%, the last element stretches to the edge of the screen. The subElements can be either a single Widget or any combination of Rows and Columns.

func RowHeightPercWithOpts added in v0.9.0

func RowHeightPercWithOpts(heightPerc int, cOpts []container.Option, subElements ...Element) Element

RowHeightPercWithOpts is like RowHeightPerc, but also allows to apply additional options to the container that represents the row.

func Widget

func Widget(w widgetapi.Widget, cOpts ...container.Option) Element

Widget adds a widget into the Row or Column. The options will be applied to the container that directly holds this widget.

Jump to

Keyboard shortcuts

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