Skip to content
File Picker

File Picker

Opens an interactive file picker and returns selected path and is implemented on top of Dropdown.

Browsing directories in the file picker

WithStartDir

Sets initial directory. Default is current working directory.

path, err := tui.FilePicker(ctx, "Pick file",
	tui.WithStartDir("/var/log"),
)

WithExtensions

Filters non-directory entries by extension (case-insensitive).

path, err := tui.FilePicker(ctx, "Pick file",
	tui.WithExtensions(".json", ".yaml"),
)

WithIgnoreUp

Hides .. navigation entry.

path, err := tui.FilePicker(ctx, "Pick file",
	tui.WithIgnoreUp(),
)

WithIgnoreDirs

Hides directory entries.

path, err := tui.FilePicker(ctx, "Pick file",
	tui.WithIgnoreDirs(),
)

WithShowHidden

Shows dot-files and dot-directories.

path, err := tui.FilePicker(ctx, "Pick file",
	tui.WithShowHidden(),
)

WithTemplate

Forwarded to dropdown template configuration. See WithTemplate.

path, err := tui.FilePicker(ctx, "Pick file",
	tui.WithTemplate(".Name"),
)

Presentation

FilePicker draws with the FilePicker slots of the theme, which inherit from the dropdown and choice slots. See Presentation.

th := tui.DefaultTheme()
th.FilePicker.Label = `{{ accent "file:" }} {{ .Label }}`
th.FilePicker.Answer = `{{ success "picked:" }} {{ .Value }}`
if err := tui.SetDefaultTheme(th); err != nil {
	return err
}
path, err := tui.FilePicker(ctx, "Pick file")

WithHideAnswer

Forwarded to dropdown answer visibility. See WithHideAnswer.

path, err := tui.FilePicker(ctx, "Pick file",
	tui.WithHideAnswer(),
)

WithOneReturn

Forwarded to dropdown one-item auto-select behavior. See WithOneReturn.

path, err := tui.FilePicker(ctx, "Pick file",
	tui.WithOneReturn(),
)

WithDefault

Forwarded to the dropdown as a match hint (not a pre-filled filter). See WithDefault.

path, err := tui.FilePicker(ctx, "Pick file",
	tui.WithDefault("conf"),
)

Without a terminal the picker answers at every directory level from the default, after the extension, hidden and directory filters. A string is an entry name first and a prefix hint second; an os.DirEntry is found by name and kind. Other types, and missing or ambiguous entries, fail with ErrInvalidOption; a hint is never taken as a path. A directory continues the walk and only a file is returned. A directory seen twice is an error, not a loop. Without a default or WithOneReturn the call fails with ErrNoTTY before listing anything; filesystem errors are returned.

Last updated on