Giter Site home page Giter Site logo

go-color's Introduction

go-color

build

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

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/erock530/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"))
}

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.

Variables only

Unlike using the aforementioned approach, directly using the color variables will required 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/erock530/go-color"

func main() {
    println(color.Red + "This is red" + color.Reset)
    println(color.Green + "This is green" + color.Reset)
    println(color.Blue + "This is blue" + color.Reset)
    println(color.Purple + "This is purple" + color.Reset)
    println(color.Yellow + "This is yellow" + 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.

Installation

go get github.com/erock530/go-color

go-color's People

Contributors

twin avatar kawsark avatar erock530 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.