├── .gitignore ├── LICENSE ├── README.md ├── buildinfont.go ├── demo └── demo.go ├── font.go ├── render.go └── screenshot └── demo1.jpg /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | 24 | tags 25 | *.test 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 getwe 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # figlet4go 2 | _______ __ _______ __ _______ .___________. _ _ _______ ______ 3 | | ____|| | / _____|| | | ____|| || || | / _____| / __ \ 4 | | |__ | | | | __ | | | |__ `---| |----`| || |_ | | __ | | | | 5 | | __| | | | | |_ | | | | __| | | |__ _| | | |_ | | | | | 6 | | | | | | |__| | | `----.| |____ | | | | | |__| | | `--' | 7 | |__| |__| \______| |_______||_______| |__| |_| \______| \______/ 8 | 9 | A port of [figlet](http://www.figlet.org/) to golang. 10 | Make it easier to use,add some new feature such as colorized outputs. 11 | 12 | ## Usage 13 | 14 | 15 | ### Install 16 | 17 | ``` 18 | go get -u github.com/getwe/figlet4go 19 | ``` 20 | 21 | ### Demo 22 | 23 | ``` 24 | cd demo/ 25 | go build 26 | ./demo -str="golang" 27 | #Maybe you have to `brew install figlet` if you need 3D fond in mac osx. 28 | ``` 29 | 30 | see details in `demo/demo.go` . 31 | 32 | ![screenshot](./screenshot/demo1.jpg) 33 | -------------------------------------------------------------------------------- /buildinfont.go: -------------------------------------------------------------------------------- 1 | package figlet4go 2 | 3 | // Build in source font from starwars.flf 4 | const builtInFont = `flf2a$ 7 6 22 15 4 5 | starwars.flf by Ryan Youck (youck@cs.uregina.ca) Dec 25/1994 6 | I am not responsible for use of this font 7 | Based on Big.flf by Glenn Chappell 8 | 9 | $ $@ 10 | $ $@ 11 | $ $@ 12 | $ $@ 13 | $ $@ 14 | $ $@ 15 | $ $@@ 16 | __ $@ 17 | | |$@ 18 | | |$@ 19 | | |$@ 20 | |__|$@ 21 | (__)$@ 22 | $@@ 23 | _ _ @ 24 | ( | )@ 25 | V V @ 26 | $ @ 27 | $ @ 28 | $ @ 29 | @@ 30 | _ _ @ 31 | _| || |_$@ 32 | |_ __ _|@ 33 | _| || |_ @ 34 | |_ __ _|@ 35 | |_||_| $@ 36 | @@ 37 | __,--,_.@ 38 | / |@ 39 | | (----` + "`@" + ` 40 | \ \ $@ 41 | .----) | $@ 42 | |_ __/ $@ 43 | '--' $@@ 44 | _ ___$ @ 45 | / \ / /$ @ 46 | ( o ) / / $ @ 47 | \_/ / / _$ @ 48 | / / / \ @ 49 | / / ( o )@ 50 | /__/ \_/ @@ 51 | @ 52 | ___ @ 53 | ( _ ) $@ 54 | / _ \/\@ 55 | | (_> <@ 56 | \___/\/@ 57 | $@@ 58 | __ @ 59 | (_ )@ 60 | |/ @ 61 | $ @ 62 | $ @ 63 | $ @ 64 | @@ 65 | ___@ 66 | / /@ 67 | | |$@ 68 | | |$@ 69 | | |$@ 70 | | |$@ 71 | \__\@@ 72 | ___ @ 73 | \ \ @ 74 | | |@ 75 | | |@ 76 | | |@ 77 | | |@ 78 | /__/ @@ 79 | _ @ 80 | /\| |/\ @ 81 | \ ` + "`" + ` ' /$@ 82 | |_ _|@ 83 | / , . \$@ 84 | \/|_|\/ @ 85 | @@ 86 | @ 87 | _ @ 88 | _| |_$@ 89 | |_ _|@ 90 | |_| $@ 91 | $ @ 92 | @@ 93 | @ 94 | @ 95 | $ @ 96 | $ @ 97 | __ @ 98 | (_ )@ 99 | |/ @@ 100 | @ 101 | @ 102 | ______ @ 103 | |______|@ 104 | $ @ 105 | $ @ 106 | @@ 107 | @ 108 | @ 109 | @ 110 | $ @ 111 | __ @ 112 | (__)@ 113 | @@ 114 | ___@ 115 | / /@ 116 | / / @ 117 | / /$ @ 118 | / /$ @ 119 | /__/$ @ 120 | @@ 121 | ___ $@ 122 | / _ \ $@ 123 | | | | |$@ 124 | | | | |$@ 125 | | |_| |$@ 126 | \___/ $@ 127 | $@@ 128 | __ $@ 129 | /_ |$@ 130 | | |$@ 131 | | |$@ 132 | | |$@ 133 | |_|$@ 134 | $@@ 135 | ___ $@ 136 | |__ \ $@ 137 | $) |$@ 138 | / / $@ 139 | / /_ $@ 140 | |____|$@ 141 | $@@ 142 | ____ $@ 143 | |___ \ $@ 144 | __) |$@ 145 | |__ < $@ 146 | ___) |$@ 147 | |____/ $@ 148 | $@@ 149 | _ _ $@ 150 | | || | $@ 151 | | || |_ $@ 152 | |__ _|$@ 153 | | | $@ 154 | |_| $@ 155 | $@@ 156 | _____ $@ 157 | | ____|$@ 158 | | |__ $@ 159 | |___ \ $@ 160 | ___) |$@ 161 | |____/ $@ 162 | $@@ 163 | __ $@ 164 | / / $@ 165 | / /_ $@ 166 | | '_ \ $@ 167 | | (_) |$@ 168 | \___/ $@ 169 | $@@ 170 | ______ $@ 171 | |____ |$@ 172 | $/ / $@ 173 | / / $@ 174 | / / $@ 175 | /_/ $@ 176 | $@@ 177 | ___ $@ 178 | / _ \ $@ 179 | | (_) |$@ 180 | > _ < $@ 181 | | (_) |$@ 182 | \___/ $@ 183 | $@@ 184 | ___ $@ 185 | / _ \ $@ 186 | | (_) |$@ 187 | \__, |$@ 188 | / / $@ 189 | /_/ $@ 190 | $@@ 191 | @ 192 | _ @ 193 | (_)@ 194 | $ @ 195 | _ @ 196 | (_)@ 197 | @@ 198 | @ 199 | _ @ 200 | (_)@ 201 | $ @ 202 | _ @ 203 | ( )@ 204 | |/ @@ 205 | ___@ 206 | / /@ 207 | / /$@ 208 | < <$ @ 209 | \ \$@ 210 | \__\@ 211 | @@ 212 | @ 213 | ______ @ 214 | |______|@ 215 | ______ @ 216 | |______|@ 217 | @ 218 | @@ 219 | ___ @ 220 | \ \$ @ 221 | \ \ @ 222 | > >@ 223 | / / @ 224 | /__/$ @ 225 | @@ 226 | ______ $@ 227 | | \ $@ 228 | ` + "`" + `----) |$@ 229 | / / $@ 230 | |__| $@ 231 | __ $@ 232 | (__) $@@ 233 | ____ @ 234 | / __ \ @ 235 | / / _` + "`" + ` |@ 236 | | | (_| |@ 237 | \ \__,_|@ 238 | \____/ @ 239 | @@ 240 | ___ $ @ 241 | / \ $ @ 242 | / ^ \$ @ 243 | / /_\ \$ @ 244 | / _____ \$ @ 245 | /__/ \__\$@ 246 | $@@ 247 | .______ $@ 248 | | _ \ $@ 249 | | |_) |$@ 250 | | _ < $@ 251 | | |_) |$@ 252 | |______/ $@ 253 | $@@ 254 | ______$@ 255 | / |@ 256 | | ,----'@ 257 | | | $@ 258 | | ` + "`" + `----.@ 259 | \______|@ 260 | $@@ 261 | _______ $@ 262 | | \$@ 263 | | .--. |@ 264 | | | | |@ 265 | | '--' |@ 266 | |_______/$@ 267 | $@@ 268 | _______ @ 269 | | ____|@ 270 | | |__ $@ 271 | | __| $@ 272 | | |____ @ 273 | |_______|@ 274 | @@ 275 | _______ @ 276 | | ____|@ 277 | | |__ $@ 278 | | __| $@ 279 | | | $ @ 280 | |__| @ 281 | @@ 282 | _______ @ 283 | / _____|@ 284 | | | __ $@ 285 | | | |_ |$@ 286 | | |__| |$@ 287 | \______|$@ 288 | $@@ 289 | __ __ $@ 290 | | | | |$@ 291 | | |__| |$@ 292 | | __ |$@ 293 | | | | |$@ 294 | |__| |__|$@ 295 | $@@ 296 | __ $@ 297 | | |$@ 298 | | |$@ 299 | | |$@ 300 | | |$@ 301 | |__|$@ 302 | $@@ 303 | __ $@ 304 | | |$@ 305 | | |$@ 306 | .--. | |$@ 307 | | ` + "`" + `--' |$@ 308 | \______/ $@ 309 | $@@ 310 | __ ___$@ 311 | | |/ /$@ 312 | | ' / $@ 313 | | < $@ 314 | | . \ $@ 315 | |__|\__\$@ 316 | $@@ 317 | __ $@ 318 | | | $@ 319 | | | $@ 320 | | | $@ 321 | | ` + "`" + `----.@ 322 | |_______|@ 323 | $@@ 324 | .___ ___.$@ 325 | | \/ |$@ 326 | | \ / |$@ 327 | | |\/| |$@ 328 | | | | |$@ 329 | |__| |__|$@ 330 | $@@ 331 | .__ __.$@ 332 | | \ | |$@ 333 | | \| |$@ 334 | | . ` + "`" + ` |$@ 335 | | |\ |$@ 336 | |__| \__|$@ 337 | $@@ 338 | ______ $@ 339 | / __ \ $@ 340 | | | | |$@ 341 | | | | |$@ 342 | | ` + "`" + `--' |$@ 343 | \______/ $@ 344 | $@@ 345 | .______ $@ 346 | | _ \ $@ 347 | | |_) |$@ 348 | | ___/ $@ 349 | | | $ @ 350 | | _| $ @ 351 | $ @@ 352 | ______ $ @ 353 | / __ \ $ @ 354 | | | | | $ @ 355 | | | | | $ @ 356 | | ` + "`" + `--' '--. @ 357 | \_____\_____\@ 358 | $ @@ 359 | .______ $ @ 360 | | _ \ $ @ 361 | | |_) | $ @ 362 | | / $ @ 363 | | |\ \----.@ 364 | | _| ` + "`" + `._____|@ 365 | $@@ 366 | _______.@ 367 | / |@ 368 | | (----` + "`" + `@ 369 | \ \ $@ 370 | .----) | $@ 371 | |_______/ $@ 372 | $@@ 373 | .___________.@ 374 | | |@ 375 | ` + "`" + `---| |----` + "`" + `@ 376 | | | $ @ 377 | | | $ @ 378 | |__| $ @ 379 | $ @@ 380 | __ __ $@ 381 | | | | |$@ 382 | | | | |$@ 383 | | | | |$@ 384 | | ` + "`" + `--' |$@ 385 | \______/ $@ 386 | $@@ 387 | ____ ____$@ 388 | \ \ / /$@ 389 | \ \/ /$ @ 390 | \ /$ @ 391 | \ /$ @ 392 | \__/$ @ 393 | $ @@ 394 | ____ __ ____$@ 395 | \ \ / \ / /$@ 396 | \ \/ \/ /$ @ 397 | \ /$ @ 398 | \ /\ /$ @ 399 | \__/ \__/$ @ 400 | $ @@ 401 | ___ ___$@ 402 | \ \ / /$@ 403 | \ V / $@ 404 | > < $@ 405 | / . \ $@ 406 | /__/ \__\$@ 407 | $@@ 408 | ____ ____$@ 409 | \ \ / /$@ 410 | \ \/ /$ @ 411 | \_ _/$ @ 412 | | |$ @ 413 | |__|$ @ 414 | $ @@ 415 | ________ $@ 416 | | / $@ 417 | ` + "`" + `---/ / $@ 418 | / / $@ 419 | / /----.@ 420 | /________|@ 421 | $@@ 422 | ____ @ 423 | | |@ 424 | | |-` + "`" + `@ 425 | | | $@ 426 | | | $@ 427 | | |-.@ 428 | |____|@@ 429 | ___ @ 430 | \ \ $ @ 431 | \ \$ @ 432 | \ \$ @ 433 | \ \$@ 434 | \__\@ 435 | @@ 436 | ____ @ 437 | | |@ 438 | ` + "`" + `-| |@ 439 | | |@ 440 | | |@ 441 | .-| |@ 442 | |____|@@ 443 | ___ @ 444 | / \ @ 445 | /--^--\@ 446 | $@ 447 | $@ 448 | $@ 449 | $@@ 450 | @ 451 | @ 452 | @ 453 | $ @ 454 | $ @ 455 | ______ @ 456 | |______|@@ 457 | __ @ 458 | ( _)@ 459 | \| @ 460 | $ @ 461 | $ @ 462 | $ @ 463 | @@ 464 | ___ $ @ 465 | / \ $ @ 466 | / ^ \$ @ 467 | / /_\ \$ @ 468 | / _____ \$ @ 469 | /__/ \__\$@ 470 | $@@ 471 | .______ $@ 472 | | _ \ $@ 473 | | |_) |$@ 474 | | _ < $@ 475 | | |_) |$@ 476 | |______/ $@ 477 | $@@ 478 | ______$@ 479 | / |@ 480 | | ,----'@ 481 | | | $@ 482 | | ` + "`" + `----.@ 483 | \______|@ 484 | $@@ 485 | _______ $@ 486 | | \$@ 487 | | .--. |@ 488 | | | | |@ 489 | | '--' |@ 490 | |_______/$@ 491 | $@@ 492 | _______ @ 493 | | ____|@ 494 | | |__ $@ 495 | | __| $@ 496 | | |____ @ 497 | |_______|@ 498 | @@ 499 | _______ @ 500 | | ____|@ 501 | | |__ $@ 502 | | __| $@ 503 | | | $ @ 504 | |__| @ 505 | @@ 506 | _______ @ 507 | / _____|@ 508 | | | __ $@ 509 | | | |_ |$@ 510 | | |__| |$@ 511 | \______|$@ 512 | $@@ 513 | __ __ $@ 514 | | | | |$@ 515 | | |__| |$@ 516 | | __ |$@ 517 | | | | |$@ 518 | |__| |__|$@ 519 | $@@ 520 | __ $@ 521 | | |$@ 522 | | |$@ 523 | | |$@ 524 | | |$@ 525 | |__|$@ 526 | $@@ 527 | __ $@ 528 | | |$@ 529 | | |$@ 530 | .--. | |$@ 531 | | ` + "`" + `--' |$@ 532 | \______/ $@ 533 | $@@ 534 | __ ___$@ 535 | | |/ /$@ 536 | | ' / $@ 537 | | < $@ 538 | | . \ $@ 539 | |__|\__\$@ 540 | $@@ 541 | __ $@ 542 | | | $@ 543 | | | $@ 544 | | | $@ 545 | | ` + "`" + `----.@ 546 | |_______|@ 547 | $@@ 548 | .___ ___.$@ 549 | | \/ |$@ 550 | | \ / |$@ 551 | | |\/| |$@ 552 | | | | |$@ 553 | |__| |__|$@ 554 | $@@ 555 | .__ __.$@ 556 | | \ | |$@ 557 | | \| |$@ 558 | | . ` + "`" + ` |$@ 559 | | |\ |$@ 560 | |__| \__|$@ 561 | $@@ 562 | ______ $@ 563 | / __ \ $@ 564 | | | | |$@ 565 | | | | |$@ 566 | | ` + "`" + `--' |$@ 567 | \______/ $@ 568 | $@@ 569 | .______ $@ 570 | | _ \ $@ 571 | | |_) |$@ 572 | | ___/ $@ 573 | | | $ @ 574 | | _| $ @ 575 | $ @@ 576 | ______ $ @ 577 | / __ \ $ @ 578 | | | | | $ @ 579 | | | | | $ @ 580 | | ` + "`" + `--' '--. @ 581 | \_____\_____\@ 582 | $ @@ 583 | .______ $ @ 584 | | _ \ $ @ 585 | | |_) | $ @ 586 | | / $ @ 587 | | |\ \----.@ 588 | | _| ` + "`" + `._____|@ 589 | $@@ 590 | _______.@ 591 | / |@ 592 | | (----` + "`" + `@ 593 | \ \ $@ 594 | .----) | $@ 595 | |_______/ $@ 596 | $@@ 597 | .___________.@ 598 | | |@ 599 | ` + "`" + `---| |----` + "`" + `@ 600 | | | $ @ 601 | | | $ @ 602 | |__| $ @ 603 | $ @@ 604 | __ __ $@ 605 | | | | |$@ 606 | | | | |$@ 607 | | | | |$@ 608 | | ` + "`" + `--' |$@ 609 | \______/ $@ 610 | $@@ 611 | ____ ____$@ 612 | \ \ / /$@ 613 | \ \/ /$ @ 614 | \ /$ @ 615 | \ /$ @ 616 | \__/$ @ 617 | $ @@ 618 | ____ __ ____$@ 619 | \ \ / \ / /$@ 620 | \ \/ \/ /$ @ 621 | \ /$ @ 622 | \ /\ /$ @ 623 | \__/ \__/$ @ 624 | $ @@ 625 | ___ ___$@ 626 | \ \ / /$@ 627 | \ V / $@ 628 | > < $@ 629 | / . \ $@ 630 | /__/ \__\$@ 631 | $@@ 632 | ____ ____$@ 633 | \ \ / /$@ 634 | \ \/ /$ @ 635 | \_ _/$ @ 636 | | |$ @ 637 | |__|$ @ 638 | $ @@ 639 | ________ $@ 640 | | / $@ 641 | ` + "`" + `---/ / $@ 642 | / / $@ 643 | / /----.@ 644 | /________|@ 645 | $@@ 646 | ___@ 647 | / /@ 648 | | |$@ 649 | / /$ @ 650 | \ \$ @ 651 | | |$@ 652 | \__\@@ 653 | __ $@ 654 | | |$@ 655 | | |$@ 656 | | |$@ 657 | | |$@ 658 | | |$@ 659 | |__|$@@ 660 | ___ @ 661 | \ \$ @ 662 | | | @ 663 | \ \@ 664 | / /@ 665 | | | @ 666 | /__/$ @@ 667 | __ _ @ 668 | / \/ |@ 669 | |_/\__/ @ 670 | $ @ 671 | $ @ 672 | $ @ 673 | @@ 674 | _ _ @ 675 | (_)_(_) @ 676 | / \ @ 677 | / _ \ @ 678 | / ___ \ @ 679 | /_/ \_\@ 680 | @@ 681 | _ _ @ 682 | (_)_(_)@ 683 | / _ \ @ 684 | | | | |@ 685 | | |_| |@ 686 | \___/ @ 687 | @@ 688 | _ _ @ 689 | (_) (_)@ 690 | | | | |@ 691 | | | | |@ 692 | | |_| |@ 693 | \___/ @ 694 | @@ 695 | _ _ @ 696 | (_) (_)@ 697 | __ _ @ 698 | / _` + "`" + ` |@ 699 | | (_| |@ 700 | \__,_|@ 701 | @@ 702 | _ _ @ 703 | (_) (_)@ 704 | ___ @ 705 | / _ \ @ 706 | | (_) |@ 707 | \___/ @ 708 | @@ 709 | _ _ @ 710 | (_) (_)@ 711 | _ _ @ 712 | | | | |@ 713 | | |_| |@ 714 | \__,_|@ 715 | @@ 716 | ___ @ 717 | / _ \ @ 718 | | | ) |@ 719 | | |< < @ 720 | | | ) |@ 721 | | ||_/ @ 722 | |_| @@` 723 | -------------------------------------------------------------------------------- /demo/demo.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | "github.com/fatih/color" 7 | "github.com/getwe/figlet4go" 8 | ) 9 | 10 | var flag_str = flag.String("str", "golang", "input string") 11 | 12 | func main() { 13 | flag.Parse() 14 | str := *flag_str 15 | ascii := figlet4go.NewAsciiRender() 16 | // most simple Usage 17 | renderStr, _ := ascii.Render(str) 18 | fmt.Println(renderStr) 19 | 20 | // change the font color 21 | colors := [...]color.Attribute{ 22 | color.FgMagenta, 23 | color.FgYellow, 24 | color.FgBlue, 25 | color.FgCyan, 26 | color.FgRed, 27 | color.FgWhite, 28 | } 29 | options := figlet4go.NewRenderOptions() 30 | options.FontColor = make([]color.Attribute, len(str)) 31 | for i := range options.FontColor { 32 | options.FontColor[i] = colors[i%len(colors)] 33 | } 34 | renderStr, _ = ascii.RenderOpts(str, options) 35 | fmt.Println(renderStr) 36 | 37 | // change the font 38 | options.FontName = "larry3d" 39 | // except the default font,others need to be load from disk 40 | // here is the font : 41 | // ftp://ftp.figlet.org/pub/figlet/fonts/contributed.tar.gz 42 | // ftp://ftp.figlet.org/pub/figlet/fonts/international.tar.gz 43 | // download and extract to the disk,then specify the file path to load 44 | ascii.LoadFont("/usr/local/Cellar/figlet/2.2.5/share/figlet/fonts/") 45 | 46 | renderStr, _ = ascii.RenderOpts(str, options) 47 | fmt.Println(renderStr) 48 | 49 | } 50 | -------------------------------------------------------------------------------- /font.go: -------------------------------------------------------------------------------- 1 | package figlet4go 2 | 3 | import ( 4 | "errors" 5 | "io/ioutil" 6 | "os" 7 | "path/filepath" 8 | "strconv" 9 | "strings" 10 | ) 11 | 12 | type font struct { 13 | hardblank string 14 | height int 15 | fontSlice []string 16 | } 17 | 18 | type fontManager struct { 19 | // font library 20 | fontLib map[string]*font 21 | 22 | // font name to path 23 | fontList map[string]string 24 | } 25 | 26 | func newFontManager() *fontManager { 27 | this := &fontManager{} 28 | 29 | this.fontLib = make(map[string]*font) 30 | this.fontList = make(map[string]string) 31 | this.loadBuildInFont() 32 | 33 | return this 34 | } 35 | 36 | // walk through the path, load all the *.flf font file 37 | func (this *fontManager) loadFont(fontPath string) error { 38 | 39 | return filepath.Walk(fontPath, func(path string, info os.FileInfo, err error) error { 40 | if err != nil { 41 | return err 42 | } 43 | 44 | if info.IsDir() || !strings.HasSuffix(info.Name(), ".flf") { 45 | return nil 46 | } 47 | 48 | fontName := strings.TrimSuffix(info.Name(), ".flf") 49 | this.fontList[fontName] = path 50 | return nil 51 | }) 52 | } 53 | 54 | func (this *fontManager) loadBuildInFont() error { 55 | font, err := this.parseFontContent(builtInFont) 56 | if err != nil { 57 | return err 58 | } 59 | this.fontLib["default"] = font 60 | return nil 61 | } 62 | 63 | func (this *fontManager) loadDiskFont(fontName string) error { 64 | 65 | fontFilePath, ok := this.fontList[fontName] 66 | if !ok { 67 | return errors.New("FontName Not Found.") 68 | } 69 | 70 | // read full file content 71 | fileBuf, err := ioutil.ReadFile(fontFilePath) 72 | if err != nil { 73 | return err 74 | } 75 | 76 | font, err := this.parseFontContent(string(fileBuf)) 77 | if err != nil { 78 | return err 79 | } 80 | 81 | this.fontLib[fontName] = font 82 | return nil 83 | } 84 | 85 | func (this *fontManager) parseFontContent(cont string) (*font, error) { 86 | lines := strings.Split(cont, "\n") 87 | if len(lines) < 1 { 88 | return nil, errors.New("font content error") 89 | } 90 | 91 | // flf2a$ 7 5 16 -1 12 92 | // Fender by Scooter 8/94 (jkratten@law.georgetown.edu) 93 | // 94 | // Explanation of first line: 95 | // flf2 - "magic number" for file identification 96 | // a - should always be `a', for now 97 | // $ - the "hardblank" -- prints as a blank, but can't be smushed 98 | // 7 - height of a character 99 | // 5 - height of a character, not including descenders 100 | // 10 - max line length (excluding comment lines) + a fudge factor 101 | // -1 - default smushmode for this font (like "-m 15" on command line) 102 | // 12 - number of comment lines 103 | 104 | header := strings.Split(lines[0], " ") 105 | 106 | font := &font{} 107 | font.hardblank = header[0][len(header[0])-1:] 108 | font.height, _ = strconv.Atoi(header[1]) 109 | 110 | commentEndLine, _ := strconv.Atoi(header[5]) 111 | font.fontSlice = lines[commentEndLine+1:] 112 | 113 | return font, nil 114 | } 115 | 116 | func (this *fontManager) getFont(fontName string) (*font, error) { 117 | font, ok := this.fontLib[fontName] 118 | if !ok { 119 | err := this.loadDiskFont(fontName) 120 | if err != nil { 121 | font, _ := this.fontLib["default"] 122 | return font, nil 123 | } 124 | } 125 | font, _ = this.fontLib[fontName] 126 | return font, nil 127 | } 128 | -------------------------------------------------------------------------------- /render.go: -------------------------------------------------------------------------------- 1 | package figlet4go 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "github.com/fatih/color" 7 | "strings" 8 | ) 9 | 10 | type RenderOptions struct { 11 | FontName string // font name 12 | 13 | FontColor []color.Attribute // every ascii byte's color 14 | } 15 | 16 | func NewRenderOptions() *RenderOptions { 17 | opt := &RenderOptions{} 18 | opt.FontName = "default" 19 | return opt 20 | } 21 | 22 | type AsciiRender struct { 23 | fontMgr *fontManager 24 | } 25 | 26 | func NewAsciiRender() *AsciiRender { 27 | this := &AsciiRender{} 28 | 29 | this.fontMgr = newFontManager() 30 | return this 31 | } 32 | 33 | // walk through the path, load all the *.flf font file 34 | func (this *AsciiRender) LoadFont(fontPath string) error { 35 | return this.fontMgr.loadFont(fontPath) 36 | } 37 | 38 | // render with default options 39 | func (this *AsciiRender) Render(asciiStr string) (string, error) { 40 | return this.render(asciiStr, NewRenderOptions()) 41 | } 42 | 43 | // render with options 44 | func (this *AsciiRender) RenderOpts(asciiStr string, opts *RenderOptions) (string, error) { 45 | return this.render(asciiStr, opts) 46 | } 47 | 48 | func (this *AsciiRender) convertChar(font *font, char rune) ([]string, error) { 49 | 50 | if char < 0 || char > 127 { 51 | return nil, errors.New("Not Ascii") 52 | } 53 | 54 | height := font.height 55 | begintRow := (int(char) - 32) * height 56 | 57 | word := make([]string, height, height) 58 | 59 | for i := 0; i < height; i++ { 60 | row := font.fontSlice[begintRow+i] 61 | row = strings.Replace(row, "@", "", -1) 62 | row = strings.Replace(row, font.hardblank, " ", -1) 63 | word[i] = row 64 | } 65 | 66 | return word, nil 67 | } 68 | 69 | func (this *AsciiRender) render(asciiStr string, opt *RenderOptions) (string, error) { 70 | 71 | font, _ := this.fontMgr.getFont(opt.FontName) 72 | 73 | wordlist := make([][]string, 0) 74 | for _, char := range asciiStr { 75 | word, err := this.convertChar(font, char) 76 | if err != nil { 77 | return "", err 78 | } 79 | wordlist = append(wordlist, word) 80 | } 81 | 82 | result := "" 83 | 84 | wordColorFunc := make([]func(a ...interface{}) string, len(wordlist)) 85 | for i, _ := range wordColorFunc { 86 | if i < len(opt.FontColor) { 87 | wordColorFunc[i] = color.New(opt.FontColor[i]).SprintFunc() 88 | } else { 89 | wordColorFunc[i] = fmt.Sprint 90 | } 91 | } 92 | 93 | for i := 0; i < font.height; i++ { 94 | for j := 0; j < len(wordlist); j++ { 95 | result += wordColorFunc[j]((wordlist[j][i])) 96 | } 97 | result += "\n" 98 | } 99 | return result, nil 100 | } 101 | -------------------------------------------------------------------------------- /screenshot/demo1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/weberhong/figlet4go/bc879344e874dd110753d17083a2a7acf3136683/screenshot/demo1.jpg --------------------------------------------------------------------------------