fmtc

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2021 License: MIT Imports: 7 Imported by: 5

README

go-terminal-colors

Helpful utility to print colored output in the terminal.

Quick Start

To keep up to date with the most recent version:

go get github.com/jucardi/go-terminal-colors

To get a specific version:

go get gopkg.in/jucardi/go-terminal-colors.v1
Using the library

Supports two modalities, using the fmt function signatures with fmtc or creating a new IColorFlow which allows printing messages with chaining functions.

The fmt way

To print a message with colors using the fmt way, start by using fmtc.WithColors(colors...) which allows to pass multiple predefined terminal colors in the fmtc package, followed by the fmt function would use.

Example

fmtc.WithColors(fmtc.White, fmtc.Bold, fmtc.BgRed).Println("An error has ocurred!")
Using the color flow

The color flow allows using a custom io.Writer, or if not provided, it will generate the resulting string in memory which can then be retrieve by invoking the String() function.

Example using Stdout as the io.Writer

fmtc.New(os.Stdout).
	Print(" ERROR ", fmtc.White, fmtc.Bold, fmtc.BgRed).
	Print(" ").
	Print(timestamp, fmtc.Cyan).
	Print(" ").
	Println(err.Error)

Same example but using an in-memory writer to obtain the resulting string

result := fmtc.New().
	Print(" ERROR ", fmtc.White, fmtc.Bold, fmtc.BgRed).
	Print(" ").
	Print(timestamp, fmtc.Cyan).
	Print(" ").
	Println(err.Error).
	String()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fprint

func Fprint(w io.Writer, str interface{}, colors ...Color)

Fprint formats using the default formats for its operands and writes to w. Spaces are added between operands when neither is a string. It returns the number of bytes written and any write error encountered.

func Fprintf

func Fprintf(w io.Writer, format string, args Args, colors ...Color)

Fprintf formats according to a format specifier and writes to w. It returns the number of bytes written and any write error encountered.

func Fprintln

func Fprintln(w io.Writer, str interface{}, colors ...Color)

Fprintln formats using the default formats for its operands and writes to w. Spaces are always added between operands and a newline is appended. It returns the number of bytes written and any write error encountered.

Types

type Args

type Args []interface{}

Args is an alias of []interface{}

type Color

type Color int
const (
	Clear Color = iota
	Bold
	Dim
	Italic
	Underline
	Inverted
	Hidden
)

special formats

const (
	Black Color = 30 + iota
	Red
	Green
	Yellow
	Blue
	Magenta
	Cyan
	Gray
)

foreground

const (
	DarkGray Color = 90 + iota
	LightRed
	LightGreen
	LightYellow
	LightBlue
	LightMagenta
	LightCyan
	White
)
const (
	BgBlack Color = 40 + iota
	BgRed
	BgGreen
	BgYellow
	BgBlue
	BgMagenta
	BgCyan
	BgGray
)

background

const (
	BgDarkGray Color = 100 + iota
	BgLightRed
	BgLightGreen
	BgLightYellow
	BgLightBlue
	BgLightMagenta
	BgLightCyan
	BgWhite
)

func Parse

func Parse(color string) (Color, error)

Parse attempts to parse a string representing a predefined color.

type IColorFlow

type IColorFlow interface {
	String() string
	Print(str interface{}, colors ...Color) IColorFlow
	PrintLn(str interface{}, colors ...Color) IColorFlow
	Printf(format string, args Args, colors ...Color) IColorFlow
}

func New

func New(writer ...io.Writer) IColorFlow

New creates a new IColorFlow

- writer: (Optional) An io.Writer where the messages will be written. If using a writer, `String()` is not supported.

type IFmt

type IFmt interface {
	Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error)
	Printf(format string, a ...interface{}) (n int, err error)
	Sprintf(format string, a ...interface{}) string
	Errorf(format string, a ...interface{}) error
	Fprint(w io.Writer, a ...interface{}) (n int, err error)
	Print(a ...interface{}) (n int, err error)
	Sprint(a ...interface{}) string
	Fprintln(w io.Writer, a ...interface{}) (n int, err error)
	Println(a ...interface{}) (n int, err error)
	Sprintln(a ...interface{}) string
}

func WithColors

func WithColors(colors ...Color) IFmt

WithColors is an easy way to use the same function signature presented in the 'fmt' to print color messages.

Eg:   fmtc.WithColors(fmtc.White, fmtc.BgRed).Println(" danger! ")

Jump to

Keyboard shortcuts

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