cfmt is a small library for simple and convenient formatted stylized output to the console, providing an interface that is exactly the same as the standard fmt
and log
libraries.
go get -v github.com/i582/cfmt
To switch to cfmt
anywhere in the code, it is enough to add one letter c
to fmt
(or to log
), import the required part (cfmt
or clog
):
import (
"github.com/i582/cfmt/cmd/cfmt"
// or
"github.com/i582/cfmt/cmd/clog"
)
and you get all the possibilities, the library is fully compatible with the standard libraries fmt
and log
Let's take an example:
Suppose you need to display text and highlight some word or phrase. Using cfmt
, all that is needed is to wrap the desired part in {{}}
and after ::
write the desired styles:
cfmt.Println("This is a {{red color}}::red")
If we compare this with the gookit/color
library, then this example looks more complicated and less readable there:
fmt.Printf("This is a %s", color.Red.Sprintf("red color"))
It is even more convenient if you need complex styles, if you want to make the text also bold, then all you need to add is in the format after |
write bold
:
cfmt.Println("This is a {{red color}}::red|bold")
In gookit/color
, this is done as follows:
redBold := color.New(color.FgRed, color.OpBold)
fmt.Printf("This is a %s", redBold.Sprintf("red color"))
In the gookit/color
we can reuse the created style, in cfmt
this is also possible. To do this, you need to register a new style using the cfmt.RegisterStyle
function:
cfmt.RegisterStyle("code", func(s string) string {
return cfmt.Sprintf("{{%s}}::red|underline", s)
})
And now it can be used anywhere just by writing its name:
cfmt.Println("This is a {{red color}}::code")
The new style can of course also be used in conjunction with others:
cfmt.Println("This is a {{red color}}::code|bold")
Also you can do whatever you want with the text, which is styled.
cfmt.RegisterStyle("flag", func(s string) string {
return cfmt.Sprintf("{{--%s}}::green (-%c)", s, s[0])
})
flag := "help"
cfmt.Println("{{%s}}::flag ", flag)
If the standard colors are not enough for you, then you can use the colors in the HEX
format (NOTE: not all terminals support all colors fully!).
cfmt.Println("This is a {{red color}}::#ff0000")
To set the background color, you need to add the prefix bg
to the color, in the case of standard colors, the first letter of the color must be capitalized:
cfmt.Println("This is a {{red color}}::bgRed")
For HEX
it will look like this:
cfmt.Println("This is a {{red color}}::bg#ff0000")
cfmt.RegisterStyle("code", func(s string) string {
return cfmt.Sprintf("{{%s}}::red|underline", s)
})
cfmt.Printf(`
{{Example of reports}}::bold
{{ }}::bgRed
{{ Critical errors found }}::bgRed|#ffffff
{{ }}::bgRed
{{100}}::#ffffff myStyle := color.{{New(color.FgWhite, color.BgBlack, color.OpBold)}}::code|bold
{{[100, 17]}}::blue Undefined function New at {{~/projects/test}}::underline:100
{{101}}::#ffffff {{myStyle}}::code.Print("t")
{{[101, 0]}}::blue Undefined variable myStyle at {{~/projects/test}}::underline:101
`)
cfmt.Print(`
{{ -/+:. }}::green
{{ :++++. }}::green
{{ /+++/. }}::green
{{ .:-::- .+/:-''.::- }}::green
{{ .:/++++++/::::/++++++//: }}::green
{{ .:///////////////////////: }}::yellow
{{ //////////////////////// }}::yellow
{{ -+++++++++++++++++++++++ }}::red
{{ /++++++++++++++++++++++/ }}::red
{{ /sssssssssssssssssssssss. }}::red
{{ :ssssssssssssssssssssssss- }}::red
{{ osssssssssssssssssssssssso/ }}::magenta
{{ syyyyyyyyyyyyyyyyyyyyyyyy+ }}::magenta
{{ ossssssssssssssssssssss/ }}::blue
{{ :ooooooooooooooooooo+. }}::blue
{{ :+oo+/:-..-:/+o+/- }}::blue
`)
Styles |
---|
italic |
bold |
underline |
concealed |
reverse |
blink |
Colors | |||
---|---|---|---|
black | darkGray | bgDarkGray | bgBlack |
red | lightRed | bgLightRed | bgRed |
green | lightGreen | bgLightGreen | bgGreen |
yellow | lightYellow | bgLightYellow | bgYellow |
blue | lightBlue | bgLightBlue | bgBlue |
magenta | lightMagenta | bgLightMagent | bgMagenta |
cyan | lightCyan | bgLightCyan | bgCyan |
white | lightWhite | bgLightWhite | bgWhite |
gray | bgGray |
And colors in HEX format. See HEX colors part.
The existing libraries for styling the output are very powerful and this library builds on one of them (gookit/color). However, they are not very useful for styling certain words or sentences, since you need to use Sprintf
and put the styled ones in a format string, which greatly reduces readability if you need to style many elements.
I believe that the library will be useful primarily for formatting ready-made text, for reference, or examples. However, in other cases, it should be just as convenient.
The library aims to make formatted text look readable in code, even with complex formatting.
For any questions — tg: @petr_makhnev
.
This project is under the MIT License. See the LICENSE file for the full license text.