grid

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2019 License: Apache-2.0 Imports: 3 Imported by: 0

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.

tbx, err := termbox.New()
if err != nil {
	panic(err)
}
defer tbx.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(tbx, gridOpts...)
if err != nil {
	panic(err)
}

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := termdash.Run(ctx, tbx, 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.

tbx, err := termbox.New()
if err != nil {
	panic(err)
}
defer tbx.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(tbx, gridOpts...)
if err != nil {
	panic(err)
}

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := termdash.Run(ctx, tbx, 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 RowHeightPerc() and Columns are created using ColWidthPerc(). 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 ColWidthPerc

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

ColWidthPerc creates a column of the specified 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 RowHeightPerc

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

RowHeightPerc creates a row of the specified 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 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