Documentation ¶
Overview ¶
Package fmtc provides methods similar to fmt for colored output
Index ¶
- Variables
- 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 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 LPrintln(maxSize int, a ...any) (int, error)
- func NameColor(name, tag string) error
- func NewLine(num ...int) (int, error)
- func Print(a ...any) (int, error)
- func Printf(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 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) 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) 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) Println(a ...any) (int, error)
- func (cw CondWrapper) Sprint(a ...any) string
- func (cw CondWrapper) Sprintf(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 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 ¶ 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
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
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{!}", "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 ¶ added in v12.56.0
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 ¶
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 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 ¶ 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 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 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 ¶ added in v12.50.0
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 ¶ added in v12.50.0
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 ¶ 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
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
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
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
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
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