Documentation
¶
Overview ¶
Package transferlist is two lists side by side with items that move between them: what is available on the left, what was chosen on the right. Up and Down move a cursor in one list, Left and Right change list, and Enter or Space moves the item under the cursor across. With the mouse on, a click on an item moves it.
Stability: experimental. Its API may change in any minor release.
Example ¶
Down moves the cursor; Enter moves the item under it to the other list.
package main
import (
"fmt"
"github.com/ows4444/tui"
"github.com/ows4444/tui/transferlist"
)
func main() {
cols := transferlist.New("Name", "Size", "Modified")
cols.LeftTitle, cols.RightTitle = "Hidden", "Shown"
cols.Focus()
cols, _ = cols.Update(tui.Key{Type: tui.KeyDown})
cols, cmd := cols.Update(tui.Key{Type: tui.KeyEnter})
fmt.Println(cmd().(transferlist.ChangedMsg).Moved)
fmt.Println(cols.Left, cols.Right)
}
Output: [Size] [Name Modified] [Size]
Index ¶
- type ChangedMsg
- type KeyMap
- type Model
- func (m Model) Active() Side
- func (m Model) Bindings() []keymap.Binding
- func (m *Model) Blur()
- func (m Model) Cursor(s Side) int
- func (m *Model) Focus() tui.Cmd
- func (m Model) Focused() bool
- func (m Model) LayoutNode() layout.Node
- func (m Model) Linearize() string
- func (m Model) Rows() int
- func (m *Model) SetCursor(s Side, i int)
- func (m Model) SetTheme(t theme.Theme) Model
- func (m Model) Tokens() theme.Tokens
- func (m Model) Update(msg tui.Msg) (Model, tui.Cmd)
- func (m Model) View() string
- func (m Model) Width() int
- func (m Model) WithTokens(tok theme.Tokens) Model
- type Side
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ChangedMsg ¶
type ChangedMsg struct {
// ID is the pair's ID.
ID string
// Moved are the items that moved, in the order they were added.
Moved []string
// To is the list they moved to.
To Side
}
ChangedMsg is delivered (via the Cmd Update returns) when items move, by key or by pointer. The lists as they now stand are the Model's Left and Right.
type KeyMap ¶
type KeyMap struct {
Up keymap.Binding // move the cursor up
Down keymap.Binding // move the cursor down
First keymap.Binding // move the cursor to the first item
Last keymap.Binding // move the cursor to the last item
ToLeft keymap.Binding // put the cursor in the left list
ToRight keymap.Binding // put the cursor in the right list
Move keymap.Binding // move the item under the cursor across
MoveAll keymap.Binding // move every item of the cursor's list across
}
KeyMap names the keys of each action of a Model.
func DefaultKeyMap ¶
func DefaultKeyMap() KeyMap
DefaultKeyMap returns Up and Down, Home and End, Left and Right, Enter or Space to move one item, and Ctrl+A to move them all.
type Model ¶
type Model struct {
// ID is carried by ChangedMsg, to tell one pair's change from another's.
ID string
// Left and Right are the items of the two lists, in the order drawn. An
// item that moves is added to the end of the other list. Update never
// writes into the arrays behind them: a move builds new slices.
Left, Right []string
// LeftTitle and RightTitle head the two lists. Empty is read as
// "Available" and "Chosen".
LeftTitle, RightTitle string
// Height is the number of item rows drawn. A list with more scrolls to
// keep its cursor in view. Zero or less draws every item of the longer
// list.
Height int
// ColumnWidth is the width of each list in cells; longer text is cut
// and ends in an ellipsis. 1 is read as 2, the two end cells.
// Zero or less fits each list to its title and its items, so a list's
// width changes as items move.
ColumnWidth int
// Disabled draws the lists faint and makes Update ignore keys and the
// pointer.
Disabled bool
Theme theme.Theme
// KeyMap holds the keys for each action. New fills it with DefaultKeyMap;
// a Model built as a struct literal with a zero KeyMap behaves as if it
// held DefaultKeyMap.
KeyMap KeyMap
// Mouse, when true, makes Update handle tui.MouseEvent: a left press on
// an item moves it across, and the wheel scrolls the list under it. Off
// (the default) ignores the mouse.
Mouse bool
// Bounds is the screen rectangle where the app draws the lists.
Bounds hittest.Rect
// contains filtered or unexported fields
}
Model is one pair of lists. The zero value is two empty lists; build one with New.
func (Model) Active ¶
Active returns the list the cursor is in. It is never an empty list while the other has items.
func (Model) Bindings ¶
Bindings returns the actions the lists honour, with descriptions, for help text.
func (Model) Cursor ¶
Cursor returns the index of the item under the cursor in list s, or -1 when that list is empty. Each list keeps its own.
func (*Model) Focus ¶
Focus gives the lists keyboard focus. It returns no Cmd; the result is there so they can stand where any focusable widget does.
func (Model) LayoutNode ¶
LayoutNode adapts the widget to a layout.Node of its own width and its Rows. The Model is not changed.
func (Model) Linearize ¶
Linearize renders the lists as plain text for accessible output (see tui.Linearizer): each title with its count, then its items one to a line, every item whether or not it is scrolled into view.
func (*Model) SetCursor ¶
SetCursor puts the cursor on item i of list s. An index out of range is ignored.
func (Model) SetTheme ¶
SetTheme returns m with t applied. It makes Model a tui.ThemeSetter, so a root model can forward the Program's theme (see tui.WithTheme).
func (Model) Tokens ¶
Tokens returns the colour tokens the widget renders with: its theme's roles, overridden by any theme.WithTokens(theme.ComponentTransferList, ...) and then by WithTokens.
func (Model) Update ¶
Update moves the cursor, changes list and moves items on the keys of the KeyMap when the lists have focus. With Mouse on, a left press on an item moves it across and the wheel scrolls the list under the pointer. Left or Right towards an empty list does nothing.
func (Model) View ¶
View renders the two lists side by side under their titles, each title with its count, and a rule between them:
Available (2) │ Chosen (1) Apples │ Dates Cherries │
Every item has a blank cell either side. The item under the cursor of lists that have focus takes angle brackets in those two cells, "<Apples>", so focus reads without colour and does not change the width.