github.com/Aoi-hosizora/ahlib@v1.5.1-0.20230404072829-241b93cf91c7/xcolor/xcolor.go (about) 1 package xcolor 2 3 import ( 4 "bytes" 5 "fmt" 6 "io" 7 "os" 8 "strconv" 9 "strings" 10 ) 11 12 // =================== 13 // types and constants 14 // =================== 15 16 // ansiEscapeCode is an interface type to represent Style, Color, Background and MixCode types. 17 type ansiEscapeCode interface { 18 String() string 19 unexportedXXX() 20 // Code() uint8 // -> don't use Code() method because of MixCode type 21 } 22 23 var ( 24 _ ansiEscapeCode = (*Style)(nil) 25 _ ansiEscapeCode = (*Color)(nil) 26 _ ansiEscapeCode = (*Background)(nil) 27 _ ansiEscapeCode = (*MixCode)(nil) 28 ) 29 30 // Style represents a style code. Visit https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters for details. 31 type Style uint8 32 33 const ( 34 Bold Style = iota + 1 // Style for bold, 1. 35 Faint // Style for faint, 2. 36 Italic // Style for italic, 3. 37 Underline // Style for underline, 4. 38 Reverse Style = 7 // Style for reverse, 7. 39 Strikethrough Style = 9 // Style for strikethrough, 9. 40 ) 41 42 // String returns the string value of the code. 43 func (s Style) String() string { 44 return strconv.Itoa(int(s)) 45 } 46 47 // unexportedXXX implements unexported ansiEscapeCode interface. 48 func (s Style) unexportedXXX() {} 49 50 // Code returns the numeric value of the code. 51 func (s Style) Code() uint8 { 52 return uint8(s) 53 } 54 55 // WithStyle creates a MixCode with current Style and a new Style. 56 func (s Style) WithStyle(s2 Style) MixCode { 57 return MixCode{s.Code(), s2.Code()} 58 } 59 60 // WithColor creates a MixCode with current Style and a new Color. 61 func (s Style) WithColor(c Color) MixCode { 62 return MixCode{s.Code(), c.Code()} 63 } 64 65 // WithBackground creates a MixCode with current Style and a new Background. 66 func (s Style) WithBackground(b Background) MixCode { 67 return MixCode{s.Code(), b.Code()} 68 } 69 70 // Color represents a color code. Visit https://en.wikipedia.org/wiki/ANSI_escape_code#Colors for details. 71 type Color uint8 72 73 const ( 74 Black Color = iota + 30 // Color for black, 30. 75 Red // Color for red, 31. 76 Green // Color for green, 32. 77 Yellow // Color for yellow, 33. 78 Blue // Color for blue, 34. 79 Magenta // Color for magenta, 35. 80 Cyan // Color for cyan, 36. 81 White // Color for white, 37. 82 Default Color = 39 // Color for default, 39. 83 ) 84 85 const ( 86 BrightBlack Color = iota + 90 // Color for bright black, 90. 87 BrightRed // Color for bright red, 91. 88 BrightGreen // Color for bright green, 92. 89 BrightYellow // Color for bright yellow, 93. 90 BrightBlue // Color for bright blue, 94. 91 BrightMagenta // Color for bright magenta, 95. 92 BrightCyan // Color for bright cyan, 96. 93 BrightWhite // Color for bright white, 97. 94 ) 95 96 // String returns the string value of the code. 97 func (c Color) String() string { 98 return strconv.Itoa(int(c)) 99 } 100 101 // unexportedXXX implements unexported ansiEscapeCode interface. 102 func (c Color) unexportedXXX() {} 103 104 // Code returns the numeric value of the code. 105 func (c Color) Code() uint8 { 106 return uint8(c) 107 } 108 109 // WithStyle creates a MixCode with current Color and a new Style. 110 func (c Color) WithStyle(s Style) MixCode { 111 return MixCode{c.Code(), s.Code()} 112 } 113 114 // WithBackground creates a MixCode with current Color and a new Background. 115 func (c Color) WithBackground(b Background) MixCode { 116 return MixCode{c.Code(), b.Code()} 117 } 118 119 // Background represents a background color code. Visit https://en.wikipedia.org/wiki/ANSI_escape_code#Colors for details. 120 type Background uint8 121 122 const ( 123 BGBlack Background = iota + 40 // Background for black, 40. 124 BGRed // Background for red, 41. 125 BGGreen // Background for green, 42. 126 BGYellow // Background for yellow, 43. 127 BGBlue // Background for blue, 44. 128 BGMagenta // Background for magenta, 45. 129 BGCyan // Background for cyan, 46. 130 BGWhite // Background for white, 47. 131 BGDefault Background = 49 // Background for default, 49. 132 ) 133 134 const ( 135 BGBrightBlack Background = iota + 100 // Background for bright black, 100. 136 BGBrightRed // Background for bright red, 101. 137 BGBrightGreen // Background for bright green, 102. 138 BGBrightYellow // Background for bright yellow, 103. 139 BGBrightBlue // Background for bright blue, 104. 140 BGBrightMagenta // Background for bright magenta, 105. 141 BGBrightCyan // Background for bright cyan, 106. 142 BGBrightWhite // Background for bright white, 107. 143 ) 144 145 // String returns the string value of the code. 146 func (b Background) String() string { 147 return strconv.Itoa(int(b)) 148 } 149 150 // unexportedXXX implements unexported ansiEscapeCode interface. 151 func (b Background) unexportedXXX() {} 152 153 // Code returns the numeric value of the code. 154 func (b Background) Code() uint8 { 155 return uint8(b) 156 } 157 158 // WithStyle creates a MixCode with current Background and a new Style. 159 func (b Background) WithStyle(s Style) MixCode { 160 return MixCode{b.Code(), s.Code()} 161 } 162 163 // WithColor creates a MixCode with current Background and a new Color. 164 func (b Background) WithColor(c Color) MixCode { 165 return MixCode{b.Code(), c.Code()} 166 } 167 168 // MixCode represents an ANSI escape code, has mix styles in Style, Color and Background. 169 // Visit https://en.wikipedia.org/wiki/ANSI_escape_code and https://tforgione.fr/posts/ansi-escape-codes/ for details. 170 type MixCode []uint8 171 172 // String returns the string value of the code. 173 func (m MixCode) String() string { 174 if len(m) == 0 { 175 return "0" 176 } 177 codes := make([]string, len(m)) 178 for i, c := range m { 179 codes[i] = strconv.Itoa(int(c)) 180 } 181 return strings.Join(codes, ";") 182 } 183 184 // unexportedXXX implements unexported ansiEscapeCode interface. 185 func (m MixCode) unexportedXXX() {} 186 187 // Codes returns the numeric value of the code. 188 func (m MixCode) Codes() []uint8 { 189 return m 190 } 191 192 // WithStyle creates a new MixCode with current MixCode and a new Style. 193 func (m MixCode) WithStyle(s Style) MixCode { 194 return append(m, s.Code()) 195 } 196 197 // WithColor creates a new MixCode with current MixCode and a new Color. 198 func (m MixCode) WithColor(c Color) MixCode { 199 return append(m, c.Code()) 200 } 201 202 // WithBackground creates a new MixCode with current MixCode and a new Background. 203 func (m MixCode) WithBackground(b Background) MixCode { 204 return append(m, b.Code()) 205 } 206 207 // ============= 208 // print helpers 209 // ============= 210 211 const ( 212 // fullTpl is ANSI escape code template, that is `ESC[Xm...ESC[0m`. 213 fullTpl = "\x1b[%sm%s\x1b[0m" 214 215 // fullTplLn equals to fullTpl with a newline followed. 216 fullTplLn = fullTpl + "\n" 217 ) 218 219 // prepareAlignment adds or subtracts given alignment with ANSI escape code template, and returns the stringed one. 220 func prepareAlignment(alignment int, code string) string { 221 // ESC [ $code m $sss ESC [ 0 m 222 // ¯¯¯ ¯ ¯¯¯¯¯ ¯ ¯¯¯¯ ¯¯¯ ¯ ¯ ¯ => 7 + #$code + #$sss 223 const fullTplLength = 7 224 switch { 225 case alignment > 0: // %10s 226 alignment += fullTplLength + len(code) 227 case alignment < 0: // %-10s 228 alignment -= fullTplLength + len(code) 229 } 230 return strconv.Itoa(alignment) 231 } 232 233 // doFprint writes the ANSI escaped message to io.Writer, with given ansiEscapeCode and alignment. 234 func doFprint(c ansiEscapeCode, w io.Writer, message string, alignment int) (n int, err error) { 235 code := c.String() 236 switch { 237 case alignment == 0: 238 return fmt.Fprintf(w, fullTpl, code, message) 239 default: 240 a := prepareAlignment(alignment, code) 241 return fmt.Fprintf(w, "%"+a+"s", fmt.Sprintf(fullTpl, code, message)) 242 } 243 } 244 245 // doFprintln writes the ANSI escaped message and a line feed to io.Writer, with given ansiEscapeCode and alignment. 246 func doFprintln(c ansiEscapeCode, w io.Writer, message string, alignment int) (n int, err error) { 247 code := c.String() 248 message = message[:len(message)-1] // there must be '\n' at the end of message 249 switch { 250 case alignment == 0: 251 return fmt.Fprintf(w, fullTplLn, code, message) 252 default: 253 a := prepareAlignment(alignment, code) 254 return fmt.Fprintf(w, "%"+a+"s\n", fmt.Sprintf(fullTpl, code, message)) 255 } 256 } 257 258 // doPrint prints the ANSI escaped message, with given ansiEscapeCode and alignment. 259 func doPrint(c ansiEscapeCode, message string, alignment int) { 260 _, _ = doFprint(c, os.Stdout, message, alignment) 261 } 262 263 // doPrintln prints the ANSI escaped message and a line feed, with given ansiEscapeCode and alignment. 264 func doPrintln(c ansiEscapeCode, message string, alignment int) { 265 _, _ = doFprintln(c, os.Stdout, message, alignment) 266 } 267 268 // doSprint returns the ANSI escaped message, with given ansiEscapeCode and alignment. 269 func doSprint(c ansiEscapeCode, message string, alignment int) string { 270 buf := &bytes.Buffer{} 271 _, _ = doFprint(c, buf, message, alignment) 272 return buf.String() 273 } 274 275 // doSprintln returns the ANSI escaped message and a line feed, with given ansiEscapeCode and alignment. 276 func doSprintln(c ansiEscapeCode, message string, alignment int) string { 277 buf := &bytes.Buffer{} 278 _, _ = doFprintln(c, buf, message, alignment) 279 return buf.String() 280 } 281 282 // =================== 283 // style print methods 284 // =================== 285 286 // Print prints the ANSI styled string, with given Style. 287 func (s Style) Print(v ...interface{}) { 288 msg := fmt.Sprint(v...) 289 doPrint(s, msg, 0) 290 } 291 292 // Printf prints the formatted ANSI styled string, with given Style. 293 func (s Style) Printf(f string, v ...interface{}) { 294 msg := fmt.Sprintf(f, v...) 295 doPrint(s, msg, 0) 296 } 297 298 // Println prints the ANSI styled string and a newline followed, with given Style. 299 func (s Style) Println(v ...interface{}) { 300 msg := fmt.Sprintln(v...) 301 doPrintln(s, msg, 0) 302 } 303 304 // Sprint returns the ANSI styled string, with given Style. 305 func (s Style) Sprint(v ...interface{}) string { 306 msg := fmt.Sprint(v...) 307 return doSprint(s, msg, 0) 308 } 309 310 // Sprintf returns the formatted ANSI styled string, with given Style. 311 func (s Style) Sprintf(f string, v ...interface{}) string { 312 msg := fmt.Sprintf(f, v...) 313 return doSprint(s, msg, 0) 314 } 315 316 // Sprintln returns the ANSI styled string and a newline followed, with given Style. 317 func (s Style) Sprintln(v ...interface{}) string { 318 msg := fmt.Sprintln(v...) 319 return doSprintln(s, msg, 0) 320 } 321 322 // Fprint writes the ANSI styled string to io.Writer, with given Style. 323 func (s Style) Fprint(w io.Writer, v ...interface{}) (n int, err error) { 324 msg := fmt.Sprint(v...) 325 return doFprint(s, w, msg, 0) 326 } 327 328 // Fprintf writes the formats ANSI styled string to io.Writer, with given Style. 329 func (s Style) Fprintf(w io.Writer, f string, v ...interface{}) (n int, err error) { 330 msg := fmt.Sprintf(f, v...) 331 return doFprint(s, w, msg, 0) 332 } 333 334 // Fprintln writes the ANSI styled string and a newline followed to io.Writer, with given Style. 335 func (s Style) Fprintln(w io.Writer, v ...interface{}) (n int, err error) { 336 msg := fmt.Sprintln(v...) 337 return doFprintln(s, w, msg, 0) 338 } 339 340 // APrint prints the ANSI styled string, with given Style and alignment. 341 func (s Style) APrint(a int, v ...interface{}) { 342 msg := fmt.Sprint(v...) 343 doPrint(s, msg, a) 344 } 345 346 // APrintf prints the formatted ANSI styled string, with given Style and alignment. 347 func (s Style) APrintf(a int, f string, v ...interface{}) { 348 msg := fmt.Sprintf(f, v...) 349 doPrint(s, msg, a) 350 } 351 352 // APrintln prints the ANSI styled string and a newline followed, with given Style and alignment. 353 func (s Style) APrintln(a int, v ...interface{}) { 354 msg := fmt.Sprintln(v...) 355 doPrintln(s, msg, a) 356 } 357 358 // ASprint returns the ANSI styled string, with given Style and alignment. 359 func (s Style) ASprint(a int, v ...interface{}) string { 360 msg := fmt.Sprint(v...) 361 return doSprint(s, msg, a) 362 } 363 364 // ASprintf returns the formatted ANSI styled string, with given Style and alignment. 365 func (s Style) ASprintf(a int, f string, v ...interface{}) string { 366 msg := fmt.Sprintf(f, v...) 367 return doSprint(s, msg, a) 368 } 369 370 // ASprintln returns the ANSI styled string and a newline followed, with given Style and alignment. 371 func (s Style) ASprintln(a int, v ...interface{}) string { 372 msg := fmt.Sprintln(v...) 373 return doSprintln(s, msg, a) 374 } 375 376 // AFprint writes the ANSI styled string to io.Writer, with given Style and alignment. 377 func (s Style) AFprint(a int, w io.Writer, v ...interface{}) (n int, err error) { 378 msg := fmt.Sprint(v...) 379 return doFprint(s, w, msg, a) 380 } 381 382 // AFprintf writes the formats ANSI styled string to io.Writer, with given Style and alignment. 383 func (s Style) AFprintf(a int, w io.Writer, f string, v ...interface{}) (n int, err error) { 384 msg := fmt.Sprintf(f, v...) 385 return doFprint(s, w, msg, a) 386 } 387 388 // AFprintln writes the ANSI styled string and a newline followed to io.Writer, with given Style and alignment. 389 func (s Style) AFprintln(a int, w io.Writer, v ...interface{}) (n int, err error) { 390 msg := fmt.Sprintln(v...) 391 return doFprintln(s, w, msg, a) 392 } 393 394 // =================== 395 // color print methods 396 // =================== 397 398 // Print prints the ANSI colored string, with given Color. 399 func (c Color) Print(v ...interface{}) { 400 msg := fmt.Sprint(v...) 401 doPrint(c, msg, 0) 402 } 403 404 // Printf prints the formatted ANSI colored string, with given Color. 405 func (c Color) Printf(f string, v ...interface{}) { 406 msg := fmt.Sprintf(f, v...) 407 doPrint(c, msg, 0) 408 } 409 410 // Println prints the colored string and a newline followed, with given Color. 411 func (c Color) Println(v ...interface{}) { 412 msg := fmt.Sprintln(v...) 413 doPrintln(c, msg, 0) 414 } 415 416 // Sprint returns the ANSI colored string, with given Color. 417 func (c Color) Sprint(v ...interface{}) string { 418 msg := fmt.Sprint(v...) 419 return doSprint(c, msg, 0) 420 } 421 422 // Sprintf returns the formatted ANSI colored string, with given Color. 423 func (c Color) Sprintf(f string, v ...interface{}) string { 424 msg := fmt.Sprintf(f, v...) 425 return doSprint(c, msg, 0) 426 } 427 428 // Sprintln returns the ANSI colored string and a newline followed, with given Color. 429 func (c Color) Sprintln(v ...interface{}) string { 430 msg := fmt.Sprintln(v...) 431 return doSprintln(c, msg, 0) 432 } 433 434 // Fprint writes the ANSI colored string to io.Writer, with given Color. 435 func (c Color) Fprint(w io.Writer, v ...interface{}) (n int, err error) { 436 msg := fmt.Sprint(v...) 437 return doFprint(c, w, msg, 0) 438 } 439 440 // Fprintf writes the formats ANSI colored string to io.Writer, with given Color. 441 func (c Color) Fprintf(w io.Writer, f string, v ...interface{}) (n int, err error) { 442 msg := fmt.Sprintf(f, v...) 443 return doFprint(c, w, msg, 0) 444 } 445 446 // Fprintln writes the ANSI colored string and a newline followed to io.Writer, with given Color. 447 func (c Color) Fprintln(w io.Writer, v ...interface{}) (n int, err error) { 448 msg := fmt.Sprintln(v...) 449 return doFprintln(c, w, msg, 0) 450 } 451 452 // APrint prints the ANSI colored string, with given Color and alignment. 453 func (c Color) APrint(a int, v ...interface{}) { 454 msg := fmt.Sprint(v...) 455 doPrint(c, msg, a) 456 } 457 458 // APrintf prints the formatted ANSI colored string, with given Color and alignment. 459 func (c Color) APrintf(a int, f string, v ...interface{}) { 460 msg := fmt.Sprintf(f, v...) 461 doPrint(c, msg, a) 462 } 463 464 // APrintln prints the colored string and a newline followed, with given Color and alignment. 465 func (c Color) APrintln(a int, v ...interface{}) { 466 msg := fmt.Sprintln(v...) 467 doPrintln(c, msg, a) 468 } 469 470 // ASprint returns the ANSI colored string, with given Color and alignment. 471 func (c Color) ASprint(a int, v ...interface{}) string { 472 msg := fmt.Sprint(v...) 473 return doSprint(c, msg, a) 474 } 475 476 // ASprintf returns the formatted ANSI colored string, with given Color and alignment. 477 func (c Color) ASprintf(a int, f string, v ...interface{}) string { 478 msg := fmt.Sprintf(f, v...) 479 return doSprint(c, msg, a) 480 } 481 482 // ASprintln returns the ANSI colored string and a newline followed, with given Color and alignment. 483 func (c Color) ASprintln(a int, v ...interface{}) string { 484 msg := fmt.Sprintln(v...) 485 return doSprintln(c, msg, a) 486 } 487 488 // AFprint writes the ANSI colored string to io.Writer, with given Color and alignment. 489 func (c Color) AFprint(a int, w io.Writer, v ...interface{}) (n int, err error) { 490 msg := fmt.Sprint(v...) 491 return doFprint(c, w, msg, a) 492 } 493 494 // AFprintf writes the formats ANSI colored string to io.Writer, with given Color and alignment. 495 func (c Color) AFprintf(a int, w io.Writer, f string, v ...interface{}) (n int, err error) { 496 msg := fmt.Sprintf(f, v...) 497 return doFprint(c, w, msg, a) 498 } 499 500 // AFprintln writes the ANSI colored string and a newline followed to io.Writer, with given Color and alignment. 501 func (c Color) AFprintln(a int, w io.Writer, v ...interface{}) (n int, err error) { 502 msg := fmt.Sprintln(v...) 503 return doFprintln(c, w, msg, a) 504 } 505 506 // ======================== 507 // background print methods 508 // ======================== 509 510 // Print prints the ANSI colored string, with given Background. 511 func (b Background) Print(v ...interface{}) { 512 msg := fmt.Sprint(v...) 513 doPrint(b, msg, 0) 514 } 515 516 // Printf prints the formatted ANSI colored string, with given Background. 517 func (b Background) Printf(f string, v ...interface{}) { 518 msg := fmt.Sprintf(f, v...) 519 doPrint(b, msg, 0) 520 } 521 522 // Println prints the ANSI colored string and a newline followed, with given Background. 523 func (b Background) Println(v ...interface{}) { 524 msg := fmt.Sprintln(v...) 525 doPrintln(b, msg, 0) 526 } 527 528 // Sprint returns the ANSI colored string, with given Background. 529 func (b Background) Sprint(v ...interface{}) string { 530 msg := fmt.Sprint(v...) 531 return doSprint(b, msg, 0) 532 } 533 534 // Sprintf returns the formatted ANSI colored string, with given Background. 535 func (b Background) Sprintf(f string, v ...interface{}) string { 536 msg := fmt.Sprintf(f, v...) 537 return doSprint(b, msg, 0) 538 } 539 540 // Sprintln returns the ANSI colored string and a newline followed, with given Background. 541 func (b Background) Sprintln(v ...interface{}) string { 542 msg := fmt.Sprintln(v...) 543 return doSprintln(b, msg, 0) 544 } 545 546 // Fprint writes the ANSI colored string to io.Writer, with given Background. 547 func (b Background) Fprint(w io.Writer, v ...interface{}) (n int, err error) { 548 msg := fmt.Sprint(v...) 549 return doFprint(b, w, msg, 0) 550 } 551 552 // Fprintf writes the formats ANSI colored string to io.Writer, with given Background. 553 func (b Background) Fprintf(w io.Writer, f string, v ...interface{}) (n int, err error) { 554 msg := fmt.Sprintf(f, v...) 555 return doFprint(b, w, msg, 0) 556 } 557 558 // Fprintln writes the ANSI colored string and a newline followed to io.Writer, with given Background. 559 func (b Background) Fprintln(w io.Writer, v ...interface{}) (n int, err error) { 560 msg := fmt.Sprintln(v...) 561 return doFprintln(b, w, msg, 0) 562 } 563 564 // APrint prints the ANSI colored string, with given Background and alignment. 565 func (b Background) APrint(a int, v ...interface{}) { 566 msg := fmt.Sprint(v...) 567 doPrint(b, msg, a) 568 } 569 570 // APrintf prints the formatted ANSI colored string, with given Background and alignment. 571 func (b Background) APrintf(a int, f string, v ...interface{}) { 572 msg := fmt.Sprintf(f, v...) 573 doPrint(b, msg, a) 574 } 575 576 // APrintln prints the ANSI colored string and a newline followed, with given Background and alignment. 577 func (b Background) APrintln(a int, v ...interface{}) { 578 msg := fmt.Sprintln(v...) 579 doPrintln(b, msg, a) 580 } 581 582 // ASprint returns the ANSI colored string, with given Background and alignment. 583 func (b Background) ASprint(a int, v ...interface{}) string { 584 msg := fmt.Sprint(v...) 585 return doSprint(b, msg, a) 586 } 587 588 // ASprintf returns the formatted ANSI colored string, with given Background and alignment. 589 func (b Background) ASprintf(a int, f string, v ...interface{}) string { 590 msg := fmt.Sprintf(f, v...) 591 return doSprint(b, msg, a) 592 } 593 594 // ASprintln returns the ANSI colored string and a newline followed, with given Background and alignment. 595 func (b Background) ASprintln(a int, v ...interface{}) string { 596 msg := fmt.Sprintln(v...) 597 return doSprintln(b, msg, a) 598 } 599 600 // AFprint writes the ANSI colored string to io.Writer, with given Background and alignment. 601 func (b Background) AFprint(a int, w io.Writer, v ...interface{}) (n int, err error) { 602 msg := fmt.Sprint(v...) 603 return doFprint(b, w, msg, a) 604 } 605 606 // AFprintf writes the formats ANSI colored string to io.Writer, with given Background and alignment. 607 func (b Background) AFprintf(a int, w io.Writer, f string, v ...interface{}) (n int, err error) { 608 msg := fmt.Sprintf(f, v...) 609 return doFprint(b, w, msg, a) 610 } 611 612 // AFprintln writes the ANSI colored string and a newline followed to io.Writer, with given Background and alignment. 613 func (b Background) AFprintln(a int, w io.Writer, v ...interface{}) (n int, err error) { 614 msg := fmt.Sprintln(v...) 615 return doFprintln(b, w, msg, a) 616 } 617 618 // ====================== 619 // mix code print methods 620 // ====================== 621 622 // Print prints the ANSI styled and colored string, with given MixCode. 623 func (m MixCode) Print(v ...interface{}) { 624 msg := fmt.Sprint(v...) 625 doPrint(m, msg, 0) 626 } 627 628 // Printf prints the formatted ANSI styled and colored string, with given MixCode. 629 func (m MixCode) Printf(f string, v ...interface{}) { 630 msg := fmt.Sprintf(f, v...) 631 doPrint(m, msg, 0) 632 } 633 634 // Println prints the ANSI styled and colored string and a newline followed, with given MixCode. 635 func (m MixCode) Println(v ...interface{}) { 636 msg := fmt.Sprintln(v...) 637 doPrintln(m, msg, 0) 638 } 639 640 // Sprint returns the ANSI styled and colored string, with given MixCode. 641 func (m MixCode) Sprint(v ...interface{}) string { 642 msg := fmt.Sprint(v...) 643 return doSprint(m, msg, 0) 644 } 645 646 // Sprintf returns the formatted ANSI styled and colored string, with given MixCode. 647 func (m MixCode) Sprintf(f string, v ...interface{}) string { 648 msg := fmt.Sprintf(f, v...) 649 return doSprint(m, msg, 0) 650 } 651 652 // Sprintln returns the ANSI styled and colored string and a newline followed, with given MixCode. 653 func (m MixCode) Sprintln(v ...interface{}) string { 654 msg := fmt.Sprintln(v...) 655 return doSprintln(m, msg, 0) 656 } 657 658 // Fprint writes the ANSI styled and colored string to io.Writer, with given MixCode. 659 func (m MixCode) Fprint(w io.Writer, v ...interface{}) (n int, err error) { 660 msg := fmt.Sprint(v...) 661 return doFprint(m, w, msg, 0) 662 } 663 664 // Fprintf writes the formats ANSI styled and colored string to io.Writer, with given MixCode. 665 func (m MixCode) Fprintf(w io.Writer, f string, v ...interface{}) (n int, err error) { 666 msg := fmt.Sprintf(f, v...) 667 return doFprint(m, w, msg, 0) 668 } 669 670 // Fprintln writes the ANSI styled and colored string and a newline followed to io.Writer, with given MixCode. 671 func (m MixCode) Fprintln(w io.Writer, v ...interface{}) (n int, err error) { 672 msg := fmt.Sprintln(v...) 673 return doFprintln(m, w, msg, 0) 674 } 675 676 // APrint prints the ANSI styled and colored string, with given MixCode and alignment. 677 func (m MixCode) APrint(a int, v ...interface{}) { 678 msg := fmt.Sprint(v...) 679 doPrint(m, msg, a) 680 } 681 682 // APrintf prints the formatted ANSI styled and colored string, with given MixCode and alignment. 683 func (m MixCode) APrintf(a int, f string, v ...interface{}) { 684 msg := fmt.Sprintf(f, v...) 685 doPrint(m, msg, a) 686 } 687 688 // APrintln prints the ANSI styled and colored string and a newline followed, with given MixCode and alignment. 689 func (m MixCode) APrintln(a int, v ...interface{}) { 690 msg := fmt.Sprintln(v...) 691 doPrintln(m, msg, a) 692 } 693 694 // ASprint returns the ANSI styled and colored string, with given MixCode and alignment. 695 func (m MixCode) ASprint(a int, v ...interface{}) string { 696 msg := fmt.Sprint(v...) 697 return doSprint(m, msg, a) 698 } 699 700 // ASprintf returns the formatted ANSI styled and colored string, with given MixCode and alignment. 701 func (m MixCode) ASprintf(a int, f string, v ...interface{}) string { 702 msg := fmt.Sprintf(f, v...) 703 return doSprint(m, msg, a) 704 } 705 706 // ASprintln returns the ANSI styled and colored string and a newline followed, with given MixCode and alignment. 707 func (m MixCode) ASprintln(a int, v ...interface{}) string { 708 msg := fmt.Sprintln(v...) 709 return doSprintln(m, msg, a) 710 } 711 712 // AFprint writes the ANSI styled and colored string to io.Writer, with given MixCode and alignment. 713 func (m MixCode) AFprint(a int, w io.Writer, v ...interface{}) (n int, err error) { 714 msg := fmt.Sprint(v...) 715 return doFprint(m, w, msg, a) 716 } 717 718 // AFprintf writes the formats ANSI styled and colored string to io.Writer, with given MixCode and alignment. 719 func (m MixCode) AFprintf(a int, w io.Writer, f string, v ...interface{}) (n int, err error) { 720 msg := fmt.Sprintf(f, v...) 721 return doFprint(m, w, msg, a) 722 } 723 724 // AFprintln writes the ANSI styled and colored string and a newline followed to io.Writer, with given MixCode and alignment. 725 func (m MixCode) AFprintln(a int, w io.Writer, v ...interface{}) (n int, err error) { 726 msg := fmt.Sprintln(v...) 727 return doFprintln(m, w, msg, a) 728 }