Documentation
¶
Overview ¶
Package pick draws keyboard-driven prompts on a terminal: the interactive counterpart of a flag the user did not pass.
There are three shapes of prompt:
- A list (Select, SelectOrType, SelectManyOrType, SelectRange, SelectView) picks one row, several rows or a range of rows from many. Rows are aligned columns; typing filters them, views narrow them, and rows can carry an action, a flag, details and their own colors.
- A text field (Input) takes a value, optionally without echoing it.
- Switches (Toggles, Checklist) set several yes/no values at once.
Every prompt switches its input to raw mode, draws itself in place from the line the cursor is on, and erases itself before returning, restoring the terminal however it ends. A prompt needs both its input and its output to be a terminal; otherwise it returns ErrNotATerminal and the caller falls back to its non-interactive behavior. Colors come from the active theme (see package theme) and are used only where term.Colored allows them.
Text ¶
Everything a prompt is given to draw (titles, notes, rows, labels, the value a text field starts with) is shown as plain text on one line: escape sequences and control characters are dropped, a line break or a tab becomes a space, and the characters that reorder text on screen go. Text from elsewhere, such as the title of a pull request or the name of a branch, can be handed in as it is. Colors are set with styles (Options.CellStyles, Options.PaintCell, the theme), not inside the text. The same holds for what is typed: a control character pasted into a filter or a text field is left out.
The terminal ¶
A prompt takes the line the cursor is on: what was written on that line before is erased with the prompt, so end what you print before a prompt with a newline. The block is as tall as the terminal lets it be. On a terminal too short for all of it, the lines around the rows go first (the note, the blank lines, the key line, the position line) and the title last, so the rows stay.
The terminal is put back as it was found when the prompt is answered or left, when the program panics, and, on Unix, when one of the signals that end a process arrives: SIGINT, SIGTERM, SIGHUP or SIGQUIT. The prompt erases itself, shows the cursor and restores the terminal mode, and then raises the signal again, so it does what it would have done without a prompt on screen. A program that handles such a signal itself keeps its handler, and the prompt returns ErrInterrupted to it; its handler sees the signal twice, once as delivered and once as raised again, since nothing tells a prompt that somebody else is listening. A signal the process ignores is left alone. SIGKILL cannot be caught: a process ended that way leaves the terminal in raw mode.
A terminal that takes no more output, as after Ctrl-S, cannot be written to, so the prompt cannot erase itself there. The mode is put back all the same, a moment after the signal: the prompt then stays on the screen, and the cursor is shown once the terminal takes output again.
A prompt also registers with package live for as long as it is up, so a program that uses live to leave its terminal in order has its prompts covered: the signal handler of live.InstallSignalRestore, which ends the process itself, puts the prompt's terminal back first, and so do live.RestoreAll and a panic passed through live's Cursor.RestoreOnPanic. On Windows, where a prompt watches no signals of its own, that handler is what puts the terminal back. A prompt put away like that is not drawn again. Should the process still be there a moment later, as after a call of live.RestoreAll that was not followed by an exit, the prompt returns ErrInterrupted.
Keys typed ahead are not lost between two prompts: what one prompt read past its answer, such as an answer pasted for the question after it, goes to the next prompt that reads from the same input. Ctrl-C drops it.
A prompt redraws itself by moving the cursor back over the rows it drew, counted at the width the terminal has by then. That is right for a terminal that wraps its lines again when it is made narrower, as nearly all do. One that cuts them off instead (GNU screen, the Linux console) has fewer rows on screen than counted, and making it narrower while a prompt is up erases lines above the prompt on the next key.
Keys ¶
In a list:
↑ ↓, Ctrl-P Ctrl-N move the cursor
PgUp PgDn move a page
Home End, Ctrl-A Ctrl-E
move to the first or last row
typing filters the rows; every word typed has to match
Backspace, Ctrl-U delete from the filter, or clear it
Tab Shift-Tab, → ← switch to the next or previous view
Space mark a row, in a list that takes several
Enter choose the row, or anchor and end a range
Esc go back (an open range is dropped first)
Ctrl-C, Ctrl-D interrupt
In a text field the arrows, Home and End move the caret, Ctrl-W deletes a word, Ctrl-U deletes to the start and Ctrl-K to the end. The caret steps over a character as it is drawn, however many code points it is made of, and a value longer than the line scrolls with it. In a set of switches Space or Tab throws the switch under the cursor, "a" turns all on and "n" all off.
Alt with an arrow, or with another key that sends a sequence of its own, acts as that key alone; Alt with a character is ignored.
Errors ¶
A prompt that is left without an answer returns an error matching ErrAborted. Two more specific errors tell why:
- ErrBack: the user pressed Esc and wants the previous question again.
- ErrInterrupted: the user pressed Ctrl-C or Ctrl-D, or nobody is left to answer (the input ended or was closed, the terminal went away, or a signal ended the prompt), and the whole run is over.
A caller that does not distinguish the two checks errors.Is(err, ErrAborted). A list with nothing to show returns ErrNoItems.
Example ¶
package main
import (
"errors"
"fmt"
"os"
"github.com/mittwald/cli-kit/pick"
)
func main() {
index, err := pick.Select(os.Stdin, os.Stderr, pick.Options{
Title: "Which version of %s?",
Subject: "tool",
Items: []pick.Item{
{Cells: []string{"1.4.2", "latest"}, Action: "install"},
{Cells: []string{"1.3.9", "installed ✓"}, Disabled: true},
{Cells: []string{"1.2.0 (legacy)"}, Action: "install", Muted: true},
},
})
switch {
case errors.Is(err, pick.ErrNotATerminal):
fmt.Println("pass --version instead")
case errors.Is(err, pick.ErrAborted):
fmt.Println("nothing selected")
case err == nil:
fmt.Println("chose row", index)
}
}
Output:
Index ¶
- Constants
- Variables
- func Checklist(in, out *os.File, opts ChecklistOptions) ([]bool, error)
- func Input(in, out *os.File, opts InputOptions) (string, error)
- func Name(w io.Writer, text string) string
- func Prompt(w io.Writer, prompt, subject string) string
- func Select(in, out *os.File, opts Options) (int, error)
- func SelectManyOrType(in, out *os.File, opts Options) ([]int, []string, error)
- func SelectOrType(in, out *os.File, opts Options) (int, string, error)
- func SelectRange(in, out *os.File, opts Options) (first, last int, err error)
- func SelectView(in, out *os.File, opts Options) (view, index int, err error)
- func Status(w io.Writer, text string) func()
- func Toggles(in, out *os.File, opts ToggleOptions) ([]bool, error)
- type Box
- type ChecklistOptions
- type Field
- type InputOptions
- type Item
- type Lens
- type MatchField
- type Options
- type Toggle
- type ToggleOptions
- type Truncation
- type View
Examples ¶
Constants ¶
const ( // PriorityLow suits a column whose full value is available elsewhere, // such as in the title line through Item.Detail. PriorityLow = iota // PriorityNormal is the priority of every column not ranked otherwise. PriorityNormal // PriorityHigh suits the column a reader actually scans. PriorityHigh )
Column priorities for Options.ColumnPriority. When a row does not fit the terminal, the lowest-ranked column is shortened first.
Variables ¶
var ErrAborted = errors.New("prompt aborted")
ErrAborted reports that a prompt was left without an answer. Every way out of a prompt other than answering it matches ErrAborted, so a caller that only needs to know "nothing was chosen" checks for it alone.
var ErrBack = fmt.Errorf("%w: went back", ErrAborted)
ErrBack reports that a prompt was left with Esc, or with Backspace on an empty filter where Options.DismissOnBackspace allows it: the user wants to return to the question asked before this one. It matches ErrAborted.
var ErrInterrupted = fmt.Errorf("%w: interrupted", ErrAborted)
ErrInterrupted reports that a prompt was left with Ctrl-C or Ctrl-D, or that there is nobody left to answer it: its input ended or was closed, the terminal went away, or the prompt was taken off the screen for the process to end (by a signal, or by live.RestoreAll) and the application lived on. The user wants out of the whole run, not just this question. It matches ErrAborted.
var ErrNoItems = errors.New("pick: nothing to select")
ErrNoItems reports that there was nothing to choose from. The caller knows what an empty list means in its context and says so itself.
var ErrNotATerminal = errors.New("pick: not a terminal")
ErrNotATerminal reports that the input or the output of a prompt is not a terminal, so nothing can be drawn. Callers fall back to their non-interactive behavior, such as requiring a flag.
Functions ¶
func Checklist ¶
func Checklist(in, out *os.File, opts ChecklistOptions) ([]bool, error)
Checklist draws a list of boxes and returns which were left ticked, in the order given. It takes the same keys as Toggles, but draws the box in front of each label, with the label's note beside it, the way a form does.
Example ¶
package main
import (
"fmt"
"os"
"github.com/mittwald/cli-kit/pick"
)
func main() {
ticked, err := pick.Checklist(os.Stdin, os.Stderr, pick.ChecklistOptions{
Title: "Which tokens should be created?",
Boxes: []pick.Box{
{Label: "feed", Note: "calendar feed", Checked: true},
{Label: "smtp", Note: "send mail"},
},
})
if err == nil {
fmt.Println(ticked)
}
}
Output:
func Input ¶
func Input(in, out *os.File, opts InputOptions) (string, error)
Input draws a one-line text field and returns what was typed. The field supports the usual line-editing keys: the arrows, Home and End (or Ctrl-A and Ctrl-E), Backspace and Delete, Ctrl-W to delete a word, Ctrl-U to delete to the start, and Ctrl-K to delete to the end. Enter answers, Esc returns ErrBack, and Ctrl-C or Ctrl-D returns ErrInterrupted. A cleared field answers with the empty string.
The caret moves and deletes by whole characters as they are drawn: a letter with its combining marks, or an emoji made of several code points, is one step. A value longer than the line scrolls with the caret, an ellipsis standing where text is out of sight.
Example ¶
package main
import (
"fmt"
"os"
"github.com/mittwald/cli-kit/pick"
)
func main() {
password, err := pick.Input(os.Stdin, os.Stderr, pick.InputOptions{
Prompt: "Password for %s: ",
Subject: "alice@example.com",
Secret: true,
})
if err == nil && password != "" {
fmt.Println("got a password")
}
}
Output:
func Name ¶
Name renders a value the way a list draws a row's name: in the subject style, with whatever it says in parentheses receding. Use it for lines written beside the prompts, so a value reads the same whether it is being chosen or reported. Like a row's name, the value is made plain single-line text first: escape sequences and control characters in it are dropped, and a line break or a tab becomes a space. Where w takes no color that plain text is what Name returns.
Example ¶
package main
import (
"fmt"
"os"
"github.com/mittwald/cli-kit/pick"
)
func main() {
fmt.Println("Installing", pick.Name(os.Stdout, "tool (stable)"))
}
Output: Installing tool (stable)
func Prompt ¶
Prompt renders a question the way Input draws it: the subject styled where the question holds %s for it, and the answers it spells out, such as y/N, receding. Use it for the line that keeps an answered prompt on screen once the field has erased itself. Like Input, it fills the subject in for the first %s only and makes both plain single-line text. Where w takes no color it returns the question with the subject filled in.
Example ¶
package main
import (
"fmt"
"os"
"github.com/mittwald/cli-kit/pick"
)
func main() {
// Off a terminal the prompt is plain text with the subject filled in.
fmt.Println(pick.Prompt(os.Stdout, "Recreate %s? [y/N]: ", "tool-1.2") + "y")
}
Output: Recreate tool-1.2? [y/N]: y
func Select ¶
Select draws the list on out, reads keys from in, and returns the index of the item chosen: an index into opts.Items, or, with views that hold their own rows, into the Items of the view the list ended on (see SelectView). in is switched to raw mode for the duration and restored before returning, and the drawn block is erased so the caller's output starts on a clean line. With Options.Range, Select returns the first row of the range.
func SelectManyOrType ¶
SelectManyOrType is SelectOrType for a list that takes several rows at once: Space marks the row under the cursor or unmarks it, and Enter answers with every row marked. It returns the indexes of the items marked, in the order of opts.Items, and the texts typed and marked, in the order they were marked. With nothing marked, Enter answers with the row under the cursor. Space marks rather than filters, so the filter is a single term.
func SelectOrType ¶
SelectOrType is Select for a list that also takes an answer it does not hold (see Options.Typed). It returns the index of the item chosen and "", or -1 and the text typed. A list without items is drawn all the same as long as it takes typed answers. It cannot be combined with views that hold their own rows.
Example ¶
package main
import (
"fmt"
"os"
"strings"
"github.com/mittwald/cli-kit/pick"
)
func main() {
versions := []pick.Item{{Cells: []string{"1.4.2"}}, {Cells: []string{"1.4.1"}}}
index, typed, err := pick.SelectOrType(os.Stdin, os.Stderr, pick.Options{
Title: "Release which version?",
Items: versions,
Typed: func(text string) bool { return strings.Count(text, ".") == 2 },
TypedNote: "new version",
})
if err != nil {
return
}
if index < 0 {
fmt.Println("new version", typed)
}
}
Output:
func SelectRange ¶
SelectRange is Select for a list with Options.Range: it returns the two ends of the chosen range in the order the rows are drawn, first above last. Without Options.Range both are the row chosen.
Example ¶
package main
import (
"fmt"
"os"
"github.com/mittwald/cli-kit/pick"
"github.com/mittwald/cli-kit/theme"
)
func main() {
first, last, err := pick.SelectRange(os.Stdin, os.Stderr, pick.Options{
Title: "Which changes?",
Items: []pick.Item{
{Cells: []string{"c3", "newest"}, Detail: "c3"},
{Cells: []string{"b2", "middle"}, Detail: "b2"},
{Cells: []string{"a1", "oldest"}, Detail: "a1"},
},
Range: true,
RangeUnit: "change",
CellStyles: []theme.Style{theme.Fg256(71)},
})
if err == nil {
fmt.Println(first, last)
}
}
Output:
func SelectView ¶
SelectView is Select for a list of views: it returns the index of the view the list ended on and the index of the item chosen. For views that hold their own rows the item index refers to that view's Items, otherwise to opts.Items.
Example ¶
package main
import (
"fmt"
"os"
"github.com/mittwald/cli-kit/pick"
)
func main() {
view, index, err := pick.SelectView(os.Stdin, os.Stderr, pick.Options{
Title: "Pick a branch",
Views: []pick.View{
{Name: "local", Items: []pick.Item{{Cells: []string{"main"}, Key: "main"}}},
{Name: "remote", Loading: "fetching remote branches…", Load: func() ([]pick.Item, error) {
return []pick.Item{{Cells: []string{"origin/main"}, Key: "main"}}, nil
}},
},
ColumnPriority: []int{pick.PriorityHigh},
ColumnTruncation: []pick.Truncation{pick.TruncateMiddle},
})
if err == nil {
fmt.Println(view, index)
}
}
Output:
func Status ¶
Status writes text as a line of its own (or several, where it has newlines) and returns a function that erases it again. Use it for what is only of interest while it lasts, such as "Fetching releases...". On a terminal the text is drawn in the theme's Status style and the returned function takes it back off the screen; anywhere else the line stays, since a log is read after the fact.
The text is written as plain text: escape sequences and control characters other than the newline are dropped and tabs become spaces, as term.Plain does it, so a name from elsewhere inside it cannot do more than be read.
The returned function erases by counting screen rows up from the cursor, so it must be called before anything else is written. It erases once: calling it again, as a deferred call after an explicit one does, leaves what was written since alone.
Example ¶
package main
import (
"os"
"github.com/mittwald/cli-kit/pick"
)
func main() {
done := pick.Status(os.Stdout, "Fetching releases...")
// ... fetch ...
done() // erases the line on a terminal; keeps it anywhere else
}
Output: Fetching releases...
func Toggles ¶
func Toggles(in, out *os.File, opts ToggleOptions) ([]bool, error)
Toggles draws a set of switches and returns the position each was left in, in the order given. The arrows move the cursor, Space or Tab throws the switch under it, "a" turns all on and "n" all off, and Enter answers. A thrown switch shows what it was before. Esc returns ErrBack and Ctrl-C or Ctrl-D returns ErrInterrupted, without values.
Example ¶
package main
import (
"fmt"
"os"
"github.com/mittwald/cli-kit/pick"
)
func main() {
values, err := pick.Toggles(os.Stdin, os.Stderr, pick.ToggleOptions{
Title: "Settings for %s",
Subject: "service-a",
Toggles: []pick.Toggle{
{Label: "Enabled", On: "enabled", Off: "disabled", Value: true},
{Label: "Public", On: "public", Off: "private"},
},
})
if err == nil {
fmt.Println(values)
}
}
Output:
Types ¶
type Box ¶
Box is one row of a checklist: what it is called, an optional note on what ticking it means, and whether it starts ticked. Label and Note are shown as plain single-line text, as everything a prompt draws is.
type ChecklistOptions ¶
type ChecklistOptions struct {
// Title is the question, with one %s where Subject goes.
Title string
// Subject is the thing the checklist is about.
Subject string
// Note is an optional line under the title.
Note string
Boxes []Box
// Initial is the box the cursor starts on.
Initial int
// Nested names Esc "back" instead of "cancel" in the key line.
Nested bool
}
ChecklistOptions configures one checklist. Title, Subject and Note are shown as plain single-line text: escape sequences and control characters in them are dropped, and a line break or a tab becomes a space.
type InputOptions ¶
type InputOptions struct {
// Prompt is written in front of the value, with one %s where Subject
// goes, such as "Name for %s: ". Anything more to say belongs on a line
// the caller prints before the prompt, which stays on screen when the
// prompt is erased.
Prompt string
// Subject is the thing the prompt is about, drawn in the theme's subject
// style inside the prompt.
Subject string
// Value is what the field starts out holding, with the cursor at its end.
// It is made plain text like the rest, so a value left as it is comes
// back the way it was shown.
Value string
// Secret draws nothing for what is typed, the way a password prompt does.
Secret bool
}
InputOptions configures one text prompt. The prompt is a single line with the terminal's own cursor in the text, so it reads like any other prompt a terminal puts up.
Prompt, Subject and Value are shown as plain single-line text: escape sequences and control characters in them are dropped, and a line break or a tab becomes a space.
type Item ¶
type Item struct {
// Cells are the row's columns. The first names the row and carries the
// weight; text in parentheses inside it recedes. Later cells qualify the
// row and recede as a whole.
Cells []string
// Action is what choosing the row does, such as "install" or "merge #31".
// It is drawn after every cell, in a lane and a color of its own, so the
// thing a row is decided on sits in the same place on every row.
Action string
// Disabled marks a row there is nothing to do with: it is drawn receded
// and Enter on it does nothing.
Disabled bool
// Muted recedes a row the way Disabled does, but it can still be chosen:
// something that can be done, but that nobody is waiting on.
Muted bool
// Flag is a mark drawn right after the first cell in the theme's flag
// color, on every row alike: something about the name worth stopping at.
Flag string
// Match overrides what type-to-filter searches. By default it searches
// every cell and the action.
Match string
// MatchFields splits what the filter searches into parts, each of which
// may demand a minimum term length: a field a short term must not reach,
// such as an opaque identifier where any two characters hit some row, sets
// MinTerm. It replaces Match when set.
MatchFields []MatchField
// Groups are the kinds of row this is, which views narrow the list by. A
// row may be of several kinds and then shows in the view of each.
Groups []string
// Key identifies the row across views that hold their own rows: when the
// view changes, the cursor stays on the row with the same key. Rows
// without a key match nothing, and the cursor starts at the top.
Key string
// CellStyles overrides Options.CellStyles for this row, indexed like
// Cells. An empty style falls back to the column's style.
CellStyles []theme.Style
// Detail is the row's full value, shown after the title while the row is
// under the cursor. What a narrow column had to shorten stays readable
// there.
Detail string
// Details is Detail in several parts, each in its own style, joined by
// the separator the list uses elsewhere. It replaces Detail when set.
Details []Field
}
Item is one selectable row. Cells are drawn as aligned columns, the first of them naming what the row is.
Everything a row draws (Cells, Action, Flag, Detail and Details) is shown as plain single-line text: escape sequences and control characters in it are dropped, and a line break or a tab becomes a space. Rows made from what a server or a repository holds can therefore be handed in as they are. Colors come from CellStyles and Options.PaintCell, not from the text.
type Lens ¶
Lens is how a list was narrowed: what was typed into its filter and the view it was left on. View holds that view's name, or, for a view without one, its position among the views, counted from 1, the way the status line labels it. An empty View names no view: a list opened with it starts on the view Options.View asks for.
type MatchField ¶
MatchField is one searchable part of a row. MinTerm is the shortest filter term that may search it; 0 lets every term through.
type Options ¶
type Options struct {
// Title is the question, drawn above the rows. It may hold one %s where
// Subject goes.
Title string
// Subject is the thing the question is about, drawn in the theme's
// subject style inside the title.
Subject string
// Note is an optional line under the title for what has to be said about
// the offer as a whole.
Note string
// Items are the rows.
Items []Item
// Views are the ways of looking at the list that Tab switches between.
// Views that narrow Items need at least two to be shown at all.
Views []View
// View is the index of the view the list opens on.
View int
// Initial is the index of the item the cursor starts on. An item the
// filter or view hides, or an index out of range, starts at the first row.
// Where the list opens on a view that loads its rows, the cursor goes
// there once they have arrived.
Initial int
// MaxVisible caps how many rows are drawn at once; 0 derives the cap from
// the terminal height.
MaxVisible int
// Hint replaces the key line under the list.
Hint string
// Nested says the list was opened from another prompt, so the key line
// names Esc "back" instead of "cancel". Esc returns ErrBack either way.
Nested bool
// DismissOnBackspace lets Backspace on an empty filter leave the list the
// way Esc does. It suits a nested prompt, where Backspace reads as back.
DismissOnBackspace bool
// Typed lets the list take an answer it does not hold: the filter is
// offered as a row of its own, after the rows it matches, as long as
// Typed accepts it and none of the rows shown has exactly that text as
// its first cell. A row of that name which the view hides is not on
// offer, so the text can be typed all the same. Only SelectOrType and
// SelectManyOrType honor it.
Typed func(string) bool
// TypedNote is what the typed row says about itself, in its second
// column.
TypedNote string
// Lens, when set, is the filter and view the list opens on, and is left
// holding the ones the list was closed on, so the next list about the
// same rows opens narrowed the same way.
Lens *Lens
// CellStyles colors the columns, indexed like Item.Cells. Columns without
// a style are drawn the default way: the first bold, the rest receding.
CellStyles []theme.Style
// PaintCell paints inside a cell, for columns whose parts deserve colors
// of their own. It gets the column, the cell text already cut to its
// column, and whether the row is under the cursor, and returns the
// painted text and true, or false to leave the cell to the default
// styles. It is called only when color is on and the row is not receded.
// What it returns is drawn as it is, so it adds colors to the text it
// was given and nothing else.
PaintCell func(column int, text string, selected bool) (string, bool)
// DetailStyle is the style of Item.Detail in the title line; the theme's
// info style when empty.
DetailStyle theme.Style
// ColumnPriority ranks the columns, indexed like Item.Cells, by how long
// they keep their width when a row does not fit: the lowest-ranked column
// is shortened first. Unranked columns are PriorityNormal.
ColumnPriority []int
// ColumnTruncation says how each column's cells are shortened, indexed
// like Item.Cells. Unlisted columns are TruncateEnd.
ColumnTruncation []Truncation
// Range turns the list into a range selection: the first Enter anchors
// the range on the row under the cursor, moving extends it, and the
// second Enter ends the list. Esc drops an open range before it leaves
// the list. Read the range back with SelectRange.
//
// A range covers every item between its two ends, so every one of them
// is on screen while it is open: anchoring clears the filter and sets a
// view that narrows the items aside, and Esc brings the view back.
Range bool
// RangeUnit names what the range covers, in the singular ("commit"), for
// the summary in the title line; an "s" is appended for any count but
// one.
RangeUnit string
// contains filtered or unexported fields
}
Options configures one list. Its texts (Title, Subject, Note, Hint, TypedNote and RangeUnit, and the names and lines of its Views) are shown as plain single-line text, as the rows are (see Item).
type Toggle ¶
Toggle is one switch: what it is called, the two words its positions stand for, and where it starts. On and Off are the caller's vocabulary, such as "enabled" and "disabled"; they default to "yes" and "no". Label, On and Off are shown as plain single-line text, as everything a prompt draws is.
type ToggleOptions ¶
type ToggleOptions struct {
// Title is the question, with one %s where Subject goes.
Title string
// Subject is the thing the switches belong to.
Subject string
// Note is an optional line under the title.
Note string
Toggles []Toggle
// Initial is the switch the cursor starts on.
Initial int
// Nested names Esc "back" instead of "cancel" in the key line.
Nested bool
}
ToggleOptions configures one set of switches. Title, Subject and Note are shown as plain single-line text: escape sequences and control characters in them are dropped, and a line break or a tab becomes a space.
type Truncation ¶
type Truncation int
Truncation is how a cell too wide for its column is shortened.
const ( // TruncateEnd cuts the end of the cell. It is the default. TruncateEnd Truncation = iota // TruncateMiddle cuts the middle of the cell, keeping both ends: right for // values told apart by their ends, such as long identifiers or paths. TruncateMiddle )
type View ¶
type View struct {
// Name labels the view in the status line. An empty name is shown as the
// view's position.
Name string
// Groups narrows Options.Items to the rows of these groups.
Groups []string
// Title replaces Options.Title while the view is shown.
Title string
// Items are the view's own rows.
Items []Item
// Load supplies the view's rows the first time it is shown, for rows that
// are expensive to produce. The list shows Loading in their place, runs
// Load once, and keeps what it returns. A failure is shown where the rows
// would be, together with whatever rows Load did return.
//
// Load runs on a goroutine of its own while the list keeps taking keys:
// Tab moves on to another view, and Esc or Ctrl-C leaves the list. Load
// is not stopped by that. It runs to its end in the background, possibly
// after the function that drew the list has returned, and what it returns
// is then dropped; a Load that should stop early watches something the
// caller cancels. It must not write to the terminal. A panic in Load is
// raised again on the goroutine that drew the list. Where waiting for a
// key cannot be interrupted, Load runs before the next key is read and
// the list cannot be left until it returns: on Windows, and on macOS in
// a process with so many files open that the terminal, or the pipe the
// wait is interrupted through, has a descriptor past what select(2)
// takes (FD_SETSIZE, 1024).
Load func() ([]Item, error)
// Loading is the line shown while Load runs; "" uses a default.
Loading string
// Empty is the line shown in place of rows when the view has none; ""
// uses a default.
Empty string
}
View is one way of looking at a list. Tab and the right arrow switch to the next view, Shift-Tab and the left arrow to the previous one, and the filter carries over.
A view either narrows Options.Items to the rows with a group among Groups (every row when Groups is empty), or holds rows of its own in Items or Load. When any view holds its own rows, all of them do: a view with neither Items nor Load is then empty, and row indexes refer to the items of the view the list ended on.