| 1 |
// |
| 2 |
// Copyright (c) 2016-2022 The Aurora Authors. All rights reserved. |
| 3 |
// This program is free software. It comes without any warranty, |
| 4 |
// to the extent permitted by applicable law. You can redistribute |
| 5 |
// it and/or modify it under the terms of the Unlicense. See LICENSE |
| 6 |
// file for more details or see below. |
| 7 |
// |
| 8 |
|
| 9 |
// |
| 10 |
// This is free and unencumbered software released into the public domain. |
| 11 |
// |
| 12 |
// Anyone is free to copy, modify, publish, use, compile, sell, or |
| 13 |
// distribute this software, either in source code form or as a compiled |
| 14 |
// binary, for any purpose, commercial or non-commercial, and by any |
| 15 |
// means. |
| 16 |
// |
| 17 |
// In jurisdictions that recognize copyright laws, the author or authors |
| 18 |
// of this software dedicate any and all copyright interest in the |
| 19 |
// software to the public domain. We make this dedication for the benefit |
| 20 |
// of the public at large and to the detriment of our heirs and |
| 21 |
// successors. We intend this dedication to be an overt act of |
| 22 |
// relinquishment in perpetuity of all present and future rights to this |
| 23 |
// software under copyright law. |
| 24 |
// |
| 25 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 26 |
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 27 |
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 28 |
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 29 |
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 30 |
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 31 |
// OTHER DEALINGS IN THE SOFTWARE. |
| 32 |
// |
| 33 |
// For more information, please refer to <http://unlicense.org/> |
| 34 |
// |
| 35 |
|
| 36 |
package aurora |
| 37 |
|
| 38 |
import ( |
| 39 |
"fmt" |
| 40 |
"strconv" |
| 41 |
"unicode/utf8" |
| 42 |
) |
| 43 |
|
| 44 |
// compile-time check |
| 45 |
var ( |
| 46 |
_ fmt.Stringer = Value{} |
| 47 |
_ fmt.Formatter = Value{} |
| 48 |
_ Colored = Value{} |
| 49 |
) |
| 50 |
|
| 51 |
func coloredFormat(color Color, s fmt.State, verb rune) string { |
| 52 |
|
| 53 |
// it's enough for many cases (%-+020.10f) |
| 54 |
// % - 1 |
| 55 |
// availFlags - 3 (5) |
| 56 |
// width - 2 |
| 57 |
// prec - 3 (.23) |
| 58 |
// verb - 1 |
| 59 |
// -------------- |
| 60 |
// 10 |
| 61 |
// + |
| 62 |
// \033[ 5 |
| 63 |
// 0;1;3;4;5;7;8;9;20;21;51;52;53 30 |
| 64 |
// 38;5;216 8 |
| 65 |
// 48;5;216 8 |
| 66 |
// m 1 |
| 67 |
// + |
| 68 |
// \033[0m 7 |
| 69 |
// |
| 70 |
// x2 (possible tail color) |
| 71 |
// |
| 72 |
// 10 + 59 * 2 = 128 |
| 73 |
|
| 74 |
var format = make([]byte, 0, 128) |
| 75 |
|
| 76 |
if color != 0 { |
| 77 |
format = append(format, esc...) |
| 78 |
format = color.appendNos(format, false) |
| 79 |
format = append(format, 'm') |
| 80 |
} |
| 81 |
|
| 82 |
format = append(format, '%') |
| 83 |
|
| 84 |
var f byte |
| 85 |
for i := 0; i < len(availFlags); i++ { |
| 86 |
if f = availFlags[i]; s.Flag(int(f)) { |
| 87 |
format = append(format, f) |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
var ( |
| 92 |
width, prec int |
| 93 |
ok bool |
| 94 |
) |
| 95 |
if width, ok = s.Width(); ok { |
| 96 |
format = strconv.AppendInt(format, int64(width), 10) |
| 97 |
} |
| 98 |
|
| 99 |
if prec, ok = s.Precision(); ok { |
| 100 |
format = append(format, '.') |
| 101 |
format = strconv.AppendInt(format, int64(prec), 10) |
| 102 |
} |
| 103 |
|
| 104 |
if verb > utf8.RuneSelf { |
| 105 |
format = append(format, string(verb)...) |
| 106 |
} else { |
| 107 |
format = append(format, byte(verb)) |
| 108 |
} |
| 109 |
|
| 110 |
if color != 0 { |
| 111 |
format = append(format, clear...) // just clear |
| 112 |
} |
| 113 |
|
| 114 |
return string(format) |
| 115 |
} |
| 116 |
|
| 117 |
type colorConfig uint64 |
| 118 |
|
| 119 |
const ( |
| 120 |
colorPin colorConfig = 1 << 32 |
| 121 |
hyperlinksPin colorConfig = 1 << 33 |
| 122 |
) |
| 123 |
|
| 124 |
func (cc colorConfig) colorsEnabled() bool { |
| 125 |
return cc&colorPin != 0 |
| 126 |
} |
| 127 |
|
| 128 |
func (cc colorConfig) hyperlinksEnbaled() bool { |
| 129 |
return cc&hyperlinksPin != 0 |
| 130 |
} |
| 131 |
|
| 132 |
func (cc colorConfig) color() Color { |
| 133 |
if cc.colorsEnabled() { |
| 134 |
return Color(uint32(cc)) // lower 32 bits only |
| 135 |
} |
| 136 |
return 0 // even if a color set |
| 137 |
} |
| 138 |
|
| 139 |
func (cc colorConfig) resetColor() colorConfig { |
| 140 |
return cc & (colorPin | hyperlinksPin) |
| 141 |
} |
| 142 |
|
| 143 |
// A Value represents any printable value |
| 144 |
// with or without colors, formats and a link. |
| 145 |
type Value struct { |
| 146 |
value interface{} // value as is |
| 147 |
cc colorConfig // color & config |
| 148 |
hyperlink *hyperlink // hyperlink target and parameters |
| 149 |
} |
| 150 |
|
| 151 |
// String implements standard fmt.Stringer interface. |
| 152 |
func (v Value) String() string { |
| 153 |
var ( |
| 154 |
t []byte |
| 155 |
val = fmt.Sprint(v.value) |
| 156 |
color = v.cc.color() |
| 157 |
) |
| 158 |
|
| 159 |
if v.cc.hyperlinksEnbaled() && v.hyperlink.isExists() { |
| 160 |
var ( |
| 161 |
ln = len(val) |
| 162 |
nos string |
| 163 |
) |
| 164 |
// calculate length |
| 165 |
ln += v.hyperlink.headLen() |
| 166 |
if color != 0 { |
| 167 |
ln += len(esc) |
| 168 |
nos = color.Nos(false) |
| 169 |
ln += len(nos) + len("m") |
| 170 |
ln += len(clear) |
| 171 |
} |
| 172 |
ln += v.hyperlink.tailLen() |
| 173 |
// fill |
| 174 |
t = make([]byte, 0, ln) |
| 175 |
t = append(t, v.hyperlink.headBytes()...) |
| 176 |
if color != 0 { |
| 177 |
t = append(t, esc...) |
| 178 |
t = append(t, nos...) |
| 179 |
t = append(t, 'm') |
| 180 |
t = append(t, val...) |
| 181 |
t = append(t, clear...) |
| 182 |
} else { |
| 183 |
t = append(t, val...) |
| 184 |
} |
| 185 |
t = append(t, v.hyperlink.tailBytes()...) |
| 186 |
return string(t) |
| 187 |
} |
| 188 |
|
| 189 |
// no links, only colors & formats |
| 190 |
if color != 0 { |
| 191 |
return esc + color.Nos(false) + "m" + val + clear |
| 192 |
} |
| 193 |
|
| 194 |
// no links, no colors, no formats, just the value |
| 195 |
return val |
| 196 |
} |
| 197 |
|
| 198 |
// Color returns colors and formats of the Value. |
| 199 |
func (v Value) Color() Color { |
| 200 |
return v.cc.color() |
| 201 |
} |
| 202 |
|
| 203 |
// Reset colors, formats and links. |
| 204 |
func (v Value) Reset() Value { |
| 205 |
v.cc, v.hyperlink = v.cc.resetColor(), nil |
| 206 |
return v |
| 207 |
} |
| 208 |
|
| 209 |
// Clear colors and formats, preserving links. |
| 210 |
func (v Value) Clear() Value { |
| 211 |
v.cc = v.cc.resetColor() |
| 212 |
return v |
| 213 |
} |
| 214 |
|
| 215 |
// Value returns value's value (welcome to the tautology club) |
| 216 |
func (v Value) Value() interface{} { |
| 217 |
return v.value |
| 218 |
} |
| 219 |
|
| 220 |
// Format implements standard fmt.Formatter interface. |
| 221 |
func (v Value) Format(s fmt.State, verb rune) { |
| 222 |
if !v.cc.hyperlinksEnbaled() { |
| 223 |
fmt.Fprintf(s, coloredFormat(v.Color(), s, verb), v.value) |
| 224 |
return |
| 225 |
} |
| 226 |
v.hyperlink.writeHead(s) |
| 227 |
fmt.Fprintf(s, coloredFormat(v.Color(), s, verb), v.value) |
| 228 |
v.hyperlink.writeTail(s) |
| 229 |
} |
| 230 |
|
| 231 |
// Formats |
| 232 |
// |
| 233 |
// Bold or increased intensity (1). |
| 234 |
func (v Value) Bold() Value { |
| 235 |
v.cc = colorConfig(v.cc.color().Bold()) | v.cc.resetColor() |
| 236 |
return v |
| 237 |
} |
| 238 |
|
| 239 |
// Faint, decreased intensity, reset the Bold (2). |
| 240 |
func (v Value) Faint() Value { |
| 241 |
v.cc = colorConfig(v.cc.color().Faint()) | v.cc.resetColor() |
| 242 |
return v |
| 243 |
} |
| 244 |
|
| 245 |
// DoublyUnderline or Bold off, double-underline per ECMA-48 (21). It depends. |
| 246 |
func (v Value) DoublyUnderline() Value { |
| 247 |
v.cc = colorConfig(v.cc.color().DoublyUnderline()) | v.cc.resetColor() |
| 248 |
return v |
| 249 |
} |
| 250 |
|
| 251 |
// Fraktur, rarely supported (20). |
| 252 |
func (v Value) Fraktur() Value { |
| 253 |
v.cc = colorConfig(v.cc.color().Fraktur()) | v.cc.resetColor() |
| 254 |
return v |
| 255 |
} |
| 256 |
|
| 257 |
// Italic, not widely supported, sometimes treated as inverse (3). |
| 258 |
func (v Value) Italic() Value { |
| 259 |
v.cc = colorConfig(v.cc.color().Italic()) | v.cc.resetColor() |
| 260 |
return v |
| 261 |
} |
| 262 |
|
| 263 |
// Underline (4). |
| 264 |
func (v Value) Underline() Value { |
| 265 |
v.cc = colorConfig(v.cc.color().Underline()) | v.cc.resetColor() |
| 266 |
return v |
| 267 |
} |
| 268 |
|
| 269 |
// SlowBlink, blinking less than 150 per minute (5). |
| 270 |
func (v Value) SlowBlink() Value { |
| 271 |
v.cc = colorConfig(v.cc.color().SlowBlink()) | v.cc.resetColor() |
| 272 |
return v |
| 273 |
} |
| 274 |
|
| 275 |
// RapidBlink, blinking 150+ per minute, not widely supported (6). |
| 276 |
func (v Value) RapidBlink() Value { |
| 277 |
v.cc = colorConfig(v.cc.color().RapidBlink()) | v.cc.resetColor() |
| 278 |
return v |
| 279 |
} |
| 280 |
|
| 281 |
// Blink is alias for the SlowBlink. |
| 282 |
func (v Value) Blink() Value { |
| 283 |
return v.SlowBlink() |
| 284 |
} |
| 285 |
|
| 286 |
// Reverse video, swap foreground and background colors (7). |
| 287 |
func (v Value) Reverse() Value { |
| 288 |
v.cc = colorConfig(v.cc.color().Reverse()) | v.cc.resetColor() |
| 289 |
return v |
| 290 |
} |
| 291 |
|
| 292 |
// Inverse is alias for the Reverse. |
| 293 |
func (v Value) Inverse() Value { |
| 294 |
return v.Reverse() |
| 295 |
} |
| 296 |
|
| 297 |
// Conceal, hidden, not widely supported (8). |
| 298 |
func (v Value) Conceal() Value { |
| 299 |
v.cc = colorConfig(v.cc.color().Conceal()) | v.cc.resetColor() |
| 300 |
return v |
| 301 |
} |
| 302 |
|
| 303 |
// Hidden is alias for the Conceal. |
| 304 |
func (v Value) Hidden() Value { |
| 305 |
return v.Conceal() |
| 306 |
} |
| 307 |
|
| 308 |
// CrossedOut, characters legible, but marked for deletion (9). |
| 309 |
func (v Value) CrossedOut() Value { |
| 310 |
v.cc = colorConfig(v.cc.color().CrossedOut()) | v.cc.resetColor() |
| 311 |
return v |
| 312 |
} |
| 313 |
|
| 314 |
// StrikeThrough is alias for the CrossedOut. |
| 315 |
func (v Value) StrikeThrough() Value { |
| 316 |
return v.CrossedOut() |
| 317 |
} |
| 318 |
|
| 319 |
// Framed (51). |
| 320 |
func (v Value) Framed() Value { |
| 321 |
v.cc = colorConfig(v.cc.color().Framed()) | v.cc.resetColor() |
| 322 |
return v |
| 323 |
} |
| 324 |
|
| 325 |
// Encircled (52). |
| 326 |
func (v Value) Encircled() Value { |
| 327 |
v.cc = colorConfig(v.cc.color().Encircled()) | v.cc.resetColor() |
| 328 |
return v |
| 329 |
} |
| 330 |
|
| 331 |
// Overlined (53). |
| 332 |
func (v Value) Overlined() Value { |
| 333 |
v.cc = colorConfig(v.cc.color().Overlined()) | v.cc.resetColor() |
| 334 |
return v |
| 335 |
} |
| 336 |
|
| 337 |
// Foreground colors. |
| 338 |
// |
| 339 |
// Black foreground color (30). |
| 340 |
func (v Value) Black() Value { |
| 341 |
v.cc = colorConfig(v.cc.color().Black()) | v.cc.resetColor() |
| 342 |
return v |
| 343 |
} |
| 344 |
|
| 345 |
// Red foreground color (31). |
| 346 |
func (v Value) Red() Value { |
| 347 |
v.cc = colorConfig(v.cc.color().Red()) | v.cc.resetColor() |
| 348 |
return v |
| 349 |
} |
| 350 |
|
| 351 |
// Green foreground color (32). |
| 352 |
func (v Value) Green() Value { |
| 353 |
v.cc = colorConfig(v.cc.color().Green()) | v.cc.resetColor() |
| 354 |
return v |
| 355 |
} |
| 356 |
|
| 357 |
// Yellow foreground color (33). |
| 358 |
func (v Value) Yellow() Value { |
| 359 |
v.cc = colorConfig(v.cc.color().Yellow()) | v.cc.resetColor() |
| 360 |
return v |
| 361 |
} |
| 362 |
|
| 363 |
// Blue foreground color (34). |
| 364 |
func (v Value) Blue() Value { |
| 365 |
v.cc = colorConfig(v.cc.color().Blue()) | v.cc.resetColor() |
| 366 |
return v |
| 367 |
} |
| 368 |
|
| 369 |
// Magenta foreground color (35). |
| 370 |
func (v Value) Magenta() Value { |
| 371 |
v.cc = colorConfig(v.cc.color().Magenta()) | v.cc.resetColor() |
| 372 |
return v |
| 373 |
} |
| 374 |
|
| 375 |
// Cyan foreground color (36). |
| 376 |
func (v Value) Cyan() Value { |
| 377 |
v.cc = colorConfig(v.cc.color().Cyan()) | v.cc.resetColor() |
| 378 |
return v |
| 379 |
} |
| 380 |
|
| 381 |
// White foreground color (37). |
| 382 |
func (v Value) White() Value { |
| 383 |
v.cc = colorConfig(v.cc.color().White()) | v.cc.resetColor() |
| 384 |
return v |
| 385 |
} |
| 386 |
|
| 387 |
// Bright foreground colors. |
| 388 |
// |
| 389 |
// BrightBlack foreground color (90). |
| 390 |
func (v Value) BrightBlack() Value { |
| 391 |
v.cc = colorConfig(v.cc.color().BrightBlack()) | v.cc.resetColor() |
| 392 |
return v |
| 393 |
} |
| 394 |
|
| 395 |
// BrightRed foreground color (91). |
| 396 |
func (v Value) BrightRed() Value { |
| 397 |
v.cc = colorConfig(v.cc.color().BrightRed()) | v.cc.resetColor() |
| 398 |
return v |
| 399 |
} |
| 400 |
|
| 401 |
// BrightGreen foreground color (92). |
| 402 |
func (v Value) BrightGreen() Value { |
| 403 |
v.cc = colorConfig(v.cc.color().BrightGreen()) | v.cc.resetColor() |
| 404 |
return v |
| 405 |
} |
| 406 |
|
| 407 |
// BrightYellow foreground color (93). |
| 408 |
func (v Value) BrightYellow() Value { |
| 409 |
v.cc = colorConfig(v.cc.color().BrightYellow()) | v.cc.resetColor() |
| 410 |
return v |
| 411 |
} |
| 412 |
|
| 413 |
// BrightBlue foreground color (94). |
| 414 |
func (v Value) BrightBlue() Value { |
| 415 |
v.cc = colorConfig(v.cc.color().BrightBlue()) | v.cc.resetColor() |
| 416 |
return v |
| 417 |
} |
| 418 |
|
| 419 |
// BrightMagenta foreground color (95). |
| 420 |
func (v Value) BrightMagenta() Value { |
| 421 |
v.cc = colorConfig(v.cc.color().BrightMagenta()) | v.cc.resetColor() |
| 422 |
return v |
| 423 |
} |
| 424 |
|
| 425 |
// BrightCyan foreground color (96). |
| 426 |
func (v Value) BrightCyan() Value { |
| 427 |
v.cc = colorConfig(v.cc.color().BrightCyan()) | v.cc.resetColor() |
| 428 |
return v |
| 429 |
} |
| 430 |
|
| 431 |
// BrightWhite foreground color (97). |
| 432 |
func (v Value) BrightWhite() Value { |
| 433 |
v.cc = colorConfig(v.cc.color().BrightWhite()) | v.cc.resetColor() |
| 434 |
return v |
| 435 |
} |
| 436 |
|
| 437 |
// Other colors. |
| 438 |
// |
| 439 |
// Index of pre-defined 8-bit foreground color from 0 to 255 (38;5;n). |
| 440 |
// |
| 441 |
// 0- 7: standard colors (as in ESC [ 30–37 m) |
| 442 |
// 8- 15: high intensity colors (as in ESC [ 90–97 m) |
| 443 |
// 16-231: 6 × 6 × 6 cube (216 colors): 16 + 36 × r + 6 × g + b (0 ≤ r, g, b ≤ 5) |
| 444 |
// 232-255: grayscale from black to white in 24 steps |
| 445 |
func (v Value) Index(n ColorIndex) Value { |
| 446 |
v.cc = colorConfig(v.cc.color().Index(n)) | v.cc.resetColor() |
| 447 |
return v |
| 448 |
} |
| 449 |
|
| 450 |
// Gray from 0 to 24. |
| 451 |
func (v Value) Gray(n GrayIndex) Value { |
| 452 |
v.cc = colorConfig(v.cc.color().Gray(n)) | v.cc.resetColor() |
| 453 |
return v |
| 454 |
} |
| 455 |
|
| 456 |
// Background colors |
| 457 |
// |
| 458 |
// BgBlack background color (40). |
| 459 |
func (v Value) BgBlack() Value { |
| 460 |
v.cc = colorConfig(v.cc.color().BgBlack()) | v.cc.resetColor() |
| 461 |
return v |
| 462 |
} |
| 463 |
|
| 464 |
// BgRed background color (41). |
| 465 |
func (v Value) BgRed() Value { |
| 466 |
v.cc = colorConfig(v.cc.color().BgRed()) | v.cc.resetColor() |
| 467 |
return v |
| 468 |
} |
| 469 |
|
| 470 |
// BgGreen background color (42). |
| 471 |
func (v Value) BgGreen() Value { |
| 472 |
v.cc = colorConfig(v.cc.color().BgGreen()) | v.cc.resetColor() |
| 473 |
return v |
| 474 |
} |
| 475 |
|
| 476 |
// BgYellow background color (43). |
| 477 |
func (v Value) BgYellow() Value { |
| 478 |
v.cc = colorConfig(v.cc.color().BgYellow()) | v.cc.resetColor() |
| 479 |
return v |
| 480 |
} |
| 481 |
|
| 482 |
// BgBlue background color (44). |
| 483 |
func (v Value) BgBlue() Value { |
| 484 |
v.cc = colorConfig(v.cc.color().BgBlue()) | v.cc.resetColor() |
| 485 |
return v |
| 486 |
} |
| 487 |
|
| 488 |
// BgMagenta background color (45). |
| 489 |
func (v Value) BgMagenta() Value { |
| 490 |
v.cc = colorConfig(v.cc.color().BgMagenta()) | v.cc.resetColor() |
| 491 |
return v |
| 492 |
} |
| 493 |
|
| 494 |
// BgCyan background color (46). |
| 495 |
func (v Value) BgCyan() Value { |
| 496 |
v.cc = colorConfig(v.cc.color().BgCyan()) | v.cc.resetColor() |
| 497 |
return v |
| 498 |
} |
| 499 |
|
| 500 |
// BgWhite background color (47). |
| 501 |
func (v Value) BgWhite() Value { |
| 502 |
v.cc = colorConfig(v.cc.color().BgWhite()) | v.cc.resetColor() |
| 503 |
return v |
| 504 |
} |
| 505 |
|
| 506 |
// Bright background colors. |
| 507 |
// |
| 508 |
// BgBrightBlack background color (100). |
| 509 |
func (v Value) BgBrightBlack() Value { |
| 510 |
v.cc = colorConfig(v.cc.color().BgBrightBlack()) | v.cc.resetColor() |
| 511 |
return v |
| 512 |
} |
| 513 |
|
| 514 |
// BgBrightRed background color (101). |
| 515 |
func (v Value) BgBrightRed() Value { |
| 516 |
v.cc = colorConfig(v.cc.color().BgBrightRed()) | v.cc.resetColor() |
| 517 |
return v |
| 518 |
} |
| 519 |
|
| 520 |
// BgBrightGreen background color (102). |
| 521 |
func (v Value) BgBrightGreen() Value { |
| 522 |
v.cc = colorConfig(v.cc.color().BgBrightGreen()) | v.cc.resetColor() |
| 523 |
return v |
| 524 |
} |
| 525 |
|
| 526 |
// BgBrightYellow background color (103). |
| 527 |
func (v Value) BgBrightYellow() Value { |
| 528 |
v.cc = colorConfig(v.cc.color().BgBrightYellow()) | v.cc.resetColor() |
| 529 |
return v |
| 530 |
} |
| 531 |
|
| 532 |
// BgBrightBlue background color (104). |
| 533 |
func (v Value) BgBrightBlue() Value { |
| 534 |
v.cc = colorConfig(v.cc.color().BgBrightBlue()) | v.cc.resetColor() |
| 535 |
return v |
| 536 |
} |
| 537 |
|
| 538 |
// BgBrightMagenta background color (105). |
| 539 |
func (v Value) BgBrightMagenta() Value { |
| 540 |
v.cc = colorConfig(v.cc.color().BgBrightMagenta()) | v.cc.resetColor() |
| 541 |
return v |
| 542 |
} |
| 543 |
|
| 544 |
// BgBrightCyan background color (106). |
| 545 |
func (v Value) BgBrightCyan() Value { |
| 546 |
v.cc = colorConfig(v.cc.color().BgBrightCyan()) | v.cc.resetColor() |
| 547 |
return v |
| 548 |
} |
| 549 |
|
| 550 |
// BgBrightWhite background color (107). |
| 551 |
func (v Value) BgBrightWhite() Value { |
| 552 |
v.cc = colorConfig(v.cc.color().BgBrightWhite()) | v.cc.resetColor() |
| 553 |
return v |
| 554 |
} |
| 555 |
|
| 556 |
// Other background colors. |
| 557 |
// |
| 558 |
// BgIndex of 8-bit pre-defined background color from 0 to 255 (48;5;n). |
| 559 |
// |
| 560 |
// 0- 7: standard colors (as in ESC [ 40–47 m) |
| 561 |
// 8- 15: high intensity colors (as in ESC [100–107 m) |
| 562 |
// 16-231: 6 × 6 × 6 cube (216 colors): 16 + 36 × r + 6 × g + b (0 ≤ r, g, b ≤ 5) |
| 563 |
// 232-255: grayscale from black to white in 24 steps |
| 564 |
func (v Value) BgIndex(n ColorIndex) Value { |
| 565 |
v.cc = colorConfig(v.cc.color().BgIndex(n)) | v.cc.resetColor() |
| 566 |
return v |
| 567 |
} |
| 568 |
|
| 569 |
// BgGray from 0 to 24. |
| 570 |
func (v Value) BgGray(n GrayIndex) Value { |
| 571 |
v.cc = colorConfig(v.cc.color().BgGray(n)) | v.cc.resetColor() |
| 572 |
return v |
| 573 |
} |
| 574 |
|
| 575 |
// Special colorization method. |
| 576 |
// |
| 577 |
// Colorize removes existing colors and formats of the argument and applies |
| 578 |
// given. |
| 579 |
func (v Value) Colorize(color Color) Value { |
| 580 |
v.cc = colorConfig(color) | v.cc.resetColor() |
| 581 |
return v |
| 582 |
} |
| 583 |
|
| 584 |
// Hyperlinks feature |
| 585 |
// |
| 586 |
// Hyperlink with given target and parameters. If hyperlinks feature is |
| 587 |
// disabled, then the 'arg' argument dropped and the 'target' used instead, |
| 588 |
// inheriting all colors and format from the 'arg' (if it's a Colored). |
| 589 |
// |
| 590 |
// See https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda |
| 591 |
// for details about the hyperlinks feature. |
| 592 |
// |
| 593 |
// The Hyperlink doesn't escape the target and the parameters. They should be |
| 594 |
// checked and escaped before. See HyperlinkEscape function. |
| 595 |
// |
| 596 |
// See also HyperlinkID function. |
| 597 |
// |
| 598 |
// For a simple example |
| 599 |
// |
| 600 |
// val.Hyperlink("http://example.com") |
| 601 |
// |
| 602 |
// and an example with ID |
| 603 |
// |
| 604 |
// val.Hyperlink("http://example.com", aurora.HyperlinkID("10")) |
| 605 |
// |
| 606 |
// Successive calls replace previously set target and parameters. |
| 607 |
func (v Value) Hyperlink(target string, params ...HyperlinkParam) Value { |
| 608 |
if !v.cc.hyperlinksEnbaled() { |
| 609 |
v.value = target // drop value, use the target |
| 610 |
v.hyperlink = &hyperlink{ |
| 611 |
target: target, // keep for the HyperlinkTarget method |
| 612 |
} |
| 613 |
return v |
| 614 |
} |
| 615 |
if v.hyperlink == nil { |
| 616 |
v.hyperlink = new(hyperlink) |
| 617 |
} |
| 618 |
v.hyperlink.target = target |
| 619 |
v.hyperlink.params = params |
| 620 |
return v |
| 621 |
} |
| 622 |
|
| 623 |
// HyperlinkTarget if any. |
| 624 |
func (v Value) HyperlinkTarget() (target string) { |
| 625 |
if v.hyperlink != nil { |
| 626 |
return v.hyperlink.target |
| 627 |
} |
| 628 |
return // nothing |
| 629 |
} |
| 630 |
|
| 631 |
// HyperlinkParams if any. |
| 632 |
func (v Value) HyperlinkParams() (params []HyperlinkParam) { |
| 633 |
if v.hyperlink != nil { |
| 634 |
return v.hyperlink.params |
| 635 |
} |
| 636 |
return // nil |
| 637 |
} |