If the only arguement is a directory open a TUI at that path (see #234)

This commit is contained in:
Christian Rocha 2021-01-15 15:58:58 -05:00
parent 3d15953707
commit 8d0ebb7249
4 changed files with 53 additions and 15 deletions

41
main.go
View file

@ -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

View file

@ -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]

View file

@ -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

View file

@ -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)