Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect NO_COLOR and NO_HISTORY environment variables #215

Merged
merged 1 commit into from
Oct 2, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions cmd/genji/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,40 @@ func Run(opts *Options) error {
return nil
}

e := prompt.New(
sh.execute,
sh.completer,
promptOpts := []prompt.Option{
prompt.OptionPrefix("genji> "),
prompt.OptionTitle("genji"),
prompt.OptionLivePrefix(sh.changelivePrefix),
prompt.OptionHistory(history),
}

// If NO_COLOR env var is present, disable color. See https://no-color.org
if _, ok := os.LookupEnv("NO_COLOR"); ok {
// A list of color options we have to reset.
colorOpts := []func(prompt.Color) prompt.Option{
prompt.OptionPrefixTextColor,
prompt.OptionPreviewSuggestionTextColor,
prompt.OptionSuggestionTextColor,
prompt.OptionSuggestionBGColor,
prompt.OptionSelectedSuggestionTextColor,
prompt.OptionSelectedSuggestionBGColor,
prompt.OptionDescriptionTextColor,
prompt.OptionDescriptionBGColor,
prompt.OptionSelectedDescriptionTextColor,
prompt.OptionSelectedDescriptionBGColor,
prompt.OptionScrollbarThumbColor,
prompt.OptionScrollbarBGColor,
}
for _, opt := range colorOpts {
resetColor := opt(prompt.DefaultColor)
promptOpts = append(promptOpts, resetColor)
}
}

e := prompt.New(
sh.execute,
sh.completer,
promptOpts...,
)

e.Run()
Expand All @@ -146,6 +173,9 @@ func (sh *Shell) loadCommandSuggestions() {
}

func (sh *Shell) loadHistory() ([]string, error) {
if _, ok := os.LookupEnv("NO_HISTORY"); ok {
return nil, nil
}
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, err
Expand Down Expand Up @@ -174,6 +204,9 @@ func (sh *Shell) loadHistory() ([]string, error) {
}

func (sh *Shell) dumpHistory() error {
if _, ok := os.LookupEnv("NO_HISTORY"); ok {
return nil
}
homeDir, err := os.UserHomeDir()
if err != nil {
return err
Expand Down