├── LICENSE ├── README.md ├── aec.go ├── ansi.go ├── builder.go ├── checkansi ├── README.md ├── main.go └── sample.gif ├── sample.gif └── sgr.go /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Taihei Morikuni 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, 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, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # aec 2 | 3 | [![GoDoc](https://godoc.org/github.com/morikuni/aec?status.svg)](https://godoc.org/github.com/morikuni/aec) 4 | 5 | Go wrapper for ANSI escape code. 6 | 7 | ## Install 8 | 9 | ```bash 10 | go get github.com/morikuni/aec 11 | ``` 12 | 13 | ## Features 14 | 15 | ANSI escape codes depend on terminal environment. 16 | Some of these features may not work. 17 | Check supported Font-Style/Font-Color features with [checkansi](./checkansi). 18 | 19 | [Wikipedia](https://en.wikipedia.org/wiki/ANSI_escape_code) for more detail. 20 | 21 | ### Cursor 22 | 23 | - `Up(n)` 24 | - `Down(n)` 25 | - `Right(n)` 26 | - `Left(n)` 27 | - `NextLine(n)` 28 | - `PreviousLine(n)` 29 | - `Column(col)` 30 | - `Position(row, col)` 31 | - `Save` 32 | - `Restore` 33 | - `Hide` 34 | - `Show` 35 | - `Report` 36 | 37 | ### Erase 38 | 39 | - `EraseDisplay(mode)` 40 | - `EraseLine(mode)` 41 | 42 | ### Scroll 43 | 44 | - `ScrollUp(n)` 45 | - `ScrollDown(n)` 46 | 47 | ### Font Style 48 | 49 | - `Bold` 50 | - `Faint` 51 | - `Italic` 52 | - `Underline` 53 | - `BlinkSlow` 54 | - `BlinkRapid` 55 | - `Inverse` 56 | - `Conceal` 57 | - `CrossOut` 58 | - `Frame` 59 | - `Encircle` 60 | - `Overline` 61 | 62 | ### Font Color 63 | 64 | Foreground color. 65 | 66 | - `DefaultF` 67 | - `BlackF` 68 | - `RedF` 69 | - `GreenF` 70 | - `YellowF` 71 | - `BlueF` 72 | - `MagentaF` 73 | - `CyanF` 74 | - `WhiteF` 75 | - `LightBlackF` 76 | - `LightRedF` 77 | - `LightGreenF` 78 | - `LightYellowF` 79 | - `LightBlueF` 80 | - `LightMagentaF` 81 | - `LightCyanF` 82 | - `LightWhiteF` 83 | - `Color3BitF(color)` 84 | - `Color8BitF(color)` 85 | - `FullColorF(r, g, b)` 86 | 87 | Background color. 88 | 89 | - `DefaultB` 90 | - `BlackB` 91 | - `RedB` 92 | - `GreenB` 93 | - `YellowB` 94 | - `BlueB` 95 | - `MagentaB` 96 | - `CyanB` 97 | - `WhiteB` 98 | - `LightBlackB` 99 | - `LightRedB` 100 | - `LightGreenB` 101 | - `LightYellowB` 102 | - `LightBlueB` 103 | - `LightMagentaB` 104 | - `LightCyanB` 105 | - `LightWhiteB` 106 | - `Color3BitB(color)` 107 | - `Color8BitB(color)` 108 | - `FullColorB(r, g, b)` 109 | 110 | ### Color Converter 111 | 112 | 24bit RGB color to ANSI color. 113 | 114 | - `NewRGB3Bit(r, g, b)` 115 | - `NewRGB8Bit(r, g, b)` 116 | 117 | ### Builder 118 | 119 | To mix these features. 120 | 121 | ```go 122 | custom := aec.EmptyBuilder.Right(2).RGB8BitF(128, 255, 64).RedB().ANSI 123 | custom.Apply("Hello World") 124 | ``` 125 | 126 | ## Usage 127 | 128 | 1. Create ANSI by `aec.XXX().With(aec.YYY())` or `aec.EmptyBuilder.XXX().YYY().ANSI` 129 | 2. Print ANSI by `fmt.Print(ansi, "some string", aec.Reset)` or `fmt.Print(ansi.Apply("some string"))` 130 | 131 | `aec.Reset` should be added when using font style or font color features. 132 | 133 | ## Example 134 | 135 | Simple progressbar. 136 | 137 | ![sample](./sample.gif) 138 | 139 | ```go 140 | package main 141 | 142 | import ( 143 | "fmt" 144 | "strings" 145 | "time" 146 | 147 | "github.com/morikuni/aec" 148 | ) 149 | 150 | func main() { 151 | const n = 20 152 | builder := aec.EmptyBuilder 153 | 154 | up2 := aec.Up(2) 155 | col := aec.Column(n + 2) 156 | bar := aec.Color8BitF(aec.NewRGB8Bit(64, 255, 64)) 157 | label := builder.LightRedF().Underline().With(col).Right(1).ANSI 158 | 159 | // for up2 160 | fmt.Println() 161 | fmt.Println() 162 | 163 | for i := 0; i <= n; i++ { 164 | fmt.Print(up2) 165 | fmt.Println(label.Apply(fmt.Sprint(i, "/", n))) 166 | fmt.Print("[") 167 | fmt.Print(bar.Apply(strings.Repeat("=", i))) 168 | fmt.Println(col.Apply("]")) 169 | time.Sleep(100 * time.Millisecond) 170 | } 171 | } 172 | ``` 173 | 174 | ## License 175 | 176 | [MIT](./LICENSE) 177 | 178 | 179 | -------------------------------------------------------------------------------- /aec.go: -------------------------------------------------------------------------------- 1 | package aec 2 | 3 | import "fmt" 4 | 5 | // EraseMode is listed in a variable EraseModes. 6 | type EraseMode uint 7 | 8 | var ( 9 | // EraseModes is a list of EraseMode. 10 | EraseModes struct { 11 | // All erase all. 12 | All EraseMode 13 | 14 | // Head erase to head. 15 | Head EraseMode 16 | 17 | // Tail erase to tail. 18 | Tail EraseMode 19 | } 20 | 21 | // Save saves the cursor position. 22 | Save ANSI 23 | 24 | // Restore restores the cursor position. 25 | Restore ANSI 26 | 27 | // Hide hides the cursor. 28 | Hide ANSI 29 | 30 | // Show shows the cursor. 31 | Show ANSI 32 | 33 | // Report reports the cursor position. 34 | Report ANSI 35 | ) 36 | 37 | // Up moves up the cursor. 38 | func Up(n uint) ANSI { 39 | if n == 0 { 40 | return empty 41 | } 42 | return newAnsi(fmt.Sprintf(esc+"%dA", n)) 43 | } 44 | 45 | // Down moves down the cursor. 46 | func Down(n uint) ANSI { 47 | if n == 0 { 48 | return empty 49 | } 50 | return newAnsi(fmt.Sprintf(esc+"%dB", n)) 51 | } 52 | 53 | // Right moves right the cursor. 54 | func Right(n uint) ANSI { 55 | if n == 0 { 56 | return empty 57 | } 58 | return newAnsi(fmt.Sprintf(esc+"%dC", n)) 59 | } 60 | 61 | // Left moves left the cursor. 62 | func Left(n uint) ANSI { 63 | if n == 0 { 64 | return empty 65 | } 66 | return newAnsi(fmt.Sprintf(esc+"%dD", n)) 67 | } 68 | 69 | // NextLine moves down the cursor to head of a line. 70 | func NextLine(n uint) ANSI { 71 | if n == 0 { 72 | return empty 73 | } 74 | return newAnsi(fmt.Sprintf(esc+"%dE", n)) 75 | } 76 | 77 | // PreviousLine moves up the cursor to head of a line. 78 | func PreviousLine(n uint) ANSI { 79 | if n == 0 { 80 | return empty 81 | } 82 | return newAnsi(fmt.Sprintf(esc+"%dF", n)) 83 | } 84 | 85 | // Column set the cursor position to a given column. 86 | func Column(col uint) ANSI { 87 | return newAnsi(fmt.Sprintf(esc+"%dG", col)) 88 | } 89 | 90 | // Position set the cursor position to a given absolute position. 91 | func Position(row, col uint) ANSI { 92 | return newAnsi(fmt.Sprintf(esc+"%d;%dH", row, col)) 93 | } 94 | 95 | // EraseDisplay erases display by given EraseMode. 96 | func EraseDisplay(m EraseMode) ANSI { 97 | return newAnsi(fmt.Sprintf(esc+"%dJ", m)) 98 | } 99 | 100 | // EraseLine erases lines by given EraseMode. 101 | func EraseLine(m EraseMode) ANSI { 102 | return newAnsi(fmt.Sprintf(esc+"%dK", m)) 103 | } 104 | 105 | // ScrollUp scrolls up the page. 106 | func ScrollUp(n int) ANSI { 107 | if n == 0 { 108 | return empty 109 | } 110 | return newAnsi(fmt.Sprintf(esc+"%dS", n)) 111 | } 112 | 113 | // ScrollDown scrolls down the page. 114 | func ScrollDown(n int) ANSI { 115 | if n == 0 { 116 | return empty 117 | } 118 | return newAnsi(fmt.Sprintf(esc+"%dT", n)) 119 | } 120 | 121 | func init() { 122 | EraseModes = struct { 123 | All EraseMode 124 | Head EraseMode 125 | Tail EraseMode 126 | }{ 127 | Tail: 0, 128 | Head: 1, 129 | All: 2, 130 | } 131 | 132 | Save = newAnsi(esc + "s") 133 | Restore = newAnsi(esc + "u") 134 | Hide = newAnsi(esc + "?25l") 135 | Show = newAnsi(esc + "?25h") 136 | Report = newAnsi(esc + "6n") 137 | } 138 | -------------------------------------------------------------------------------- /ansi.go: -------------------------------------------------------------------------------- 1 | package aec 2 | 3 | import ( 4 | "fmt" 5 | "strings" 6 | ) 7 | 8 | const esc = "\x1b[" 9 | 10 | // Reset resets SGR effect. 11 | const Reset string = "\x1b[0m" 12 | 13 | var empty = newAnsi("") 14 | 15 | // ANSI represents ANSI escape code. 16 | type ANSI interface { 17 | fmt.Stringer 18 | 19 | // With adapts given ANSIs. 20 | With(...ANSI) ANSI 21 | 22 | // Apply wraps given string in ANSI. 23 | Apply(string) string 24 | } 25 | 26 | type ansiImpl string 27 | 28 | func newAnsi(s string) *ansiImpl { 29 | r := ansiImpl(s) 30 | return &r 31 | } 32 | 33 | func (a *ansiImpl) With(ansi ...ANSI) ANSI { 34 | return concat(append([]ANSI{a}, ansi...)) 35 | } 36 | 37 | func (a *ansiImpl) Apply(s string) string { 38 | return a.String() + s + Reset 39 | } 40 | 41 | func (a *ansiImpl) String() string { 42 | return string(*a) 43 | } 44 | 45 | // Apply wraps given string in ANSIs. 46 | func Apply(s string, ansi ...ANSI) string { 47 | if len(ansi) == 0 { 48 | return s 49 | } 50 | return concat(ansi).Apply(s) 51 | } 52 | 53 | func concat(ansi []ANSI) ANSI { 54 | strs := make([]string, 0, len(ansi)) 55 | for _, p := range ansi { 56 | strs = append(strs, p.String()) 57 | } 58 | return newAnsi(strings.Join(strs, "")) 59 | } 60 | -------------------------------------------------------------------------------- /builder.go: -------------------------------------------------------------------------------- 1 | package aec 2 | 3 | // Builder is a lightweight syntax to construct customized ANSI. 4 | type Builder struct { 5 | ANSI ANSI 6 | } 7 | 8 | // EmptyBuilder is an initialized Builder. 9 | var EmptyBuilder *Builder 10 | 11 | // NewBuilder creates a Builder from existing ANSI. 12 | func NewBuilder(a ...ANSI) *Builder { 13 | return &Builder{concat(a)} 14 | } 15 | 16 | // With is a syntax for With. 17 | func (builder *Builder) With(a ...ANSI) *Builder { 18 | return NewBuilder(builder.ANSI.With(a...)) 19 | } 20 | 21 | // Up is a syntax for Up. 22 | func (builder *Builder) Up(n uint) *Builder { 23 | return builder.With(Up(n)) 24 | } 25 | 26 | // Down is a syntax for Down. 27 | func (builder *Builder) Down(n uint) *Builder { 28 | return builder.With(Down(n)) 29 | } 30 | 31 | // Right is a syntax for Right. 32 | func (builder *Builder) Right(n uint) *Builder { 33 | return builder.With(Right(n)) 34 | } 35 | 36 | // Left is a syntax for Left. 37 | func (builder *Builder) Left(n uint) *Builder { 38 | return builder.With(Left(n)) 39 | } 40 | 41 | // NextLine is a syntax for NextLine. 42 | func (builder *Builder) NextLine(n uint) *Builder { 43 | return builder.With(NextLine(n)) 44 | } 45 | 46 | // PreviousLine is a syntax for PreviousLine. 47 | func (builder *Builder) PreviousLine(n uint) *Builder { 48 | return builder.With(PreviousLine(n)) 49 | } 50 | 51 | // Column is a syntax for Column. 52 | func (builder *Builder) Column(col uint) *Builder { 53 | return builder.With(Column(col)) 54 | } 55 | 56 | // Position is a syntax for Position. 57 | func (builder *Builder) Position(row, col uint) *Builder { 58 | return builder.With(Position(row, col)) 59 | } 60 | 61 | // EraseDisplay is a syntax for EraseDisplay. 62 | func (builder *Builder) EraseDisplay(m EraseMode) *Builder { 63 | return builder.With(EraseDisplay(m)) 64 | } 65 | 66 | // EraseLine is a syntax for EraseLine. 67 | func (builder *Builder) EraseLine(m EraseMode) *Builder { 68 | return builder.With(EraseLine(m)) 69 | } 70 | 71 | // ScrollUp is a syntax for ScrollUp. 72 | func (builder *Builder) ScrollUp(n int) *Builder { 73 | return builder.With(ScrollUp(n)) 74 | } 75 | 76 | // ScrollDown is a syntax for ScrollDown. 77 | func (builder *Builder) ScrollDown(n int) *Builder { 78 | return builder.With(ScrollDown(n)) 79 | } 80 | 81 | // Save is a syntax for Save. 82 | func (builder *Builder) Save() *Builder { 83 | return builder.With(Save) 84 | } 85 | 86 | // Restore is a syntax for Restore. 87 | func (builder *Builder) Restore() *Builder { 88 | return builder.With(Restore) 89 | } 90 | 91 | // Hide is a syntax for Hide. 92 | func (builder *Builder) Hide() *Builder { 93 | return builder.With(Hide) 94 | } 95 | 96 | // Show is a syntax for Show. 97 | func (builder *Builder) Show() *Builder { 98 | return builder.With(Show) 99 | } 100 | 101 | // Report is a syntax for Report. 102 | func (builder *Builder) Report() *Builder { 103 | return builder.With(Report) 104 | } 105 | 106 | // Bold is a syntax for Bold. 107 | func (builder *Builder) Bold() *Builder { 108 | return builder.With(Bold) 109 | } 110 | 111 | // Faint is a syntax for Faint. 112 | func (builder *Builder) Faint() *Builder { 113 | return builder.With(Faint) 114 | } 115 | 116 | // Italic is a syntax for Italic. 117 | func (builder *Builder) Italic() *Builder { 118 | return builder.With(Italic) 119 | } 120 | 121 | // Underline is a syntax for Underline. 122 | func (builder *Builder) Underline() *Builder { 123 | return builder.With(Underline) 124 | } 125 | 126 | // BlinkSlow is a syntax for BlinkSlow. 127 | func (builder *Builder) BlinkSlow() *Builder { 128 | return builder.With(BlinkSlow) 129 | } 130 | 131 | // BlinkRapid is a syntax for BlinkRapid. 132 | func (builder *Builder) BlinkRapid() *Builder { 133 | return builder.With(BlinkRapid) 134 | } 135 | 136 | // Inverse is a syntax for Inverse. 137 | func (builder *Builder) Inverse() *Builder { 138 | return builder.With(Inverse) 139 | } 140 | 141 | // Conceal is a syntax for Conceal. 142 | func (builder *Builder) Conceal() *Builder { 143 | return builder.With(Conceal) 144 | } 145 | 146 | // CrossOut is a syntax for CrossOut. 147 | func (builder *Builder) CrossOut() *Builder { 148 | return builder.With(CrossOut) 149 | } 150 | 151 | // BlackF is a syntax for BlackF. 152 | func (builder *Builder) BlackF() *Builder { 153 | return builder.With(BlackF) 154 | } 155 | 156 | // RedF is a syntax for RedF. 157 | func (builder *Builder) RedF() *Builder { 158 | return builder.With(RedF) 159 | } 160 | 161 | // GreenF is a syntax for GreenF. 162 | func (builder *Builder) GreenF() *Builder { 163 | return builder.With(GreenF) 164 | } 165 | 166 | // YellowF is a syntax for YellowF. 167 | func (builder *Builder) YellowF() *Builder { 168 | return builder.With(YellowF) 169 | } 170 | 171 | // BlueF is a syntax for BlueF. 172 | func (builder *Builder) BlueF() *Builder { 173 | return builder.With(BlueF) 174 | } 175 | 176 | // MagentaF is a syntax for MagentaF. 177 | func (builder *Builder) MagentaF() *Builder { 178 | return builder.With(MagentaF) 179 | } 180 | 181 | // CyanF is a syntax for CyanF. 182 | func (builder *Builder) CyanF() *Builder { 183 | return builder.With(CyanF) 184 | } 185 | 186 | // WhiteF is a syntax for WhiteF. 187 | func (builder *Builder) WhiteF() *Builder { 188 | return builder.With(WhiteF) 189 | } 190 | 191 | // DefaultF is a syntax for DefaultF. 192 | func (builder *Builder) DefaultF() *Builder { 193 | return builder.With(DefaultF) 194 | } 195 | 196 | // BlackB is a syntax for BlackB. 197 | func (builder *Builder) BlackB() *Builder { 198 | return builder.With(BlackB) 199 | } 200 | 201 | // RedB is a syntax for RedB. 202 | func (builder *Builder) RedB() *Builder { 203 | return builder.With(RedB) 204 | } 205 | 206 | // GreenB is a syntax for GreenB. 207 | func (builder *Builder) GreenB() *Builder { 208 | return builder.With(GreenB) 209 | } 210 | 211 | // YellowB is a syntax for YellowB. 212 | func (builder *Builder) YellowB() *Builder { 213 | return builder.With(YellowB) 214 | } 215 | 216 | // BlueB is a syntax for BlueB. 217 | func (builder *Builder) BlueB() *Builder { 218 | return builder.With(BlueB) 219 | } 220 | 221 | // MagentaB is a syntax for MagentaB. 222 | func (builder *Builder) MagentaB() *Builder { 223 | return builder.With(MagentaB) 224 | } 225 | 226 | // CyanB is a syntax for CyanB. 227 | func (builder *Builder) CyanB() *Builder { 228 | return builder.With(CyanB) 229 | } 230 | 231 | // WhiteB is a syntax for WhiteB. 232 | func (builder *Builder) WhiteB() *Builder { 233 | return builder.With(WhiteB) 234 | } 235 | 236 | // DefaultB is a syntax for DefaultB. 237 | func (builder *Builder) DefaultB() *Builder { 238 | return builder.With(DefaultB) 239 | } 240 | 241 | // Frame is a syntax for Frame. 242 | func (builder *Builder) Frame() *Builder { 243 | return builder.With(Frame) 244 | } 245 | 246 | // Encircle is a syntax for Encircle. 247 | func (builder *Builder) Encircle() *Builder { 248 | return builder.With(Encircle) 249 | } 250 | 251 | // Overline is a syntax for Overline. 252 | func (builder *Builder) Overline() *Builder { 253 | return builder.With(Overline) 254 | } 255 | 256 | // LightBlackF is a syntax for LightBlueF. 257 | func (builder *Builder) LightBlackF() *Builder { 258 | return builder.With(LightBlackF) 259 | } 260 | 261 | // LightRedF is a syntax for LightRedF. 262 | func (builder *Builder) LightRedF() *Builder { 263 | return builder.With(LightRedF) 264 | } 265 | 266 | // LightGreenF is a syntax for LightGreenF. 267 | func (builder *Builder) LightGreenF() *Builder { 268 | return builder.With(LightGreenF) 269 | } 270 | 271 | // LightYellowF is a syntax for LightYellowF. 272 | func (builder *Builder) LightYellowF() *Builder { 273 | return builder.With(LightYellowF) 274 | } 275 | 276 | // LightBlueF is a syntax for LightBlueF. 277 | func (builder *Builder) LightBlueF() *Builder { 278 | return builder.With(LightBlueF) 279 | } 280 | 281 | // LightMagentaF is a syntax for LightMagentaF. 282 | func (builder *Builder) LightMagentaF() *Builder { 283 | return builder.With(LightMagentaF) 284 | } 285 | 286 | // LightCyanF is a syntax for LightCyanF. 287 | func (builder *Builder) LightCyanF() *Builder { 288 | return builder.With(LightCyanF) 289 | } 290 | 291 | // LightWhiteF is a syntax for LightWhiteF. 292 | func (builder *Builder) LightWhiteF() *Builder { 293 | return builder.With(LightWhiteF) 294 | } 295 | 296 | // LightBlackB is a syntax for LightBlackB. 297 | func (builder *Builder) LightBlackB() *Builder { 298 | return builder.With(LightBlackB) 299 | } 300 | 301 | // LightRedB is a syntax for LightRedB. 302 | func (builder *Builder) LightRedB() *Builder { 303 | return builder.With(LightRedB) 304 | } 305 | 306 | // LightGreenB is a syntax for LightGreenB. 307 | func (builder *Builder) LightGreenB() *Builder { 308 | return builder.With(LightGreenB) 309 | } 310 | 311 | // LightYellowB is a syntax for LightYellowB. 312 | func (builder *Builder) LightYellowB() *Builder { 313 | return builder.With(LightYellowB) 314 | } 315 | 316 | // LightBlueB is a syntax for LightBlueB. 317 | func (builder *Builder) LightBlueB() *Builder { 318 | return builder.With(LightBlueB) 319 | } 320 | 321 | // LightMagentaB is a syntax for LightMagentaB. 322 | func (builder *Builder) LightMagentaB() *Builder { 323 | return builder.With(LightMagentaB) 324 | } 325 | 326 | // LightCyanB is a syntax for LightCyanB. 327 | func (builder *Builder) LightCyanB() *Builder { 328 | return builder.With(LightCyanB) 329 | } 330 | 331 | // LightWhiteB is a syntax for LightWhiteB. 332 | func (builder *Builder) LightWhiteB() *Builder { 333 | return builder.With(LightWhiteB) 334 | } 335 | 336 | // Color3BitF is a syntax for Color3BitF. 337 | func (builder *Builder) Color3BitF(c RGB3Bit) *Builder { 338 | return builder.With(Color3BitF(c)) 339 | } 340 | 341 | // Color3BitB is a syntax for Color3BitB. 342 | func (builder *Builder) Color3BitB(c RGB3Bit) *Builder { 343 | return builder.With(Color3BitB(c)) 344 | } 345 | 346 | // Color8BitF is a syntax for Color8BitF. 347 | func (builder *Builder) Color8BitF(c RGB8Bit) *Builder { 348 | return builder.With(Color8BitF(c)) 349 | } 350 | 351 | // Color8BitB is a syntax for Color8BitB. 352 | func (builder *Builder) Color8BitB(c RGB8Bit) *Builder { 353 | return builder.With(Color8BitB(c)) 354 | } 355 | 356 | // FullColorF is a syntax for FullColorF. 357 | func (builder *Builder) FullColorF(r, g, b uint8) *Builder { 358 | return builder.With(FullColorF(r, g, b)) 359 | } 360 | 361 | // FullColorB is a syntax for FullColorB. 362 | func (builder *Builder) FullColorB(r, g, b uint8) *Builder { 363 | return builder.With(FullColorB(r, g, b)) 364 | } 365 | 366 | // RGB3BitF is a syntax for Color3BitF with NewRGB3Bit. 367 | func (builder *Builder) RGB3BitF(r, g, b uint8) *Builder { 368 | return builder.Color3BitF(NewRGB3Bit(r, g, b)) 369 | } 370 | 371 | // RGB3BitB is a syntax for Color3BitB with NewRGB3Bit. 372 | func (builder *Builder) RGB3BitB(r, g, b uint8) *Builder { 373 | return builder.Color3BitB(NewRGB3Bit(r, g, b)) 374 | } 375 | 376 | // RGB8BitF is a syntax for Color8BitF with NewRGB8Bit. 377 | func (builder *Builder) RGB8BitF(r, g, b uint8) *Builder { 378 | return builder.Color8BitF(NewRGB8Bit(r, g, b)) 379 | } 380 | 381 | // RGB8BitB is a syntax for Color8BitB with NewRGB8Bit. 382 | func (builder *Builder) RGB8BitB(r, g, b uint8) *Builder { 383 | return builder.Color8BitB(NewRGB8Bit(r, g, b)) 384 | } 385 | 386 | func init() { 387 | EmptyBuilder = &Builder{empty} 388 | } 389 | -------------------------------------------------------------------------------- /checkansi/README.md: -------------------------------------------------------------------------------- 1 | # checkansi 2 | 3 | ![sample](./sample.gif) 4 | 5 | ## Usage 6 | 7 | ```bash 8 | go run checkansi/main.go 9 | ``` 10 | 11 | or install 12 | 13 | ```bash 14 | go install github.com/morikuni/aec/checkansi 15 | ``` 16 | -------------------------------------------------------------------------------- /checkansi/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/morikuni/aec" 7 | ) 8 | 9 | func main() { 10 | fmt.Printf("%13s %s\n", "black", aec.BlackB.Apply(" ")) 11 | fmt.Printf("%13s %s\n", "red", aec.RedB.Apply(" ")) 12 | fmt.Printf("%13s %s\n", "green", aec.GreenB.Apply(" ")) 13 | fmt.Printf("%13s %s\n", "yellow", aec.YellowB.Apply(" ")) 14 | fmt.Printf("%13s %s\n", "blue", aec.BlueB.Apply(" ")) 15 | fmt.Printf("%13s %s\n", "magenta", aec.MagentaB.Apply(" ")) 16 | fmt.Printf("%13s %s\n", "cyan", aec.CyanB.Apply(" ")) 17 | fmt.Printf("%13s %s\n", "white", aec.WhiteB.Apply(" ")) 18 | fmt.Println() 19 | 20 | fmt.Printf("%13s %s\n", "light-black", aec.LightBlackB.Apply(" ")) 21 | fmt.Printf("%13s %s\n", "light-red", aec.LightRedB.Apply(" ")) 22 | fmt.Printf("%13s %s\n", "light-green", aec.LightGreenB.Apply(" ")) 23 | fmt.Printf("%13s %s\n", "light-yellow", aec.LightYellowB.Apply(" ")) 24 | fmt.Printf("%13s %s\n", "light-blue", aec.LightBlueB.Apply(" ")) 25 | fmt.Printf("%13s %s\n", "light-magenta", aec.LightMagentaB.Apply(" ")) 26 | fmt.Printf("%13s %s\n", "light-cyan", aec.LightCyanB.Apply(" ")) 27 | fmt.Printf("%13s %s\n", "light-white", aec.LightWhiteB.Apply(" ")) 28 | fmt.Println() 29 | 30 | for r := uint8(0); r < 6; r++ { 31 | for g := uint8(0); g < 6; g++ { 32 | for b := uint8(0); b < 6; b++ { 33 | ansi := aec.Color8BitB(aec.NewRGB8Bit(r*43, g*43, b*43)) 34 | fmt.Printf("%02x-%02x-%02x %s ", r*43, g*43, b*43, ansi.Apply(" ")) 35 | } 36 | fmt.Println() 37 | } 38 | fmt.Println() 39 | } 40 | 41 | fmt.Println(aec.Bold.Apply("bold")) 42 | fmt.Println(aec.Faint.Apply("faint")) 43 | fmt.Println(aec.Italic.Apply("italic")) 44 | fmt.Println(aec.Underline.Apply("underline")) 45 | fmt.Println(aec.BlinkSlow.Apply("blink-slow")) 46 | fmt.Println(aec.BlinkRapid.Apply("blink-rapid")) 47 | fmt.Println(aec.Inverse.Apply("inverse")) 48 | fmt.Println(aec.Conceal.Apply("conceal")) 49 | fmt.Println(aec.CrossOut.Apply("cross-out")) 50 | fmt.Println(aec.Frame.Apply("frame")) 51 | fmt.Println(aec.Encircle.Apply("encircle")) 52 | fmt.Println(aec.Overline.Apply("overline")) 53 | } 54 | -------------------------------------------------------------------------------- /checkansi/sample.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/morikuni/aec/39771216ff4c63d11f5e604076f9c45e8be1067b/checkansi/sample.gif -------------------------------------------------------------------------------- /sample.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/morikuni/aec/39771216ff4c63d11f5e604076f9c45e8be1067b/sample.gif -------------------------------------------------------------------------------- /sgr.go: -------------------------------------------------------------------------------- 1 | package aec 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | // RGB3Bit is a 3bit RGB color. 8 | type RGB3Bit uint8 9 | 10 | // RGB8Bit is a 8bit RGB color. 11 | type RGB8Bit uint8 12 | 13 | func newSGR(n uint) ANSI { 14 | return newAnsi(fmt.Sprintf(esc+"%dm", n)) 15 | } 16 | 17 | // NewRGB3Bit create a RGB3Bit from given RGB. 18 | func NewRGB3Bit(r, g, b uint8) RGB3Bit { 19 | return RGB3Bit((r >> 7) | ((g >> 6) & 0x2) | ((b >> 5) & 0x4)) 20 | } 21 | 22 | // NewRGB8Bit create a RGB8Bit from given RGB. 23 | func NewRGB8Bit(r, g, b uint8) RGB8Bit { 24 | return RGB8Bit(16 + 36*(r/43) + 6*(g/43) + b/43) 25 | } 26 | 27 | // Color3BitF set the foreground color of text. 28 | func Color3BitF(c RGB3Bit) ANSI { 29 | return newAnsi(fmt.Sprintf(esc+"%dm", c+30)) 30 | } 31 | 32 | // Color3BitB set the background color of text. 33 | func Color3BitB(c RGB3Bit) ANSI { 34 | return newAnsi(fmt.Sprintf(esc+"%dm", c+40)) 35 | } 36 | 37 | // Color8BitF set the foreground color of text. 38 | func Color8BitF(c RGB8Bit) ANSI { 39 | return newAnsi(fmt.Sprintf(esc+"38;5;%dm", c)) 40 | } 41 | 42 | // Color8BitB set the background color of text. 43 | func Color8BitB(c RGB8Bit) ANSI { 44 | return newAnsi(fmt.Sprintf(esc+"48;5;%dm", c)) 45 | } 46 | 47 | // FullColorF set the foreground color of text. 48 | func FullColorF(r, g, b uint8) ANSI { 49 | return newAnsi(fmt.Sprintf(esc+"38;2;%d;%d;%dm", r, g, b)) 50 | } 51 | 52 | // FullColorB set the foreground color of text. 53 | func FullColorB(r, g, b uint8) ANSI { 54 | return newAnsi(fmt.Sprintf(esc+"48;2;%d;%d;%dm", r, g, b)) 55 | } 56 | 57 | // Style 58 | var ( 59 | // Bold set the text style to bold or increased intensity. 60 | Bold ANSI 61 | 62 | // Faint set the text style to faint. 63 | Faint ANSI 64 | 65 | // Italic set the text style to italic. 66 | Italic ANSI 67 | 68 | // Underline set the text style to underline. 69 | Underline ANSI 70 | 71 | // BlinkSlow set the text style to slow blink. 72 | BlinkSlow ANSI 73 | 74 | // BlinkRapid set the text style to rapid blink. 75 | BlinkRapid ANSI 76 | 77 | // Inverse swap the foreground color and background color. 78 | Inverse ANSI 79 | 80 | // Conceal set the text style to conceal. 81 | Conceal ANSI 82 | 83 | // CrossOut set the text style to crossed out. 84 | CrossOut ANSI 85 | 86 | // Frame set the text style to framed. 87 | Frame ANSI 88 | 89 | // Encircle set the text style to encircled. 90 | Encircle ANSI 91 | 92 | // Overline set the text style to overlined. 93 | Overline ANSI 94 | ) 95 | 96 | // Foreground color of text. 97 | var ( 98 | // DefaultF is the default color of foreground. 99 | DefaultF ANSI 100 | 101 | // Normal color 102 | BlackF ANSI 103 | RedF ANSI 104 | GreenF ANSI 105 | YellowF ANSI 106 | BlueF ANSI 107 | MagentaF ANSI 108 | CyanF ANSI 109 | WhiteF ANSI 110 | 111 | // Light color 112 | LightBlackF ANSI 113 | LightRedF ANSI 114 | LightGreenF ANSI 115 | LightYellowF ANSI 116 | LightBlueF ANSI 117 | LightMagentaF ANSI 118 | LightCyanF ANSI 119 | LightWhiteF ANSI 120 | ) 121 | 122 | // Background color of text. 123 | var ( 124 | // DefaultB is the default color of background. 125 | DefaultB ANSI 126 | 127 | // Normal color 128 | BlackB ANSI 129 | RedB ANSI 130 | GreenB ANSI 131 | YellowB ANSI 132 | BlueB ANSI 133 | MagentaB ANSI 134 | CyanB ANSI 135 | WhiteB ANSI 136 | 137 | // Light color 138 | LightBlackB ANSI 139 | LightRedB ANSI 140 | LightGreenB ANSI 141 | LightYellowB ANSI 142 | LightBlueB ANSI 143 | LightMagentaB ANSI 144 | LightCyanB ANSI 145 | LightWhiteB ANSI 146 | ) 147 | 148 | func init() { 149 | Bold = newSGR(1) 150 | Faint = newSGR(2) 151 | Italic = newSGR(3) 152 | Underline = newSGR(4) 153 | BlinkSlow = newSGR(5) 154 | BlinkRapid = newSGR(6) 155 | Inverse = newSGR(7) 156 | Conceal = newSGR(8) 157 | CrossOut = newSGR(9) 158 | 159 | BlackF = newSGR(30) 160 | RedF = newSGR(31) 161 | GreenF = newSGR(32) 162 | YellowF = newSGR(33) 163 | BlueF = newSGR(34) 164 | MagentaF = newSGR(35) 165 | CyanF = newSGR(36) 166 | WhiteF = newSGR(37) 167 | 168 | DefaultF = newSGR(39) 169 | 170 | BlackB = newSGR(40) 171 | RedB = newSGR(41) 172 | GreenB = newSGR(42) 173 | YellowB = newSGR(43) 174 | BlueB = newSGR(44) 175 | MagentaB = newSGR(45) 176 | CyanB = newSGR(46) 177 | WhiteB = newSGR(47) 178 | 179 | DefaultB = newSGR(49) 180 | 181 | Frame = newSGR(51) 182 | Encircle = newSGR(52) 183 | Overline = newSGR(53) 184 | 185 | LightBlackF = newSGR(90) 186 | LightRedF = newSGR(91) 187 | LightGreenF = newSGR(92) 188 | LightYellowF = newSGR(93) 189 | LightBlueF = newSGR(94) 190 | LightMagentaF = newSGR(95) 191 | LightCyanF = newSGR(96) 192 | LightWhiteF = newSGR(97) 193 | 194 | LightBlackB = newSGR(100) 195 | LightRedB = newSGR(101) 196 | LightGreenB = newSGR(102) 197 | LightYellowB = newSGR(103) 198 | LightBlueB = newSGR(104) 199 | LightMagentaB = newSGR(105) 200 | LightCyanB = newSGR(106) 201 | LightWhiteB = newSGR(107) 202 | } 203 | --------------------------------------------------------------------------------