woordklok/woordklok.go

121 lines
2.2 KiB
Go
Raw Permalink Normal View History

2022-10-20 18:42:30 +00:00
package main
import (
"flag"
2022-10-20 18:42:30 +00:00
"fmt"
"os"
2022-10-20 18:42:30 +00:00
"strings"
"time"
)
const ANSI_RED = "\u001b[31m"
const ANSI_GREEN = "\u001b[32m"
const ANSI_RESET = "\u001b[0m"
2022-10-20 18:42:30 +00:00
// Replace occurences of repl with their uppercase in str
func replace(str string, repl []string, prefix string, suffix string) string {
2022-10-20 18:42:30 +00:00
ret := str
for _, r := range repl {
rx := fmt.Sprintf("%s%s%s", prefix, strings.ToUpper(r), suffix)
ret = strings.ReplaceAll(ret, r, rx)
2022-10-20 18:42:30 +00:00
}
return ret
}
func woordklok(tsp time.Time, color bool) string {
2022-10-20 18:42:30 +00:00
// minutes part
klok1 := "hetknisadvzvijftiengkwartbovervoormhalf"
2022-10-20 18:42:30 +00:00
// hours
klok2 := "yachtweezesdrielftienxzevenegenviertwaalfeenvijfuur"
mm := tsp.Minute()
hh := tsp.Hour() % 12
// get the matching text
m := "het is "
2022-10-20 18:42:30 +00:00
if mm >= 55 {
m += "vijf voor"
2022-10-20 18:42:30 +00:00
} else if mm >= 50 {
m += "tien voor"
2022-10-20 18:42:30 +00:00
} else if mm >= 45 {
m += "kwart voor"
2022-10-20 18:42:30 +00:00
} else if mm >= 40 {
m += "tien over half"
2022-10-20 18:42:30 +00:00
} else if mm >= 35 {
m += "vijf over half"
2022-10-20 18:42:30 +00:00
} else if mm >= 30 {
m += "half"
2022-10-20 18:42:30 +00:00
} else if mm >= 25 {
m += "vijf voor half"
2022-10-20 18:42:30 +00:00
} else if mm >= 20 {
m += "tien voor half"
2022-10-20 18:42:30 +00:00
hh++
} else if mm >= 15 {
m += "kwart over"
2022-10-20 18:42:30 +00:00
} else if mm >= 10 {
m += "tien over"
2022-10-20 18:42:30 +00:00
} else if mm >= 5 {
m += "vijf over"
} else {
m += " uur"
2022-10-20 18:42:30 +00:00
}
hours := []string{
"twaalf",
"een",
"twee",
"drie",
"vier",
"vijf",
"zes",
"zeven",
"acht",
"negen",
"tien",
"elf",
}
hour := hours[hh] + " uur"
2022-10-20 18:42:30 +00:00
ret := ""
2022-10-20 18:42:30 +00:00
// and replace the matching text with the uppercase
prefix := ""
suffix := ""
if color {
// Add color definitions
prefix = ANSI_GREEN
suffix = ANSI_RED
ret = ANSI_RED
}
ret += fmt.Sprintf("%s%s", replace(klok1, strings.Split(m, " "), prefix, suffix), replace(klok2, strings.Split(hour, " "), prefix, suffix))
if color {
ret += ANSI_RESET
}
return ret
}
func IsTTY() bool {
if fileInfo, _ := os.Stdout.Stat(); (fileInfo.Mode() & os.ModeCharDevice) != 0 {
return true
} else {
return false
}
2022-10-20 18:42:30 +00:00
}
func main() {
color := flag.Bool("c", false, "Always use colors")
batch := flag.Bool("b", false, "Batch mode (no colors)")
flag.Parse()
colors := *color
if !colors {
colors = IsTTY()
}
if *batch {
colors = false
}
woord := woordklok(time.Now(), colors)
2022-10-20 18:42:30 +00:00
fmt.Println(woord)
}