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"
|
|
|
|
"path/filepath"
|
|
|
|
|
2024-03-28 21:19:06 +01:00
|
|
|
"github.com/charmbracelet/huh"
|
|
|
|
"github.com/charmbracelet/lipgloss"
|
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
|
|
|
}
|
|
|
|
|
2024-03-28 21:19:06 +01:00
|
|
|
theme := huh.ThemeCharm()
|
|
|
|
theme.Focused.Base = lipgloss.NewStyle()
|
|
|
|
theme.Focused.File = o.FileStyle.ToLipgloss()
|
|
|
|
theme.Focused.Directory = o.DirectoryStyle.ToLipgloss()
|
|
|
|
theme.Focused.SelectedOption = o.SelectedStyle.ToLipgloss()
|
|
|
|
|
|
|
|
// XXX: These should be file selected specific.
|
|
|
|
theme.Focused.TextInput.Placeholder = o.PermissionsStyle.ToLipgloss()
|
|
|
|
theme.Focused.TextInput.Prompt = o.CursorStyle.ToLipgloss()
|
|
|
|
|
|
|
|
err = huh.NewForm(
|
|
|
|
huh.NewGroup(
|
|
|
|
huh.NewFilePicker().
|
|
|
|
Picking(true).
|
|
|
|
CurrentDirectory(path).
|
|
|
|
DirAllowed(o.Directory).
|
|
|
|
FileAllowed(o.File).
|
|
|
|
Height(o.Height).
|
|
|
|
ShowHidden(o.All).
|
|
|
|
Value(&path),
|
|
|
|
),
|
|
|
|
).
|
|
|
|
WithShowHelp(false).
|
|
|
|
WithTheme(theme).
|
|
|
|
Run()
|
2023-03-06 17:54:02 +01:00
|
|
|
|
2022-10-01 00:40:10 +02:00
|
|
|
if err != nil {
|
2024-03-28 21:19:06 +01:00
|
|
|
return err
|
2022-10-01 00:40:10 +02:00
|
|
|
}
|
|
|
|
|
2024-03-28 21:19:06 +01:00
|
|
|
fmt.Println(path)
|
2022-10-01 00:40:10 +02:00
|
|
|
return nil
|
|
|
|
}
|