colorize

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 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
Examples
package main

import (
	"flag"
	"fmt"
	"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)
	style := colorize.Style{
		Foreground: colorize.RGB(88, 188, 88),
		Background: colorize.RGB(188, 88, 8),
		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 [R, G, B]",
		"\n=======================",
	)
	println(sample(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 sample(colorized *colorize.Colorable) string {
	sample := make([]string, 0)
	for colorIndex := 0; colorIndex <= 255; colorIndex++ {
		red := uint8((colorIndex + 5) % 256)
		green := uint8(colorIndex * 3 % 256)
		blue := uint8(255 - colorIndex)

		style := colorize.Style{
			Background: colorize.RGB(red, green, blue),
		}
		sample = append(
			sample,
			colorized.Sprint(
				style,
				fmt.Sprintf(
					" %-3d, %-3d, %-3d ",
					red,
					green,
					blue,
				),
			),
			" ",
		)

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

	return strings.Join(sample, "")
}

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:

ln -sf ../../assets/git/hooks/pre-commit.sh .git/hooks/pre-commit && \
ln -sf ../../assets/git/hooks/pre-push.sh .git/hooks/pre-push     && \
ln -sf ../../assets/git/hooks/commit-msg.sh .git/hooks/commit-msg

πŸ‘¨β€πŸ’» 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
	Red() uint8
	Green() uint8
	Blue() uint8
}

Color representation interface.

func RGB ΒΆ added in v1.1.0

func RGB(red, green, blue uint8) 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 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 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 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 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 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 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 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 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 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 of the 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