transferlist

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

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 New

func New(items ...string) Model

New returns a pair of lists with the given items on the left and none on the right.

func (Model) Active

func (m Model) Active() Side

Active returns the list the cursor is in. It is never an empty list while the other has items.

func (Model) Bindings

func (m Model) Bindings() []keymap.Binding

Bindings returns the actions the lists honour, with descriptions, for help text.

func (*Model) Blur

func (m *Model) Blur()

Blur takes keyboard focus away.

func (Model) Cursor

func (m Model) Cursor(s Side) int

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

func (m *Model) Focus() tui.Cmd

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) Focused

func (m Model) Focused() bool

Focused reports whether the lists have keyboard focus.

func (Model) LayoutNode

func (m Model) LayoutNode() layout.Node

LayoutNode adapts the widget to a layout.Node of its own width and its Rows. The Model is not changed.

func (Model) Linearize

func (m Model) Linearize() string

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) Rows

func (m Model) Rows() int

Rows is the number of rows View takes: the titles and the item rows.

func (*Model) SetCursor

func (m *Model) SetCursor(s Side, i int)

SetCursor puts the cursor on item i of list s. An index out of range is ignored.

func (Model) SetTheme

func (m Model) SetTheme(t theme.Theme) Model

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

func (m Model) Tokens() theme.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

func (m Model) Update(msg tui.Msg) (Model, tui.Cmd)

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

func (m Model) View() string

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.

func (Model) Width

func (m Model) Width() int

Width is the number of cells View takes: the two lists and the rule between them.

func (Model) WithTokens

func (m Model) WithTokens(tok theme.Tokens) Model

WithTokens returns m with tok as its per-instance colour override. Nil fields inherit from the theme, so only the roles tok names change; the theme and every other widget are untouched. A second call replaces the first.

type Side

type Side int

Side names one of the two lists.

const (
	Left Side = iota
	Right
)

The two lists.

Jump to

Keyboard shortcuts

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