├── .travis.yml ├── Makefile ├── assets ├── sample.png └── go-main-example.png ├── misc └── colors.sh ├── examples ├── animation-neon.go └── example.go ├── .editorconfig ├── rainbow_test.go ├── LICENSE.md ├── README.md └── rainbow.go /.travis.yml: -------------------------------------------------------------------------------- 1 | language: go 2 | go: 3 | - 1.8.x 4 | - tip 5 | 6 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | install: 2 | go get github.com/tomnomnom/xtermcolor/cmd/xtermcolor -------------------------------------------------------------------------------- /assets/sample.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/raphamorim/go-rainbow/HEAD/assets/sample.png -------------------------------------------------------------------------------- /assets/go-main-example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/raphamorim/go-rainbow/HEAD/assets/go-main-example.png -------------------------------------------------------------------------------- /misc/colors.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | for i in $(seq 0 255) 3 | do 4 | printf '\033[1;38;5;%dmcolor %d\n' "$i" "$i" 5 | done 6 | -------------------------------------------------------------------------------- /examples/animation-neon.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/raphamorim/go-rainbow" 5 | ) 6 | 7 | func main() { 8 | rainbow.Animation("MY AWESOME NEON ANIMATION", "neon") 9 | } 10 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | end_of_line = lf 6 | indent_style = space 7 | indent_size = 4 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [*.sh] 12 | indent_size = 2 13 | 14 | [Makefile] 15 | indent_style = tab 16 | -------------------------------------------------------------------------------- /rainbow_test.go: -------------------------------------------------------------------------------- 1 | package rainbow 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | // Test params on a Color Decorator 8 | func TestColor(t *testing.T) { 9 | if Blue("MyString") != "\x1b[34m"+"MyString"+"\u001b[0m" { 10 | t.Error("Two black colors are not equal") 11 | } 12 | } 13 | 14 | func TestColorBright(t *testing.T) { 15 | if RedBright("MyString") != "\x1b[91m"+"MyString"+"\u001b[0m" { 16 | t.Error("Red bright colors don't match colors are not equal") 17 | } 18 | } 19 | 20 | func TestBgColorBright(t *testing.T) { 21 | if GreenBright(BgBlueBright("MyString")) != "\x1b[92m"+"\x1b[104m"+"MyString"+"\u001b[0m"+"\u001b[0m" { 22 | t.Error("Red bright colors don't match colors are not equal") 23 | } 24 | } 25 | 26 | func TestNestedStyles(t *testing.T) { 27 | if Underscore(GreenBright("foo")) != "\x1b[4m"+"\x1b[92m"+"foo"+"\u001b[0m"+"\u001b[0m" { 28 | t.Error("Nesting of styles fails") 29 | } 30 | 31 | if BgBlack(GreenBright("foo")) != "\x1b[40m"+"\x1b[92m"+"foo"+"\u001b[0m"+"\u001b[0m" { 32 | t.Error("Nesting of styles fails") 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Raphael Amorim 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /examples/example.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/raphamorim/go-rainbow" 7 | ) 8 | 9 | func main() { 10 | fmt.Println(rainbow.Bold(rainbow.Hex("#8E44AD", "raphael"))) 11 | fmt.Printf("%s %s %s %s %s %s %s %s", rainbow.Blue("blue"), rainbow.Black("black"), rainbow.Red("red"), rainbow.Green("green"), rainbow.Yellow("yellow"), rainbow.Magenta("magenta"), rainbow.Cyan("cyan"), rainbow.White("white")) 12 | 13 | fmt.Printf("\n%s", rainbow.Hex("#000080", "String from HEX")) 14 | fmt.Printf("\n%s", rainbow.FromInt32(0xCC66FFFF, "String from Int32")) 15 | fmt.Printf("\n%s %s %s %s %s %s %s %s", rainbow.BgBlue("bgBlue"), rainbow.BgBlack("BgBlack"), rainbow.BgRed("bgRed"), rainbow.BgGreen("bgGreen"), rainbow.BgYellow("bgYellow"), rainbow.BgMagenta("bgMagenta"), rainbow.BgCyan("bgCyan"), rainbow.BgWhite("bgWhite")) 16 | fmt.Printf("\n%s %s %s %s %s %s", rainbow.Bold("Bold"), rainbow.Dim("Dim"), rainbow.Underscore("Underscore"), rainbow.Blink("Blink"), rainbow.Reverse("Reverse"), rainbow.Hidden("Hidden")) 17 | fmt.Printf("\n%s %s %s", rainbow.Bold(rainbow.BgYellow(rainbow.Blue("Bold+BgYellow+Blue"))), rainbow.Dim(rainbow.Underscore("Dim+Underscore")), rainbow.BgMagenta(rainbow.Red("BgMagenta+Red"))) 18 | 19 | fmt.Printf("\n%s %s %s %s %s %s %s %s", rainbow.Blue("blue"), rainbow.Black("black"), rainbow.Red("red"), rainbow.Green("green"), rainbow.Yellow("yellow"), rainbow.Magenta("magenta"), rainbow.Cyan("cyan"), rainbow.White("white")) 20 | fmt.Printf("\n%s %s %s %s %s %s %s %s", rainbow.BlueBright("blue"), rainbow.Gray("Gray"), rainbow.RedBright("red"), rainbow.GreenBright("green"), rainbow.YellowBright("yellow"), rainbow.MagentaBright("magenta"), rainbow.CyanBright("cyan"), rainbow.WhiteBright("white")) 21 | 22 | fmt.Printf("\n%s %s %s %s %s %s %s %s", rainbow.BgBlueBright("bgBlue"), rainbow.BgBlackBright("BgBlack"), rainbow.BgRedBright("bgRed"), rainbow.BgGreenBright("bgGreen"), rainbow.BgYellowBright("bgYellow"), rainbow.BgMagentaBright("bgMagenta"), rainbow.BgCyanBright("bgCyan"), rainbow.BgWhiteBright("bgWhite")) 23 | 24 | fmt.Printf("\n%s %s", rainbow.BgCyan(rainbow.Yellow("Green")), rainbow.BgCyan(rainbow.YellowBright("Bright!"))) 25 | fmt.Printf("\n%s %s", rainbow.Green(rainbow.Underscore("UnderscoreBright")), rainbow.Underscore(rainbow.GreenBright("UnderscoreBright"))) 26 | fmt.Printf("\n%s %s", rainbow.BgWhite(rainbow.Red("Red")), rainbow.BgWhite(rainbow.RedBright("Bright!"))) 27 | fmt.Printf("\n%s %s", rainbow.BgWhiteBright(rainbow.Red("Red")), rainbow.BgWhiteBright(rainbow.RedBright("Bright!"))) 28 | fmt.Printf("\n%s %s\n", rainbow.BgBlack(rainbow.Green("Green")), rainbow.BgBlack(rainbow.GreenBright("Bright!"))) 29 | } 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # go-rainbow [![Build Status](https://img.shields.io/travis/raphamorim/go-rainbow.svg?style=flat-square)](https://travis-ci.org/raphamorim/go-rainbow) 2 | 3 | Extremely inspired by [chalk.js](https://github.com/chalk/chalk) 4 | 5 | `go-rainbow` allows you to: 6 | 7 | - Iterate modifiers and colors on a String. 8 | - Use HEX as you wish. 9 | - Use from Int as you wish. 10 | - Use RGB as you wish (work in progress). 11 | - Bring magical Rainbow on a String (work in progress). 12 | 13 | ![Sample](assets/sample.png) 14 | 15 | ## Available animations 16 | 17 | | Name | Preview | 18 | |:---------:|:------------------------------------------:| 19 | | rainbow | on going... | 20 | | pulse | on going... | 21 | | glitch | on going... | 22 | | radar | on going... | 23 | | neon | ![rainbow](http://i.imgur.com/Hl98alo.gif) | 24 | 25 | # Summary 26 | 27 | - [Install](#install) 28 | - [Example](#example) 29 | - [Hex](#hex) 30 | - [FromInt32](#fromint32) 31 | - [Iterables](#iterables) 32 | - [Modifiers](#modifiers) 33 | - `Bold` 34 | - `Dim` 35 | - `Italic` 36 | - `Underline` 37 | - `Reverse` 38 | - `Hidden` 39 | - [Colors](#colors) 40 | - `Black` 41 | - `Red` 42 | - `Green` 43 | - `Yellow` 44 | - `Blue` 45 | - `Magenta` 46 | - `Cyan` 47 | - `White` 48 | - `RedBright` 49 | - `GreenBright` 50 | - `YellowBright` 51 | - `BlueBright` 52 | - `MagentaBright` 53 | - `CyanBright` 54 | - `WhiteBright` 55 | - [Background Colors](#background-colors) 56 | - `BgBlack` 57 | - `BgRed` 58 | - `BgGreen` 59 | - `BgYellow` 60 | - `BgBlue` 61 | - `BgMagenta` 62 | - `BgCyan` 63 | - `BgWhite` 64 | - `BgBlackBright` 65 | - `BgRedBright` 66 | - `BgGreenBright` 67 | - `BgYellowBright` 68 | - `BgBlueBright` 69 | - `BgMagentaBright` 70 | - `BgCyanBright` 71 | - `BgWhiteBright` 72 | - [Animations](#animations) 73 | - `Neon` 74 | 75 | ## Install 76 | 77 | ```bash 78 | go get github.com/raphamorim/go-rainbow 79 | ``` 80 | 81 | ## Example 82 | 83 | **main.go** 84 | 85 | ```go 86 | package main 87 | import ( 88 | "fmt" 89 | 90 | "github.com/raphamorim/go-rainbow" 91 | ) 92 | 93 | func main() { 94 | fmt.Println(rainbow.Bold(rainbow.Hex("#8E44AD", "raphael"))) 95 | } 96 | ``` 97 | 98 | ![go-main-example](assets/go-main-example.png) 99 | 100 | ## Hex 101 | 102 | ```go 103 | ftm.Printf("%s", rainbow.Hex("#000080", "String from HEX")) // should be index 4 (navy blue) 104 | ``` 105 | 106 | ## FromInt32 107 | 108 | 32-bit unsigned integer equivalent (`uint32`) 109 | 110 | ```go 111 | fmt.Printf("%s", rainbow.FromInt32(0xCC66FFFF, "String from Int32")) 112 | ``` 113 | 114 | ## Modifiers 115 | 116 | ### Usage 117 | 118 | ```go 119 | rainbow.Bold("String in Bold") 120 | ``` 121 | 122 | - `Bold` 123 | - `Dim` 124 | - `Italic (Not widely supported)` 125 | - `Underline` 126 | - `Reverse` 127 | - `Hidden` 128 | - `Strikethrough (Not widely supported)` 129 | 130 | ## Colors 131 | 132 | ### Usage 133 | 134 | ```go 135 | rainbow.Blue("String in Blue") 136 | ``` 137 | 138 | ### List 139 | 140 | - `Black` 141 | - `Red` 142 | - `Green` 143 | - `Yellow` 144 | - `Blue` 145 | - `Magenta` 146 | - `Cyan` 147 | - `White` 148 | - `RedBright` 149 | - `GreenBright` 150 | - `YellowBright` 151 | - `BlueBright` 152 | - `MagentaBright` 153 | - `CyanBright` 154 | - `WhiteBright` 155 | 156 | #### Soon (send a PR to help us!): 157 | 158 | - `Gray` 159 | 160 | ## Background Colors 161 | 162 | ```go 163 | rainbow.BgBlue("String in Blue Background") 164 | ``` 165 | 166 | ### List 167 | 168 | - `BgBlack` 169 | - `BgRed` 170 | - `BgGreen` 171 | - `BgYellow` 172 | - `BgBlue` 173 | - `BgMagenta` 174 | - `BgCyan` 175 | - `BgWhite` 176 | - `BgBlackBright` 177 | - `BgRedBright` 178 | - `BgGreenBright` 179 | - `BgYellowBright` 180 | - `BgBlueBright` 181 | - `BgMagentaBright` 182 | - `BgCyanBright` 183 | - `BgWhiteBright` 184 | 185 | ## Iterables 186 | 187 | ```go 188 | abc := Bold(BgYellow(Blue("Bold+BgYellow+Blue"))) 189 | xyz := Dim(Underscore("Dim+Underscore")) 190 | ``` 191 | 192 | ## Animations 193 | 194 | Animations have a different behavior of all methods, they realize the act of print, instead of only returning `string`. 195 | 196 | ### Neon 197 | 198 | **code** 199 | 200 | ```go 201 | rainbow.Animation("AWESOME NEON ANIMATION", "neon") 202 | ``` 203 | 204 | **sample** 205 | 206 | ![rainbow](http://i.imgur.com/ZXMBJoB.gif) 207 | 208 | ## RGB (working on it) 209 | 210 | ```go 211 | rainbow.RGB(100, 110, 100) 212 | ``` 213 | 214 | ## License 215 | 216 | The MIT License (MIT) - see [`LICENSE.md`](https://github.com/raphamorim/go-rainbow/blob/master/LICENSE.md) for more details 217 | -------------------------------------------------------------------------------- /rainbow.go: -------------------------------------------------------------------------------- 1 | package rainbow 2 | 3 | import ( 4 | "fmt" 5 | "strconv" 6 | "time" 7 | 8 | "github.com/tomnomnom/xtermcolor" 9 | ) 10 | 11 | var ( 12 | reset = "\u001b[0m" 13 | 14 | colorBlack = "\x1b[30m" 15 | colorBlue = "\x1b[34m" 16 | colorRed = "\x1b[31m" 17 | colorGreen = "\x1b[32m" 18 | colorYellow = "\x1b[33m" 19 | colorMagenta = "\x1b[35m" 20 | colorCyan = "\x1b[36m" 21 | colorWhite = "\x1b[37m" 22 | 23 | colorGray = "\x1b[90m" 24 | colorBlueBright = "\x1b[94m" 25 | colorRedBright = "\x1b[91m" 26 | colorGreenBright = "\x1b[92m" 27 | colorYellowBright = "\x1b[93m" 28 | colorMagentaBright = "\x1b[95m" 29 | colorCyanBright = "\x1b[96m" 30 | colorWhiteBright = "\x1b[97m" 31 | 32 | bgBlack = "\x1b[40m" 33 | bgBlue = "\x1b[44m" 34 | bgRed = "\x1b[41m" 35 | bgGreen = "\x1b[42m" 36 | bgYellow = "\x1b[43m" 37 | bgMagenta = "\x1b[45m" 38 | bgCyan = "\x1b[46m" 39 | bgWhite = "\x1b[47m" 40 | 41 | bgBlackBright = "\x1b[100m" 42 | bgBlueBright = "\x1b[104m" 43 | bgRedBright = "\x1b[101m" 44 | bgGreenBright = "\x1b[102m" 45 | bgYellowBright = "\x1b[103m" 46 | bgMagentaBright = "\x1b[105m" 47 | bgCyanBright = "\x1b[106m" 48 | bgWhiteBright = "\x1b[107m" 49 | 50 | modifierBold = "\x1b[1m" 51 | modifierDim = "\x1b[2m" 52 | modifierUnderscore = "\x1b[4m" 53 | modifierBlink = "\x1b[5m" 54 | modifierReverse = "\x1b[7m" 55 | modifierHidden = "\x1b[8m" 56 | ) 57 | 58 | func Black(text string) string { 59 | return colorBlack + text + reset 60 | } 61 | 62 | func Blue(text string) string { 63 | return colorBlue + text + reset 64 | } 65 | 66 | func Red(text string) string { 67 | return colorRed + text + reset 68 | } 69 | 70 | func Green(text string) string { 71 | return colorGreen + text + reset 72 | } 73 | 74 | func Yellow(text string) string { 75 | return colorYellow + text + reset 76 | } 77 | 78 | func Magenta(text string) string { 79 | return colorMagenta + text + reset 80 | } 81 | 82 | func Cyan(text string) string { 83 | return colorCyan + text + reset 84 | } 85 | 86 | func White(text string) string { 87 | return colorWhite + text + reset 88 | } 89 | 90 | func Gray(text string) string { 91 | return colorGray + text + reset 92 | } 93 | 94 | func BlueBright(text string) string { 95 | return colorBlueBright + text + reset 96 | } 97 | 98 | func RedBright(text string) string { 99 | return colorRedBright + text + reset 100 | } 101 | 102 | func GreenBright(text string) string { 103 | return colorGreenBright + text + reset 104 | } 105 | 106 | func YellowBright(text string) string { 107 | return colorYellowBright + text + reset 108 | } 109 | 110 | func MagentaBright(text string) string { 111 | return colorMagentaBright + text + reset 112 | } 113 | 114 | func CyanBright(text string) string { 115 | return colorCyanBright + text + reset 116 | } 117 | 118 | func WhiteBright(text string) string { 119 | return colorWhiteBright + text + reset 120 | } 121 | 122 | func BgBlack(text string) string { 123 | return bgBlack + text + reset 124 | } 125 | 126 | func BgBlue(text string) string { 127 | return bgBlue + text + reset 128 | } 129 | 130 | func BgRed(text string) string { 131 | return bgRed + text + reset 132 | } 133 | 134 | func BgGreen(text string) string { 135 | return bgGreen + text + reset 136 | } 137 | 138 | func BgYellow(text string) string { 139 | return bgYellow + text + reset 140 | } 141 | 142 | func BgMagenta(text string) string { 143 | return bgMagenta + text + reset 144 | } 145 | 146 | func BgCyan(text string) string { 147 | return bgCyan + text + reset 148 | } 149 | 150 | func BgWhite(text string) string { 151 | return bgWhite + text + reset 152 | } 153 | 154 | func BgBlackBright(text string) string { 155 | return bgBlackBright + text + reset 156 | } 157 | 158 | func BgBlueBright(text string) string { 159 | return bgBlueBright + text + reset 160 | } 161 | 162 | func BgRedBright(text string) string { 163 | return bgRedBright + text + reset 164 | } 165 | 166 | func BgGreenBright(text string) string { 167 | return bgGreenBright + text + reset 168 | } 169 | 170 | func BgYellowBright(text string) string { 171 | return bgYellowBright + text + reset 172 | } 173 | 174 | func BgMagentaBright(text string) string { 175 | return bgMagentaBright + text + reset 176 | } 177 | 178 | func BgCyanBright(text string) string { 179 | return bgCyanBright + text + reset 180 | } 181 | 182 | func BgWhiteBright(text string) string { 183 | return bgWhiteBright + text + reset 184 | } 185 | 186 | func Bold(text string) string { 187 | return modifierBold + text + reset 188 | } 189 | 190 | func Dim(text string) string { 191 | return modifierDim + text + reset 192 | } 193 | 194 | func Underscore(text string) string { 195 | return modifierUnderscore + text + reset 196 | } 197 | 198 | func Blink(text string) string { 199 | return modifierBlink + text + reset 200 | } 201 | 202 | func Reverse(text string) string { 203 | return modifierReverse + text + reset 204 | } 205 | 206 | func Hidden(text string) string { 207 | return modifierHidden + text + reset 208 | } 209 | 210 | func Hex(hexColor, text string) string { 211 | code, err := xtermcolor.FromHexStr(hexColor) 212 | 213 | if err != nil { 214 | return White(text) 215 | } 216 | 217 | return "\033[1;38;5;" + strconv.Itoa(int(code)) + "m" + text 218 | } 219 | 220 | func FromInt32(Int uint32, text string) string { 221 | code := xtermcolor.FromInt(Int) 222 | return "\033[1;38;5;" + strconv.Itoa(int(code)) + "m" + text 223 | } 224 | 225 | func Animation(text string, animation string) { 226 | if animation == "neon" { 227 | go neonAnimation(text) 228 | } 229 | 230 | select {} 231 | } 232 | 233 | func neonAnimation(text string) { 234 | fmt.Println("\033[E") 235 | light := false 236 | for range time.Tick(time.Second) { 237 | if light { 238 | fmt.Println("\033[A\033[K" + Bold(Magenta(text))) 239 | } else { 240 | fmt.Println("\033[A\033[K" + Bold(Dim(text))) 241 | } 242 | light = !light 243 | } 244 | } 245 | --------------------------------------------------------------------------------