Skip to content

Commit

Permalink
Move format switching to format package.
Browse files Browse the repository at this point in the history
  • Loading branch information
drn committed Nov 29, 2018
1 parent 3f470d3 commit cc82460
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
20 changes: 20 additions & 0 deletions format/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,31 @@ import (
"regexp"
"github.com/fatih/color"
"github.com/drn/nerd-ls/node"
"github.com/drn/nerd-ls/list"
"github.com/drn/nerd-ls/options"
)

var errorRegex = regexp.MustCompile(`.*\.orig$`)
var ignoreRegex = regexp.MustCompile(`^.DS_Store$`)

// Display - Runs display logic based on input address and options
func Display(address string, opts options.Options) {
nodes := list.Fetch(
address,
map[string]bool{
"all": opts.All,
"long": opts.Long,
},
)

formatOptions := map[string]bool{"icon": opts.Icon}
if opts.Long {
Long(nodes, formatOptions)
} else {
Compact(nodes, formatOptions)
}
}

func nodeColor(node node.Node) *color.Color {
if node.Symlink != "" { return color.New(color.FgMagenta) }
if node.IsDir { return color.New(color.FgCyan, color.Bold) }
Expand Down
23 changes: 3 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@ import (
"fmt"
"github.com/fatih/color"
"github.com/jessevdk/go-flags"
"github.com/drn/nerd-ls/list"
"github.com/drn/nerd-ls/options"
"github.com/drn/nerd-ls/format"
)

var opts struct {
All bool `short:"a" long:"all" description:"Include directory entries whose names begin with a dot (.)"`
Long bool `short:"l" long:"long" description:"List in long format"`
Icon bool `short:"i" long:"icon" description:"Display nerd-font icons"`
}
var opts options.Options

func main() {
args, err := flags.ParseArgs(&opts, os.Args)
Expand All @@ -40,19 +36,6 @@ func main() {
)
}

nodes := list.Fetch(
dir,
map[string]bool{
"all": opts.All,
"long": opts.Long,
},
)

formatOptions := map[string]bool{"icon": opts.Icon}
if opts.Long {
format.Long(nodes, formatOptions)
} else {
format.Compact(nodes, formatOptions)
}
format.Display(dir, opts)
}
}
8 changes: 8 additions & 0 deletions options/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package options

// Options - Parsed input flags schema
type Options struct {
All bool `short:"a" long:"all" description:"Include directory entries whose names begin with a dot (.)"`
Long bool `short:"l" long:"long" description:"List in long format"`
Icon bool `short:"i" long:"icon" description:"Display nerd-font icons"`
}

0 comments on commit cc82460

Please sign in to comment.