diff --git a/woordklok.go b/woordklok.go index 2ab3868..15d2c9d 100644 --- a/woordklok.go +++ b/woordklok.go @@ -8,6 +8,10 @@ import ( "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 @@ -72,15 +76,22 @@ func woordklok(tsp time.Time, color bool) string { } hour := hours[hh] + " uur" + ret := "" + // and replace the matching text with the uppercase prefix := "" suffix := "" if color { // Add color definitions - prefix = "\u001b[32m" // ANSI green - suffix = "\u001b[0m" // ANSI reset + prefix = ANSI_GREEN + suffix = ANSI_RED + ret = ANSI_RED } - return fmt.Sprintf("%s%s", replace(klok1, strings.Split(m, " "), prefix, suffix), replace(klok2, strings.Split(hour, " "), prefix, suffix)) + 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 { @@ -96,7 +107,6 @@ func main() { batch := flag.Bool("b", false, "Batch mode (no colors)") flag.Parse() - // Determine color mode colors := *color if !colors { colors = IsTTY()