fortlit/main.go

78 lines
1.4 KiB
Go
Raw Normal View History

2021-02-26 13:53:44 +01:00
// nolint: exhaustivestruct, gochecknoglobals, gomnd
2020-01-07 11:45:10 +01:00
package main
import (
"fmt"
"math/rand"
2020-02-12 11:14:56 +01:00
"regexp"
2021-02-26 13:53:44 +01:00
"strings"
2020-01-07 11:45:10 +01:00
"time"
2020-02-12 11:14:56 +01:00
2021-02-26 13:53:44 +01:00
"github.com/pterm/pterm"
"go.xsfx.dev/fortlit/quotes"
2020-01-07 11:45:10 +01:00
)
2021-02-26 13:53:44 +01:00
//go:generate go run tools/gen/gen.go
//go:generate gofumpt -w quotes/quotes.go
2020-02-12 11:14:56 +01:00
2021-02-26 13:53:44 +01:00
const lineWords = 8
2020-01-07 11:45:10 +01:00
2021-02-26 13:53:44 +01:00
var (
ColorAuthor = pterm.NewRGB(189, 147, 249)
ColorTime = pterm.NewRGB(80, 250, 123)
ColorBook = pterm.NewRGB(255, 121, 198)
2020-01-07 11:45:10 +01:00
)
2021-02-26 13:53:44 +01:00
func getQuote(qs map[string][]quotes.Quote, t string) quotes.Quote {
var q quotes.Quote
2020-01-07 11:45:10 +01:00
2021-02-26 13:53:44 +01:00
if _, ok := qs[t]; ok {
2020-01-07 11:45:10 +01:00
if len(qs[t]) != 1 {
rand.Seed(time.Now().Unix())
2021-02-26 13:53:44 +01:00
q = qs[t][rand.Intn(len(qs[t]))] // nolint: gosec
} else {
2021-02-26 13:53:44 +01:00
q = qs[t][0]
2020-01-07 11:45:10 +01:00
}
}
2021-02-26 13:53:44 +01:00
return q
2020-01-07 11:45:10 +01:00
}
2021-02-26 13:53:44 +01:00
func stringWrap(text string, limit int) string {
ts := strings.Fields(text)
rs := ""
2020-01-07 11:45:10 +01:00
2021-02-26 13:53:44 +01:00
wc := 0
for _, i := range ts {
if wc < limit {
rs = rs + " " + i
wc++
} else {
rs = rs + " " + i + "\n"
wc = 0
}
2020-01-07 11:45:10 +01:00
}
2021-02-26 13:53:44 +01:00
return rs
2020-01-07 11:45:10 +01:00
}
func main() {
now := time.Now()
t := fmt.Sprintf("%02d:%02d", now.Hour(), now.Minute())
2021-02-26 13:53:44 +01:00
q := getQuote(quotes.FortData, t)
2020-02-12 11:14:56 +01:00
2021-02-26 13:53:44 +01:00
if q == (quotes.Quote{}) {
return
2020-01-07 11:45:10 +01:00
}
2021-02-26 13:53:44 +01:00
m := regexp.MustCompile(fmt.Sprintf("(?i)(%s)", q.Time))
text := m.ReplaceAllString(q.Text, ColorTime.Sprint("$1"))
pterm.DefaultCenter.Println(
pterm.DefaultBox.Sprint(
stringWrap(text, lineWords),
),
)
pterm.DefaultCenter.Println(fmt.Sprintf("✍️ %s - 📖 %s", ColorAuthor.Sprint(q.Author), ColorBook.Sprint(q.Book)))
2020-01-07 11:45:10 +01:00
}