Skip to content

Commit

Permalink
More refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jessp01 committed Jul 30, 2023
1 parent 9be15ac commit e790cd0
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 149 deletions.
83 changes: 8 additions & 75 deletions cmd/super-zaje/super-zaje.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"path/filepath"
"regexp"
"strings"
"time"

"github.com/jessp01/zaje"
"github.com/otiai10/gosseract/v2"
Expand All @@ -21,80 +20,14 @@ import (
func main() {

app := cli.NewApp()
cli.AppHelpTemplate = `NAME:
{{.Name}} - {{.Usage}}
USAGE:
{{.HelpName}} {{if .VisibleFlags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[input-file || - ]{{end}}
{{if len .Authors}}
COMMANDS:
{{range .Commands}}{{if not .HideHelp}} {{join .Names ", "}}{{ "\t"}}{{.Usage}}{{ "\n" }}{{end}}{{end}}{{end}}{{if .VisibleFlags}}
GLOBAL OPTIONS:
{{range .VisibleFlags}}{{.}}{{ "\n" }}
{{end}}{{end}}{{if .Copyright }}
EXAMPLES:
To use zaje as a cat replacement:
$ zaje /path/to/file
To replace tail -f:
$ tail -f /path/to/file | zaje -l server-log -
(- will make zaje read progressively from STDIN)
AUTHOR:
{{range .Authors}}{{ . }}{{end}}
{{end}}{{if .Commands}}
COPYRIGHT:
{{.Copyright}}
{{end}}
`
app.Name = "zaje"
app.Usage = "Syntax highlighter to cover all your shell needs"
app.Version = "0.21.3-2"
app.EnableBashCompletion = true
cli.VersionFlag = cli.BoolFlag{
Name: "print-version, V",
Usage: "print only the version",
}
app.Compiled = time.Now()
app.Description = "Highlights text based on regular expressions/strings/characters matching.\n Can operate on files or data sent to STDIN.\n"
app.Authors = []cli.Author{
{
Name: "Jesse Portnoy",
Email: "jesse@packman.io",
},
}
app.Copyright = "(c) packman.io"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "syn-dir, s",
Usage: "Path to lexer files. The `ZAJE_SYNDIR` ENV var is also honoured.\n If neither is set, ~/.config/zaje/syntax_files will be used.\n",
EnvVar: "ZAJE_SYNDIR",
Destination: &zaje.SynDir,
},
cli.StringFlag{
Name: "lexer, l",
Usage: `config file to use when parsing input.
When none is passed, zaje will attempt to autodetect based on the file name or first line of input.
You can set the path to lexer files by exporting the ZAJE_SYNDIR ENV var.
If not exported, /etc/zaje/highlight will be used.`,
Destination: &zaje.HighlightLexer,
},
cli.BoolFlag{
Name: "debug, d",
Usage: "Run in debug mode.\n",
Destination: &zaje.Debug,
},
cli.BoolFlag{
Name: "add-line-numbers, ln",
Usage: "Add line numbers.\n",
Destination: &zaje.AddLineNumbers,
},
cli.BoolFlag{
Name: "remove-line-numbers, rln",
Usage: "Remove line numbers.\n",
Destination: &zaje.RemoveLineNumbers,
},
}
zaje.PopulateAppMetadata(app)

app.Flags = append(app.Flags, cli.BoolFlag{
Name: "remove-line-numbers, rln",
Usage: "Remove line numbers.\n",
Destination: &zaje.RemoveLineNumbers,
},
)

app.Action = func(c *cli.Context) error {
log.SetFlags(log.LstdFlags | log.Lshortfile)
Expand Down
Binary file modified cmd/zaje
Binary file not shown.
71 changes: 1 addition & 70 deletions cmd/zaje.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"net/http"
"os"
"regexp"
"time"

// "reflect"

Expand All @@ -20,75 +19,7 @@ import (
func main() {

app := cli.NewApp()
cli.AppHelpTemplate = `NAME:
{{.Name}} - {{.Usage}}
USAGE:
{{.HelpName}} {{if .VisibleFlags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[input-file || - ]{{end}}
{{if len .Authors}}
COMMANDS:
{{range .Commands}}{{if not .HideHelp}} {{join .Names ", "}}{{ "\t"}}{{.Usage}}{{ "\n" }}{{end}}{{end}}{{end}}{{if .VisibleFlags}}
GLOBAL OPTIONS:
{{range .VisibleFlags}}{{.}}{{ "\n" }}
{{end}}{{end}}{{if .Copyright }}
EXAMPLES:
To use zaje as a cat replacement:
$ zaje /path/to/file
To replace tail -f:
$ tail -f /path/to/file | zaje -l server-log -
(- will make zaje read progressively from STDIN)
AUTHOR:
{{range .Authors}}{{ . }}{{end}}
{{end}}{{if .Commands}}
COPYRIGHT:
{{.Copyright}}
{{end}}
`
app.Name = "zaje"
app.Usage = "Syntax highlighter to cover all your shell needs"
app.Version = "0.21.3-2"
app.EnableBashCompletion = true
cli.VersionFlag = cli.BoolFlag{
Name: "print-version, V",
Usage: "print only the version",
}
app.Compiled = time.Now()
app.Description = "Highlights text based on regular expressions/strings/characters matching.\n Can operate on files or data sent to STDIN.\n"
app.Authors = []cli.Author{
{
Name: "Jesse Portnoy",
Email: "jesse@packman.io",
},
}
app.Copyright = "(c) packman.io"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "syn-dir, s",
Usage: "Path to lexer files. The `ZAJE_SYNDIR` ENV var is also honoured.\n If neither is set, ~/.config/zaje/syntax_files will be used.\n",
EnvVar: "ZAJE_SYNDIR",
Destination: &zaje.SynDir,
},
cli.StringFlag{
Name: "lexer, l",
Usage: `config file to use when parsing input.
When none is passed, zaje will attempt to autodetect based on the file name or first line of input.
You can set the path to lexer files by exporting the ZAJE_SYNDIR ENV var.
If not exported, /etc/zaje/highlight will be used.`,
Destination: &zaje.HighlightLexer,
},
cli.BoolFlag{
Name: "debug, d",
Usage: "Run in debug mode.\n",
Destination: &zaje.Debug,
},
cli.BoolFlag{
Name: "add-line-numbers, ln",
Usage: "Add line numbers.\n",
Destination: &zaje.AddLineNumbers,
},
}
zaje.PopulateAppMetadata(app)

app.Action = func(c *cli.Context) error {
log.SetFlags(log.LstdFlags | log.Lshortfile)
Expand Down
84 changes: 80 additions & 4 deletions common_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import (
"net/http"
"os"
"strings"
"time"

// "reflect"

"github.com/fatih/color"
highlight "github.com/jessp01/gohighlight"
"github.com/urfave/cli"
)

var def *highlight.Def
Expand All @@ -34,6 +36,78 @@ var AddLineNumbers bool
// RemoveLineNumbers useful when working on an image input
var RemoveLineNumbers bool

// PopulateAppMetadata see https://github.com/urfave/cli/blob/v1.22.14/docs/v1/manual.md#customization-1
func PopulateAppMetadata(app *cli.App) {
cli.AppHelpTemplate = `NAME:
{{.Name}} - {{.Usage}}
USAGE:
{{.HelpName}} {{if .VisibleFlags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[input-file || - ]{{end}}
{{if len .Authors}}
COMMANDS:
{{range .Commands}}{{if not .HideHelp}} {{join .Names ", "}}{{ "\t"}}{{.Usage}}{{ "\n" }}{{end}}{{end}}{{end}}{{if .VisibleFlags}}
GLOBAL OPTIONS:
{{range .VisibleFlags}}{{.}}{{ "\n" }}
{{end}}{{end}}{{if .Copyright }}
EXAMPLES:
To use {{.Name}} as a cat replacement:
$ {{.Name}} /path/to/file
To replace tail -f:
$ tail -f /path/to/file | {{.Name}} -l server-log -
(- will make {{.Name}} read progressively from STDIN)
AUTHOR:
{{range .Authors}}{{ . }}{{end}}
{{end}}{{if .Commands}}
COPYRIGHT:
{{.Copyright}}
{{end}}
`
app.Usage = "Syntax highlighter to cover all your shell needs"
app.Version = "0.21.3-2"
app.EnableBashCompletion = true
cli.VersionFlag = cli.BoolFlag{
Name: "print-version, V",
Usage: "print only the version",
}
app.Compiled = time.Now()
app.Description = "Highlights text based on regular expressions/strings/characters matching.\n Can operate on files or data sent to STDIN.\n"
app.Authors = []cli.Author{
{
Name: "Jesse Portnoy",
Email: "jesse@packman.io",
},
}
app.Copyright = "(c) packman.io"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "syn-dir, s",
Usage: "Path to lexer files. The `ZAJE_SYNDIR` ENV var is also honoured.\n If neither is set, ~/.config/zaje/syntax_files will be used.\n",
EnvVar: "ZAJE_SYNDIR",
Destination: &SynDir,
},
cli.StringFlag{
Name: "lexer, l",
Usage: `config file to use when parsing input.
When none is passed, zaje will attempt to autodetect based on the file name or first line of input.
You can set the path to lexer files by exporting the ZAJE_SYNDIR ENV var.
If not exported, /etc/zaje/highlight will be used.`,
Destination: &HighlightLexer,
},
cli.BoolFlag{
Name: "debug, d",
Usage: "Run in debug mode.\n",
Destination: &Debug,
},
cli.BoolFlag{
Name: "add-line-numbers, ln",
Usage: "Add line numbers.\n",
Destination: &AddLineNumbers,
},
}
}

func printDebugInfo() {
fmt.Println("DEBUG INFO:")
fmt.Println("Syntax files dir: " + SynDir)
Expand All @@ -46,11 +120,13 @@ func getDefs(filename string, data []byte) []highlight.LineMatch {

if SynDir == "" {
if SynDir == "" {
if stat, err := os.Stat(os.Getenv("HOME") + "/.config/zaje/syntax_files"); err == nil && stat.IsDir() {
SynDir = os.Getenv("HOME") + "/.config/zaje/syntax_files"
checkPath := os.Getenv("HOME") + "/.config/zaje/syntax_files"
if stat, err := os.Stat(checkPath); err == nil && stat.IsDir() {
SynDir = checkPath
} else {
if stat, err := os.Stat("/etc/zaje/syntax_files"); err == nil && stat.IsDir() {
SynDir = "/etc/zaje/syntax_files"
checkPath := "/etc/zaje/syntax_files"
if stat, err := os.Stat(checkPath); err == nil && stat.IsDir() {
SynDir = checkPath
}
}
}
Expand Down

0 comments on commit e790cd0

Please sign in to comment.