prime8dev

prime8dev
git clone https://git.prime8.dev/prime8dev.git
Log | Files | Refs | README | LICENSE

ascii.go (712B)


      1 package utils
      2 
      3 import (
      4 	"strings"
      5 	"sync"
      6 
      7 	asciiart "github.com/romance-dev/ascii-art"
      8 	_ "github.com/romance-dev/ascii-art/fonts/doom"
      9 )
     10 
     11 var aboutNameASCIICache sync.Map
     12 
     13 func AboutNameASCII(name string) string {
     14 	name = strings.TrimSpace(name)
     15 	if name == "" {
     16 		return ""
     17 	}
     18 
     19 	if cached, ok := aboutNameASCIICache.Load(name); ok {
     20 		if ascii, ok := cached.(string); ok {
     21 			return ascii
     22 		}
     23 	}
     24 
     25 	rendered := name
     26 	func() {
     27 		defer func() {
     28 			if recover() != nil {
     29 				rendered = name
     30 			}
     31 		}()
     32 
     33 		fig := asciiart.NewFigure(name, "doom", false)
     34 		ascii := strings.TrimRight(fig.String(), "\n")
     35 		if ascii != "" {
     36 			rendered = ascii
     37 		}
     38 	}()
     39 
     40 	aboutNameASCIICache.Store(name, rendered)
     41 	return rendered
     42 }