chalk

package module
v0.0.0-...-1e9e304 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2017 License: MIT Imports: 4 Imported by: 2

README

Chalk

Easy to use terminal coloring module. It is written as a partial replacement for fmt (you can use Print, Sprint, Println, Sprintln, Printf, Sprintf) and have multiple ways of use.

This module has been written for Mac & Linux and not tested on Windows.

Formatter

You can call the name of color (Black, Red...) or format (Bold, Italic...) as module function. This will return a Formatter that has Print, Sprint, Println, Sprintln, Printf, Sprintf methods.

Basic Formatting

chalk.Bold().Println("bold text")
chalk.Red().Printf("red %d", 42)
chalk.GreenBackground().Print("green", "text")

Format Chaining

chalk.Bold().Cyan().Println("bold cyan text")
chalk.Red().Italic().Printf("red italic %d", 42)
chalk.GreenBackground().Bold().Print("green", "bold", "text")

As Variable

You can also "cook" Formatters to variable and reuse.

cyan := chalk.Cyan().Bold()
boldi := chalk.Bold().Italic()

for index, msg := range messages {
    boldi.Println(index)
    cyan.Println(msg)
}

Caching

If you are using just certain formatting all the time, it is better to use Cached Formatter instead. Cached Formatter has reduced overhead, but you will lose the ability to change it any further. Following methods are available on this formatter: Print, Sprint, Println, Sprintln, Printf, Sprintf & Set.

var (
    cyan = chalk.Cyan.Cached() // No longer editable, but faster
)

func PrintVeryLongListOfMessages() {
    for _, msg := range veryLongListOfMessages {
        cyan.Println(msg) // Same calls as on regular Formatter
    }
}

Set & Clear

You can call Set function on Formatter to set formatter's color format active until Clear or other formatting function is called.

// Set bold + italic formatting
chalk.Bold().Italic().Set()

// Print formatted messages
log.Println("Bold Italic formatted message 1")
log.Println("Bold Italic formatted message 2")
log.Println("Bold Italic formatted message 3")

// Add cyan formatting to previous formatting
// This becomes Bold+Italic+Cyan formatting in terminal
chalk.Cyan().Set() 

// Print newly formatted messages
log.Println("Bold Italic Cyan formatted message 1")

 // Clear all formatting
 // This sets terminal to default color formatting
chalk.Clear()

// Print non-formatted (or default formatted) message
log.Println("Non formatted message")

Disable/Enable coloring

You can temporarily disable/enable coloring with Disable() or Enable() functions. This only overrides behaviour and you can still use formatting functions as is, but they won't do any formating.

// Print bold text
chalk.Bold().Println("bold text")

// Disable formatting
chalk.Disable()

// Print non-bold text (because of disabled formatting)
chalk.Bold().Println("not-bold text")

// Enable formatting again
chalk.Enable()

// Print bold text
chalk.Bold().Println("bold text")

JSON Color formatting (Experimental)

If you are brave enough you can play with JSON(v interface{}) and JSON(v interface{}, prefix, indent string) function (similar signature to json.Marshal & json.MarshalIndent) that parses given value and reports new color formatted json. Output should be similar to that of json.Marshal*, but as stated before; This functionality is experimental and should mostly be used for debugging purposes.

Shorthand calls

You can call the Formatter functions with As prefix (AsBlack, AsRed, AsBold, AsItalic...) to directly report color formatted string. This shorthand syntax supports multiple arguments (just as most of fmt.Print* functions).

chalk.AsBold("bold text")
chalk.AsRed("red", 42)
chalk.AsGreenBackground("green", "text")

List of Colors & Formats

  • Bold
  • Faint
  • Italic
  • Underline
  • BlinkSlow
  • BlinkRapid
  • ReverseVideo
  • Concealed
  • CrossedOut

  • Black ⚫️
  • Red 🔴
  • Green 🍏
  • Yellow 💛
  • Blue 🔵
  • Magenta 💜
  • Cyan 🚙
  • White ⚪️

  • BlackBackground ⚫️
  • RedBackground 🔴
  • GreenBackground 🍏
  • YellowBackground 💛
  • BlueBackground 🔵
  • MagentaBackground 💜
  • CyanBackground 🚙
  • WhiteBackground ⚪️

  • BlackIntense ⚫️
  • RedIntense 🔴
  • GreenIntense 🍏
  • YellowIntense 💛
  • BlueIntense 🔵
  • MagentaIntense 💜
  • CyanIntense 🚙
  • WhiteIntense ⚪️

  • BlackIntenseBackground ⚫️
  • RedIntenseBackground 🔴
  • GreenIntenseBackground 🍏
  • YellowIntenseBackground 💛
  • BlueIntenseBackground 🔵
  • MagentaIntenseBackground 💜
  • CyanIntenseBackground 🚙
  • WhiteIntenseBackground ⚪️

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AsBlack

func AsBlack(a ...interface{}) string

AsBlack reports Black string based on provided content

func AsBlackBackground

func AsBlackBackground(a ...interface{}) string

AsBlackBackground reports Black string based on provided content

func AsBlackIntense

func AsBlackIntense(a ...interface{}) string

AsBlackIntense reports Black string based on provided content

func AsBlackIntenseBackground

func AsBlackIntenseBackground(a ...interface{}) string

AsBlackIntenseBackground reports Black string based on provided content

func AsBlinkRapid

func AsBlinkRapid(a ...interface{}) string

AsBlinkRapid reports BlinkRapid string based on provided content

func AsBlinkSlow

func AsBlinkSlow(a ...interface{}) string

AsBlinkSlow reports BlinkSlow string based on provided content

func AsBlue

func AsBlue(a ...interface{}) string

AsBlue reports Blue string based on provided content

func AsBlueBackground

func AsBlueBackground(a ...interface{}) string

AsBlueBackground reports Blue string based on provided content

func AsBlueIntense

func AsBlueIntense(a ...interface{}) string

AsBlueIntense reports Blue string based on provided content

func AsBlueIntenseBackground

func AsBlueIntenseBackground(a ...interface{}) string

AsBlueIntenseBackground reports Blue string based on provided content

func AsBold

func AsBold(a ...interface{}) string

AsBold reports Bold string based on provided content

func AsConcealed

func AsConcealed(a ...interface{}) string

AsConcealed reports Concealed string based on provided content

func AsCrossedOut

func AsCrossedOut(a ...interface{}) string

AsCrossedOut reports CrossedOut string based on provided content

func AsCyan

func AsCyan(a ...interface{}) string

AsCyan reports Cyan string based on provided content

func AsCyanBackground

func AsCyanBackground(a ...interface{}) string

AsCyanBackground reports Cyan string based on provided content

func AsCyanIntense

func AsCyanIntense(a ...interface{}) string

AsCyanIntense reports Cyan string based on provided content

func AsCyanIntenseBackground

func AsCyanIntenseBackground(a ...interface{}) string

AsCyanIntenseBackground reports Cyan string based on provided content

func AsFaint

func AsFaint(a ...interface{}) string

AsFaint reports Faint string based on provided content

func AsGreen

func AsGreen(a ...interface{}) string

AsGreen reports Green string based on provided content

func AsGreenBackground

func AsGreenBackground(a ...interface{}) string

AsGreenBackground reports Green string based on provided content

func AsGreenIntense

func AsGreenIntense(a ...interface{}) string

AsGreenIntense reports Green string based on provided content

func AsGreenIntenseBackground

func AsGreenIntenseBackground(a ...interface{}) string

AsGreenIntenseBackground reports Green string based on provided content

func AsItalic

func AsItalic(a ...interface{}) string

AsItalic reports Italic string based on provided content

func AsMagenta

func AsMagenta(a ...interface{}) string

AsMagenta reports Magenta string based on provided content

func AsMagentaBackground

func AsMagentaBackground(a ...interface{}) string

AsMagentaBackground reports Magenta string based on provided content

func AsMagentaIntense

func AsMagentaIntense(a ...interface{}) string

AsMagentaIntense reports Magenta string based on provided content

func AsMagentaIntenseBackground

func AsMagentaIntenseBackground(a ...interface{}) string

AsMagentaIntenseBackground reports Magenta string based on provided content

func AsRed

func AsRed(a ...interface{}) string

AsRed reports Red string based on provided content

func AsRedBackground

func AsRedBackground(a ...interface{}) string

AsRedBackground reports Red string based on provided content

func AsRedIntense

func AsRedIntense(a ...interface{}) string

AsRedIntense reports Red string based on provided content

func AsRedIntenseBackground

func AsRedIntenseBackground(a ...interface{}) string

AsRedIntenseBackground reports Red string based on provided content

func AsReverseVideo

func AsReverseVideo(a ...interface{}) string

AsReverseVideo reports ReverseVideo string based on provided content

func AsUnderline

func AsUnderline(a ...interface{}) string

AsUnderline reports Underline string based on provided content

func AsWhite

func AsWhite(a ...interface{}) string

AsWhite reports White string based on provided content

func AsWhiteBackground

func AsWhiteBackground(a ...interface{}) string

AsWhiteBackground reports White string based on provided content

func AsWhiteIntense

func AsWhiteIntense(a ...interface{}) string

AsWhiteIntense reports White string based on provided content

func AsWhiteIntenseBackground

func AsWhiteIntenseBackground(a ...interface{}) string

AsWhiteIntenseBackground reports White string based on provided content

func AsYellow

func AsYellow(a ...interface{}) string

AsYellow reports Yellow string based on provided content

func AsYellowBackground

func AsYellowBackground(a ...interface{}) string

AsYellowBackground reports Yellow string based on provided content

func AsYellowIntense

func AsYellowIntense(a ...interface{}) string

AsYellowIntense reports Yellow string based on provided content

func AsYellowIntenseBackground

func AsYellowIntenseBackground(a ...interface{}) string

AsYellowIntenseBackground reports Yellow string based on provided content

func Clear

func Clear()

Clear clears formatting set by Set function

func Disable

func Disable()

Disable disables coloring

func Enable

func Enable()

Enable enables coloring

func JSON

func JSON(v interface{}) string

JSON repoerts string with colorized syntax of given json string This is experimental, but works on basic cases

func JSONIndent

func JSONIndent(v interface{}, prefix, indent string) string

JSONIndent repoerts string with colorized syntax of given json string This is experimental, but works on basic cases

Types

type CachedFormatter

type CachedFormatter string

CachedFormatter prepares formatting and reuses it

func (CachedFormatter) Print

func (f CachedFormatter) Print(a ...interface{})

Print is colored equivalent of fmt.Print

func (CachedFormatter) Printf

func (f CachedFormatter) Printf(format string, a ...interface{})

Printf is colored equivalent of fmt.Printf

func (CachedFormatter) Println

func (f CachedFormatter) Println(a ...interface{})

Println is colored equivalent of fmt.Println

func (CachedFormatter) Set

func (f CachedFormatter) Set()

Set sets formatting until other formatting function or Clear is called

func (CachedFormatter) Sprint

func (f CachedFormatter) Sprint(a ...interface{}) string

Sprint is colored equivalent of fmt.Sprint

func (CachedFormatter) Sprintf

func (f CachedFormatter) Sprintf(format string, a ...interface{}) string

Sprintf is colored equivalent of fmt.Sprintf

func (CachedFormatter) Sprintln

func (f CachedFormatter) Sprintln(a ...interface{}) string

Sprintln is colored equivalent of fmt.Sprintln

type Formatter

type Formatter []attribute

Formatter is color formatter

func Black

func Black() Formatter

Black reports Formatter with Black as initial format

func BlackBackground

func BlackBackground() Formatter

BlackBackground reports Formatter with Black as initial format

func BlackIntense

func BlackIntense() Formatter

BlackIntense reports Formatter with Black as initial format

func BlackIntenseBackground

func BlackIntenseBackground() Formatter

BlackIntenseBackground reports Formatter with Black as initial format

func BlinkRapid

func BlinkRapid() Formatter

BlinkRapid reports Formatter with BlinkRapid as initial format

func BlinkSlow

func BlinkSlow() Formatter

BlinkSlow reports Formatter with BlinkSlow as initial format

func Blue

func Blue() Formatter

Blue reports Formatter with Blue as initial format

func BlueBackground

func BlueBackground() Formatter

BlueBackground reports Formatter with Blue as initial format

func BlueIntense

func BlueIntense() Formatter

BlueIntense reports Formatter with Blue as initial format

func BlueIntenseBackground

func BlueIntenseBackground() Formatter

BlueIntenseBackground reports Formatter with Blue as initial format

func Bold

func Bold() Formatter

Bold reports Formatter with Bold as initial format

func Concealed

func Concealed() Formatter

Concealed reports Formatter with Concealed as initial format

func CrossedOut

func CrossedOut() Formatter

CrossedOut reports Formatter with CrossedOut as initial format

func Cyan

func Cyan() Formatter

Cyan reports Formatter with Cyan as initial format

func CyanBackground

func CyanBackground() Formatter

CyanBackground reports Formatter with Cyan as initial format

func CyanIntense

func CyanIntense() Formatter

CyanIntense reports Formatter with Cyan as initial format

func CyanIntenseBackground

func CyanIntenseBackground() Formatter

CyanIntenseBackground reports Formatter with Cyan as initial format

func Faint

func Faint() Formatter

Faint reports Formatter with Faint as initial format

func Green

func Green() Formatter

Green reports Formatter with Green as initial format

func GreenBackground

func GreenBackground() Formatter

GreenBackground reports Formatter with Green as initial format

func GreenIntense

func GreenIntense() Formatter

GreenIntense reports Formatter with Green as initial format

func GreenIntenseBackground

func GreenIntenseBackground() Formatter

GreenIntenseBackground reports Formatter with Green as initial format

func Italic

func Italic() Formatter

Italic reports Formatter with Italic as initial format

func Magenta

func Magenta() Formatter

Magenta reports Formatter with Magenta as initial format

func MagentaBackground

func MagentaBackground() Formatter

MagentaBackground reports Formatter with Magenta as initial format

func MagentaIntense

func MagentaIntense() Formatter

MagentaIntense reports Formatter with Magenta as initial format

func MagentaIntenseBackground

func MagentaIntenseBackground() Formatter

MagentaIntenseBackground reports Formatter with Magenta as initial format

func Red

func Red() Formatter

Red reports Formatter with Red as initial format

func RedBackground

func RedBackground() Formatter

RedBackground reports Formatter with Red as initial format

func RedIntense

func RedIntense() Formatter

RedIntense reports Formatter with Red as initial format

func RedIntenseBackground

func RedIntenseBackground() Formatter

RedIntenseBackground reports Formatter with Red as initial format

func ReverseVideo

func ReverseVideo() Formatter

ReverseVideo reports Formatter with ReverseVideo as initial format

func Underline

func Underline() Formatter

Underline reports Formatter with Underline as initial format

func White

func White() Formatter

White reports Formatter with White as initial format

func WhiteBackground

func WhiteBackground() Formatter

WhiteBackground reports Formatter with White as initial format

func WhiteIntense

func WhiteIntense() Formatter

WhiteIntense reports Formatter with White as initial format

func WhiteIntenseBackground

func WhiteIntenseBackground() Formatter

WhiteIntenseBackground reports Formatter with White as initial format

func Yellow

func Yellow() Formatter

Yellow reports Formatter with Yellow as initial format

func YellowBackground

func YellowBackground() Formatter

YellowBackground reports Formatter with Yellow as initial format

func YellowIntense

func YellowIntense() Formatter

YellowIntense reports Formatter with Yellow as initial format

func YellowIntenseBackground

func YellowIntenseBackground() Formatter

YellowIntenseBackground reports Formatter with Yellow as initial format

func (Formatter) Black

func (f Formatter) Black() Formatter

Black reports Formatter with Black as additional format

func (Formatter) BlackBackground

func (f Formatter) BlackBackground() Formatter

BlackBackground reports Formatter with Black as additional format

func (Formatter) BlackIntense

func (f Formatter) BlackIntense() Formatter

BlackIntense reports Formatter with Black as additional format

func (Formatter) BlackIntenseBackground

func (f Formatter) BlackIntenseBackground() Formatter

BlackIntenseBackground reports Formatter with Black as additional format

func (Formatter) BlinkRapid

func (f Formatter) BlinkRapid() Formatter

BlinkRapid reports Formatter with BlinkRapid as additional format

func (Formatter) BlinkSlow

func (f Formatter) BlinkSlow() Formatter

BlinkSlow reports Formatter with BlinkSlow as additional format

func (Formatter) Blue

func (f Formatter) Blue() Formatter

Blue reports Formatter with Blue as additional format

func (Formatter) BlueBackground

func (f Formatter) BlueBackground() Formatter

BlueBackground reports Formatter with Blue as additional format

func (Formatter) BlueIntense

func (f Formatter) BlueIntense() Formatter

BlueIntense reports Formatter with Blue as additional format

func (Formatter) BlueIntenseBackground

func (f Formatter) BlueIntenseBackground() Formatter

BlueIntenseBackground reports Formatter with Blue as additional format

func (Formatter) Bold

func (f Formatter) Bold() Formatter

Bold reports Formatter with Bold as additional format

func (Formatter) Cache

func (f Formatter) Cache() CachedFormatter

Cache caches formatter for faster performance This will reduce the overhead, but you cannot edit it further Important: Make sure you formatting is not disabled

func (Formatter) Concealed

func (f Formatter) Concealed() Formatter

Concealed reports Formatter with Concealed as additional format

func (Formatter) CrossedOut

func (f Formatter) CrossedOut() Formatter

CrossedOut reports Formatter with CrossedOut as additional format

func (Formatter) Cyan

func (f Formatter) Cyan() Formatter

Cyan reports Formatter with Cyan as additional format

func (Formatter) CyanBackground

func (f Formatter) CyanBackground() Formatter

CyanBackground reports Formatter with Cyan as additional format

func (Formatter) CyanIntense

func (f Formatter) CyanIntense() Formatter

CyanIntense reports Formatter with Cyan as additional format

func (Formatter) CyanIntenseBackground

func (f Formatter) CyanIntenseBackground() Formatter

CyanIntenseBackground reports Formatter with Cyan as additional format

func (Formatter) Faint

func (f Formatter) Faint() Formatter

Faint reports Formatter with Faint as additional format

func (Formatter) Green

func (f Formatter) Green() Formatter

Green reports Formatter with Green as additional format

func (Formatter) GreenBackground

func (f Formatter) GreenBackground() Formatter

GreenBackground reports Formatter with Green as additional format

func (Formatter) GreenIntense

func (f Formatter) GreenIntense() Formatter

GreenIntense reports Formatter with Green as additional format

func (Formatter) GreenIntenseBackground

func (f Formatter) GreenIntenseBackground() Formatter

GreenIntenseBackground reports Formatter with Green as additional format

func (Formatter) Italic

func (f Formatter) Italic() Formatter

Italic reports Formatter with Italic as additional format

func (Formatter) Magenta

func (f Formatter) Magenta() Formatter

Magenta reports Formatter with Magenta as additional format

func (Formatter) MagentaBackground

func (f Formatter) MagentaBackground() Formatter

MagentaBackground reports Formatter with Magenta as additional format

func (Formatter) MagentaIntense

func (f Formatter) MagentaIntense() Formatter

MagentaIntense reports Formatter with Magenta as additional format

func (Formatter) MagentaIntenseBackground

func (f Formatter) MagentaIntenseBackground() Formatter

MagentaIntenseBackground reports Formatter with Magenta as additional format

func (Formatter) Print

func (f Formatter) Print(a ...interface{})

Print is colored equivalent of fmt.Print

func (Formatter) Printf

func (f Formatter) Printf(format string, a ...interface{})

Printf is colored equivalent of fmt.Printf

func (Formatter) Println

func (f Formatter) Println(a ...interface{})

Println is colored equivalent of fmt.Println

func (Formatter) Red

func (f Formatter) Red() Formatter

Red reports Formatter with Red as additional format

func (Formatter) RedBackground

func (f Formatter) RedBackground() Formatter

RedBackground reports Formatter with Red as additional format

func (Formatter) RedIntense

func (f Formatter) RedIntense() Formatter

RedIntense reports Formatter with Red as additional format

func (Formatter) RedIntenseBackground

func (f Formatter) RedIntenseBackground() Formatter

RedIntenseBackground reports Formatter with Red as additional format

func (Formatter) ReverseVideo

func (f Formatter) ReverseVideo() Formatter

ReverseVideo reports Formatter with ReverseVideo as additional format

func (Formatter) Set

func (f Formatter) Set()

Set sets formatting until other formatting function or Clear is called

func (Formatter) Sprint

func (f Formatter) Sprint(a ...interface{}) string

Sprint is colored equivalent of fmt.Sprint

func (Formatter) Sprintf

func (f Formatter) Sprintf(format string, a ...interface{}) string

Sprintf is colored equivalent of fmt.Sprintf

func (Formatter) Sprintln

func (f Formatter) Sprintln(a ...interface{}) string

Sprintln is colored equivalent of fmt.Sprintln

func (Formatter) Underline

func (f Formatter) Underline() Formatter

Underline reports Formatter with Underline as additional format

func (Formatter) White

func (f Formatter) White() Formatter

White reports Formatter with White as additional format

func (Formatter) WhiteBackground

func (f Formatter) WhiteBackground() Formatter

WhiteBackground reports Formatter with White as additional format

func (Formatter) WhiteIntense

func (f Formatter) WhiteIntense() Formatter

WhiteIntense reports Formatter with White as additional format

func (Formatter) WhiteIntenseBackground

func (f Formatter) WhiteIntenseBackground() Formatter

WhiteIntenseBackground reports Formatter with White as additional format

func (Formatter) Yellow

func (f Formatter) Yellow() Formatter

Yellow reports Formatter with Yellow as additional format

func (Formatter) YellowBackground

func (f Formatter) YellowBackground() Formatter

YellowBackground reports Formatter with Yellow as additional format

func (Formatter) YellowIntense

func (f Formatter) YellowIntense() Formatter

YellowIntense reports Formatter with Yellow as additional format

func (Formatter) YellowIntenseBackground

func (f Formatter) YellowIntenseBackground() Formatter

YellowIntenseBackground reports Formatter with Yellow as additional format

Jump to

Keyboard shortcuts

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