ColoredСobra is a library that allows to colorize the text output of the Cobra library

  • By Ivan Pirog
  • Last update: Dec 30, 2022
  • Comments: 3

ColoredCobra Logo


Cobra library for creating powerful modern CLI doesn't support color settings for console output. ColoredCobra is a small library that allows you to colorize the text output of the Cobra library, making the console output look better.

ColoredCobra Look

ColoredCobra provides very simple set of settings that allows you to customize individual parts of Cobra text output by specifying a color for them, as well as bold, italic, underlined styles.

ColoredCobra Config

It's very easy to add ColoredCobra to your project!


Installing

Open terminal and execute:

go get -u github.com/ivanpirog/coloredcobra

Quick start

Open your cmd/root.go and insert this code:

import cc "github.com/ivanpirog/coloredcobra"

Or:

import (
    ...
    cc "github.com/ivanpirog/coloredcobra"
)

Then put this code at the beginning of the Execute() function:

    cc.Init(&cc.Config{
        RootCmd:       rootCmd,
        Headings:      cc.HiCyan + cc.Bold + cc.Underline,
        Commands:      cc.HiYellow + cc.Bold,
        Example:       cc.Italic,
        ExecName:      cc.Bold,
        Flags:         cc.Bold,
    })

That's all. Now build your project and see the output of the help command.

Overview

Config{} has just one required parameter RootCmd. This is a pointer to the Cobra's root command. Rest of parameters have default values.

Style of any part of text output is represented by a sum of predefined constants. For example:

Headings: cc.HiYellow + cc.Bold + cc.Underline
Commands: cc.Red + cc.Bold
ExecName: cc.Bold      // equals cc.White + cc.Bold
Example:  cc.Underline // equals cc.White + cc.Underline

Available color constants:

Black
Red
Green
Yellow
Blue
Magenta
Cyan
White (default)
HiRed (Hi-Intensity Red)
HiGreen
HiYellow
HiBlue
HiMagenta
HiCyan
HiWhite

Available text formatting constants:

Bold
Italic
Underline

Available config parameters:

Config Parameters

  • Headings: headers style.

  • Commands: commands style.

  • CmdShortDescr: short description of commands style.

  • ExecName: executable name style.

  • Flags: short and long flag names (-f, --flag) style.

  • FlagsDataType: style of flags data type.

  • FlagsDescr: flags description text style.

  • Aliases: list of command aliases style.

  • Example: example text style.

  • NoExtraNewlines: no line breaks before and after headings, if true. By default: false.

  • NoBottomNewline: no line break at the end of Cobra's output, if true. By default: false.


NoExtraNewlines parameter results:

extranewlines

How it works

ColoredCobra patches Cobra's usage template and extends it with functions for text styling. fatih/color library is used for coloring text output in console.

License

ColoredCobra is released under the MIT license. See LICENSE.

Download

coloredcobra.zip

Comments(3)

  • 1

    hyphenated flags break color scheme

    i found coloredcobra within the first week of its presence on github, serendipitously.

    its now used in skycoin/skywire develop and im very satisfied with its impact on the help menus.

    While i was implementing it, I noticed that the color scheme breaks at the hyphen for any flags which contain a hyphen (i.e. --hyphen-flag) with the text returning to white at the hyphen until the end of the flag text.

    I neglected to file an issue on this when i encountered it, and instead i just removed the hyphenation of the flags to avoid breaking the color scheme.

    Also, there seems to be no way to change the color of the variable type the flag takes, or the color for the default value of the flag, if it is displayed.

    i further worked around that by not initially setting a default value for certain flags, and showing the default as part of the help text where possible. There were other functional reasons for doing this so it wasnt all an asthetic consideration.

    Some screenshots of the help menus, in case you are interested

    image

    image

    This one shows what i was talking about with the default value

    image

    coloredcobra is a fantastic addition to any cobra help menu, and i highly recommend it.

  • 2

    Command group headings are not colorized

    Version 1.6.0 of cobra added the feature to group sub-commands together. It seems like coloredcobra is not capable of coloring the headings for the sub-command groups yet.

  • 3

    Regex to detect flags can be improved

    Currently the regex is (--?\S+) this will incorrectly match words like if-available. Changing it to \s+(--?\S+) will prevent that. Although the ideal solution is to of course iterate over all flags and build up a regex from that list.