1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
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
}
var (
winIcon = lipgloss.NewStyle().Foreground(lipgloss.Color("#f1c40f")).Render("🏆")
loseIcon = lipgloss.NewStyle().Foreground(lipgloss.Color("#895129")).Render("💩")
drawIcon = lipgloss.NewStyle().Foreground(lipgloss.Color("#bdc3c7")).Render("🤝")
)
func formatGamesForPage(userID int, 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))
icon := " "
if game.Outcome != "*" {
if len(game.Outcome) >= 2 {
if game.Outcome[0:2] == "1-" {
if game.Player1.ID == userID {
icon = winIcon
} else {
icon = loseIcon
}
} else if game.Outcome[0:2] == "0-" {
if game.Player2 != nil && game.Player2.ID == userID {
icon = winIcon
} else {
icon = loseIcon
}
} else {
icon = drawIcon
}
}
}
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,
icon,
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,
)),
)
}
|