Small, fast library to create ANSI colored strings and codes. [go, golang]

ansi

Package ansi is a small, fast library to create ANSI colored strings and codes.

Install

Get it

go get -u github.com/mgutz/ansi

Example

import "github.com/mgutz/ansi"

// colorize a string, SLOW
msg := ansi.Color("foo", "red+b:white")

// create a FAST closure function to avoid computation of ANSI code
phosphorize := ansi.ColorFunc("green+h:black")
msg = phosphorize("Bring back the 80s!")
msg2 := phospohorize("Look, I'm a CRT!")

// cache escape codes and build strings manually
lime := ansi.ColorCode("green+h:black")
reset := ansi.ColorCode("reset")

fmt.Println(lime, "Bring back the 80s!", reset)

Other examples

Color(s, "red")            // red
Color(s, "red+d")          // red dim
Color(s, "red+b")          // red bold
Color(s, "red+B")          // red blinking
Color(s, "red+u")          // red underline
Color(s, "red+bh")         // red bold bright
Color(s, "red:white")      // red on white
Color(s, "red+b:white+h")  // red bold on white bright
Color(s, "red+B:white+h")  // red blink on white bright
Color(s, "off")            // turn off ansi codes

To view color combinations, from project directory in terminal.

go test

Style format

"foregroundColor+attributes:backgroundColor+attributes"

Colors

  • black
  • red
  • green
  • yellow
  • blue
  • magenta
  • cyan
  • white
  • 0...255 (256 colors)

Foreground Attributes

  • B = Blink
  • b = bold
  • h = high intensity (bright)
  • d = dim
  • i = inverse
  • s = strikethrough
  • u = underline

Background Attributes

  • h = high intensity (bright)

Constants

  • ansi.Reset
  • ansi.DefaultBG
  • ansi.DefaultFG
  • ansi.Black
  • ansi.Red
  • ansi.Green
  • ansi.Yellow
  • ansi.Blue
  • ansi.Magenta
  • ansi.Cyan
  • ansi.White
  • ansi.LightBlack
  • ansi.LightRed
  • ansi.LightGreen
  • ansi.LightYellow
  • ansi.LightBlue
  • ansi.LightMagenta
  • ansi.LightCyan
  • ansi.LightWhite

References

Wikipedia ANSI escape codes Colors

General tips and formatting

What about support on Windows? Use colorable by mattn. Ansi and colorable are used by logxi to support logging in color on Windows.

MIT License

Copyright (c) 2013 Mario Gutierrez [email protected]

See the file LICENSE for copying permission.

Owner
Similar Resources

A small CLI tool to compress and decompress files using Golang

A small CLI tool to compress and decompress files using Golang

Goflate A simple & small CLI tool to compress and decompress files using Golang Usage Install the binary to your local machine with the below command

May 27, 2022

Strumt is a library to create prompt chain

Strumt is a library to create prompt chain

Strumt Strumt is a library to create prompt chain. It provides multiline prompt, input validation, retry on error, ability to create typesafe prompt,

Sep 26, 2022

Run your MapReduce workloads as a single binary on a single machine with multiple CPUs and high memory. Pricing of a lot of small machines vs heavy machines is the same on most cloud providers.

gomap Run your MapReduce workloads as a single binary on a single machine with multiple CPUs and high memory. Pricing of a lot of small machines vs he

Sep 16, 2022

Go-api-cli - Small CLI to fetch data from an API sync and async

Async API Cli CLI to fetch data on "todos" from a given API in a number of ways.

Jan 13, 2022

Tiny binary serializer and deserializer to create on demand parsers and compilers

Parco Hobbyist binary compiler and parser built with as less reflection as possible, highly extensible and with zero dependencies. There are plenty pa

Nov 9, 2022

Project-1 - Create a service that accepts input as text and provides Json Output as Top ten most used words and times of occurrence in the text

Project Assignment Steps to run the project: download or clone repo in your loca

Jan 27, 2022

A small CLI tool to check connection from a local machine to a remote target in various protocols.

CHK chk is a small CLI tool to check connection from a local machine to a remote target in various protocols.

Oct 10, 2022

BDIX Tester is a small CLI application for testing connectivity BDIX connected servers

BDIX Tester is a small CLI application for testing connectivity BDIX connected servers. It's primary purpose is to check whether a BDIX connected server is accessible from your current ISP or not.

May 17, 2021

A small utility command line application that can recursively download Notion pages

notionbackup A small utility command line application that can recursively download Notion pages. I needed something scriptable that could periodicall

Dec 5, 2022
Comments
  • add support for strikethrough (not supported on all terminals)

    add support for strikethrough (not supported on all terminals)

    Not sure if this was omitted on purpose. I added to the tests and the cmd line tool as well but wasn't 100% sure what the output was supposed to look like. Let me know if you want this cleaned up somehow.

  • Reset attributes on each color set, add

    Reset attributes on each color set, add "dim" attribute.

    This fixes an issue where some attributes (like bold or underline) cannot be reset without explicitly writing a reset escape code. See the added test for an example.

    You can combine high intensity and dim! Try "black+hb" for a dark gray.

  • please version ansi

    please version ansi

    Hello,

    Could you please tag this repository?

    I am the Debian Maintainer for this project and tags would help Debian keep up with new releases/bugfixes.

    See:

    • http://semver.org/
    • http://dave.cheney.net/2016/06/24/gophers-please-tag-your-releases
Advanced ANSI style & color support for your terminal applications
Advanced ANSI style & color support for your terminal applications

termenv lets you safely use advanced styling options on the terminal. It gathers information about the terminal environment in terms of its ANSI & col

Dec 31, 2022
Generate ANSI-/Ascii-art version images/Gifs in your terminal.
Generate ANSI-/Ascii-art version images/Gifs in your terminal.

ANSI-Art NOTE: This toy project is not yet finished. ANSI-version Logo Block ANSI-version Logo ASCII-version Logo Support Platform You are kindly remi

Jan 6, 2023
Library for easy named formatting strings

go-celsium Library for easy named formatting translations Documentation All translations with named parameters are stored in next format: Hello, {name

Apr 12, 2021
Node is where client will send data to, create block send to miner, create block send to parent and receive tick from validator and do validate

Node Receive Tick from validator Validate POH of tick send result to validator Receive confirm block from validator Send Checked block to validator Ho

Dec 31, 2021
Generate QR-Codes for checking into events using the official Corona Warn App.

Corona Warn App QR-Code Generator Generate QR-Codes for checking into events using the official Corona Warn App. Table of Contents Introduction Instal

Oct 5, 2022
Branch is a small CLI tool to automatically create git branches based on tickets

________ ________ ________ ________ ________ ___ ___ |\ __ \ |\ __ \ |\ __ \ |\ ___ \ |\ ___

Nov 15, 2022
Rpfaudio - A small opinionated cli to create Readium Audiobooks from a directory containing mp3 files

rpfaudio A small opinionated cli to create Readium Audiobooks from a directory c

Feb 18, 2022
linenoise-classic is a command-line tool that generates strings of random characters that can be used as reasonably secure passwords.

linenoise-classic is a command-line tool that generates strings of random characters that can be used as reasonably secure passwords.

Aug 21, 2022
Go cmd utility that prints its command line arguments using strings.Join

Results This is an exercise of the book The Go Programming Language, by Alan A. A. Donovan and Brian Kernighan. Comparison between different versions

Dec 18, 2021
Zenstring - Randomly generated, friendly & zen strings for your app

zenstring Randomly generated, friendly & zen strings for your app. Started as a

Feb 3, 2022