mirror of
https://github.com/charmbracelet/gum
synced 2024-06-16 12:35:06 +02:00
75 lines
1.6 KiB
Go
75 lines
1.6 KiB
Go
package file
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/charmbracelet/gum/internal/exit"
|
|
|
|
"github.com/charmbracelet/bubbles/filepicker"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
)
|
|
|
|
// 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.ShowHidden = o.All
|
|
fp.ShowPermissions = o.Permissions
|
|
fp.ShowSize = o.Size
|
|
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,
|
|
}
|
|
|
|
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.selectedPath == "" {
|
|
os.Exit(1)
|
|
}
|
|
|
|
fmt.Println(m.selectedPath)
|
|
|
|
return nil
|
|
}
|