diff --git a/cmd/lottery/main.go b/cmd/lottery/main.go index d5d7e1a..0b5ee6d 100644 --- a/cmd/lottery/main.go +++ b/cmd/lottery/main.go @@ -81,10 +81,19 @@ var cmd = &cobra.Command{ } count := viper.GetInt("count") + includeDrawHeading := count > 1 + renders := make([]string, 0, count) + drawTitle := "Lottery" - for range count { + for i := range count { l.Draw() - fmt.Println(renderDraw(l)) + title, entry := renderDrawEntry(l, i+1, includeDrawHeading) + drawTitle = title + renders = append(renders, entry) + } + + if len(renders) > 0 { + fmt.Println(renderDrawCollection(drawTitle, renders)) } return nil diff --git a/cmd/lottery/render.go b/cmd/lottery/render.go index e0acccc..2853934 100644 --- a/cmd/lottery/render.go +++ b/cmd/lottery/render.go @@ -9,6 +9,24 @@ import ( "github.com/onyx-and-iris/lottery-cli" ) +// nolint:misspell +var ( + titleStyle = lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("12")) + drawHeadingStyle = lipgloss.NewStyle(). + Bold(true). + Underline(true). + Foreground(lipgloss.Color("14")) + labelStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("8")) + numbersStyle = lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("10")) + specialStyle = lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("11")) + separatorStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("8")) + cardStyle = lipgloss.NewStyle(). + BorderStyle(lipgloss.RoundedBorder()). + BorderForeground(lipgloss.Color("8")). + Padding(0, 1). + MarginTop(1) +) + func formatNumberList(numbers []int) string { parts := make([]string, len(numbers)) for i, n := range numbers { @@ -17,13 +35,7 @@ func formatNumberList(numbers []int) string { return strings.Join(parts, " ") } -// nolint:misspell -func renderDraw(l lottery.Lottery) string { - titleStyle := lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("12")) - labelStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("8")) - numbersStyle := lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("10")) - specialStyle := lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("11")) - +func drawTitleAndLines(l lottery.Lottery) (string, []string) { var title string var lines []string @@ -91,12 +103,44 @@ func renderDraw(l lottery.Lottery) string { lines = append(lines, "Unknown lottery type") } - body := strings.Join(lines, "\n") - card := lipgloss.NewStyle(). - BorderStyle(lipgloss.RoundedBorder()). - BorderForeground(lipgloss.Color("8")). - Padding(0, 1). - MarginTop(1) - - return card.Render(titleStyle.Render(title+" draw") + "\n" + body) + return title, lines +} + +func renderDrawEntry(l lottery.Lottery, drawNumber int, includeHeading bool) (string, string) { + title, lines := drawTitleAndLines(l) + + entry := make([]string, 0, len(lines)+1) + if includeHeading { + entry = append(entry, drawHeadingStyle.Render("Draw "+strconv.Itoa(drawNumber))) + } + entry = append(entry, lines...) + + return title, strings.Join(entry, "\n") +} + +func maxLineWidth(blocks []string) int { + maxWidth := 0 + for _, block := range blocks { + for line := range strings.SplitSeq(block, "\n") { + width := lipgloss.Width(line) + if width > maxWidth { + maxWidth = width + } + } + } + if maxWidth < 12 { + return 12 + } + return maxWidth +} + +func renderDrawCollection(title string, entries []string) string { + if len(entries) == 0 { + return "" + } + + separator := separatorStyle.Render(strings.Repeat("─", maxLineWidth(entries))) + body := strings.Join(entries, "\n"+separator+"\n") + + return cardStyle.Render(titleStyle.Render(title+" draws") + "\n" + body) }