twidgets

package module
v0.0.0-...-36dc9b5 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2021 License: Apache-2.0 Imports: 3 Imported by: 5

README

Terminal widgets

Some commonly used widgets for terminal applications. Based on rivo/tview.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Banner struct {
	*cview.Grid

	//Selectable are all primitives that can be navigated and selected inside Banner.
	Selectable []Selectable
	// contains filtered or unexported fields
}

Banner combines grid layout and form-movement. To use, configure grid and add elements to it. To allow some item to be selected, add it to Banner.Selectable. Order in this array is same as with selections. Only buttons are supported as selectables. Most of the logic has been copied cview.Form.

func NewBanner

func NewBanner() *Banner

NewBanner initializes new banner

func (*Banner) Focus

func (b *Banner) Focus(delegate func(p cview.Primitive))

Focus. Copied from cview/form

type ListItem

type ListItem interface {
	cview.Primitive
	SetSelected(selected Selection)
}

ListItem is an item that can be used in ScrollList. Additional SetSelected is required since item doesn't receive focus on selection but still gets to change its visual style.

type Modal interface {
	//Primitive
	cview.Primitive
	//SetDoneFunc sets function that get's called when modal wants to close itself
	SetDoneFunc(doneFunc func())
	//Setvisible tells modal to show or hide itself
	SetVisible(visible bool)
}

Modal interface is primitive that is drawn on top of other views and get's destroyed when it's ready

type ModalLayout

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

ModalLayout is a grid layout that draws modal on center of grid. To add ordinary items to layout, get grid with Grid() function. Layout consists of 10 columns and rows, each one of size -1. This can be changed with SetGridSize, but size must still 10. Modal is drawn on middle 4 cells Use AddModal and RemoveModal to manage modals. Only single modal can be shown at a time.

func NewModalLayout

func NewModalLayout() *ModalLayout

NewModalLayout creates new modal layout. Default grid is [-1, -1, -1, -1, -1]. This can be modified by accessing grid with ModalLayout.Grid()

func (*ModalLayout) AddDynamicModal

func (m *ModalLayout) AddDynamicModal(modal Modal, size ModalSize)

AddDynamicModal adds modal of dynamic size

func (*ModalLayout) AddFixedModal

func (m *ModalLayout) AddFixedModal(modal Modal, height, width uint, size ModalSize)

AddFixedModal adds modal of fixed size. Size parameter controls how many rows and columns are used for modal

func (*ModalLayout) Blur

func (m *ModalLayout) Blur()

func (*ModalLayout) Draw

func (m *ModalLayout) Draw(screen tcell.Screen)

func (*ModalLayout) Focus

func (m *ModalLayout) Focus(delegate func(p cview.Primitive))

func (*ModalLayout) GetFocusable

func (m *ModalLayout) GetFocusable() cview.Focusable

func (*ModalLayout) GetGridSize

func (m *ModalLayout) GetGridSize() []int

GetGridSize returns grid that's in use

func (*ModalLayout) GetRect

func (m *ModalLayout) GetRect() (int, int, int, int)

func (*ModalLayout) GetVisible

func (m *ModalLayout) GetVisible() bool

func (*ModalLayout) Grid

func (m *ModalLayout) Grid() *cview.Grid

GetGrid returns underlying grid that items are added to

func (*ModalLayout) InputHandler

func (m *ModalLayout) InputHandler() func(event *tcell.EventKey, setFocus func(p cview.Primitive))

func (*ModalLayout) MouseHandler

func (m *ModalLayout) MouseHandler() func(action cview.MouseAction, event *tcell.EventMouse,
	setFocus func(p cview.Primitive)) (consumed bool, capture cview.Primitive)

func (*ModalLayout) RemoveModal

func (m *ModalLayout) RemoveModal(modal Modal)

RemoveModal removes modal

func (*ModalLayout) SetGridXSize

func (m *ModalLayout) SetGridXSize(grid []int) error

SetGrixXSize sets grid in horizontal direction

func (*ModalLayout) SetGridYSize

func (m *ModalLayout) SetGridYSize(grid []int) error

SetGrixYSize sets grid in vertical direction

func (*ModalLayout) SetRect

func (m *ModalLayout) SetRect(x, y, width, height int)

func (*ModalLayout) SetVisible

func (m *ModalLayout) SetVisible(v bool)

type ModalSize

type ModalSize int

ModalSize describes both horizontal & vertical size for modal

const (
	//ModalSizeSmall creates modal of 1/5 size of layout, or 2 columns
	ModalSizeSmall ModalSize = 2
	//ModalSizeMedium creates modal of 2/5 size of layout, or 4 columns
	ModalSizeMedium ModalSize = 3
	//ModalSizeMedium creates modal of 3/5 size of layout, or 6 columns
	ModalSizeLarge ModalSize = 4
)
type NavBar struct {
	// contains filtered or unexported fields
}

NavBar implements navigation bar with multiple buttons. Buttons can be added one by one, each one having their own callbacks. In addition, optional DoneFunc is called with label of the selected button.

func NewNavBar

func NewNavBar(colors *NavBarColors, doneFunc func(label string)) *NavBar

NewNavBar creates new navigation bar. DoneFunc is called with buttons label whenever user clicks some button. DoneFunc can be set nil.

func (n *NavBar) AddButton(button *cview.Button, key tcell.Key)

AddButton adds a new button to right side of existing buttons. Key is used to print and highlight key to user

func (n *NavBar) Blur()
func (n *NavBar) Draw(screen tcell.Screen)
func (n *NavBar) Focus(delegate func(p cview.Primitive))
func (n *NavBar) GetFocusable() cview.Focusable
func (n *NavBar) GetRect() (int, int, int, int)
func (n *NavBar) GetVisible() bool
func (n *NavBar) InputHandler() func(event *tcell.EventKey, setFocus func(p cview.Primitive))
func (n *NavBar) MouseHandler() func(action cview.MouseAction, event *tcell.EventMouse, setFocus func(p cview.Primitive)) (consumed bool, capture cview.Primitive)
func (n *NavBar) SetRect(x, y, width, height int)
func (n *NavBar) SetVisible(v bool)
type NavBarColors struct {
	Background            tcell.Color
	BackgroundFocus       tcell.Color
	ButtonBackground      tcell.Color
	ButtonBackgroundFocus tcell.Color
	Text                  tcell.Color
	TextFocus             tcell.Color
	Shortcut              tcell.Color
	ShortcutFocus         tcell.Color
}

NacBarColors are colors that fully describe navbar look

type ScrollList

type ScrollList struct {
	*cview.Grid
	*cview.ContextMenu

	// Padding is num of rows or relative expansion, see cview.Grid.SetColumns() for usage
	Padding    int
	ItemHeight int

	// PreInputHandler is called before actual InputHandler, if any.
	PreInputHandler func(event *tcell.EventKey, setFocus func(p cview.Primitive))
	// contains filtered or unexported fields
}

ScrollGrid is a list that can have more items than it can currently show. It allows user to scroll items. It also manages rows dynamically. Use Padding and ItemHeight to change grid size. Use Up/Down + (vim: j/k/g/G) to navigate between items and Enter to select item.

func NewScrollList

func NewScrollList(selectFunc func(index int)) *ScrollList

NewScrollList creates new scroll grid. selectFunc is called whenever user presses Enter on some item. SelectFunc can be nil.

func (*ScrollList) AddItem

func (s *ScrollList) AddItem(i ListItem)

AddItem appends single item

func (*ScrollList) AddItems

func (s *ScrollList) AddItems(i ...ListItem)

AddItems appends multiple items

func (*ScrollList) Blur

func (s *ScrollList) Blur()

func (*ScrollList) Clear

func (s *ScrollList) Clear()

Clear clears list items an updates view

func (*ScrollList) Draw

func (s *ScrollList) Draw(screen tcell.Screen)

func (*ScrollList) Focus

func (s *ScrollList) Focus(delegate func(p cview.Primitive))

func (*ScrollList) GetItems

func (s *ScrollList) GetItems() []ListItem

func (*ScrollList) GetSelectedIndex

func (s *ScrollList) GetSelectedIndex() int

func (*ScrollList) HasFocus

func (s *ScrollList) HasFocus() bool

func (*ScrollList) InputHandler

func (s *ScrollList) InputHandler() func(event *tcell.EventKey, setFocus func(p cview.Primitive))

func (*ScrollList) MouseHandler

func (s *ScrollList) MouseHandler() func(action cview.MouseAction, event *tcell.EventMouse, setFocus func(p cview.Primitive)) (consumed bool, capture cview.Primitive)

MouseHandler returns the mouse handler for this primitive.

func (*ScrollList) SetBlurFunc

func (s *ScrollList) SetBlurFunc(blur func(key tcell.Key))

func (*ScrollList) SetBorder

func (s *ScrollList) SetBorder(b bool) *cview.Box

func (*ScrollList) SetIndexChangedFunc

func (s *ScrollList) SetIndexChangedFunc(indexChanged func(int) bool)

SetIndexChangedFunc sets a function that gets called every time list index is about to change. New index is passed to function. If it returns true, index changes. If false, index is being retained.

func (*ScrollList) SetRect

func (s *ScrollList) SetRect(x, y, w, h int)

func (*ScrollList) SetSelected

func (s *ScrollList) SetSelected(index int)

SetSelected sets active index. First item is 0. If value is out of bounds, do nothing.

type Selectable

type Selectable interface {
	cview.Primitive
	// SetBlurFunc sets blur function that gets called upon blurring primitive
	SetBlurFunc(func(key tcell.Key))
}

Selectable is a primitive that's able to set blur func

type Selection

type Selection int
const (
	Selected Selection = iota
	Blurred
	Deselected
)

type Sort

type Sort int

SortType is a direction that can be sorted with

const (
	// Sort ascending
	SortAsc Sort = iota
	// Sort descending
	SortDesc
)

type Table

type Table struct {
	*cview.Table
	// contains filtered or unexported fields
}

Table extends cview.Table with some helpers for managing rows. In addition it provides sorting capabilities.

func NewTable

func NewTable() *Table

NewTable creates new table instance

func (*Table) AddRow

func (t *Table) AddRow(index int, content ...string) *Table

AddRow adds single row to table

func (*Table) Clear

func (t *Table) Clear(headers bool) *Table

Clear clears the content of the table. If headers==true, remove headers as well

func (*Table) InputHandler

func (t *Table) InputHandler() func(event *tcell.EventKey, setFocus func(p cview.Primitive))

Inputhandler handles header row inputs

func (*Table) SetAddCellFunc

func (t *Table) SetAddCellFunc(cellFunc func(cell *cview.TableCell, header bool, col int)) *Table

SetAddCellFunc add function callback that gets called every time a new cell is added with flag of whether the cell is in header row. Use this to modify e.g. style of the cell when it gets added to table.

func (*Table) SetColumnExpansions

func (t *Table) SetColumnExpansions(expansions []int)

SetColumnExpansions sets how each column will expand / shrink when changing terminal size. If index is included as first row, it must be included in here.

func (*Table) SetColumnWidths

func (t *Table) SetColumnWidths(widths []int)

SetColumnWidths sets each columns maximum width. If index is included as first row, it must be included in here.

func (*Table) SetColumns

func (t *Table) SetColumns(columns []string) *Table

SetColumns set column header names. This will clear the table

func (*Table) SetShowIndex

func (t *Table) SetShowIndex(index bool)

SetShowIndex configure whether first column in table is item index. If set, first item is in index 1. Changing this does not update existing data. Thus data needs to be cleared and rows added again for changes to take effect

func (*Table) SetSort

func (t *Table) SetSort(column int, sort Sort) *Table

SetSort sets default sort column and type

func (*Table) SetSortFunc

func (t *Table) SetSortFunc(sortFunc func(column string, sort Sort)) *Table

SetSortFunc sets sorting function that gets called whenever user calls sorting some column

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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