fmtc

package
v12.41.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2022 License: Apache-2.0 Imports: 8 Imported by: 156

README

fmtc color tags

Modificators

Name Tag Code
Reset {!} 0
Bold {*} 1
Dim {^} 2
Underline {_} 4
Blink {~} 5
Reverse {@} 7

8/16 Colors

Foreground (Text)
Name Tag Code Color Preview
Black {d} 30
Red {r} 31
Green {g} 32
Yellow {y} 33
Blue {b} 34
Magenta {m} 35
Cyan {c} 36
Light gray {s} 37
Dark gray {s-} 90
Light red {r-} 91
Light green {g-} 92
Light yellow {y-} 93
Light blue {b-} 94
Light magenta {m-} 95
Light cyan {c-} 96
White {w-} 97
Background
Name Tag Code Color Preview
Black {D} 40
Red {R} 41
Green {G} 42
Yellow {Y} 43
Blue {B} 44
Magenta {M} 45
Cyan {C} 46
Light gray {S} 47
Dark gray {S-} 100
Light red {R-} 101
Light green {G-} 102
Light yellow {Y-} 103
Light blue {B-} 104
Light magenta {M-} 105
Light cyan {C-} 106
White {W-} 107

88/256 Colors

Foreground (Text)

Tag: {#code}

Image from FLOZz' MISC website

Background

Tag: {%code}

Image from FLOZz' MISC website

24-bit Colors (TrueColor)

Foreground (Text)

Tag: {#hex}

Background

Tag: {%hex}

Examples

{r*}Important!{!*} File deleted!{!}
 ┬             ┬─                ┬
 │             │                 │
 │             │                 └ Reset everything
 │             │
 │             └ Unset bold modificator
 │
 └ Bold, red text 
{rG*}OMG!{!} Check your mail{!}
 ┬──                         ┬
 │                           │
 │                           └ Reset everything
 │
 └ Bold, red text with green background
{r}File {_}file.log{!_} deleted{!}
 ┬       ┬          ┬─          ┬
 │       │          │           │ 
 │       │          │           └ Reset everything
 │       │          │
 │       │          └ Unset underline modificator
 │       │
 │       └ Set underline modificator
 │
 └ Set text color to red
{*@y}Warning!{!@} Can't find user bob.{!}
 ┬──          ┬─                       ┬
 │            │                        │
 │            │                        └ Reset everything
 │            │
 │            └ Unset reverse modificator (keep yellow color)
 │
 └ Bold text with yellow background (due to reverse modificator)
{#213}{%240}Hi all!{!}
 ┬───  ┬───         ┬
 │     │            │
 │     │            └ Reset everything
 │     │
 │     └ Set background color to grey
 │
 └ Set text color to pink
{#ff1493}{%191970}Hi all!{!}
 ┬──────  ┬──────         ┬
 │        │               │
 │        │               └ Reset everything
 │        │
 │        └ Set background color to midnightblue
 │
 └ Set text color to deeppink

Documentation

Overview

Package fmtc provides methods similar to fmt for colored output

Index

Examples

Constants

This section is empty.

Variables

View Source
var DisableColors = os.Getenv("NO_COLOR") != ""

DisableColors disables all colors and modificators in output

Functions

func Bell

func Bell()

Bell prints alert (bell) symbol

Example
// terminal bell
Bell()
Output:

func Clean

func Clean(s string) string

Clean returns string without color tags

Example
// Remove color tags from text
fmt.Println(Clean("{r}Text{!}"))
Output:

Text

func Errorf

func Errorf(f string, a ...interface{}) error

Errorf formats according to a format specifier and returns the string as a value that satisfies error.

func Fprint

func Fprint(w io.Writer, a ...interface{}) (int, error)

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, f string, a ...interface{}) (int, error)

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, a ...interface{}) (int, error)

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.

func Is256ColorsSupported

func Is256ColorsSupported() bool

Is256ColorsSupported returns true if 256 colors is supported by terminal

Example
fmt.Printf("256 Colors Supported: %t\n", Is256ColorsSupported())
Output:

func IsTrueColorSupported

func IsTrueColorSupported() bool

IsTrueColorSupported returns true if TrueColor (24-bit colors) is supported by terminal

Example
fmt.Printf("TrueColor Supported: %t\n", IsTrueColorSupported())
Output:

func LPrintf

func LPrintf(maxSize int, f string, a ...interface{}) (int, error)

LPrintf formats according to a format specifier and writes to standard output limited by the text size

Example
// Only "This is text" will be shown
LPrintf(12, "{r}This is %s {g} with colors{!}", "text")
Output:

func LPrintln

func LPrintln(maxSize int, a ...interface{}) (int, error)

LPrintln formats using the default formats for its operands and writes to standard output limited by the text size

func NewLine

func NewLine() (int, error)

NewLine prints a newline to standard output

Example
// just print a new line
NewLine()
Output:

func Print

func Print(a ...interface{}) (int, error)

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

Supported color codes:

Modificators:
 - Light colors
 ! Default
 * Bold
 ^ Dim
 _ Underline
 ~ Blink
 @ Reverse

Foreground colors:
 d Black (Dark)
 r Red
 g Green
 y Yellow
 b Blue
 m Magenta
 c Cyan
 s Gray (Smokey)
 w White

Background colors:
 D Black (Dark)
 R Red
 G Green
 Y Yellow
 B Blue
 M Magenta
 C Cyan
 S Gray (Smokey)
 W White

256 colors:
 #code foreground color
 %code background color

24-bit colors (TrueColor):
  #hex foreground color
  %hex background color

func Printf

func Printf(f string, a ...interface{}) (int, error)

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

Example
var (
	user         = "Bob"
	isUserOnline = true
)

if isUserOnline {
	Printf("User {c}%s{!} is {g}online{!}\n", user)
} else {
	Printf("User {c}%s{!} is {r}offline{!}\n", user)
}
Output:

func Println

func Println(a ...interface{}) (int, error)

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

Example
// print colored text
// {!} is tag for style reset
Println("{d}black{!}")
Println("{r}red{!}")
Println("{y}yellow{!}")
Println("{b}blue{!}")
Println("{c}cyan{!}")
Println("{m}magenta{!}")
Println("{g}green{!}")
Println("{s}light grey{!}")

// use text modificators

// light colors
Println("{r-}light red{!}")
Println("{r-}dark grey{!}")

// bold + color
Println("{r*}red{!}")
Println("{g*}green{!}")

// dim
Println("{r^}red{!}")
Println("{g^}green{!}")

// underline
Println("{r_}red{!}")
Println("{g_}green{!}")

// blink
Println("{r~}red{!}")
Println("{g~}green{!}")

// reverse
Println("{r@}red{!}")
Println("{g@}green{!}")

// background color
Println("{D}black{!}")
Println("{R}red{!}")
Println("{Y}yellow{!}")
Println("{B}blue{!}")
Println("{C}cyan{!}")
Println("{M}magenta{!}")
Println("{G}green{!}")
Println("{S}light grey{!}")

// many tags at once
// underline, cyan text with the red background
Println("{cR_}text{!}")

// many tags in once
Println("{r}{*}red and bold{!}")

// modificator reset
Println("{r}{*}red and bold {!*}just red{!}")

// 256 colors (# for foreground, % for background)
Println("{#201}pink text{!}")
Println("{%201}pink background{!}")

// 24-bit colors (# for foreground, % for background)
Println("{#7cfc00}lawngreen text{!}")
Println("{%6a5acd}slateblue background{!}")
Output:

func Sprint

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

Sprint formats using the default formats for its operands and returns the resulting string. Spaces are added between operands when neither is a string.

func Sprintf

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

Sprintf formats according to a format specifier and returns the resulting string.

func Sprintln

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

Sprintln formats using the default formats for its operands and returns the resulting string. Spaces are always added between operands and a newline is appended.

func TLPrintf

func TLPrintf(maxSize int, f string, a ...interface{}) (int, error)

TLPrintf removes all content on the current line and prints the new message limited by the text size

Example
// Power of TPrintf and LPrintf in one method
TLPrintf(22, "This is temporary text")
time.Sleep(time.Second)
TLPrintf(22, "This message will replace previous message after 1 sec")
Output:

func TLPrintln

func TLPrintln(maxSize int, a ...interface{}) (int, error)

TPrintln removes all content on the current line and prints the new message limited by the text size with a new line symbol at the end

func TPrintf

func TPrintf(f string, a ...interface{}) (int, error)

TPrintf removes all content on the current line and prints the new message

Example
TPrintf("This is temporary text")
time.Sleep(time.Second)
TPrintf("This message replace previous message after 1 sec")
Output:

func TPrintln

func TPrintln(a ...interface{}) (int, error)

TPrintln removes all content on the current line and prints the new message with a new line symbol at the end

Types

This section is empty.

Directories

Path Synopsis
Package lscolors provides methods for colorizing file names based on colors from dircolors
Package lscolors provides methods for colorizing file names based on colors from dircolors

Jump to

Keyboard shortcuts

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