diff --git a/cmd-ls.go b/cmd-ls.go index 502feb42..1490271f 100644 --- a/cmd-ls.go +++ b/cmd-ls.go @@ -17,7 +17,6 @@ package main import ( - "fmt" "time" "github.com/cheggaaa/pb" @@ -35,10 +34,7 @@ const ( // printBuckets lists buckets and its metadata func printBuckets(v []*client.Bucket) { for _, b := range v { - // TODO: escape sequences internally in color package clobber the output, format this prior - // find a way out - msg := fmt.Sprintf("%23s %13s %s", b.CreationDate.Local().Format(printDate), "", b.Name) - console.Infoln(msg) + console.Infof("%23s %13s %s\n", b.CreationDate.Local().Format(printDate), "", b.Name) } } @@ -54,10 +50,7 @@ func printObjects(v []*client.Item) { // printObject prints object meta-data func printObject(date time.Time, v int64, key string) { - // TODO: escape sequences internally in color package clobber the output, format this prior - // find a way out - msg := fmt.Sprintf("%23s %13s %s", date.Local().Format(printDate), pb.FormatBytes(v), key) - console.Infoln(msg) + console.Infof("%23s %13s %s\n", date.Local().Format(printDate), pb.FormatBytes(v), key) } // doListCmd lists objects inside a bucket diff --git a/cmd-options.go b/cmd-options.go index 8db7cc17..8d78eadd 100644 --- a/cmd-options.go +++ b/cmd-options.go @@ -17,7 +17,11 @@ package main import ( + "fmt" + "strings" + "github.com/minio-io/cli" + "github.com/minio-io/mc/pkg/console" ) // List of commands @@ -201,6 +205,20 @@ var options = []cli.Command{ var ( flags = []cli.Flag{ + cli.StringFlag{ + Name: "theme", + Usage: fmt.Sprintf("Choose a console theme from this list [%s]", func() string { + keys := []string{} + for key := range console.ThemesDB { + if console.GetTheme() == key { + key = "*" + key + "*" + } + + keys = append(keys, key) + } + return strings.Join(keys, ", ") + }()), + }, cli.BoolFlag{ Name: "debug", Usage: "Enable HTTP tracing", diff --git a/main.go b/main.go index f0e26ae2..01106348 100644 --- a/main.go +++ b/main.go @@ -101,6 +101,14 @@ func main() { } else { log.Debug = log.New(ioutil.Discard, "", 0) } + + theme := ctx.GlobalString("theme") + if theme != "" { + err := console.SetTheme(theme) + if err != nil { + console.Fatalf("Unable to set theme [%s]\n", console.GetTheme()) + } + } checkConfig() return nil } diff --git a/pkg/console/console.go b/pkg/console/console.go index 9d8cb300..828b5a47 100644 --- a/pkg/console/console.go +++ b/pkg/console/console.go @@ -17,72 +17,137 @@ package console import ( + "errors" + "fmt" "os" + "sync" "github.com/fatih/color" ) -// Fatal - print in red and exit -func Fatal(msgs ...interface{}) { - red := color.New(color.FgRed, color.Bold) - colorPrint(red, msgs) - os.Exit(1) +var ( + mutex = &sync.RWMutex{} + currTheme = "minimal" + + // ThemesDB contains supported list of Themes + ThemesDB = map[string]Theme{"minimal": MiniTheme, "nocolor": NoColorTheme, "white": WhiteTheme} + // Fatal prints a fatal message and exits + Fatal = ThemesDB[currTheme].Fatal.PrintFunc() + // Fatalln prints a fatal message with a new line and exits + Fatalln = ThemesDB[currTheme].Fatal.PrintlnFunc() + // Fatalf prints a fatal message with formatting and exits + Fatalf = ThemesDB[currTheme].Fatal.PrintfFunc() + // Error prints a error message + Error = ThemesDB[currTheme].Error.PrintFunc() + // Errorln prints a error message with a new line + Errorln = ThemesDB[currTheme].Error.PrintlnFunc() + // Errorf prints a error message with formatting + Errorf = ThemesDB[currTheme].Error.PrintfFunc() + // Info prints a informational message + Info = ThemesDB[currTheme].Info.PrintFunc() + // Infoln prints a informational message with a new line + Infoln = ThemesDB[currTheme].Info.PrintlnFunc() + // Infof prints a informational message with formatting + Infof = ThemesDB[currTheme].Info.PrintfFunc() + // Debug prints a debug message + Debug = ThemesDB[currTheme].Debug.PrintFunc() + // Debugln prints a debug message with a new line + Debugln = ThemesDB[currTheme].Debug.PrintlnFunc() + // Debugf prints a debug message with formatting + Debugf = ThemesDB[currTheme].Debug.PrintfFunc() +) + +// Theme holds console color scheme +type Theme struct { + Fatal *color.Color + Error *color.Color + Info *color.Color + Debug *color.Color } -// Fatalln - print in red with newline and exit -func Fatalln(msgs ...interface{}) { - red := color.New(color.FgRed, color.Bold) - colorPrintln(red, msgs) - os.Exit(1) +// MiniTheme is a color scheme set by minimal +var MiniTheme = Theme{ + Fatal: (color.New(color.FgRed, color.Bold)), + Error: (color.New(color.FgYellow, color.Bold)), + Info: (color.New(color.FgGreen, color.Bold)), + Debug: (color.New(color.FgCyan, color.Bold)), } -// Fatalf - format in red and exit -func Fatalf(format string, msgs ...interface{}) { - red := color.New(color.FgRed, color.Bold) - red.Printf(format, msgs) - os.Exit(1) +// MiniTheme is a color scheme set by minimal +var WhiteTheme = Theme{ + Fatal: (color.New(color.FgWhite, color.Bold)), + Error: (color.New(color.FgWhite, color.Bold)), + Info: (color.New(color.FgWhite, color.Bold)), + Debug: (color.New(color.FgWhite, color.Bold)), } -// Error - print in yellow -func Error(msgs ...interface{}) { - yellow := color.New(color.FgYellow, color.Bold) - colorPrint(yellow, msgs) -} +// NoColorTheme disables color theme +var NoColorTheme = Theme{} -// Errorln - print in yellow with newline -func Errorln(msgs ...interface{}) { - yellow := color.New(color.FgYellow, color.Bold) - colorPrintln(yellow, msgs) -} - -// Info - print in green -func Info(msgs ...interface{}) { - green := color.New(color.FgGreen, color.Bold) - colorPrint(green, msgs) -} - -// Infoln - print in green with newline -func Infoln(msgs ...interface{}) { - green := color.New(color.FgGreen, color.Bold) - colorPrintln(green, msgs) -} - -// Infof - format in green -func Infof(format string, msgs ...interface{}) { - green := color.New(color.FgGreen, color.Bold) - green.Printf(format, msgs) -} - -func colorPrint(c *color.Color, msgs []interface{}) { - for i, msg := range msgs { - if i != 0 { - c.Print(" ") +func isValidTheme(name string) bool { + for key := range ThemesDB { + if key == name { + return true } - c.Print(msg) } + return false } -func colorPrintln(c *color.Color, msgs []interface{}) { - colorPrint(c, msgs) - c.Println() +// SetTheme sets a color theme +func SetTheme(name string) error { + mutex.Lock() + currTheme = name + if currTheme == "" { + currTheme = "minimal" + } + if !isValidTheme(currTheme) { + return errors.New("Invalid theme") + } + + print := func(a ...interface{}) { fmt.Print(a...) } + println := func(a ...interface{}) { fmt.Println(a...) } + printf := func(f string, a ...interface{}) { fmt.Printf(f, a...) } + + fatalPrint := func(a ...interface{}) { fmt.Print(a...); os.Exit(1) } + fatalPrintln := func(a ...interface{}) { fmt.Println(a...); os.Exit(1) } + fatalPrintf := func(f string, a ...interface{}) { fmt.Printf(f, a...); os.Exit(1) } + + if currTheme == "nocolor" { + + Fatal = fatalPrint + Fatalln = fatalPrintln + Fatalf = fatalPrintf + Error = print + Errorln = println + Errorf = printf + Info = print + Infoln = println + Infof = printf + Debug = print + Debugln = println + Debugf = printf + } else { + if currTheme == "" { + currTheme = "minimal" + } + Fatal = func(a ...interface{}) { ThemesDB[currTheme].Fatal.Print(a...); os.Exit(1) } + Fatalln = func(a ...interface{}) { ThemesDB[currTheme].Fatal.Println(a...); os.Exit(1) } + Fatalf = func(f string, a ...interface{}) { ThemesDB[currTheme].Fatal.Printf(f, a...); os.Exit(1) } + Error = func(a ...interface{}) { ThemesDB[currTheme].Error.Print(a...) } + Errorln = func(a ...interface{}) { ThemesDB[currTheme].Error.Println(a...) } + Errorf = func(f string, a ...interface{}) { ThemesDB[currTheme].Error.Printf(f, a...) } + Info = func(a ...interface{}) { ThemesDB[currTheme].Info.Print(a...) } + Infoln = func(a ...interface{}) { ThemesDB[currTheme].Info.Println(a...) } + Infof = func(f string, a ...interface{}) { ThemesDB[currTheme].Info.Printf(f, a...) } + Debug = func(a ...interface{}) { ThemesDB[currTheme].Debug.Print(a...) } + Debugln = func(a ...interface{}) { ThemesDB[currTheme].Debug.Println(a...) } + Debugf = func(f string, a ...interface{}) { ThemesDB[currTheme].Debug.Printf(f, a...) } + } + mutex.Unlock() + return nil +} + +// GetTheme returns currently set theme +func GetTheme() string { + return currTheme }