colorize

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2021 License: MIT Imports: 8 Imported by: 3

README

Colorize CircleCI

license release Travis CI Coverage Status codecov GolangCI Go Report Card Codacy Badge GoDoc DepShield Badge FOSSA Status

   _____      _            _
  / ____|    | |          (_)
 | |     ___ | | ___  _ __ _ _______
 | |    / _ \| |/ _ \| '__| |_  / _ \
 | |___| (_) | | (_) | |  | |/ /  __/
  \_____\___/|_|\___/|_|  |_/___\___|

is a library that helps to apply RGB colors, based on 24 bit - ANSI escape sequences for console output.

Table of Contents

🏎️ Getting Started

Prerequisites
Installation
go get -u github.com/ahmedkamals/colorize
cp .env.sample .env
Examples
package main

import (
    "flag"
    "github.com/ahmedkamals/colorize"
    "os"
    "strings"
)

func main() {
    var IsColorDisabled = flag.Bool("no-color", false, "Disable color output.")
    colorize.IsColorDisabled = *IsColorDisabled // disables/enables colorized output.

    colorized := colorize.NewColorable(os.Stdout)
    red, _ := colorize.Hex("#81BEF3")
    style := colorize.Style{
        Foreground: colorize.RGB(218, 44, 128),
        Background: red,
        Font: []colorize.FontEffect{
            colorize.Bold,
            colorize.Italic,
            colorize.Underline,
            colorize.CrossedOut,
        },
    }

    callback := colorized.SprintlnFunc()
    print(callback(style, "I am ", "stylish!"))

    printDirectColors(colorized)

    colorized.Set(colorize.Style{
        Foreground: colorize.RGB(255, 188, 88),
        Font:       []colorize.FontEffect{colorize.Bold},
    })
    print("Output will be styled.\nTill next reset!")
    colorized.Reset()
    colorized.Println(
        colorize.Style{
            Foreground: colorize.RGB(188, 81, 188),
        },
        "\n\nSample colors in Hexadecimal and RGB",
        "\n====================================",
    )
    println(sampleColors(colorized))
}

func printDirectColors(colorized *colorize.Colorable) {
    println(colorized.Black("Text in Black!"))
    println(colorized.Blue("Deep Blue C!"))
    println(colorized.Cyan("Hi Cyan!"))
    println(colorized.Gray("Gray logged text!"))
    println(colorized.Green("50 shades of Green!"))
    println(colorized.Magenta("Go Magenta!"))
    println(colorized.Orange("Orange is the new black!"))
    println(colorized.Purple("The Purple hurdle!"))
    println(colorized.Red("The thin Red light!"))
    println(colorized.White("Twice White!"))
    println(colorized.Yellow("Hello Yellow!"))
}

func sampleColors(colorized *colorize.Colorable) string {
    const columns = 10
    sample := make([]string, 0)
    for colorIndex := 0; colorIndex <= 255; colorIndex++ {
        red := byte((colorIndex + 5) % 256)
        green := byte(colorIndex * 3 % 256)
        blue := byte(255 - colorIndex)

        style := colorize.Style{
            Background: colorize.RGB(red, green, blue),
        }
        sample = append(
            sample,
            getSampleContent(colorized, style),
            " ",
        )

        if (colorIndex-9)%columns == 0 {
            sample = append(sample, "\n")
        }
    }

    return strings.Join(sample, "")
}

func getSampleContent(colorized *colorize.Colorable, style colorize.Style) string {
    return colorized.Sprintf(
        style,
        " %-7s  %-13s",
        style.Background.Hex(),
        style.Background.RGB(),
    )
}

Sample output

🕸️ Tests

make test
📈 Benchmarks

Benchmarks Flamegraph

🤝 Contribution

Please refer to the CONTRIBUTING.md file.

⚓ Git Hooks

In order to set up tests running on each commit do the following steps:

git config --local core.hooksPath .githooks

👨‍💻 Credits

🆓 LICENSE

Colorize is released under MIT license, please refer to the LICENSE.md file.

FOSSA Status

Happy Coding 🙂

Analytics

Documentation

Overview

Package colorize adds more color to your console.

Index

Constants

This section is empty.

Variables

View Source
var (
	// IsColorDisabled is a global option to dictate if the output should be colored or not.
	// The value is dynamically set, based on the stdout's file descriptor, if it is a terminal or not.
	// To disable color for specific color sections please use the DisableColor() method individually.
	IsColorDisabled = os.Getenv("TERM") == "dump" ||
		(!isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd()))
)

Functions

This section is empty.

Types

type Color added in v1.1.0

type Color interface {
	Comparable
	Formatter
	fmt.Stringer
	Red() byte
	Green() byte
	Blue() byte
	Alpha() byte
	Hex() string
	RGB() string
}

Color representation interface.

func Hex added in v1.2.0

func Hex(color string) (Color, error)

Hex parses a hexadecimal color string, represented either in the 3 "#abc" or 6 "#abcdef" digits.

func RGB added in v1.1.0

func RGB(red, green, blue byte) Color

RGB returns a new/cached instance of the Color.

type Colorable

type Colorable struct {
	// contains filtered or unexported fields
}

Colorable wrapper for color operations.

func NewColorable

func NewColorable(output io.Writer) *Colorable

NewColorable allocates and returns a new Colorable. e.g.: colorized := NewColorable(os.Stdout)

func (*Colorable) AppliedStyle added in v1.1.0

func (c *Colorable) AppliedStyle() Style

AppliedStyle returns the applied style by Set().

func (*Colorable) Black

func (c *Colorable) Black(s ...interface{}) string

Black returns a black foreground color effect.

func (*Colorable) Blue

func (c *Colorable) Blue(s ...interface{}) string

Blue returns a blue foreground color effect.

func (*Colorable) Cyan

func (c *Colorable) Cyan(s ...interface{}) string

Cyan returns a cyan foreground color effect.

func (*Colorable) DisableColor added in v1.1.0

func (c *Colorable) DisableColor() *Colorable

DisableColor used to disable colored output, useful with a defined flag e.g. --no-color, so without modifying existing code output is done normally but having the color disabled.

func (*Colorable) EnableColor added in v1.1.0

func (c *Colorable) EnableColor() *Colorable

EnableColor to re-enable colored output used in conjunction with DisableColor(). Otherwise, it will have no side effect.

func (*Colorable) Fprint added in v1.1.0

func (c *Colorable) Fprint(w io.Writer, style Style, s ...interface{}) (n int, err error)

Fprint acts as the standard fmt.Fprint() method, wrapped with the given style.

func (*Colorable) FprintFunc added in v1.1.0

func (c *Colorable) FprintFunc() func(w io.Writer, style Style, s ...interface{}) (n int, err error)

FprintFunc returns a new callback that prints the passed arguments as Colorable.Fprint().

func (*Colorable) Fprintf added in v1.1.0

func (c *Colorable) Fprintf(w io.Writer, style Style, format string, s ...interface{}) (n int, err error)

Fprintf acts as the standard fmt.Fprintf() method, wrapped with the given style.

func (*Colorable) FprintfFunc added in v1.1.0

func (c *Colorable) FprintfFunc() func(w io.Writer, style Style, format string, s ...interface{}) (n int, err error)

FprintfFunc returns a new callback that prints the passed arguments as Colorable.Fprintf().

func (*Colorable) Fprintln added in v1.1.0

func (c *Colorable) Fprintln(w io.Writer, style Style, s ...interface{}) (n int, err error)

Fprintln acts as the standard fmt.Fprintln() method, wrapped with the given style.

func (*Colorable) FprintlnFunc added in v1.1.0

func (c *Colorable) FprintlnFunc() func(w io.Writer, style Style, s ...interface{}) (n int, err error)

FprintlnFunc returns a new callback that prints the passed arguments as Colorable.Fprintln().

func (*Colorable) Gray

func (c *Colorable) Gray(s ...interface{}) string

Gray returns a gray foreground color effect.

func (*Colorable) Green

func (c *Colorable) Green(s ...interface{}) string

Green returns a green foreground color effect.

func (*Colorable) Magenta

func (c *Colorable) Magenta(s ...interface{}) string

Magenta returns a magenta foreground color effect.

func (*Colorable) Orange

func (c *Colorable) Orange(s ...interface{}) string

Orange returns an orange foreground color effect.

func (*Colorable) Print added in v1.1.0

func (c *Colorable) Print(style Style, s ...interface{}) (n int, err error)

Print acts as the standard fmt.Print() method, wrapped with the given style.

func (*Colorable) PrintFunc added in v1.1.0

func (c *Colorable) PrintFunc() func(style Style, s ...interface{}) (n int, err error)

PrintFunc returns a new callback that prints the passed arguments as Colorable.Print().

func (*Colorable) Printf added in v1.1.0

func (c *Colorable) Printf(style Style, format string, s ...interface{}) (n int, err error)

Printf acts as the standard fmt.Printf() method, wrapped with the given style.

func (*Colorable) PrintfFunc added in v1.1.0

func (c *Colorable) PrintfFunc() func(style Style, format string, s ...interface{}) (n int, err error)

PrintfFunc returns a new callback that prints the passed arguments as Colorable.Printf().

func (*Colorable) Println added in v1.1.0

func (c *Colorable) Println(style Style, s ...interface{}) (n int, err error)

Println acts as the standard fmt.Println() method, wrapped with the given style.

func (*Colorable) PrintlnFunc added in v1.1.0

func (c *Colorable) PrintlnFunc() func(style Style, s ...interface{}) (n int, err error)

PrintlnFunc returns a new callback that prints the passed arguments as Colorable.Println().

func (*Colorable) Purple added in v1.1.0

func (c *Colorable) Purple(s ...interface{}) string

Purple returns a purple foreground color effect.

func (*Colorable) Red

func (c *Colorable) Red(s ...interface{}) string

Red returns a red foreground color effect.

func (*Colorable) Reset

func (c *Colorable) Reset() *Colorable

Reset the color value to the default.

func (*Colorable) Set

func (c *Colorable) Set(style Style) *Colorable

Set a Style for the next output operations.

func (*Colorable) Sprint added in v1.1.0

func (c *Colorable) Sprint(style Style, s ...interface{}) string

Sprint acts as the standard fmt.Sprint() method, wrapped with the given style.

func (*Colorable) SprintFunc added in v1.1.0

func (c *Colorable) SprintFunc() func(style Style, s ...interface{}) string

SprintFunc returns a new callback that prints the passed arguments as Colorable.Sprint().

func (*Colorable) Sprintf added in v1.1.0

func (c *Colorable) Sprintf(style Style, format string, s ...interface{}) string

Sprintf acts as the standard fmt.Sprintf() method, wrapped with the given style.

func (*Colorable) SprintfFunc added in v1.1.0

func (c *Colorable) SprintfFunc() func(style Style, format string, s ...interface{}) string

SprintfFunc returns a new callback that prints the passed arguments as Colorable.Sprintf().

func (*Colorable) Sprintln added in v1.1.0

func (c *Colorable) Sprintln(style Style, s ...interface{}) string

Sprintln acts as the standard fmt.Sprintln() method, wrapped with the given style.

func (*Colorable) SprintlnFunc added in v1.1.0

func (c *Colorable) SprintlnFunc() func(style Style, s ...interface{}) string

SprintlnFunc returns a new callback that prints the passed arguments as Colorable.Sprintln().

func (*Colorable) White added in v1.1.0

func (c *Colorable) White(s ...interface{}) string

White returns a white foreground color effect.

func (*Colorable) Yellow added in v1.1.0

func (c *Colorable) Yellow(s ...interface{}) string

Yellow returns a yellow foreground color effect.

type Comparable added in v1.1.1

type Comparable interface {
	Equals(Color) bool
}

Comparable representation interface.

type FontEffect

type FontEffect int

FontEffect value.

const (
	Normal FontEffect = iota
	Bold
	Faint
	Italic
	Underline
	BlinkSlow
	BlinkRapid
	ReverseVideo
	Concealed
	CrossedOut
)

Font effects. Some effects are not supported on all terminals.

type Formatter added in v1.1.1

type Formatter interface {
	// contains filtered or unexported methods
}

Formatter representation interface.

type Style

type Style struct {
	fmt.Formatter
	fmt.Stringer
	Foreground Color
	Background Color
	Font       []FontEffect
}

Style to be applied to the text.

func (Style) Equals added in v1.1.0

func (s Style) Equals(style Style) bool

Equals compares style with a given style, and returns true if they are the same.

func (Style) Format added in v1.1.0

func (s Style) Format(fs fmt.State, verb rune)

Format to an 24-bit ANSI escape sequence an example output might be: "�[38;2;255;0;0m" -> Red color

func (Style) String added in v1.1.0

func (s Style) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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