diff --git a/go.mod b/go.mod index b337391..1f8f4fb 100755 --- a/go.mod +++ b/go.mod @@ -7,4 +7,5 @@ require ( github.com/gizak/termui/v3 v3.1.0 github.com/mitchellh/go-wordwrap v1.0.1 // indirect github.com/nsf/termbox-go v0.0.0-20201124104050-ed494de23a00 // indirect + github.com/spf13/pflag v1.0.5 // indirect ) diff --git a/go.sum b/go.sum index fb4e612..42f7b64 100755 --- a/go.sum +++ b/go.sum @@ -37,6 +37,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= diff --git a/main.go b/main.go index 9be9bc5..b2ae77f 100644 --- a/main.go +++ b/main.go @@ -2,10 +2,12 @@ package main import ( "bufio" - "flag" "fmt" "log" "os" + + "github.com/alecthomas/chroma/styles" + "github.com/spf13/pflag" ) // Check is a helper function to error check functions which can be used to error check deferred functions @@ -34,16 +36,26 @@ func getFileContent(filePath string) []string { func main() { var filePath string - flag.StringVar(&filePath, "f", "", "Parse given file as Makefile") - helpFlag := flag.Bool("h", false, "Print this message and exit") - allFlag := flag.Bool("a", false, "Display all targets including special built-in targets") - flag.Parse() + var theme string + + pflag.StringVarP(&filePath, "file-name", "f", "", "Parse given file as Makefile") + help := pflag.BoolP("help", "h", false, "Print this message and exit") + all := pflag.BoolP("all", "a", false, "Display all targets including special built-in targets") + list := pflag.Bool("list-themes", false, "List built-in syntax highlighting themes") + pflag.StringVar(&theme, "theme", "vim", "Use a built-in syntax highlighting theme") + pflag.Parse() - if *helpFlag { + if *help { // Print help message fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0]) - flag.PrintDefaults() - os.Exit(1) + pflag.PrintDefaults() + os.Exit(0) + } else if *list { + fmt.Println("Built-in themes:") + for theme := range styles.Registry { + fmt.Println("\t" + theme) + } + os.Exit(0) } else if filePath == "" { // Attempt to find makefile in current directory defaultMakefileNames := []string{"GNUmakefile", "makefile", "Makefile"} @@ -57,16 +69,22 @@ func main() { } } if !foundFile { - log.Fatal("no Makefile found") - os.Exit(1) + log.Fatalln("No Makefile found.") + } + } + + if theme != "" { + // Ensure given theme exists in registry. + if _, ok := styles.Registry[theme]; !ok { + log.Fatalln("\"" + theme + "\" is not a built-in theme.") } } content := NewParsedContent(filePath, getFileContent(filePath)) - if *allFlag { + if *all { content.SetIncludeSpecialTargets(true) } content.Parse() - Render(content) + Render(content, theme) } diff --git a/render.go b/render.go index 6ee4451..d479cc0 100644 --- a/render.go +++ b/render.go @@ -11,7 +11,7 @@ import ( ) // Render sets up and renders widgets to build the user interface -func Render(content *ParsedContent) { +func Render(content *ParsedContent, theme string) { if err := ui.Init(); err != nil { log.Fatalf("failed to initialize termui: %v", err) } @@ -22,7 +22,7 @@ func Render(content *ParsedContent) { target := NewTarget(0, len(content.Rules), content.Rules) target.Rows = getTargets(content.Rules) - highlighter := NewHighlighter("vim") + highlighter := NewHighlighter(theme) dependencyWidget := widgets.NewParagraph() dependencyWidget.Title = "Dependencies"