Cobra: Can we custom output as colorize?

Created on 14 May 2018  路  7Comments  路  Source: spf13/cobra

kinstale

Most helpful comment

I had some success with this (via https://github.com/fatih/color) by:

  1. rootCmd.SetOutput(color.Output)
  2. Augmenting the UsageTemplate via something like the following:
    cobra.AddTemplateFunc("StyleHeading", color.New(color.FgGreen).SprintFunc())
    usageTemplate := rootCmd.UsageTemplate()
    usageTemplate = strings.NewReplacer(
        `Usage:`, `{{StyleHeading "Usage:"}}`,
        `Aliases:`, `{{StyleHeading "Aliases:"}}`,
        `Available Commands:`, `{{StyleHeading "Available Commands:"}}`,
        `Global Flags:`, `{{StyleHeading "Global Flags:"}}`,
        // The following one steps on "Global Flags:"
        // `Flags:`, `{{StyleHeading "Flags:"}}`,
    ).Replace(usageTemplate)
    re := regexp.MustCompile(`(?m)^Flags:\s*$`)
    usageTemplate = re.ReplaceAllLiteralString(usageTemplate, `{{StyleHeading "Flags:"}}`)
    rootCmd.SetUsageTemplate(usageTemplate)

All 7 comments

Are you asking about colorizing the output of the commands you write in your own apps, or are you asking about colorizing the output of the cobra command ( https://github.com/spf13/cobra/blob/master/cobra/README.md ) ?

My own

AFAIK, there's nothing built in to Cobra for colorizing the output. You can use a library like https://github.com/fatih/color in your own commands, though, to colorize the output you control. You can also use a library to colorize the output of the Help and Usage commands by writing your own functions (see Defining your own help and Defining your own usage in the README. Hope this helps.

In help command?

Yes -- go here https://github.com/spf13/cobra#help-command and read the section Defining your own help for how you can write your own help command. Since it's your own, you can colorize however you want.

I had some success with this (via https://github.com/fatih/color) by:

  1. rootCmd.SetOutput(color.Output)
  2. Augmenting the UsageTemplate via something like the following:
    cobra.AddTemplateFunc("StyleHeading", color.New(color.FgGreen).SprintFunc())
    usageTemplate := rootCmd.UsageTemplate()
    usageTemplate = strings.NewReplacer(
        `Usage:`, `{{StyleHeading "Usage:"}}`,
        `Aliases:`, `{{StyleHeading "Aliases:"}}`,
        `Available Commands:`, `{{StyleHeading "Available Commands:"}}`,
        `Global Flags:`, `{{StyleHeading "Global Flags:"}}`,
        // The following one steps on "Global Flags:"
        // `Flags:`, `{{StyleHeading "Flags:"}}`,
    ).Replace(usageTemplate)
    re := regexp.MustCompile(`(?m)^Flags:\s*$`)
    usageTemplate = re.ReplaceAllLiteralString(usageTemplate, `{{StyleHeading "Flags:"}}`)
    rootCmd.SetUsageTemplate(usageTemplate)

This issue is being marked as stale due to a long period of inactivity

Was this page helpful?
0 / 5 - 0 ratings

Related issues

groenborg picture groenborg  路  5Comments

eine picture eine  路  5Comments

niski84 picture niski84  路  4Comments

garthk picture garthk  路  3Comments

icholy picture icholy  路  6Comments