mirror of
https://github.com/charmbracelet/gum
synced 2024-05-05 07:53:14 +02:00
feat(file): allow distinctly file/dirs selections
This commit is contained in:
parent
f0a8011b95
commit
5ed1f2b1b8
|
@ -1,6 +1,7 @@
|
||||||
package file
|
package file
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -13,6 +14,10 @@ import (
|
||||||
|
|
||||||
// Run is the interface to picking a file.
|
// Run is the interface to picking a file.
|
||||||
func (o Options) Run() error {
|
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 == "" {
|
if o.Path == "" {
|
||||||
o.Path = "."
|
o.Path = "."
|
||||||
}
|
}
|
||||||
|
@ -27,6 +32,8 @@ func (o Options) Run() error {
|
||||||
cursor: o.Cursor,
|
cursor: o.Cursor,
|
||||||
selected: 0,
|
selected: 0,
|
||||||
showHidden: o.All,
|
showHidden: o.All,
|
||||||
|
dirAllowed: o.Directory,
|
||||||
|
fileAllowed: o.File,
|
||||||
autoHeight: o.Height == 0,
|
autoHeight: o.Height == 0,
|
||||||
height: o.Height,
|
height: o.Height,
|
||||||
max: 0,
|
max: 0,
|
||||||
|
|
15
file/file.go
15
file/file.go
|
@ -29,10 +29,12 @@ import (
|
||||||
const marginBottom = 5
|
const marginBottom = 5
|
||||||
|
|
||||||
type model struct {
|
type model struct {
|
||||||
quitting bool
|
quitting bool
|
||||||
path string
|
path string
|
||||||
files []os.DirEntry
|
files []os.DirEntry
|
||||||
showHidden bool
|
showHidden bool
|
||||||
|
dirAllowed bool
|
||||||
|
fileAllowed bool
|
||||||
|
|
||||||
selected int
|
selected int
|
||||||
selectedStack stack.Stack
|
selectedStack stack.Stack
|
||||||
|
@ -165,12 +167,15 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !isDir {
|
if (!isDir && m.fileAllowed) || (isDir && m.dirAllowed) {
|
||||||
if msg.String() == "enter" {
|
if msg.String() == "enter" {
|
||||||
m.path = filepath.Join(m.path, f.Name())
|
m.path = filepath.Join(m.path, f.Name())
|
||||||
m.quitting = true
|
m.quitting = true
|
||||||
return m, tea.Quit
|
return m, tea.Quit
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !isDir {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,8 +7,10 @@ type Options struct {
|
||||||
// Path is the path to the folder / directory to begin traversing.
|
// Path is the path to the folder / directory to begin traversing.
|
||||||
Path string `arg:"" optional:"" name:"path" help:"The path to the folder to begin traversing"`
|
Path string `arg:"" optional:"" name:"path" help:"The path to the folder to begin traversing"`
|
||||||
// Cursor is the character to display in front of the current selected items.
|
// Cursor is the character to display in front of the current selected items.
|
||||||
Cursor string `short:"c" help:"The cursor character" default:">"`
|
Cursor string `short:"c" help:"The cursor character" default:">"`
|
||||||
All bool `short:"a" help:"Show hidden and 'dot' files" default:"true"`
|
All bool `short:"a" help:"Show hidden and 'dot' files" default:"true"`
|
||||||
|
File bool `help:"Allow files selection" default:"true"`
|
||||||
|
Directory bool `help:"Allow directories selection" default:"false"`
|
||||||
|
|
||||||
Height int `help:"Maximum number of files to display" default:"0"`
|
Height int `help:"Maximum number of files to display" default:"0"`
|
||||||
CursorStyle style.Styles `embed:"" prefix:"cursor." help:"The cursor style" set:"defaultForeground=212" envprefix:"GUM_FILE_CURSOR_"`
|
CursorStyle style.Styles `embed:"" prefix:"cursor." help:"The cursor style" set:"defaultForeground=212" envprefix:"GUM_FILE_CURSOR_"`
|
||||||
|
|
Loading…
Reference in a new issue