Documentation ¶
Overview ¶
Package fmtc provides methods similar to fmt for colored output
Index ¶
- Variables
- func AddColor(name, tag string) error
- func Bell()
- func Clean(s string) string
- func Errorf(f string, a ...any) error
- func Fprint(w io.Writer, a ...any) (int, error)
- func Fprintf(w io.Writer, f string, a ...any) (int, error)
- func Fprintfn(w io.Writer, f string, a ...any) (int, error)
- func Fprintln(w io.Writer, a ...any) (int, error)
- func Is256ColorsSupported() bool
- func IsColorsSupported() bool
- func IsTag(tag string) bool
- func IsTrueColorSupported() bool
- func LPrint(maxSize int, a ...any) (int, error)
- func LPrintf(maxSize int, f string, a ...any) (int, error)
- func LPrintfn(maxSize int, f string, a ...any) (int, error)
- func LPrintln(maxSize int, a ...any) (int, error)
- func NameColor(name, tag string) errordeprecated
- func NewLine(num ...int) (int, error)
- func Print(a ...any) (int, error)
- func Printf(f string, a ...any) (int, error)
- func Printfn(f string, a ...any) (int, error)
- func Println(a ...any) (int, error)
- func RemoveColor(name string)
- func Render(s string) string
- func Sprint(a ...any) string
- func Sprintf(f string, a ...any) string
- func Sprintfn(f string, a ...any) string
- func Sprintln(a ...any) string
- func TLPrint(maxSize int, a ...any) (int, error)
- func TLPrintf(maxSize int, f string, a ...any) (int, error)
- func TLPrintln(maxSize int, a ...any) (int, error)
- func TPrint(a ...any) (int, error)
- func TPrintf(f string, a ...any) (int, error)
- func TPrintln(a ...any) (int, error)
- type CondWrapper
- func (cw CondWrapper) Bell()
- func (cw CondWrapper) Fprint(w io.Writer, a ...any) (int, error)
- func (cw CondWrapper) Fprintf(w io.Writer, f string, a ...any) (int, error)
- func (cw CondWrapper) Fprintfn(w io.Writer, f string, a ...any) (int, error)
- func (cw CondWrapper) Fprintln(w io.Writer, a ...any) (int, error)
- func (cw CondWrapper) LPrint(maxSize int, a ...any) (int, error)
- func (cw CondWrapper) LPrintf(maxSize int, f string, a ...any) (int, error)
- func (cw CondWrapper) LPrintfn(maxSize int, f string, a ...any) (int, error)
- func (cw CondWrapper) LPrintln(maxSize int, a ...any) (int, error)
- func (cw CondWrapper) NewLine() (int, error)
- func (cw CondWrapper) Print(a ...any) (int, error)
- func (cw CondWrapper) Printf(f string, a ...any) (int, error)
- func (cw CondWrapper) Printfn(f string, a ...any) (int, error)
- func (cw CondWrapper) Println(a ...any) (int, error)
- func (cw CondWrapper) Sprint(a ...any) string
- func (cw CondWrapper) Sprintf(f string, a ...any) string
- func (cw CondWrapper) Sprintfn(f string, a ...any) string
- func (cw CondWrapper) Sprintln(a ...any) string
- func (cw CondWrapper) TLPrint(maxSize int, a ...any) (int, error)
- func (cw CondWrapper) TLPrintf(maxSize int, f string, a ...any) (int, error)
- func (cw CondWrapper) TLPrintln(maxSize int, a ...any) (int, error)
- func (cw CondWrapper) TPrint(a ...any) (int, error)
- func (cw CondWrapper) TPrintf(f string, a ...any) (int, error)
- func (cw CondWrapper) TPrintln(a ...any) (int, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DisableColors = os.Getenv("NO_COLOR") != ""
DisableColors disables all colors and modificators in output
Functions ¶
func Clean ¶
Clean returns string without color tags
Example ¶
// Remove color tags from text fmt.Println(Clean("{r}Text{!}"))
Output: Text
func Errorf ¶
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 ¶
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 ¶
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 Fprintfn ¶ added in v13.14.0
Fprintfn formats according to a format specifier and writes to w with the newline at the end. It returns the number of bytes written and any write error encountered.
Example ¶
Fprintfn(os.Stderr, "{r}%s{!}", "This is error message") Fprintfn(os.Stdout, "{g}%s{!}", "This is normal message")
Output:
func Fprintln ¶
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 ¶
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 ¶
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 ¶
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 ¶
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{!}\n", "text")
Output:
func LPrintfn ¶ added in v13.14.0
LPrintfn formats according to a format specifier and writes to standard output limited by the text size and with the newline at the end
Example ¶
// Only "This is text" will be shown LPrintfn(12, "{r}This is %s {g} with colors{!}", "text")
Output:
func LPrintln ¶
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
deprecated
NameColor defines or redifines named color
Deprecated: Use method AddColor instead
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 ¶
NewLine prints a newline to standard output
Example ¶
// just print a new line NewLine() // Print 3 new lines NewLine(3)
Output:
func Print ¶
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 ¶
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 Printfn ¶ added in v13.14.0
Printfn formats according to a format specifier and writes to standard output with the new line at the end. It returns the number of bytes written and any write error encountered.
Example ¶
// print colored text // {!} is tag for style reset Printfn("{d}%s{!}", "black") Printfn("{r}%s{!}", "red") Printfn("{y}%s{!}", "yellow") Printfn("{b}%s{!}", "blue") Printfn("{c}%s{!}", "cyan") Printfn("{m}%s{!}", "magenta") Printfn("{g}%s{!}", "green") Printfn("{s}%s{!}", "light grey") // use text modificators // light colors Printfn("{r-}%s{!}", "light red") Printfn("{r-}%s{!}", "dark grey") // bold + color Printfn("{r*}%s{!}", "red") Printfn("{g*}%s{!}", "green") // dim Printfn("{r^}%s{!}", "red") Printfn("{g^}%s{!}", "green") // underline Printfn("{r_}%s{!}", "red") Printfn("{g_}%s{!}", "green") // blink Printfn("{r~}%s{!}", "red") Printfn("{g~}%s{!}", "green") // reverse Printfn("{r@}%s{!}", "red") Printfn("{g@}%s{!}", "green") // background color Printfn("{D}%s{!}", "black") Printfn("{R}%s{!}", "red") Printfn("{Y}%s{!}", "yellow") Printfn("{B}%s{!}", "blue") Printfn("{C}%s{!}", "cyan") Printfn("{M}%s{!}", "magenta") Printfn("{G}%s{!}", "green") Printfn("{S}%s{!}", "light grey") // many tags at once // underline, cyan text with the red background Printfn("{cR_}%s{!}", "text") // many tags in once Printfn("{r}{*}%s{!}", "red and bold") // 256 colors (# for foreground, % for background) Printfn("{#201}%s{!}", "pink text") Printfn("{%201}%s{!}", "pink background") // 24-bit colors (# for foreground, % for background) Printfn("{#7cfc00}%s{!}", "lawngreen text") Printfn("{%6a5acd}%s{!}", "slateblue background")
Output:
func Println ¶
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 ¶
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 Sprint ¶
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 ¶
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 Sprintfn ¶ added in v13.14.0
Sprintfn formats according to a format specifier and returns the resulting string with the newline at the end.
Example ¶
msg := Sprintfn("{r}%s{!}", "This is error message") fmt.Print(msg)
Output:
func Sprintln ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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:
Types ¶
type CondWrapper ¶
type CondWrapper bool
func If ¶
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) Fprint ¶
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 ¶
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) Fprintfn ¶ added in v13.14.0
Fprintfn formats according to a format specifier and writes to w with the newline at the end. It returns the number of bytes written and any write error encountered.
func (CondWrapper) Fprintln ¶
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 ¶
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 ¶
LPrintf formats according to a format specifier and writes to standard output limited by the text size
func (CondWrapper) LPrintfn ¶ added in v13.14.0
LPrintfn formats according to a format specifier and writes to standard output limited by the text size and with the newline at the end
func (CondWrapper) LPrintln ¶
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 ¶
func (cw CondWrapper) NewLine() (int, error)
NewLine prints a newline to standard output
func (CondWrapper) Print ¶
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 ¶
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) Printfn ¶ added in v13.14.0
func (cw CondWrapper) Printfn(f string, a ...any) (int, error)
Printfn formats according to a format specifier and writes to standard output with the new line at the end. It returns the number of bytes written and any write error encountered.
func (CondWrapper) Println ¶
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 ¶
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 ¶
func (cw CondWrapper) Sprintf(f string, a ...any) string
Sprintf formats according to a format specifier and returns the resulting string.
func (CondWrapper) Sprintfn ¶ added in v13.14.0
func (cw CondWrapper) Sprintfn(f string, a ...any) string
Sprintfn formats according to a format specifier and returns the resulting string with the newline at the end.
func (CondWrapper) Sprintln ¶
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 ¶
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 ¶
TLPrintf removes all content on the current line and prints the new message limited by the text size
func (CondWrapper) TLPrintln ¶
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 ¶
func (cw CondWrapper) TPrint(a ...any) (int, error)
TPrint removes all content on the current line and prints the new message