prime8dev

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

about_ascii.go (886B)


      1 package tui
      2 
      3 import (
      4 	"path/filepath"
      5 	"strings"
      6 	"sync"
      7 
      8 	aic "github.com/TheZoraiz/ascii-image-converter/aic_package"
      9 )
     10 
     11 var aboutASCIIConvertMu sync.Mutex
     12 
     13 func (m portfolioModel) aboutASCIIFor(width int) string {
     14 	width = normalizeAboutASCIIWidth(width)
     15 
     16 	if m.aboutASCIICache != nil {
     17 		if ascii, ok := m.aboutASCIICache[width]; ok {
     18 			return ascii
     19 		}
     20 	}
     21 
     22 	return ""
     23 }
     24 
     25 func normalizeAboutASCIIWidth(width int) int {
     26 	width = max(width, 20)
     27 	if width > 56 {
     28 		return 56
     29 	}
     30 	return width
     31 }
     32 
     33 func loadAboutASCII(width int) string {
     34 	flags := aic.DefaultFlags()
     35 	flags.Width = width
     36 	flags.Colored = true
     37 	flags.Braille = false
     38 	flags.Dither = true
     39 	flags.Threshold = 160
     40 
     41 	aboutASCIIConvertMu.Lock()
     42 	defer aboutASCIIConvertMu.Unlock()
     43 
     44 	ascii, err := aic.Convert(filepath.Join("static", "me.jpg"), flags)
     45 	if err != nil {
     46 		return ""
     47 	}
     48 
     49 	return strings.TrimRight(ascii, "\n")
     50 }