checkpoint: add support for the NO_COLOR variable

This commit is contained in:
Dan Anglin 2024-05-31 09:54:13 +01:00
parent 29d6ca600f
commit fc3109df02
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
3 changed files with 33 additions and 11 deletions

View file

@ -4,6 +4,7 @@ import (
"flag" "flag"
"fmt" "fmt"
"os" "os"
"strconv"
"codeflow.dananglin.me.uk/apollo/enbas/internal/executor" "codeflow.dananglin.me.uk/apollo/enbas/internal/executor"
) )
@ -57,10 +58,21 @@ func run() error {
commandUnblock: "unblock a resource (e.g. an account)", commandUnblock: "unblock a resource (e.g. an account)",
} }
topLevelFlags := executor.TopLevelFlags{} topLevelFlags := executor.TopLevelFlags{
ConfigDir: "",
NoColor: nil,
}
flag.StringVar(&topLevelFlags.ConfigDir, "config-dir", "", "specify your config directory") flag.StringVar(&topLevelFlags.ConfigDir, "config-dir", "", "specify your config directory")
flag.BoolVar(&topLevelFlags.NoColor, "no-color", false, "disable ANSI colours when displaying text on screen") flag.BoolFunc("no-color", "disable ANSI colours when displaying text on screen", func(value string) error {
boolVal, err := strconv.ParseBool(value)
if err != nil {
return fmt.Errorf("unable to parse %q as a boolean; %w", value, err)
}
topLevelFlags.NoColor = new(bool)
*topLevelFlags.NoColor = boolVal
return nil
})
flag.Usage = usageFunc(commandSummaries) flag.Usage = usageFunc(commandSummaries)
@ -72,6 +84,16 @@ func run() error {
return nil return nil
} }
// If NoColor is still unspecified, check to see if the NO_COLOR environment variable is set
if topLevelFlags.NoColor == nil {
topLevelFlags.NoColor = new(bool)
if os.Getenv("NO_COLOR") != "" {
*topLevelFlags.NoColor = true
} else {
*topLevelFlags.NoColor = false
}
}
command := flag.Arg(0) command := flag.Arg(0)
args := flag.Args()[1:] args := flag.Args()[1:]

View file

@ -18,5 +18,5 @@ func (a *AccountNames) Set(value string) error {
type TopLevelFlags struct { type TopLevelFlags struct {
ConfigDir string ConfigDir string
NoColor bool NoColor *bool
} }

View file

@ -116,7 +116,7 @@ func (c *ShowExecutor) showAccount(gtsClient *client.Client) error {
return nil return nil
} }
utilities.Display(account, c.topLevelFlags.NoColor) utilities.Display(account, *c.topLevelFlags.NoColor)
if !c.myAccount && !c.skipAccountRelationship { if !c.myAccount && !c.skipAccountRelationship {
relationship, err := gtsClient.GetAccountRelationship(account.ID) relationship, err := gtsClient.GetAccountRelationship(account.ID)
@ -124,7 +124,7 @@ func (c *ShowExecutor) showAccount(gtsClient *client.Client) error {
return fmt.Errorf("unable to retrieve the relationship to this account; %w", err) return fmt.Errorf("unable to retrieve the relationship to this account; %w", err)
} }
utilities.Display(relationship, c.topLevelFlags.NoColor) utilities.Display(relationship, *c.topLevelFlags.NoColor)
} }
if c.myAccount && c.showUserPreferences { if c.myAccount && c.showUserPreferences {
@ -133,7 +133,7 @@ func (c *ShowExecutor) showAccount(gtsClient *client.Client) error {
return fmt.Errorf("unable to retrieve the user preferences; %w", err) return fmt.Errorf("unable to retrieve the user preferences; %w", err)
} }
utilities.Display(preferences, c.topLevelFlags.NoColor) utilities.Display(preferences, *c.topLevelFlags.NoColor)
} }
return nil return nil
@ -197,7 +197,7 @@ func (c *ShowExecutor) showTimeline(gtsClient *client.Client) error {
return nil return nil
} }
utilities.Display(timeline, c.topLevelFlags.NoColor) utilities.Display(timeline, *c.topLevelFlags.NoColor)
return nil return nil
} }
@ -226,7 +226,7 @@ func (c *ShowExecutor) showList(gtsClient *client.Client) error {
list.Accounts = accountMap list.Accounts = accountMap
} }
utilities.Display(list, c.topLevelFlags.NoColor) utilities.Display(list, *c.topLevelFlags.NoColor)
return nil return nil
} }
@ -261,7 +261,7 @@ func (c *ShowExecutor) showFollowers(gtsClient *client.Client) error {
} }
if len(followers.Accounts) > 0 { if len(followers.Accounts) > 0 {
utilities.Display(followers, c.topLevelFlags.NoColor) utilities.Display(followers, *c.topLevelFlags.NoColor)
} else { } else {
fmt.Println("There are no followers for this account or the list is hidden.") fmt.Println("There are no followers for this account or the list is hidden.")
} }
@ -281,7 +281,7 @@ func (c *ShowExecutor) showFollowing(gtsClient *client.Client) error {
} }
if len(following.Accounts) > 0 { if len(following.Accounts) > 0 {
utilities.Display(following, c.topLevelFlags.NoColor) utilities.Display(following, *c.topLevelFlags.NoColor)
} else { } else {
fmt.Println("This account is not following anyone or the list is hidden.") fmt.Println("This account is not following anyone or the list is hidden.")
} }
@ -296,7 +296,7 @@ func (c *ShowExecutor) showBlocked(gtsClient *client.Client) error {
} }
if len(blocked.Accounts) > 0 { if len(blocked.Accounts) > 0 {
utilities.Display(blocked, c.topLevelFlags.NoColor) utilities.Display(blocked, *c.topLevelFlags.NoColor)
} else { } else {
fmt.Println("You have no blocked accounts.") fmt.Println("You have no blocked accounts.")
} }