mirror of
https://github.com/onyx-and-iris/lottery-cli.git
synced 2026-08-17 00:34:25 +00:00
initial commit
This commit is contained in:
75
cmd/lottery/main.go
Normal file
75
cmd/lottery/main.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
|
||||
"charm.land/huh/v2"
|
||||
"github.com/charmbracelet/fang"
|
||||
"github.com/onyx-and-iris/lottery-cli"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var version string
|
||||
|
||||
func versionFromBuild() string {
|
||||
if version != "" {
|
||||
return version
|
||||
}
|
||||
|
||||
info, ok := debug.ReadBuildInfo()
|
||||
if !ok {
|
||||
return "(unable to read version)"
|
||||
}
|
||||
return strings.Split(info.Main.Version, "-")[0]
|
||||
}
|
||||
|
||||
var cmd = &cobra.Command{
|
||||
Use: "lottery",
|
||||
Short: "A CLI for National Lottery games.",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
var selected string
|
||||
|
||||
form := huh.NewForm(
|
||||
huh.NewGroup(
|
||||
huh.NewSelect[string]().
|
||||
Title("Pick a lottery.").
|
||||
Options(
|
||||
huh.NewOption("Lotto", "lotto"),
|
||||
huh.NewOption("EuroMillions", "euromillions"),
|
||||
huh.NewOption("SetForLife", "setforlife"),
|
||||
huh.NewOption("Thunderball", "thunderball"),
|
||||
huh.NewOption("Powerball", "powerball"),
|
||||
).
|
||||
Value(&selected),
|
||||
),
|
||||
)
|
||||
err := form.Run()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
kind, err := lottery.ParseKind(selected)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
l, err := lottery.New(kind)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
l.Draw()
|
||||
fmt.Println(renderDraw(l))
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := fang.Execute(context.Background(), cmd, fang.WithVersion(versionFromBuild())); err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
61
cmd/lottery/render.go
Normal file
61
cmd/lottery/render.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"charm.land/lipgloss/v2"
|
||||
"github.com/onyx-and-iris/lottery-cli"
|
||||
)
|
||||
|
||||
func formatNumberList(numbers []int) string {
|
||||
parts := make([]string, len(numbers))
|
||||
for i, n := range numbers {
|
||||
parts[i] = strconv.Itoa(n)
|
||||
}
|
||||
return strings.Join(parts, " ")
|
||||
}
|
||||
|
||||
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"))
|
||||
|
||||
var title string
|
||||
var lines []string
|
||||
|
||||
switch game := l.(type) {
|
||||
case *lottery.Lotto:
|
||||
title = "Lotto"
|
||||
lines = append(lines, labelStyle.Render("Numbers:")+" "+numbersStyle.Render(formatNumberList(game.Numbers[:])))
|
||||
case *lottery.EuroMillions:
|
||||
title = "EuroMillions"
|
||||
lines = append(lines, labelStyle.Render("Main:")+" "+numbersStyle.Render(formatNumberList(game.Numbers[:])))
|
||||
lines = append(lines, labelStyle.Render("Lucky Stars:")+" "+specialStyle.Render(formatNumberList(game.LuckyStars[:])))
|
||||
case *lottery.SetForLife:
|
||||
title = "Set For Life"
|
||||
lines = append(lines, labelStyle.Render("Main:")+" "+numbersStyle.Render(formatNumberList(game.Numbers[:])))
|
||||
lines = append(lines, labelStyle.Render("Life Ball:")+" "+specialStyle.Render(strconv.Itoa(game.LifeBall)))
|
||||
case *lottery.Thunderball:
|
||||
title = "Thunderball"
|
||||
lines = append(lines, labelStyle.Render("Main:")+" "+numbersStyle.Render(formatNumberList(game.Numbers[:])))
|
||||
lines = append(lines, labelStyle.Render("Thunderball:")+" "+specialStyle.Render(strconv.Itoa(game.Thunderball)))
|
||||
case *lottery.Powerball:
|
||||
title = "Powerball"
|
||||
lines = append(lines, labelStyle.Render("Main:")+" "+numbersStyle.Render(formatNumberList(game.Numbers[:])))
|
||||
lines = append(lines, labelStyle.Render("Powerball:")+" "+specialStyle.Render(strconv.Itoa(game.Powerball)))
|
||||
default:
|
||||
title = "Lottery"
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user