ProductPromotion
Logo

Go.Lang

made by https://0x3d.site

GitHub - i582/cfmt: Small library for simple and convenient formatted stylized output to the console.
Small library for simple and convenient formatted stylized output to the console. - i582/cfmt
Visit Site

GitHub - i582/cfmt: Small library for simple and convenient formatted stylized output to the console.

GitHub - i582/cfmt: Small library for simple and convenient formatted stylized output to the console.

Build Status Codecov Go Report Card GitHub license Mentioned in Awesome Go

cfmt

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.

Table of Contents

Install

go get -v github.com/i582/cfmt

Usage

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

Simple usage

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"))

Complex style

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"))

Custom styles

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)

HEX colors

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")

Background color

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")

Other examples

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
`)

Supported colors and styles

Styles
italic
bold
crossout
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.

Motivation

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.

Contact

For any questions โ€” tg: @petr_makhnev.

License

This project is under the MIT License. See the LICENSE file for the full license text.

Articles
to learn more about the golang concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

Queries
or most google FAQ's about GoLang.

mail [email protected] to add more queries here ๐Ÿ”.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory