From ef4059c144584326b8bc845d5d12e2699492913e Mon Sep 17 00:00:00 2001 From: boyska Date: Wed, 2 Jan 2019 17:37:06 +0100 Subject: [PATCH] -color more similar to grep --- cmd/circolog-tail/main.go | 45 +++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/cmd/circolog-tail/main.go b/cmd/circolog-tail/main.go index 2d335fa..3c44afe 100644 --- a/cmd/circolog-tail/main.go +++ b/cmd/circolog-tail/main.go @@ -20,6 +20,39 @@ import ( "gopkg.in/mgo.v2/bson" ) +type BoolAuto uint + +const ( + BoolAuto_NO BoolAuto = iota + BoolAuto_YES BoolAuto = iota + BoolAuto_AUTO BoolAuto = iota +) + +func (b *BoolAuto) String() string { + switch *b { + case BoolAuto_NO: + return "no" + case BoolAuto_YES: + return "always" + case BoolAuto_AUTO: + return "auto" + } + return "" +} +func (b *BoolAuto) Set(s string) error { + switch s { + case "auto": + *b = BoolAuto_AUTO + case "always": + *b = BoolAuto_YES + case "no": + *b = BoolAuto_NO + default: + return fmt.Errorf("Invalid value %s", s) + } + return nil +} + func main() { addr := flag.String("addr", "localhost:9080", "http service address") querySocket := flag.String("socket", "", "Path to a unix domain socket for the HTTP server") @@ -27,17 +60,11 @@ func main() { var filter filtering.ExprValue flag.Var(&filter, "where", "sql-like query to filter logs") // TODO: change to color-mode=auto/no/always - noColor := flag.Bool("no-color", false, "disable colors") - forceColor := flag.Bool("force-color", false, "force colors even on TTY") + hasColor := BoolAuto_AUTO + flag.Var(&hasColor, "color", "dis/enable colors") flag.Parse() - if *noColor && *forceColor { - fmt.Fprintln(os.Stderr, "Can't use both -no-color and -force-color") - flag.Usage() - os.Exit(2) - } - - if *noColor || (!isatty.IsTerminal(os.Stdout.Fd()) && !*forceColor) { + if hasColor == BoolAuto_NO || (!isatty.IsTerminal(os.Stdout.Fd()) && hasColor != BoolAuto_YES) { ansi.DisableColors(true) }