woordklok/woordklok.go
Felix Niederwanger 4a7b621986
Make background text red
Color the background text red instead of white.
2022-11-10 21:06:26 +01:00

121 lines
2.2 KiB
Go

package main
import (
"flag"
"fmt"
"os"
"strings"
"time"
)
const ANSI_RED = "\u001b[31m"
const ANSI_GREEN = "\u001b[32m"
const ANSI_RESET = "\u001b[0m"
// Replace occurences of repl with their uppercase in str
func replace(str string, repl []string, prefix string, suffix string) string {
ret := str
for _, r := range repl {
rx := fmt.Sprintf("%s%s%s", prefix, strings.ToUpper(r), suffix)
ret = strings.ReplaceAll(ret, r, rx)
}
return ret
}
func woordklok(tsp time.Time, color bool) string {
// minutes part
klok1 := "hetknisadvzvijftiengkwartbovervoormhalf"
// hours
klok2 := "yachtweezesdrielftienxzevenegenviertwaalfeenvijfuur"
mm := tsp.Minute()
hh := tsp.Hour() % 12
// get the matching text
m := "het is "
if mm >= 55 {
m += "vijf voor"
} else if mm >= 50 {
m += "tien voor"
} else if mm >= 45 {
m += "kwart voor"
} else if mm >= 40 {
m += "tien over half"
} else if mm >= 35 {
m += "vijf over half"
} else if mm >= 30 {
m += "half"
} else if mm >= 25 {
m += "vijf voor half"
} else if mm >= 20 {
m += "tien voor half"
hh++
} else if mm >= 15 {
m += "kwart over"
} else if mm >= 10 {
m += "tien over"
} else if mm >= 5 {
m += "vijf over"
} else {
m += " uur"
}
hours := []string{
"twaalf",
"een",
"twee",
"drie",
"vier",
"vijf",
"zes",
"zeven",
"acht",
"negen",
"tien",
"elf",
}
hour := hours[hh] + " uur"
ret := ""
// 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
}
}
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)
fmt.Println(woord)
}