coloredcobra

package module
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 24, 2022 License: MIT Imports: 6 Imported by: 0

README

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/crinklywrappr/coloredcobra

Quick start

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

import cc "github.com/crinklywrappr/coloredcobra"

Or:

import (
    ...
    cc "github.com/crinklywrappr/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.

Documentation

Overview

ColoredCobra allows you to colorize Cobra's text output, making it look better using simple settings to customize individual parts of console output.

Usage example:

1. Insert in cmd/root.go file of your project :

import cc "github.com/crinklywrappr/coloredcobra"

2. Put the following code to the beginning of the Execute() function:

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

3. Build & execute your code.

Copyright © 2022 Ivan Pirog <ivan.pirog@gmail.com>. Released under the MIT license. Project home: https://github.com/crinklywrappr/coloredcobra

Index

Constants

View Source
const (
	None      = 0
	Black     = 1
	Red       = 2
	Green     = 3
	Yellow    = 4
	Blue      = 5
	Magenta   = 6
	Cyan      = 7
	White     = 8
	HiRed     = 9
	HiGreen   = 10
	HiYellow  = 11
	HiBlue    = 12
	HiMagenta = 13
	HiCyan    = 14
	HiWhite   = 15
	Bold      = 16
	Italic    = 32
	Underline = 64
)

Constants for colors and B, I, U

Variables

This section is empty.

Functions

func Init

func Init(cfg *Config)

Init patches Cobra's usage template with configuration provided.

Types

type Config

type Config struct {
	RootCmd         *cobra.Command
	Headings        uint8
	Commands        uint8
	CmdShortDescr   uint8
	ExecName        uint8
	Flags           uint8
	FlagsDataType   uint8
	FlagsDescr      uint8
	Aliases         uint8
	Example         uint8
	NoExtraNewlines bool
	NoBottomNewline bool
}

Config is a settings structure which sets styles for individual parts of Cobra text output.

Note that RootCmd is required.

Example:

c := &cc.Config{
   RootCmd:       rootCmd,
   Headings:      cc.HiWhite + cc.Bold + cc.Underline,
   Commands:      cc.Yellow + cc.Bold,
   CmdShortDescr: cc.Cyan,
   ExecName:      cc.Bold,
   Flags:         cc.Bold,
   Aliases:       cc.Bold,
   Example:       cc.Italic,
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL