From 5ed1f2b1b82f3977920b2b3cfbaf54f29664e9a1 Mon Sep 17 00:00:00 2001 From: fedeztk Date: Tue, 18 Oct 2022 11:43:47 +0200 Subject: [PATCH] feat(file): allow distinctly file/dirs selections --- file/command.go | 7 +++++++ file/file.go | 15 ++++++++++----- file/options.go | 6 ++++-- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/file/command.go b/file/command.go index d9235b7..f6ece8f 100644 --- a/file/command.go +++ b/file/command.go @@ -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, diff --git a/file/file.go b/file/file.go index eab34f0..fa65260 100644 --- a/file/file.go +++ b/file/file.go @@ -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 } diff --git a/file/options.go b/file/options.go index 91228f5..5b61f2b 100644 --- a/file/options.go +++ b/file/options.go @@ -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_"`