colorify

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2023 License: MIT Imports: 5 Imported by: 0

README

colorify PkgGoDev

colorify is a very simple implementation of a text stream formtatting library, usually out of a tty into stdout.

Its mostly a simple implemention and not have all the robust options as fatih/color ANSI colorized output package. It gets the job done and I use it frequently to generate colorized output myself, especially for colorizing CLI outputs.

Install

    go get github.com/mainak90/colorify

Examples

    // Import the library into your code
    import "github.com/mainak90/colorify"
    // Initialite a colorify struct. Structs can be initialized either with only string attributes(bold/italics/reverse)
	// Or also with color values set.
	// Example --> Struct with no color set. You can also set the NoColor flag to force color output or supress it. 
	//Keeping it UnSet allows the library to determine if its a tty and output colored output in that case.
	noColStruct := colorify.Colorify{Attr: colorify.Underline, NoColor: "false"}
	// Example --> Struct with color preset.
	withColStruct := colorify.Colorify{Color: colorify.Green, Attr: colorify.Bold}
	// Supported attributes --> 
	1. `colorify.Regular`: Regular text format.
	2. `colorify.Italics`: Italicized text format.
	3. `colorify.Transparent`: Transparent text format.
	4. 	`colorify.Reverse`: Background reversed text format.
	// Invoking the print functions after this is just as easy as calling the appropriate ones with particular arguments.
	// Please note that the library has support column based coloring scheme, which means you can have as many colors in 
	// one single line as you want.
	io.WriteString(os.Stdout, noColStruct.Sprintln("This is regular", colorify.Green, "This is green!"))
	noColStruct.Println(colorify.Red, "[Error]", colorify.Yellow, "This is a new error")
	io.WriteString(os.Stdout, col.Sprintln("\n", colorify.Blue, "This", "is", "Sparta"))
	// Example with color initialized in struct. 
	withColStruct.Println("Should be green and bold!")

For more examples checkout the examples/ directory.

Output

img.png

Todo

  1. GH Actions pipelines -> Done
  2. Tests.
  3. More functions.
  4. Fix logged issues.

License

The MIT License (MIT) - see LICENSE.md for more details

Documentation

Overview

Package Colorify is a simple but straightforward attempt to create a stdout colorification/beautification module. Its based on a very simple premise of creating a simple set of colored and formatted output, only the io.Writer/os.stdout intrerfaces. Implementation is inspired by fatih/color package which is by all means a more feature rich and mature project. This is just a very simple re-implementation of the same.

Package colorify is an ANSI color package to output colorized or SGR defined output to the standard output. Quickstart on ways to use this:

	// Init colorify with color palette in struct.
	import "github.com/mainak90/colorify"
	red := colorify.Colorify{Color: colorify.Red, Attr: colorify.Underline}
	yellow := colorify.Colorify{Color: colorify.Yellow, Attr: colorify.Regular}

	// Start the init function
	redPrint := red.New()
	yellowPrint := yellow.New()

	// Use various fmt package interfaces to output in colour
	redPrint.Println("This", "is", "Sparta")
	yellowPrint.Println("This is yellow regular!!!")
	fmt.Println(yellowPrint.Sprintf("Sprintf message"))
	io.WriteString(os.Stdout, yellowPrint.Sprintln("This", "is", "Sparta"))
	redPrint.Printf("%s", "This", "is", "a", "word")

	// Colors supported
	Red, Green, Yellow, Blue, Purple, Cyan, White

// Formatting attributes supported
Bold, Italics, Underline, Reverse, Strikethrough

Help furnish this package with more add-on features.

Index

Constants

View Source
const Base = "\033["

Base const for the base str __repr__ for colors.

Variables

View Source
var (
	Reset         Color = "0m"
	Red           Color = "31m"
	Green         Color = "32m"
	Yellow        Color = "33m"
	Blue          Color = "34m"
	Purple        Color = "35m"
	Cyan          Color = "36m"
	Gray          Color = "37m"
	White         Color = "97m"
	Bold          Attr  = "1;"
	Transparent   Attr  = "2;"
	Italics       Attr  = "3;"
	Underline     Attr  = "4;"
	Reverse       Attr  = "7;"
	Strikethrough Attr  = "9;"
	Regular       Attr  = "0;"
)

Color str __repr__ vars for each supported colors.

Functions

func Done

func Done() string

Done: Resets the coloration and formatting, mostly used right after statment execution.

Types

type Attr

type Attr string

Custom type Attr stores the string attributes for the colored output. Typically Bold and Underlines are typical attributes.

type Color

type Color string

Color type represents the colors to be used for the output.

type Colorify

type Colorify struct {
	Color       Color
	Attr        Attr
	NoColor     string
	ColorScheme string
}

Main struct for interfacing with all golang fmt functions. Elems --> 1. Color --> The colored o/p choice. 2. Attr --> Added string attributes like bold/underline etc 3. NoColour --> Automatically set attribute which disables coloration if not o/p to tty 4. ColorScheme --> The string __repr__ of the generated color scheme.

func (*Colorify) Fprint

func (c *Colorify) Fprint(w io.Writer, msg ...interface{}) (n int, err error)

Fprint: Implements the fmt.Fprint interface just with the colorification addon.

func (*Colorify) Fprintf

func (c *Colorify) Fprintf(w io.Writer, format string, msg ...interface{}) (n int, err error)

Fprintf: Implements the fmt.Fprintf interface just with the colorification addon.

func (*Colorify) Fprintln

func (c *Colorify) Fprintln(w io.Writer, msg ...interface{}) (n int, err error)

Fprintln: Implements the fmt.Fprintln interface just with the colorification addon.

func (*Colorify) Printf

func (c *Colorify) Printf(format string, msg ...interface{}) (n int, err error)

Printf: Implements the fmt.Printf interface just with the colorification addon.

func (*Colorify) Println

func (c *Colorify) Println(msg ...interface{}) (n int, err error)

func (*Colorify) Sprintf

func (c *Colorify) Sprintf(msg ...interface{}) string

Sprintf: Implements the fmt.Sprintf interface just with the colorification addon.

func (*Colorify) Sprintln

func (c *Colorify) Sprintln(msg ...interface{}) string

Sprintln: Implements the fmt.Sprintln interface just with the colorification addon.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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