All Projects → i582 → cfmt

i582 / cfmt

Licence: MIT license
Small library for simple and convenient formatted stylized output to the console.

Programming Languages

go
31211 projects - #10 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to cfmt

react-native-styled-text
Styled Text for React Native
Stars: ✭ 57 (+23.91%)
Mutual labels:  color, format
color-loader
🎨 A webpack loader that extracts the color palette of an image
Stars: ✭ 14 (-69.57%)
Mutual labels:  color
colour
Validate colours.
Stars: ✭ 31 (-32.61%)
Mutual labels:  color
imageman
Image manipulation library. Use Pixie instead.
Stars: ✭ 58 (+26.09%)
Mutual labels:  color
JetBrains-scheme
JetBrains主题,更完美的高亮。支持 IntelliJ IDEA、phpstorm、goland、webstorm
Stars: ✭ 25 (-45.65%)
Mutual labels:  color
prism
Colour management for Go
Stars: ✭ 26 (-43.48%)
Mutual labels:  color
ColorBlender
A .NET library for color matching and palette design.
Stars: ✭ 27 (-41.3%)
Mutual labels:  color
SheetyColors
An action sheet styled color picker for iOS.
Stars: ✭ 101 (+119.57%)
Mutual labels:  color
Height-Based-Gradient-Color-Shaders-for-Unity
Height Based 2 color gradient shaders for Unity
Stars: ✭ 18 (-60.87%)
Mutual labels:  color
colorama
A Gem for extracting the most prevalent colors from an image
Stars: ✭ 20 (-56.52%)
Mutual labels:  color
XYColor
An easy way to adapter dark mode on CALayer. iOS 快速适配夜间模式
Stars: ✭ 76 (+65.22%)
Mutual labels:  color
shape-color-dector
opencv检擦形状和颜色
Stars: ✭ 17 (-63.04%)
Mutual labels:  color
console-logging
Better, prettier commandline logging for Python--with colors! 👻
Stars: ✭ 111 (+141.3%)
Mutual labels:  color
Bali
A fast and lightweight .Net library for reading and writing .class files.
Stars: ✭ 35 (-23.91%)
Mutual labels:  format
global-color-picker
start the script and click anywhere to get rgb value at the cursor location
Stars: ✭ 31 (-32.61%)
Mutual labels:  color
format-date
📆 A small library (around 400 B when gziped & minified) to format JavaScript `Date` object using same tokens as moment.
Stars: ✭ 25 (-45.65%)
Mutual labels:  format
ansi-to-svg
😹 convert ANSI Escaped CLI strings to SVGs
Stars: ✭ 18 (-60.87%)
Mutual labels:  color
vue-translated
Internationalization (i18n) and localization (l10n) library for Vue.js v2.
Stars: ✭ 19 (-58.7%)
Mutual labels:  format
color-api
A proposal and draft spec for a Color object for the Web Platform, loosely influenced by our Color.js work. Heavily WIP, if you landed here randomly, please move along.
Stars: ✭ 102 (+121.74%)
Mutual labels:  color
color-tailor
Dynamic theme for Firefox that uses the current website's "primary" color
Stars: ✭ 55 (+19.57%)
Mutual labels:  color

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].