about_render.go (1772B)
1 package tui 2 3 import ( 4 "strings" 5 6 "git.prime8.dev/moncheeta/prime8dev/internal/utils" 7 tea "github.com/charmbracelet/bubbletea" 8 ) 9 10 type aboutASCIIRenderedMsg struct { 11 width int 12 ascii string 13 } 14 15 type aboutNameRenderedMsg struct { 16 ascii string 17 } 18 19 func (m *portfolioModel) aboutASCIIWidthForLayout() (int, bool) { 20 bodyW := max(m.width, 20) 21 if bodyW < 72 { 22 return 0, false 23 } 24 25 leftW := min(max(bodyW/3, 26), 36) 26 return normalizeAboutASCIIWidth(leftW - 4), true 27 } 28 29 func (m *portfolioModel) maybeRenderAboutASCIICmd() tea.Cmd { 30 if m.activeTab != tabAbout { 31 return nil 32 } 33 34 width, ok := m.aboutASCIIWidthForLayout() 35 if !ok { 36 return nil 37 } 38 39 if ascii, cached := m.aboutASCIICache[width]; cached { 40 if strings.TrimSpace(ascii) != "" { 41 return nil 42 } 43 } 44 if m.aboutASCIILoads[width] { 45 return nil 46 } 47 48 m.aboutASCIILoads[width] = true 49 return renderAboutASCIICmd(width) 50 } 51 52 func (m *portfolioModel) maybeRenderAboutNameCmd() tea.Cmd { 53 if m.aboutNameReady || m.aboutNameLoading { 54 return nil 55 } 56 57 m.aboutNameLoading = true 58 name := m.about.Name 59 return func() tea.Msg { 60 return aboutNameRenderedMsg{ascii: utils.AboutNameASCII(name)} 61 } 62 } 63 64 func renderAboutASCIICmd(width int) tea.Cmd { 65 return func() tea.Msg { 66 return aboutASCIIRenderedMsg{ 67 width: width, 68 ascii: loadAboutASCII(width), 69 } 70 } 71 } 72 73 func (m *portfolioModel) applyAboutASCIIRendered(msg aboutASCIIRenderedMsg) { 74 delete(m.aboutASCIILoads, msg.width) 75 if strings.TrimSpace(msg.ascii) == "" { 76 delete(m.aboutASCIICache, msg.width) 77 return 78 } 79 m.aboutASCIICache[msg.width] = msg.ascii 80 } 81 82 func (m *portfolioModel) applyAboutNameRendered(msg aboutNameRenderedMsg) { 83 m.aboutNameLoading = false 84 m.aboutNameReady = true 85 if strings.TrimSpace(msg.ascii) != "" { 86 m.aboutNameASCII = msg.ascii 87 } 88 }