Add colors
Add color supports and the -c and -b options.
This commit is contained in:
parent
3af55f94f5
commit
a1d45eb98f
|
@ -4,7 +4,14 @@ Dutch woordklok, written in go.
|
|||
|
||||
## Usage
|
||||
|
||||
./woordklok
|
||||
The program applies colors if it is run within a tty. To explicitly use colors use `-c`, to never use colors use the batch mode `-b`
|
||||
|
||||
$ ./woordklok
|
||||
HETknISadvzVIJFtiengkwartbOVERvoormhalfyachtweezesdrielftienxzeveNEGENviertwaalfeenvijfUUR
|
||||
|
||||
Give a list of all available options with
|
||||
|
||||
./woordklok -h # Display help message
|
||||
|
||||
## Building
|
||||
|
||||
|
|
71
woordklok.go
71
woordklok.go
|
@ -1,23 +1,26 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Replace occurences of repl with their uppercase in str
|
||||
func repl_upper(str string, repl []string) string {
|
||||
func replace(str string, repl []string, prefix string, suffix string) string {
|
||||
ret := str
|
||||
for _, r := range repl {
|
||||
ret = strings.ReplaceAll(ret, r, strings.ToUpper(r))
|
||||
rx := fmt.Sprintf("%s%s%s", prefix, strings.ToUpper(r), suffix)
|
||||
ret = strings.ReplaceAll(ret, r, rx)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func woordklok(tsp time.Time) string {
|
||||
func woordklok(tsp time.Time, color bool) string {
|
||||
// minutes part
|
||||
klok1 := "HETknISadvzvijftiengkwartbovervoormhalf"
|
||||
klok1 := "hetknisadvzvijftiengkwartbovervoormhalf"
|
||||
// hours
|
||||
klok2 := "yachtweezesdrielftienxzevenegenviertwaalfeenvijfuur"
|
||||
|
||||
|
@ -25,30 +28,32 @@ func woordklok(tsp time.Time) string {
|
|||
hh := tsp.Hour() % 12
|
||||
|
||||
// get the matching text
|
||||
m := "uur"
|
||||
m := "het is "
|
||||
if mm >= 55 {
|
||||
m = "vijf voor"
|
||||
m += "vijf voor"
|
||||
} else if mm >= 50 {
|
||||
m = "tien voor"
|
||||
m += "tien voor"
|
||||
} else if mm >= 45 {
|
||||
m = "kwart voor"
|
||||
m += "kwart voor"
|
||||
} else if mm >= 40 {
|
||||
m = "tien over half"
|
||||
m += "tien over half"
|
||||
} else if mm >= 35 {
|
||||
m = "vijf over half"
|
||||
m += "vijf over half"
|
||||
} else if mm >= 30 {
|
||||
m = "half"
|
||||
m += "half"
|
||||
} else if mm >= 25 {
|
||||
m = "vijf voor half"
|
||||
m += "vijf voor half"
|
||||
} else if mm >= 20 {
|
||||
m = "tien voor half"
|
||||
m += "tien voor half"
|
||||
hh++
|
||||
} else if mm >= 15 {
|
||||
m = "kwart over"
|
||||
m += "kwart over"
|
||||
} else if mm >= 10 {
|
||||
m = "tien over"
|
||||
m += "tien over"
|
||||
} else if mm >= 5 {
|
||||
m = "vijf over"
|
||||
m += "vijf over"
|
||||
} else {
|
||||
m += " uur"
|
||||
}
|
||||
|
||||
hours := []string{
|
||||
|
@ -65,13 +70,41 @@ func woordklok(tsp time.Time) string {
|
|||
"tien",
|
||||
"elf",
|
||||
}
|
||||
hour := hours[hh]
|
||||
hour := hours[hh] + " uur"
|
||||
|
||||
// and replace the matching text with the uppercase
|
||||
return fmt.Sprintf("%s%s", repl_upper(klok1, strings.Split(m, " ")), repl_upper(klok2, strings.Split(hour, " ")))
|
||||
prefix := ""
|
||||
suffix := ""
|
||||
if color {
|
||||
// Add color definitions
|
||||
prefix = "\u001b[32m" // ANSI green
|
||||
suffix = "\u001b[0m" // ANSI reset
|
||||
}
|
||||
return fmt.Sprintf("%s%s", replace(klok1, strings.Split(m, " "), prefix, suffix), replace(klok2, strings.Split(hour, " "), prefix, suffix))
|
||||
}
|
||||
|
||||
func IsTTY() bool {
|
||||
if fileInfo, _ := os.Stdout.Stat(); (fileInfo.Mode() & os.ModeCharDevice) != 0 {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
woord := woordklok(time.Now())
|
||||
color := flag.Bool("c", false, "Always use colors")
|
||||
batch := flag.Bool("b", false, "Batch mode (no colors)")
|
||||
flag.Parse()
|
||||
|
||||
// Determine if you
|
||||
colors := *color
|
||||
if !colors {
|
||||
colors = IsTTY()
|
||||
}
|
||||
if *batch {
|
||||
colors = false
|
||||
}
|
||||
|
||||
woord := woordklok(time.Now(), colors)
|
||||
fmt.Println(woord)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue