2022-10-01 00:40:10 +02:00
|
|
|
package file
|
|
|
|
|
|
|
|
import (
|
2022-10-18 11:43:47 +02:00
|
|
|
"errors"
|
2022-10-01 00:40:10 +02:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2022-10-13 17:02:08 +02:00
|
|
|
"github.com/alecthomas/kong"
|
2022-10-01 00:40:10 +02:00
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
"github.com/charmbracelet/gum/internal/stack"
|
2022-10-13 17:02:08 +02:00
|
|
|
"github.com/charmbracelet/gum/style"
|
2022-10-01 00:40:10 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Run is the interface to picking a file.
|
|
|
|
func (o Options) Run() error {
|
2022-10-18 11:43:47 +02:00
|
|
|
if !o.File && !o.Directory {
|
|
|
|
return errors.New("at least one between --file and --directory must be set")
|
|
|
|
}
|
|
|
|
|
2022-10-01 00:40:10 +02:00
|
|
|
if o.Path == "" {
|
|
|
|
o.Path = "."
|
|
|
|
}
|
|
|
|
|
|
|
|
path, err := filepath.Abs(o.Path)
|
|
|
|
if err != nil {
|
2022-10-02 21:55:14 +02:00
|
|
|
return fmt.Errorf("file not found: %w", err)
|
2022-10-01 00:40:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
m := model{
|
|
|
|
path: path,
|
|
|
|
cursor: o.Cursor,
|
|
|
|
selected: 0,
|
|
|
|
showHidden: o.All,
|
2022-10-18 11:43:47 +02:00
|
|
|
dirAllowed: o.Directory,
|
|
|
|
fileAllowed: o.File,
|
2022-10-01 00:40:10 +02:00
|
|
|
autoHeight: o.Height == 0,
|
|
|
|
height: o.Height,
|
|
|
|
max: 0,
|
|
|
|
min: 0,
|
|
|
|
selectedStack: stack.NewStack(),
|
|
|
|
minStack: stack.NewStack(),
|
|
|
|
maxStack: stack.NewStack(),
|
2022-10-12 22:58:37 +02:00
|
|
|
cursorStyle: o.CursorStyle.ToLipgloss().Inline(true),
|
|
|
|
symlinkStyle: o.SymlinkStyle.ToLipgloss().Inline(true),
|
|
|
|
directoryStyle: o.DirectoryStyle.ToLipgloss().Inline(true),
|
|
|
|
fileStyle: o.FileStyle.ToLipgloss().Inline(true),
|
|
|
|
permissionStyle: o.PermissionsStyle.ToLipgloss().Inline(true),
|
|
|
|
selectedStyle: o.SelectedStyle.ToLipgloss().Inline(true),
|
|
|
|
fileSizeStyle: o.FileSizeStyle.ToLipgloss().Inline(true),
|
2022-10-01 00:40:10 +02:00
|
|
|
}
|
|
|
|
|
2022-10-18 02:23:59 +02:00
|
|
|
tm, err := tea.NewProgram(&m, tea.WithOutput(os.Stderr)).Run()
|
2022-10-01 00:40:10 +02:00
|
|
|
if err != nil {
|
2022-10-02 21:55:14 +02:00
|
|
|
return fmt.Errorf("unable to pick selection: %w", err)
|
2022-10-01 00:40:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
m = tm.(model)
|
|
|
|
|
|
|
|
if m.path == "" {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(m.path)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-13 17:02:08 +02:00
|
|
|
|
|
|
|
// BeforeReset hook. Used to unclutter style flags.
|
|
|
|
func (o Options) BeforeReset(ctx *kong.Context) error {
|
|
|
|
style.HideFlags(ctx)
|
|
|
|
return nil
|
|
|
|
}
|