clone

fifteen puzzle games for the terminal, with daily puzzles, weekly ladders and xp, built on bubble tea

feat/app-decomposition
puzzletea / app / notice.go
notice.go 1.7K
1package app23import (4 "fmt"5 "log"67 "github.com/FelineStateMachine/puzzletea/ui"89 "charm.land/lipgloss/v2"10)1112type noticeLevel string1314const (15 noticeLevelError noticeLevel = "error"16 noticeLevelSuccess noticeLevel = "success"17)1819type noticeState struct {20 level noticeLevel21 message string22}2324func (m model) clearNotice() model {25 m.notice = noticeState{}26 return m27}2829func (m model) setErrorf(format string, args ...any) model {30 message := fmt.Sprintf(format, args...)31 log.Printf("%s", message)32 m.notice = noticeState{33 level: noticeLevelError,34 message: message,35 }36 return m37}3839func (m model) setSuccessf(format string, args ...any) model {40 message := fmt.Sprintf(format, args...)41 m.notice = noticeState{42 level: noticeLevelSuccess,43 message: message,44 }45 return m46}4748func (m model) appendNotice(content string) string {49 return appendNoticeContent(m.width, m.notice, content)50}5152func appendNoticeContent(width int, notice noticeState, content string) string {53 if notice.message == "" {54 return content55 }5657 style := ui.ErrorNoticeStyle()58 if notice.level == noticeLevelSuccess {59 style = ui.SuccessNoticeStyle()60 }61 block := style.62 MaxWidth(max(width-8, 24)).63 Render(notice.message)64 if content == "" {65 return block66 }67 return lipgloss.JoinVertical(lipgloss.Left, content, "", block)68}6970func centerContentWithNotice(width, height int, notice noticeState, content string) string {71 return ui.CenterView(width, height, appendNoticeContent(width, notice, content))72}7374func renderPanelView(width, height int, notice noticeState, title, content, footer string) string {75 panel := ui.Panel(title, appendNoticeContent(width, notice, content), footer)76 return ui.CenterView(width, height, panel)77}