fmtc

package
v12.115.0 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2024 License: Apache-2.0 Imports: 9 Imported by: 31

README

fmtc color tags

Editors support

If you are using SublimeText 4 (4075+), we strongly recommend that you install extended Go syntax highlighting with support for fmtc tags.

Modificators

Name Tag Reset Tag Code
Reset {!} 0
Bold {*} {!*} 1
Dim {^} {!^} 2
Italic {&} {!&} 3
Underline {_} {!_} 4
Blink {~} {!~} 5
Reverse {@} {!@} 7
Hidden {+} {!+} 8
Strikethrough {=} {!=} 9

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}

Background

Tag: {%code}

24-bit Colors (TrueColor)

Foreground (Text)

Tag: {#hex}

Background

Tag: {%hex}

Named colors

Tag: {?name}

For more information about named colors see documentation for method NameColor.

Examples

{r*}Important!{!*} File deleted!{!}
 ┬             ┬─                ┬
 │             │                 │
 │             │                 └ Reset everything
 │             │
 │             └ Unset bold modificator
 │
 └ Bold, red text 
{rG*}OMG!{!*} Check your mail{!}
 ┬──      ┬─                  ┬
 │        │                   │
 │        │                   └ Reset everything
 │        │
 │        └ Unset bold modificator
 │
 └ 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
{?error}Can't find user "bob"{!}
 ┬─────                       ┬
 │                            │
 │                            └ Reset everything
 │
 │
 │
 └ Set color to named color "error"

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 ...any) error

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

Example
err := Errorf("This is error")
fmt.Print(err.Error())
Output:

func Fprint

func Fprint(w io.Writer, a ...any) (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.

Example
Fprint(os.Stderr, "{r}This is error message{!}\n")
Fprint(os.Stdout, "{g}This is normal message{!}\n")
Output:

func Fprintf

func Fprintf(w io.Writer, f string, a ...any) (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.

Example
Fprintf(os.Stderr, "{r}%s{!}\n", "This is error message")
Fprintf(os.Stdout, "{g}%s{!}\n", "This is normal message")
Output:

func Fprintln

func Fprintln(w io.Writer, a ...any) (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.

Example
Fprintln(os.Stderr, "{r}This is error message{!}")
Fprintln(os.Stdout, "{g}This is normal message{!}")
Output:

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 IsColorsSupported added in v12.88.0

func IsColorsSupported() bool

IsColorsSupported returns true if 16 colors is supported by terminal

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

func IsTag added in v12.83.0

func IsTag(tag string) bool

IsTag tests whether the given value is a valid color tag (or sequence of tags) and can be encoded into an escape sequence

Example
fmt.Printf("%s is tag: %t\n", "{r}", IsTag("{r}"))
fmt.Printf("%s is tag: %t\n", "[r]", IsTag("[r]"))
Output:

{r} is tag: true
[r] is tag: false

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 LPrint added in v12.50.0

func LPrint(maxSize int, a ...any) (int, error)

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

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

func LPrintf

func LPrintf(maxSize int, f string, a ...any) (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 ...any) (int, error)

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

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

func NameColor added in v12.56.0

func NameColor(name, tag string) error

NameColor defines or redifines named color

Example
// Add new color with name "error"
NameColor("error", "{r}")

// Print a message with named color
Print("{?error}lawngreen text{!}\n")

// Redefine "error" color to 24-bit color
NameColor("error", "{#ff0000}")

// Print a message with new color
Print("{?error}lawngreen text{!}\n")

// Create complex named color: pink, italic & underline
NameColor("notice", "{#ff728a}{_}{&}")

// Print a message with complex color
Print("{?notice}lawngreen text{!}\n")
Output:

func NewLine

func NewLine(num ...int) (int, error)

NewLine prints a newline to standard output

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

func Print

func Print(a ...any) (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
 & Italic
 ^ Dim
 _ Underline
 = Strikethrough
 ~ Blink
 @ Reverse
 + Hidden

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

Named colors:
  ?name
Example
// print colored text
// {!} is tag for style reset
Print("{d}black{!}\n")
Print("{r}red{!}\n")
Print("{y}yellow{!}\n")
Print("{b}blue{!}\n")
Print("{c}cyan{!}\n")
Print("{m}magenta{!}\n")
Print("{g}green{!}\n")
Print("{s}light grey{!}\n")

// use text modificators

// light colors
Print("{r-}light red{!}\n")
Print("{r-}dark grey{!}\n")

// bold + color
Print("{r*}red{!}\n")
Print("{g*}green{!}\n")

// dim
Print("{r^}red{!}\n")
Print("{g^}green{!}\n")

// underline
Print("{r_}red{!}\n")
Print("{g_}green{!}\n")

// blink
Print("{r~}red{!}\n")
Print("{g~}green{!}\n")

// reverse
Print("{r@}red{!}\n")
Print("{g@}green{!}\n")

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

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

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

// modificator reset
Print("{r}{*}red and bold {!*}just red{!}\n")

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

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

// Named colors
// All color names must match the next regex
// pattern: [a-zA-Z0-9_]+

// Add new color with name "error"
NameColor("error", "{r}")

// Print using named color
Print("{?error}lawngreen text{!}\n")

// Redefine "error" color to 24-bit color
NameColor("error", "{#ff0000}")

// Remove named color
RemoveColor("error")
Output:

func Printf

func Printf(f string, a ...any) (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
// print colored text
// {!} is tag for style reset
Printf("{d}%s{!}\n", "black")
Printf("{r}%s{!}\n", "red")
Printf("{y}%s{!}\n", "yellow")
Printf("{b}%s{!}\n", "blue")
Printf("{c}%s{!}\n", "cyan")
Printf("{m}%s{!}\n", "magenta")
Printf("{g}%s{!}\n", "green")
Printf("{s}%s{!}\n", "light grey")

// use text modificators

// light colors
Printf("{r-}%s{!}\n", "light red")
Printf("{r-}%s{!}\n", "dark grey")

// bold + color
Printf("{r*}%s{!}\n", "red")
Printf("{g*}%s{!}\n", "green")

// dim
Printf("{r^}%s{!}\n", "red")
Printf("{g^}%s{!}\n", "green")

// underline
Printf("{r_}%s{!}\n", "red")
Printf("{g_}%s{!}\n", "green")

// blink
Printf("{r~}%s{!}\n", "red")
Printf("{g~}%s{!}\n", "green")

// reverse
Printf("{r@}%s{!}\n", "red")
Printf("{g@}%s{!}\n", "green")

// background color
Printf("{D}%s{!}\n", "black")
Printf("{R}%s{!}\n", "red")
Printf("{Y}%s{!}\n", "yellow")
Printf("{B}%s{!}\n", "blue")
Printf("{C}%s{!}\n", "cyan")
Printf("{M}%s{!}\n", "magenta")
Printf("{G}%s{!}\n", "green")
Printf("{S}%s{!}\n", "light grey")

// many tags at once
// underline, cyan text with the red background
Printf("{cR_}%s{!}\n", "text")

// many tags in once
Printf("{r}{*}%s{!}\n", "red and bold")

// 256 colors (# for foreground, % for background)
Printf("{#201}%s{!}\n", "pink text")
Printf("{%201}%s{!}\n", "pink background")

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

func Println

func Println(a ...any) (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 RemoveColor added in v12.56.0

func RemoveColor(name string)

RemoveColor removes named color

Example
// Add new color with name "error"
NameColor("error", "{r}")

// Print a message with named color
Print("{?error}lawngreen text{!}\n")

// Remove named color
RemoveColor("error")
Output:

func Render added in v12.44.0

func Render(s string) string

Render converts color tags to ANSI escape codes

func Sprint

func Sprint(a ...any) 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.

Example
msg := Sprint("{r}This is error message{!}\n")
fmt.Print(msg)
Output:

func Sprintf

func Sprintf(f string, a ...any) string

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

Example
msg := Sprintf("{r}%s{!}\n", "This is error message")
fmt.Print(msg)
Output:

func Sprintln

func Sprintln(a ...any) 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.

Example
msg := Sprintln("{r}This is error message{!}")
fmt.Print(msg)
Output:

func TLPrint added in v12.50.0

func TLPrint(maxSize int, a ...any) (int, error)

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

Example
// Power of TPrint and LPrint in one method
TLPrint(15, "{s}This is temporary text{!}")
time.Sleep(time.Second)
TLPrint(15, "{*}This message replace previous message after 1 sec{!}")
Output:

func TLPrintf

func TLPrintf(maxSize int, f string, a ...any) (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(15, "{s}%s{!}", "This is temporary text")
time.Sleep(time.Second)
TLPrintf(15, "{*}%s{!}", "This message replace previous message after 1 sec")
Output:

func TLPrintln

func TLPrintln(maxSize int, a ...any) (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

Example
// Power of TPrintln and LPrintln in one method
TLPrintln(15, "{s}This is temporary text{!}")
time.Sleep(time.Second)
TLPrintln(15, "{*}This message replace previous message after 1 sec{!}")
Output:

func TPrint added in v12.50.0

func TPrint(a ...any) (int, error)

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

Example
TPrint("{s}This is temporary text{!}\n")
time.Sleep(time.Second)
TPrint("{*}This message replace previous message after 1 sec{!}\n")
Output:

func TPrintf

func TPrintf(f string, a ...any) (int, error)

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

Example
TPrintf("{s}%s{!}\n", "This is temporary text")
time.Sleep(time.Second)
TPrintf("{*}%s{!}\n", "This message replace previous message after 1 sec")
Output:

func TPrintln

func TPrintln(a ...any) (int, error)

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

Example
TPrintln("{s}This is temporary text{!}")
time.Sleep(time.Second)
TPrintln("{*}This message replace previous message after 1 sec{!}")
Output:

Types

type CondWrapper added in v12.52.0

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

func If added in v12.52.0

func If(cond bool) CondWrapper

If returns wrapper for printing messages if condition is true

Example
userIsAdmin := true

If(userIsAdmin).Println("You are admin!")
Output:

func (CondWrapper) Bell added in v12.52.0

func (cw CondWrapper) Bell()

Bell prints alert (bell) symbol

func (CondWrapper) Fprint added in v12.52.0

func (cw CondWrapper) Fprint(w io.Writer, a ...any) (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 (CondWrapper) Fprintf added in v12.52.0

func (cw CondWrapper) Fprintf(w io.Writer, f string, a ...any) (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 (CondWrapper) Fprintln added in v12.52.0

func (cw CondWrapper) Fprintln(w io.Writer, a ...any) (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 (CondWrapper) LPrint added in v12.52.0

func (cw CondWrapper) LPrint(maxSize int, a ...any) (int, error)

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

func (CondWrapper) LPrintf added in v12.52.0

func (cw CondWrapper) LPrintf(maxSize int, f string, a ...any) (int, error)

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

func (CondWrapper) LPrintln added in v12.52.0

func (cw CondWrapper) LPrintln(maxSize int, a ...any) (int, error)

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

func (CondWrapper) NewLine added in v12.52.0

func (cw CondWrapper) NewLine() (int, error)

NewLine prints a newline to standard output

func (CondWrapper) Print added in v12.52.0

func (cw CondWrapper) Print(a ...any) (int, error)

Print formats using the default formats for its operands and writes to standard output.

func (CondWrapper) Printf added in v12.52.0

func (cw CondWrapper) Printf(f string, a ...any) (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.

func (CondWrapper) Println added in v12.52.0

func (cw CondWrapper) Println(a ...any) (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.

func (CondWrapper) Sprint added in v12.52.0

func (cw CondWrapper) Sprint(a ...any) 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 (CondWrapper) Sprintf added in v12.52.0

func (cw CondWrapper) Sprintf(f string, a ...any) string

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

func (CondWrapper) Sprintln added in v12.52.0

func (cw CondWrapper) Sprintln(a ...any) 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 (CondWrapper) TLPrint added in v12.52.0

func (cw CondWrapper) TLPrint(maxSize int, a ...any) (int, error)

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

func (CondWrapper) TLPrintf added in v12.52.0

func (cw CondWrapper) TLPrintf(maxSize int, f string, a ...any) (int, error)

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

func (CondWrapper) TLPrintln added in v12.52.0

func (cw CondWrapper) TLPrintln(maxSize int, a ...any) (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 (CondWrapper) TPrint added in v12.52.0

func (cw CondWrapper) TPrint(a ...any) (int, error)

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

func (CondWrapper) TPrintf added in v12.52.0

func (cw CondWrapper) TPrintf(f string, a ...any) (int, error)

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

func (CondWrapper) TPrintln added in v12.52.0

func (cw CondWrapper) TPrintln(a ...any) (int, error)

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

Jump to

Keyboard shortcuts

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