Skip to content

Commit

Permalink
feat: support --color/--no-color flag and NO_COLOR env var
Browse files Browse the repository at this point in the history
  • Loading branch information
MunifTanjim committed Dec 9, 2022
1 parent 4263bd2 commit 96b8583
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions jiracli/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ func tmpTemplate(templateName string, data interface{}) (string, error) {
}

func TemplateProcessor() *template.Template {
noColor := len(os.Getenv("NO_COLOR")) > 0

funcs := map[string]interface{}{
"jira": func() string {
return os.Args[0]
Expand Down Expand Up @@ -151,6 +153,9 @@ func TemplateProcessor() *template.Template {
return content
},
"color": func(color string) string {
if noColor {
return ""
}
return ansi.ColorCode(color)
},
"remLineBreak": func(content string) string {
Expand Down
10 changes: 10 additions & 0 deletions jiracli/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ func CommandLine(fig *figtree.FigTree, o *oreo.Client) *kingpin.Application {
return nil
}).CounterVar(&verbosity)

var color bool
app.Flag("color", "Enable/Disable output color").PreAction(func(_ *kingpin.ParseContext) error {
if color {
os.Unsetenv("NO_COLOR")
} else {
os.Setenv("NO_COLOR", "1")
}
return nil
}).Hidden().BoolVar(&color)

app.Terminate(func(status int) {
for _, arg := range os.Args {
if arg == "-h" || arg == "--help" || len(os.Args) == 1 {
Expand Down

0 comments on commit 96b8583

Please sign in to comment.