feat(file): allow distinctly file/dirs selections

This commit is contained in:
fedeztk 2022-10-18 11:43:47 +02:00 committed by Maas Lalani
parent f0a8011b95
commit 5ed1f2b1b8
3 changed files with 21 additions and 7 deletions

View file

@ -1,6 +1,7 @@
package file
import (
"errors"
"fmt"
"os"
"path/filepath"
@ -13,6 +14,10 @@ import (
// 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 = "."
}
@ -27,6 +32,8 @@ func (o Options) Run() error {
cursor: o.Cursor,
selected: 0,
showHidden: o.All,
dirAllowed: o.Directory,
fileAllowed: o.File,
autoHeight: o.Height == 0,
height: o.Height,
max: 0,

View file

@ -29,10 +29,12 @@ import (
const marginBottom = 5
type model struct {
quitting bool
path string
files []os.DirEntry
showHidden bool
quitting bool
path string
files []os.DirEntry
showHidden bool
dirAllowed bool
fileAllowed bool
selected int
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" {
m.path = filepath.Join(m.path, f.Name())
m.quitting = true
return m, tea.Quit
}
}
if !isDir {
break
}

View file

@ -7,8 +7,10 @@ type Options struct {
// 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"`
// Cursor is the character to display in front of the current selected items.
Cursor string `short:"c" help:"The cursor character" default:">"`
All bool `short:"a" help:"Show hidden and 'dot' files" default:"true"`
Cursor string `short:"c" help:"The cursor character" default:">"`
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"`
CursorStyle style.Styles `embed:"" prefix:"cursor." help:"The cursor style" set:"defaultForeground=212" envprefix:"GUM_FILE_CURSOR_"`