command.go
This file
Path
file/command.go
79 lines · 1.8 KiB · Go
Last changed by
chore: prepare v2 (#1116)Drew Smirnoffbefore adoption02d6f06
History of this fileTake it
1package file23import (4 "errors"5 "fmt"6 "os"7 "path/filepath"89 "charm.land/bubbles/v2/filepicker"10 "charm.land/bubbles/v2/help"11 tea "charm.land/bubbletea/v2"12 "charm.land/gum/v2/internal/timeout"13 "charm.land/gum/v2/style"14)1516// Run is the interface to picking a file.17func (o Options) Run() error {18 if !o.File && !o.Directory {19 return errors.New("at least one between --file and --directory must be set")20 }2122 if o.Path == "" {23 o.Path = "."24 }2526 path, err := filepath.Abs(o.Path)27 if err != nil {28 return fmt.Errorf("file not found: %w", err)29 }3031 fp := filepicker.New()32 fp.CurrentDirectory = path33 fp.Path = path34 fp.SetHeight(o.Height)35 fp.AutoHeight = o.Height == 036 fp.Cursor = o.Cursor37 fp.DirAllowed = o.Directory38 fp.FileAllowed = o.File39 fp.ShowPermissions = o.Permissions40 fp.ShowSize = o.Size41 fp.ShowHidden = o.All42 fp.Styles = filepicker.DefaultStyles()43 fp.Styles.Cursor = o.CursorStyle.ToLipgloss()44 fp.Styles.Symlink = o.SymlinkStyle.ToLipgloss()45 fp.Styles.Directory = o.DirectoryStyle.ToLipgloss()46 fp.Styles.File = o.FileStyle.ToLipgloss()47 fp.Styles.Permission = o.PermissionsStyle.ToLipgloss()48 fp.Styles.Selected = o.SelectedStyle.ToLipgloss()49 fp.Styles.FileSize = o.FileSizeStyle.ToLipgloss()50 top, right, bottom, left := style.ParsePadding(o.Padding)51 m := model{52 filepicker: fp,53 padding: []int{top, right, bottom, left},54 showHelp: o.ShowHelp,55 help: help.New(),56 keymap: defaultKeymap(),57 headerStyle: o.HeaderStyle.ToLipgloss(),58 header: o.Header,59 }6061 ctx, cancel := timeout.Context(o.Timeout)62 defer cancel()6364 tm, err := tea.NewProgram(65 m,66 tea.WithOutput(os.Stderr),67 tea.WithContext(ctx),68 ).Run()69 if err != nil {70 return fmt.Errorf("unable to pick selection: %w", err)71 }72 m = tm.(model)73 if m.selectedPath == "" {74 return errors.New("no file selected")75 }7677 fmt.Println(m.selectedPath)78 return nil79}