gum/file/command.go
Carlos Alexandro Becker e30fc5ecdf
refactor: removing huh as a dep (#742)
* Revert "feat: huh gum write (#525)"

This reverts commit 4d5d53169e.

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* Revert "Use Huh for Gum Confirm (#522)"

This reverts commit f7572e387e.

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* revert: Use Huh for Gum Choose (#521)

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* revert: feat: huh for gum input (#524)

* revert: feat: huh file picker (#523)

* feat: remove huh

* fix: timeouts

* fix: lint issues

* fix(choose): quit on ctrl+q

ported over 63a3e8c8ce

* fix: ctrl+a to reverse selection

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* fix: better handle spin exit codes

* fix(file): bind --[no-]permissions and --[no-]size

* feat(confirm): show help

* fix(confirm): fix help style

* fix(file): help

* fix(input): --no-show-help doesn't work

* fix(input): help

* fix(file): keymap improvement

* fix(write): focus

* feat(write): ctrl+e, keymaps, help

* feat(choose): help

* feat(filter): help

* refactor: keymaps

* fix(choose): only show 'toggle all' if there's no limit

* fix(choose): don't show toggle if the choices are limited to 1

* fix(filter): match choose header color

* fix(filter): add space above help

* fix(filter): factor help into the height setting

* chore(choose,filter): use verb for navigation label in help

* fix(filter): hide toggle help if limit is 1

* fix(file): factor help into height setting (#746)

* fix: lint issues

* fix(file): handle ctrl+c

* fix: remove full help

* fix: lint

---------

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
Co-authored-by: Christian Rocha <christian@rocha.is>
2024-12-09 13:18:35 -03:00

76 lines
1.8 KiB
Go

package file
import (
"errors"
"fmt"
"os"
"path/filepath"
"github.com/charmbracelet/bubbles/filepicker"
"github.com/charmbracelet/bubbles/help"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/gum/internal/exit"
)
// Run is the interface to picking a file.
func (o Options) Run() error {
if !o.File && !o.Directory {
return errors.New("at least one between --file and --directory must be set")
}
if o.Path == "" {
o.Path = "."
}
path, err := filepath.Abs(o.Path)
if err != nil {
return fmt.Errorf("file not found: %w", err)
}
fp := filepicker.New()
fp.CurrentDirectory = path
fp.Path = path
fp.Height = o.Height
fp.AutoHeight = o.Height == 0
fp.Cursor = o.Cursor
fp.DirAllowed = o.Directory
fp.FileAllowed = o.File
fp.ShowPermissions = o.Permissions
fp.ShowSize = o.Size
fp.ShowHidden = o.All
fp.Styles = filepicker.DefaultStyles()
fp.Styles.Cursor = o.CursorStyle.ToLipgloss()
fp.Styles.Symlink = o.SymlinkStyle.ToLipgloss()
fp.Styles.Directory = o.DirectoryStyle.ToLipgloss()
fp.Styles.File = o.FileStyle.ToLipgloss()
fp.Styles.Permission = o.PermissionsStyle.ToLipgloss()
fp.Styles.Selected = o.SelectedStyle.ToLipgloss()
fp.Styles.FileSize = o.FileSizeStyle.ToLipgloss()
m := model{
filepicker: fp,
timeout: o.Timeout,
hasTimeout: o.Timeout > 0,
aborted: false,
showHelp: o.ShowHelp,
help: help.New(),
keymap: defaultKeymap(),
}
tm, err := tea.NewProgram(&m, tea.WithOutput(os.Stderr)).Run()
if err != nil {
return fmt.Errorf("unable to pick selection: %w", err)
}
m = tm.(model)
if m.aborted {
return exit.ErrAborted
}
if m.timedOut {
return exit.ErrTimeout
}
if m.selectedPath == "" {
return errors.New("no file selected")
}
fmt.Println(m.selectedPath)
return nil
}