Documentation
¶
Overview ¶
Package filepicker is a real filesystem browse-and-select widget — InkUI has no direct equivalent, but it follows the single-choice-list shape of picker.Model, browsing one directory at a time.
Example ¶
A filepicker lists a directory's entries.
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/ows4444/tui/filepicker"
)
func main() {
dir, err := os.MkdirTemp("", "filepicker")
if err != nil {
fmt.Println(err)
return
}
defer os.RemoveAll(dir)
for _, name := range []string{"a.txt", "b.txt"} {
if err := os.WriteFile(filepath.Join(dir, name), nil, 0o600); err != nil {
fmt.Println(err)
return
}
}
m := filepicker.New(dir)
for _, e := range m.Entries() {
fmt.Println(e.Name)
}
}
Output: a.txt b.txt
Index ¶
- type KeyMap
- type Model
- func (m Model) Bindings() []keymap.Binding
- func (m Model) Cursor() int
- func (m Model) Entries() []fsutil.Entry
- func (m Model) Err() error
- func (m Model) LayoutNode() layout.Node
- func (m Model) Linearize() string
- func (m Model) Reload() Model
- 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) WithTokens(tok theme.Tokens) Model
- type SelectedMsg
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type KeyMap ¶
type KeyMap struct {
Up keymap.Binding
Down keymap.Binding
// Select descends into the directory under the cursor, or confirms a
// file (or, in DirsOnly mode, a directory).
Select keymap.Binding
// Open descends into the directory under the cursor.
Open keymap.Binding
// Back goes to the parent directory.
Back keymap.Binding
}
KeyMap is the set of keys a file picker reacts to.
func DefaultKeyMap ¶
func DefaultKeyMap() KeyMap
DefaultKeyMap returns the default keys: up/down, enter to select, right to open, left or backspace to go back.
type Model ¶
type Model struct {
Dir string
Extensions []string // e.g. []string{".go", ".md"}; empty means no filter
DirsOnly bool
Theme theme.Theme
// Height is the number of entries View shows at once; zero shows all of
// them. With Height set, View renders (and sanitises) only that window,
// which keeps the cursor entry in view, so a directory of 10,000 entries
// costs O(Height) per frame, not O(entries).
Height int
// Raw, when true, draws file and directory names unchanged. By default
// each name is sanitised (ansi.Sanitize): a file name may hold escape
// sequences, and drawing them would let a file in the listing act on the
// terminal.
Raw bool
// KeyMap holds the keys Update reacts to. New sets DefaultKeyMap; a
// Model built as a struct literal with a zero KeyMap uses DefaultKeyMap.
KeyMap KeyMap
// Mouse turns on mouse handling in Update (off by default). Bounds is the
// screen rectangle where the app draws the list, set by the app; events
// outside it are ignored. WheelStep is the entries one wheel notch
// scrolls (0 means 3); the wheel needs Height set, since otherwise every
// entry is shown. A left press on an entry moves the cursor to it.
Mouse bool
Bounds hittest.Rect
WheelStep int
// contains filtered or unexported fields
}
Model is a keyboard-navigable file browser rooted at Dir. Enter on a directory (or Right) descends into it; Left/Backspace goes back to the parent. Enter on a file confirms it via SelectedMsg. Extensions, when non-empty, restricts which files are listed (directories are always listed, for navigation); DirsOnly excludes files entirely and makes Enter on a directory confirm it instead of descending into it. New lists Dir before either can be set, so call Reload after changing them:
m := filepicker.New(dir)
m.Extensions = []string{".go"}
m = m.Reload()
func (Model) Err ¶
Err returns the error of the last listing of Dir (it does not exist, or cannot be read), or nil. Entries is empty while it is non-nil, and View shows a line saying the directory cannot be read.
func (Model) LayoutNode ¶
LayoutNode adapts the listing to a layout.Node. Measure reports its natural size (widest "> name/" by one row per entry). Render fits the allotted Size: names are cut to the width, and when there are more entries than rows it shows a window containing the cursor entry. The Model is not changed.
func (Model) Linearize ¶
Linearize renders the listing as plain text for accessible output (see tui.Linearizer): a header with the directory and entry count, then one line per entry with its kind ("folder" or "file"), its position and ", selected" on the cursor row. No prefixes or trailing slashes. A directory that could not be listed says so in the header.
func (Model) Reload ¶
Reload lists Dir again and returns the Model with the new entries. Call it after setting Extensions, DirsOnly or Dir, on a Model built as a struct literal, or to pick up a change on disk. The cursor keeps its index, clamped to the new listing.
Example ¶
Extensions and DirsOnly take effect at the next listing, so set them and call Reload.
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/ows4444/tui/filepicker"
)
func main() {
dir, err := os.MkdirTemp("", "filepicker")
if err != nil {
fmt.Println(err)
return
}
defer os.RemoveAll(dir)
for _, name := range []string{"main.go", "notes.txt"} {
if err := os.WriteFile(filepath.Join(dir, name), nil, 0o600); err != nil {
fmt.Println(err)
return
}
}
m := filepicker.New(dir)
m.Extensions = []string{".go"}
m = m.Reload()
for _, e := range m.Entries() {
fmt.Println(e.Name)
}
}
Output: main.go
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.ComponentFilePicker, ...) and then by WithTokens.
func (Model) Update ¶
Update, with the default KeyMap, moves the cursor on Up/Down, descends into a directory on Enter or Right, goes to the parent directory on Left or Backspace, and confirms a file (or, in DirsOnly mode, a directory) on Enter, returning a Cmd that delivers SelectedMsg. With Mouse on, it also handles mouse events inside Bounds. Any other Msg is a no-op.
type SelectedMsg ¶
type SelectedMsg struct {
Path string
}
SelectedMsg is delivered (via the Cmd Update returns) when a file is confirmed with Enter, or a directory is confirmed with Enter in DirsOnly mode.