gocolor

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2023 License: MIT Imports: 6 Imported by: 0

README

GoColor

pre-commit

Most package seem overkill for my project need so. This package simply provide helper to inject ANSI escape code into the string and return string with injected ANSI code

Windows?

Try https://github.com/mattn/go-colorable

Simple Usage

import "c" github.com/diontr00/gocolor

str :=  Red("hello wolrd").Bold().Underline()
fmt.Println(str)

Global Function

Global Function take the form of

Red(format string , msg ...any) colorStr

if msg is not provided , format is treated as message returned colorStr is a thin wrapper over string to allow chaining method

str := Yellow("hello world").Bold().Underline().BlueBg()
fmt.Println(str)

Text can be style by calling global function

  • {ColorName} for foreground color method , eg: Red() , Black(), ...

  • {ColorName}Bg for background color method , eg: RedBg() , BlackBg()...

  • {Bold | Dim | Underline | StrikeOut | Inverse | Italic | Hidden} for text base method

  • Hi{ColorName}{Bg} for high intensity version of color , eg: HiBlue(), HiBlackBg(),...

Color Instance method

Let you defined the set of property on color object ,so that it can be reuse and extend

  • Color property can be add by either using property constant or by calling method on Color object
  1. Using Constant Constant can be found below
c := color.New(BoldText, FMagenta,  BCyan , UnderlineText,...)
  1. Using Method
c := color.New().Red().Bold()
// Or with Add method and constant
c.Add(BoldText , ....)

  • c.Sprintf and c.Sprint can be used to return colorStr to be Print out
// c := color.New(...property) colorStr
str1 := c.Sprint("hello world")
str2 := c.SPrintf("%s" , "hello wolrd")
fmt.Println(str1 , str2)
  • color object can be derived from other
pass := color.New(FGreen)
fail :=  pass.Derived(Fred).Bold()

pass.Sprint("Pass")
fail.Sprint("Fail")

Caution : since i want this package to be simple , so the later added property will add extra srting to the colored string, and not replace the old one. I dont think it will be the proplem, since most text on the terminal shouldn't be that complex in nested style

Available Properties

  • Base : BoldText , DimText , ItalicText , UnderlineText , InverseText , HiddenText , StrikeOut
  • Foreground Color : F{Color}
  • Background Color : B{Color}
  • HighIntensity Version : {F|B}{Color}.High()
  • Reset Base : {Base}.Reset()
Low Intensity Foreground

LowIntensity Foreground

High Intensity Foreground

HighIntensity Foreground

Low Intensity Background

LowIntensity Background

High Intensity Background

HighIntensity Background

Base

Base

Documentation

Overview

Most color package seem overkill for my project need.This package simply provide helper to inject ANSI escape code into the string and return string with injected ANSI code. Then you decide where to print the string afterward , if it detect your terminal isn't able to print color , no color will be inject The API can be used in several way. Pick the one that suit your project

Use global default helper function to set either background or foreground color

Red("Hello World")
Blue("Hello World")
Magenta("Hello World")
Green("Hello World")
Yellow("Hello World")
White("Hello World")
Cyan("Hello World")

RedBg("Hello World")
BlueBg("Hello World")
MagentaBg("Hello World")
GreenBg("Hello World")
YellowBg("Hello World")
WhiteBg("Hello World")
CyanBg("Hello World")

When multiple style need to be apply , method can be chain

Red("Hello World").BlueBg()
Green("Hello World").Bold()
Yellow("Hello World").Underline().Bold().Italic()

high intensity also available with {Hi} started

HiYellow(...)

other base method also available

Bold("Hello World")
Underline("Hello World")
StrikeOut("Hello World")
Italic("Hello World")
Dim("Hello World")
Hidden("Hello World")
Inverse("Hello World")

there are timewhen custom color mixed is required. Then you can create color object , derive from color object ,...

You can add style by either using property attributes , which is available below

New(FRed, BoldText).Sprint("hello wolrd")

Or by using method

New().Blue().RedBg().Sprintf("hello world")

Derived from base object

c :=  New(BoldText , UnderlineText)
pass := c.Derive(FGreen) //  with green as foreground color
fail := c.Derive(FRed) //  with red as foreground color

pass.Sprintf("ok")
fail.Sprintf("fail").DimText()

## Available Properties

- **Base** : BoldText , DimText , ItalicText , UnderlineText , InverseText , HiddenText , StrikeOut - **Foreground Color** : F{Color} - **Background Color** : B{Color} - **HighIntensity Version** : {F|B}{Color}.High() - **Reset Base** : {Base}.Reset()

Index

Constants

View Source
const (

	// bold.
	BoldText property
	// Dim.
	DimText
	// italic.
	ItalicText
	// underline.
	UnderlineText
	// InverseText.
	InverseText property = iota + 2
	// HiddenText.
	HiddenText
	// StrikeOutText.
	StrikeOutText
)

Base.

View Source
const (
	// Black.
	FBlack property = iota + 30

	// Red.
	FRed

	// Green.
	FGreen

	// Yellow.
	FYellow

	// Blue.
	FBlue

	// Magenta.
	FMagenta

	//  Cyan.
	FCyan

	// White.
	FWhite
)

FgColor.

View Source
const (

	// Black.
	BBlack property = iota + 40

	// Red.
	BRed

	// Green.
	BGreen

	// Yellow.
	BYellow

	// Blue.
	BBlue

	// Magenta.
	BMagenta

	// Cyan.
	BCyan

	// White.
	BWhite
)

BgColor.

Variables

This section is empty.

Functions

func Black

func Black(format string, msg ...interface{}) colorStr

Change text color to black.

func BlackBg

func BlackBg(format string, msg ...interface{}) colorStr

Change background color to black.

func Blue

func Blue(format string, msg ...interface{}) colorStr

Change text color to blue.

func BlueBg

func BlueBg(format string, msg ...interface{}) colorStr

Change background color to blue.

func Bold

func Bold(format string, msg ...interface{}) colorStr

Apply bold text style.

func Cyan

func Cyan(format string, msg ...interface{}) colorStr

Change text color to cyan.

func CyanBg

func CyanBg(format string, msg ...interface{}) colorStr

Change background color to cyan.

func Dim

func Dim(format string, msg ...interface{}) colorStr

Apply dim text style.

func Green

func Green(format string, msg ...interface{}) colorStr

Change text color to green.

func GreenBg

func GreenBg(format string, msg ...interface{}) colorStr

Change background color to green.

func HiBlack

func HiBlack(format string, msg ...interface{}) colorStr

Change text color to high-intensity black.

func HiBlackBg

func HiBlackBg(format string, msg ...interface{}) colorStr

Change background color to high-intensity black.

func HiBlue

func HiBlue(format string, msg ...interface{}) colorStr

Change text color to high-intensity blue.

func HiBlueBg

func HiBlueBg(format string, msg ...interface{}) colorStr

Change background color to high-intensity blue.

func HiCyan

func HiCyan(format string, msg ...interface{}) colorStr

Change text color to high-intensity cyan.

func HiCyanBg

func HiCyanBg(format string, msg ...interface{}) colorStr

Change background color to high-intensity cyan.

func HiGreen

func HiGreen(format string, msg ...interface{}) colorStr

Change text color to high-intensity green.

func HiGreenBg

func HiGreenBg(format string, msg ...interface{}) colorStr

Change background color to high-intensity green.

func HiMagenta

func HiMagenta(format string, msg ...interface{}) colorStr

Change text color to high-intensity magenta.

func HiMagentaBg

func HiMagentaBg(format string, msg ...interface{}) colorStr

func HiRed

func HiRed(format string, msg ...interface{}) colorStr

Change text color to high-intensity red.

func HiRedBg

func HiRedBg(format string, msg ...interface{}) colorStr

Change background color to high-intensity red.

func HiWhite

func HiWhite(format string, msg ...interface{}) colorStr

Change text color to high-intensity white.

func HiWhiteBg

func HiWhiteBg(format string, msg ...interface{}) colorStr

Change background color to high-intensity white.

func HiYellow

func HiYellow(format string, msg ...interface{}) colorStr

Change text color to high-intensity yellow.

func HiYellowBg

func HiYellowBg(format string, msg ...interface{}) colorStr

Change background color to high-intensity yellow.

func Hidden

func Hidden(format string, msg ...interface{}) colorStr

Apply hidden text style.

func Inverse

func Inverse(format string, msg ...interface{}) colorStr

Apply inverse text style.

func Italic

func Italic(format string, msg ...interface{}) colorStr

Apply italic text style.

func Magenta

func Magenta(format string, msg ...interface{}) colorStr

Change text color to magenta.

func MagentaBg

func MagentaBg(format string, msg ...interface{}) colorStr

Change background color to magenta.

func New

func New(properties ...property) *color

New color instance , properties can be added now or later.

func Red

func Red(format string, msg ...interface{}) colorStr

Change text color to red.

func RedBg

func RedBg(format string, msg ...interface{}) colorStr

Change background color to red.

func StrikeOut

func StrikeOut(format string, msg ...interface{}) colorStr

Apply strikeout text style.

func Underline

func Underline(format string, msg ...interface{}) colorStr

Apply underline text style.

func White

func White(format string, msg ...interface{}) colorStr

Change text color to white.

func WhiteBg

func WhiteBg(format string, msg ...interface{}) colorStr

Change background color to white.

func Yellow

func Yellow(format string, msg ...interface{}) colorStr

Change text color to yellow.

func YellowBg

func YellowBg(format string, msg ...interface{}) colorStr

Change background color to yellow.

Types

This section is empty.

Jump to

Keyboard shortcuts

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