fortlit/main.go

96 lines
1.7 KiB
Go
Raw Normal View History

// nolint: exhaustivestruct, gochecknoglobals, gomnd, forbidigo
2020-01-07 11:45:10 +01:00
package main
import (
"flag"
2020-01-07 11:45:10 +01:00
"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
var flagVersion bool
var (
version = "dev"
commit = "none"
date = "unknown"
)
const lineWords = 10
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() {
flag.BoolVar(&flagVersion, "version", false, "version")
flag.Parse()
if flagVersion {
fmt.Printf("revive %s, commit %s, %s", version, commit, date)
return
}
2020-01-07 11:45:10 +01:00
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"))
wrapText := stringWrap(text, lineWords)
fmt.Print("\n\n")
pterm.DefaultCenter.Println(wrapText)
2021-02-26 13:53:44 +01:00
pterm.DefaultCenter.Println(fmt.Sprintf("✍️ %s - 📖 %s", ColorAuthor.Sprint(q.Author), ColorBook.Sprint(q.Book)))
2020-01-07 11:45:10 +01:00
}