Skip to content

Commit

Permalink
If the only arguement is a directory open a TUI at that path (see #234)
Browse files Browse the repository at this point in the history
  • Loading branch information
meowgorithm committed Jan 15, 2021
1 parent aa7d927 commit 312c808
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 15 deletions.
41 changes: 28 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion stash_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
3 changes: 3 additions & 0 deletions ui/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 21 additions & 1 deletion ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -493,14 +494,33 @@ 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)
}
return errMsg{err}
}

if debug {
log.Println("local directory is:", cwd)
}

var ignore []string
if !m.common.cfg.ShowAllFiles {
ignore = ignorePatterns(m)
Expand Down

0 comments on commit 312c808

Please sign in to comment.