All Projects → TwiN → go-color

TwiN / go-color

Licence: MIT license
A lightweight, simple and cross-platform package to colorize text in terminals

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to go-color

Chalk
🖍 Terminal string styling done right
Stars: ✭ 17,566 (+26924.62%)
Mutual labels:  console, color, ansi
Mordant
Full-featured text styling for Kotlin command-line applications
Stars: ✭ 382 (+487.69%)
Mutual labels:  console, color, ansi
Rang
A Minimal, Header only Modern c++ library for terminal goodies 💄✨
Stars: ✭ 1,080 (+1561.54%)
Mutual labels:  console, color, ansi
yachalk
🖍️ Terminal string styling done right
Stars: ✭ 131 (+101.54%)
Mutual labels:  console, color, ansi
paper-terminal
Print Markdown to a paper in your terminal
Stars: ✭ 33 (-49.23%)
Mutual labels:  console, color, ansi
log-utils
Basic logging utils: colors, symbols and timestamp.
Stars: ✭ 24 (-63.08%)
Mutual labels:  console, color, ansi
leeks.js
Simple ANSI styling for your terminal
Stars: ✭ 12 (-81.54%)
Mutual labels:  console, color, ansi
concolor
Colouring template strings using tags with annotations 🎨
Stars: ✭ 35 (-46.15%)
Mutual labels:  console, color, ansi
strip-ansi-stream
Strip ANSI escape codes
Stars: ✭ 32 (-50.77%)
Mutual labels:  console, color, ansi
Sadconsole
A .NET ascii/ansi console engine written in C# for MonoGame and XNA. Create your own text roguelike (or other) games!
Stars: ✭ 853 (+1212.31%)
Mutual labels:  console, ansi
Closestx11color
Find the closest xterm-256 colors (between 0 and 255) to an arbitrary HTML hexa color (e.g. #ABCDEF)
Stars: ✭ 13 (-80%)
Mutual labels:  console, color
Colorette
Easily set the color and style of text in the terminal.
Stars: ✭ 1,047 (+1510.77%)
Mutual labels:  console, ansi
Termenv
Advanced ANSI style & color support for your terminal applications
Stars: ✭ 555 (+753.85%)
Mutual labels:  console, ansi
Gradient String
🌈 Beautiful color gradients in terminal output
Stars: ✭ 476 (+632.31%)
Mutual labels:  console, color
Crossterm
Cross platform terminal library rust
Stars: ✭ 1,023 (+1473.85%)
Mutual labels:  console, color
Colorful
Terminal string styling done right, in Python 🐍 🎉
Stars: ✭ 456 (+601.54%)
Mutual labels:  console, ansi
Asciichart
Nice-looking lightweight console ASCII line charts ╭┈╯ for NodeJS, browsers and terminal, no dependencies
Stars: ✭ 1,107 (+1603.08%)
Mutual labels:  console, ansi
Crossline
A small, self-contained, zero-config, MIT licensed, cross-platform, readline and libedit replacement.
Stars: ✭ 53 (-18.46%)
Mutual labels:  console, color
Ansi Econsole
Eclipse plugin that understands ANSI escape sequences to color the Eclipse console output.
Stars: ✭ 72 (+10.77%)
Mutual labels:  console, ansi
Console Logging
Better, prettier commandline logging for Python--with colors! 👻
Stars: ✭ 111 (+70.77%)
Mutual labels:  console, color

go-color

test

An extremely lightweight cross-platform package to colorize text in terminals.

This is not meant for maximal compatibility, nor is it meant to handle a plethora of scenarios. It will simply wrap a message with the necessary characters, if the OS handles it.

There are many cases in which this would not work, such as the output being redirected to something other than a terminal (such as a file, i.e. executable >> file.txt)

Usage

go get github.com/TwiN/go-color

Function

You can use the color.Colorize(color, message) or color.Ize(color, message) function in conjunction with a variable like so:

package main

import "github.com/TwiN/go-color"

func main() {
    println(color.Ize(color.Red, "This is red"))
    // Or if you prefer the longer version:
    println(color.Colorize(color.Red, "This is red"))
    // Or if you prefer the non-parameterized version:
    println(color.InRed("This is red"))
}

Because I felt reading color.Ize() to be more visually pleasant than color.Colorize(), I included Ize() as an alias for Colorize().

I'm not usually a big fan of having two methods doing the same thing, but since this package doesn't have much room for growth (its only purpose is to colorize terminal outputs, after all, and there aren't hundreds of ways to go about it), I believe it's acceptable to have both.

Alternatively, you can use color-specific functions:

package main

import "github.com/TwiN/go-color"

func main() {
    println(color.InBold("This is bold"))
    println(color.InUnderline("This is underlined"))
    println(color.InBlack("This is black"))
    println(color.InRed("This is red"))
    println(color.InGreen("This is green"))
    println(color.InYellow("This is yellow"))
    println(color.InBlue("This is blue"))
    println(color.InPurple("This is purple"))
    println(color.InCyan("This is cyan"))
    println(color.InGray("This is gray"))
    println(color.InWhite("This is white"))
}

Variables only

Unlike using the aforementioned approach, directly using the color variables will require you to manually prepend color.Reset at the end of your string.

You can either directly use the variables like so:

package main

import "github.com/TwiN/go-color"

func main() {
    println(color.Bold + "This is bold" + color.Reset)
    println(color.Underline + "This is underlined" + color.Reset)
    println(color.Black + "This is black" + color.Reset)
    println(color.Red + "This is red" + color.Reset)
    println(color.Green + "This is green" + color.Reset)
    println(color.Yellow + "This is yellow" + color.Reset)
    println(color.Blue + "This is blue" + color.Reset)
    println(color.Purple + "This is purple" + color.Reset)
    println(color.Cyan + "This is cyan" + color.Reset)
    println(color.Gray + "This is gray" + color.Reset)
    println(color.White + "This is white" + color.Reset)
}

NOTE: If you're going to use this approach, don't forget to prepend your string with color.Reset, otherwise everything else in your terminal will be that color until the color is reset or overridden.

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].