summaryrefslogtreecommitdiff
path: root/pkg/ui/views/play_util.go
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2025-04-17 10:52:47 +0200
committerSanto Cariotti <santo@dcariotti.me>2025-04-17 10:52:47 +0200
commit313c96613153d92e4964bef4d2469b09a9505597 (patch)
tree0b7e8e58f826a2ef4821145eb27b045cf760849c /pkg/ui/views/play_util.go
parent39a594829ebddc0bc06b92465241439f81fca205 (diff)
Split views on subfiles
Diffstat (limited to 'pkg/ui/views/play_util.go')
-rw-r--r--pkg/ui/views/play_util.go74
1 files changed, 74 insertions, 0 deletions
diff --git a/pkg/ui/views/play_util.go b/pkg/ui/views/play_util.go
new file mode 100644
index 0000000..c57f331
--- /dev/null
+++ b/pkg/ui/views/play_util.go
@@ -0,0 +1,74 @@
+package views
+
+import (
+ "fmt"
+ "strings"
+
+ "github.com/boozec/rahanna/internal/api/database"
+ tea "github.com/charmbracelet/bubbletea"
+ "github.com/charmbracelet/lipgloss"
+)
+
+func (m PlayModel) handleWindowSize(msg tea.WindowSizeMsg) (tea.Model, tea.Cmd) {
+ m.width = msg.Width
+ m.height = msg.Height
+ return m, nil
+}
+
+func (m *PlayModel) handleError(msg error) (tea.Model, tea.Cmd) {
+ m.isLoading = false
+ m.err = msg
+ return m, nil
+}
+
+func formatGamesForPage(games []database.Game, altCodeStyle lipgloss.Style) []string {
+ var gamesStrings []string
+ gamesStrings = append(gamesStrings, "Games list")
+
+ longestName := 0
+ for _, game := range games {
+ if len(game.Name) > longestName {
+ longestName = len(game.Name)
+ }
+ }
+
+ for i, game := range games {
+ indexStr := altCodeStyle.Render(fmt.Sprintf("[%d] ", i))
+ nameStr := game.Name
+ dateStr := game.UpdatedAt.Format("2006-01-02 15:04")
+
+ padding := longestName - len(nameStr)
+ paddingStr := strings.Repeat(" ", padding+4)
+
+ line := lipgloss.JoinHorizontal(lipgloss.Left,
+ indexStr,
+ nameStr,
+ paddingStr,
+ lipgloss.NewStyle().Foreground(lipgloss.Color("#d35400")).Render(dateStr),
+ )
+ gamesStrings = append(gamesStrings, line)
+ }
+ return gamesStrings
+}
+
+func (m PlayModel) buildWindowContent(content string, formWidth int) string {
+ if m.err != nil {
+ formError := fmt.Sprintf("Error: %v", m.err.Error())
+ return lipgloss.JoinVertical(
+ lipgloss.Center,
+ windowStyle.Width(formWidth).Render(lipgloss.JoinVertical(
+ lipgloss.Center,
+ errorStyle.Align(lipgloss.Center).Width(formWidth-4).Render(formError),
+ content,
+ )),
+ )
+ }
+
+ return lipgloss.JoinVertical(
+ lipgloss.Center,
+ windowStyle.Width(formWidth).Render(lipgloss.JoinVertical(
+ lipgloss.Center,
+ content,
+ )),
+ )
+}