filepicker

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: 10 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 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

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 New

func New(dir string) Model

New builds a Model rooted at dir, listing its entries immediately.

func (Model) Bindings

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

Bindings returns the active key bindings, with descriptions, for help text.

func (Model) Cursor

func (m Model) Cursor() int

Cursor returns the index of the entry currently under the cursor.

func (Model) Entries

func (m Model) Entries() []fsutil.Entry

Entries returns the currently listed entries of Dir.

func (Model) Err

func (m Model) Err() error

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

func (m Model) LayoutNode() layout.Node

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

func (m Model) Linearize() string

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

func (m Model) Reload() Model

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

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.ComponentFilePicker, ...) and then by WithTokens.

func (Model) Update

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

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.

func (Model) View

func (m Model) View() string

View renders the current directory's entries, directories marked with a trailing "/", with the entry under the cursor highlighted. An empty directory renders "". One that could not be listed (see Err) renders a single line that says so.

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 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.

Jump to

Keyboard shortcuts

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