gochalk

package module
v1.0.0 Latest Latest
Warning

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

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

README

GoChalk

A minimalist package to style your terminal output the way you want

Installation

go get github.com/shashankbhat10/GoChalk

Usage

Basic styling

import (
    "github.com/shashankbhat10/gochalk"
)

func main() {
    // Prints a string in red font color
    fmt.Println(gochalk.Red("This is a red string"))

    // Print a string with multiple styles
    fmt.Println(gochalk.StyledString("String with multiple styles", gochalk.FgCyan, gochalk.Bold, gochalk.Underlined))

    // Print a string having mix of styled string
    fmt.Println(gochalk.Green("Green", gochalk.Red("Red String"), "String"))
}

Create Chalk Objects

To reuse any style, create Chalk Objects. Chalk objects are immutable, and new styles being added or removed will return a new chalk object

// Create a Chalk object
chalk := gochalk.NewStyle(gochalk.Bold, gochalk.FgRed)
// Will print a string with styles present in chalk object
fmt.Println(chalk.toString("Bold red string"))

// Will replace red font color with yellow color
boldWarning := chalk.Remove(gochalk.FgRed).Add(gochalk.FgYellow)

// Providing foreground or background colors will replace any existing corresponding color
warning := chalk.Add(gochalk.FgYellow)

// RemoveAll will return a new chalk with all styles removed
normalSuccess := chalk.RemoveAll().Add(gochalk.FgGreen)

// To print string using object, call the Println method of the object
normalSuccess.Println("All tests passed")

Features

  • Support for all basic colors supported in terminals
  • No additional dependencies
  • 100% test coverage

To Do

Add support for 256 color range in supported terminals

Author

Shashank Bhat

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Black

func Black(value ...string) string

Method to print string with black foreground color

func BlackBg

func BlackBg(value ...string) string

Method to print string with black background color

func Blue

func Blue(value ...string) string

Method to print string with blue foreground color

func BlueBg

func BlueBg(value ...string) string

Method to print string with blue background color

func BrightBlack

func BrightBlack(value ...string) string

Method to print string with bright red foreground color

func BrightBlackBg

func BrightBlackBg(value ...string) string

Method to print string with bright black background color

func BrightBlue

func BrightBlue(value ...string) string

Method to print string with bright blue foreground color

func BrightBlueBg

func BrightBlueBg(value ...string) string

Method to print string with bright blue background color

func BrightCyan

func BrightCyan(value ...string) string

Method to print string with bright cyan foreground color

func BrightCyanBg

func BrightCyanBg(value ...string) string

Method to print string with bright cyan background color

func BrightGreen

func BrightGreen(value ...string) string

Method to print string with bright green foreground color

func BrightGreenBg

func BrightGreenBg(value ...string) string

Method to print string with bright green background color

func BrightMagenta

func BrightMagenta(value ...string) string

Method to print string with bright magenta foreground color

func BrightMagentaBg

func BrightMagentaBg(value ...string) string

Method to print string with bright magenta background color

func BrightRed

func BrightRed(value ...string) string

Method to print string with bright red foreground color

func BrightRedBg

func BrightRedBg(value ...string) string

Method to print string with bright red background color

func BrightWhite

func BrightWhite(value ...string) string

Method to print string with bight white foreground color

func BrightWhiteBg

func BrightWhiteBg(value ...string) string

Method to print string with bright white background color

func BrightYellow

func BrightYellow(value ...string) string

Method to print string with bright yellow foreground color

func BrightYellowBg

func BrightYellowBg(value ...string) string

Method to print string with bright yellow background color

func Cyan

func Cyan(value ...string) string

Method to print string with cyan foreground color

func CyanBg

func CyanBg(value ...string) string

Method to print string with cyan background color

func Green

func Green(value ...string) string

Method to print string with green foreground color

func GreenBg

func GreenBg(value ...string) string

Method to print string with green background color

func Magenta

func Magenta(value ...string) string

Method to print string with magenta foreground color

func MagentaBg

func MagentaBg(value ...string) string

Method to print string with magenta background color

func Red

func Red(value ...string) string

Method to print string with red foreground color

func RedBg

func RedBg(value ...string) string

Method to print string with red background color

func StyledString

func StyledString(val string, styles ...Style) string

Method to apply one or more styles to a string. If no style argument is provided then given string is returned as is.

If multiple foreground / background styles are provided then the last corresponding foreground / background will be applied.

examples:

gochalk.StyledString("Hello World", gochalk.FgRed) // Returns string wrapped in red foreground style
gochalk.StyledString("Hello World", gochalk.FgRed, gochalk.FgYellow, gochalk.FgGreen) // only green foreground will be applied
gochalk.StyledString("Hello World", gochalk.FgRed, gochalk.Bold) // Returns string with bold and red styles

func TextBold

func TextBold(value ...string) string

Method to print string with bold styling

func TextDim

func TextDim(value ...string) string

Method to print string with dim styling

func TextItalics

func TextItalics(value ...string) string

Method to print string with italics styling

func TextUnderlined

func TextUnderlined(value ...string) string

Method to print string with underlined styling

func White

func White(value ...string) string

Method to print string with white foreground color

func WhiteBg

func WhiteBg(value ...string) string

Method to print string with white background color

func Yellow

func Yellow(value ...string) string

Method to print string with yellow foreground color

func YellowBg

func YellowBg(value ...string) string

Method to print string with yellow background color

Types

type Chalk

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

func NewStyle

func NewStyle(styles ...Style) *Chalk

Creates a new Chalk object with the provided styles. This object can then be reused to apply required styles to strings If multiple foreground or background colors are provided as parameters, then the last one will be applied

error := gochalk.NewStyle(gochalk.FgRed) // Returns a Chalk object with red foreground style applied

func (*Chalk) Add

func (chalk *Chalk) Add(styles ...Style) *Chalk

Method to add a style to current chalk object. If no parameter given then nothing happens and same object is returned. If Foreground or Background color is provided, it will replace any corresponding foreground / background style If multiple Foreground / Background styles are provided, then only the last corresponding color will be applied

errorChalk := chalk.New(gochalk.FgRed)
errorBold := errorChalk.Add(gochalk.Bold) // Adds Bold style to 'error' chalk object
warningBold := errorBold.Add(gochalk.FgYellow) // Foreground color red is replaced by yellow
colorful := chalk.New(gochalk.FgRed).Add(gochalk.FgYellow, gochalk.FgMagenta) // Multiple colors are provided to Add so magenta is chosen and will replace existing red

func (*Chalk) Println

func (chalk *Chalk) Println(value ...string)

Method to print string provided wrapped in styles present in Chalk

func (*Chalk) Remove

func (chalk *Chalk) Remove(styles ...Style) *Chalk

Method to remove any present styling from Chalk. If given styling is not present then method will do nothing happens. Method will return a new Chalk

errorBold := gochalk.NewStyle(gochalk.FgRed, gochalk.Bold)
errorNormal := gochalk.Remove(gochalk.Bold) // Bold styling will be removed. Will not effect errorBold object as new Chalk is returned

func (*Chalk) RemoveAll

func (chalk *Chalk) RemoveAll() *Chalk

Method to remove all styles applied to Chalk

func (*Chalk) ToString

func (chalk *Chalk) ToString(value ...string) string

Method to get string with formatted style

type Style

type Style int
const (
	FgBlack Style = iota + 30
	FgRed
	FgGreen
	FgYellow
	FgBlue
	FgMagenta
	FgCyan
	FgWhite
)

Foreground color (font colors)

const (
	FgBrightBlack Style = iota + 90
	FgBrightRed
	FgBrightGreen
	FgBrightYellow
	FgBrightBlue
	FgBrightMagenta
	FgBrightCyan
	FgBrightWhite
)

Foreground color (font color) - Bright / Hi-intensity

const (
	BgBlack Style = iota + 40
	BgRed
	BgGreen
	BgYellow
	BgBlue
	BgMagenta
	BgCyan
	BgWhite
)

Background Color

const (
	BgBrightBlack Style = iota + 100
	BgBrightRed
	BgBrightGreen
	BgBrightYellow
	BgBrightBlue
	BgBrightMagenta
	BgBrightCyan
	BgBrightWhite
)

Background Color - Bright / Hi-intensity

const (
	Bold Style = iota + 1
	Dim
	Italics
	Underlined
)

Text display style

Jump to

Keyboard shortcuts

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