diff --git a/main.go b/main.go index aaf5df2..2b8fc3f 100644 --- a/main.go +++ b/main.go @@ -39,7 +39,7 @@ var ( mouse bool rootCmd = &cobra.Command{ - Use: "glow SOURCE", + Use: "glow [SOURCE|DIR]", Short: "Render markdown on the CLI, with pizzazz!", Long: formatBlock(fmt.Sprintf("\nRender markdown on the CLI, %s!", common.Keyword("with pizzazz"))), SilenceErrors: false, @@ -182,24 +182,38 @@ func execute(cmd *cobra.Command, args []string) error { return err } - if len(args) == 0 { - return executeArg(cmd, "", os.Stdout) - } + switch len(args) { - for _, arg := range args { - if err := executeArg(cmd, arg, os.Stdout); err != nil { - return err + // TUI running on cwd + case 0: + return runTUI("", false) + + // TUI with possible dir argument + case 1: + // Validate that the argument is a directory. If it's not treat it as + // an argument to the non-TUI version of Glow (via fallthrough). + info, err := os.Stat(args[0]) + if err == nil && info.IsDir() { + p, err := filepath.Abs(args[0]) + if err == nil { + return runTUI(p, false) + } + } + fallthrough + + // CLI + default: + for _, arg := range args { + if err := executeArg(cmd, arg, os.Stdout); err != nil { + return err + } } } + return nil } func executeArg(cmd *cobra.Command, arg string, w io.Writer) error { - // Only run TUI if there are no arguments (excluding flags) - if arg == "" { - return runTUI(false) - } - // create an io.Reader from the markdown source in cli-args src, err := sourceFromArg(arg) if err != nil { @@ -273,7 +287,7 @@ func executeArg(cmd *cobra.Command, arg string, w io.Writer) error { return nil } -func runTUI(stashedOnly bool) error { +func runTUI(workingDirectory string, stashedOnly bool) error { // Read environment to get debugging stuff var cfg ui.Config if err := babyenv.Parse(&cfg); err != nil { @@ -289,6 +303,7 @@ func runTUI(stashedOnly bool) error { defer f.Close() } + cfg.WorkingDirectory = workingDirectory cfg.DocumentTypes = ui.NewDocTypeSet() cfg.ShowAllFiles = showAllFiles cfg.GlamourMaxWidth = width diff --git a/stash_cmd.go b/stash_cmd.go index e1d69a7..e6b3ea4 100644 --- a/stash_cmd.go +++ b/stash_cmd.go @@ -27,7 +27,7 @@ var ( RunE: func(cmd *cobra.Command, args []string) error { initConfig() if len(args) == 0 { - return runTUI(true) + return runTUI("", true) } filePath := args[0] diff --git a/ui/config.go b/ui/config.go index e96e34a..67d94d6 100644 --- a/ui/config.go +++ b/ui/config.go @@ -8,6 +8,9 @@ type Config struct { GlamourMaxWidth uint GlamourStyle string + // Which directory should we start from? + WorkingDirectory string + // Which document types shall we show? DocumentTypes DocTypeSet diff --git a/ui/ui.go b/ui/ui.go index e167a2c..2fb335d 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "log" "os" + "path/filepath" "strings" "time" @@ -493,7 +494,22 @@ func errorView(err error, fatal bool) string { func findLocalFiles(m model) tea.Cmd { return func() tea.Msg { - cwd, err := os.Getwd() + var ( + cwd = m.common.cfg.WorkingDirectory + err error + ) + + if cwd == "" { + cwd, err = os.Getwd() + } else { + var info os.FileInfo + info, err = os.Stat(cwd) + if err == nil && info.IsDir() { + cwd, err = filepath.Abs(cwd) + } + } + + // Note that this is one error check for both cases above if err != nil { if debug { log.Println("error finding local files:", err) @@ -501,6 +517,10 @@ func findLocalFiles(m model) tea.Cmd { return errMsg{err} } + if debug { + log.Println("local directory is:", cwd) + } + var ignore []string if !m.common.cfg.ShowAllFiles { ignore = ignorePatterns(m)