From 16e8cec82a46e83eccfaed712a3eb7e125f8576b Mon Sep 17 00:00:00 2001 From: "Anand Babu (AB) Periasamy" Date: Thu, 23 Apr 2015 01:02:17 -0700 Subject: [PATCH] field specific color theme --- cmd-ls.go | 9 ++---- pkg/client/s3/trace.go | 3 +- pkg/console/console.go | 52 ++++++++++++++++------------------- pkg/console/themes.go | 62 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 35 deletions(-) create mode 100644 pkg/console/themes.go diff --git a/cmd-ls.go b/cmd-ls.go index 8c9ce518..bf9f5047 100644 --- a/cmd-ls.go +++ b/cmd-ls.go @@ -45,12 +45,9 @@ func printItems(v []*client.Item) { // printItem prints item meta-data func printItem(date time.Time, v int64, name string) { - // v is 0 for buckets - if v == 0 { - console.Infof("%23s %13s %s\n", date.Local().Format(printDate), "", name) - } else { - console.Infof("%23s %13s %s\n", date.Local().Format(printDate), pb.FormatBytes(v), name) - } + fmt.Printf(console.Time("%s ", date.Local().Format(printDate))) + fmt.Printf(console.Size("[%9s] ", pb.FormatBytes(v))) + fmt.Println(console.File("%s", name)) } func doList(clnt client.Client, targetURL string) (string, error) { diff --git a/pkg/client/s3/trace.go b/pkg/client/s3/trace.go index 3b9c1361..6af2add1 100644 --- a/pkg/client/s3/trace.go +++ b/pkg/client/s3/trace.go @@ -22,6 +22,7 @@ import ( "net/http" "net/http/httputil" + "github.com/minio-io/mc/pkg/console" "github.com/minio-io/minio/pkg/iodine" ) @@ -77,6 +78,6 @@ func (t Trace) print(data []byte) { if t.Writer != nil { fmt.Fprintf(t.Writer, "%s", data) } else { - fmt.Printf("%s", data) + console.Debugf("%s", data) } } diff --git a/pkg/console/console.go b/pkg/console/console.go index 769da00f..dd0dd23a 100644 --- a/pkg/console/console.go +++ b/pkg/console/console.go @@ -58,6 +58,13 @@ var ( Debugln = func(a ...interface{}) { themesDB[currThemeName].Debug.Println(a...) } // Debugf prints a debug message with formatting Debugf = func(f string, a ...interface{}) { themesDB[currThemeName].Debug.Printf(f, a...) } + + // File - File("foo.txt") + File = themesDB[currThemeName].File.SprintfFunc() + // Size - Size("12GB") + Size = themesDB[currThemeName].Size.SprintfFunc() + //Time - Time("12 Hours") + Time = themesDB[currThemeName].Time.SprintfFunc() ) // Theme holds console color scheme @@ -66,34 +73,10 @@ type Theme struct { Error *color.Color Info *color.Color Debug *color.Color -} - -// MiniTheme is a color scheme with -// - Red color for Fatal -// - Yellow for Error -// - Green for Info -// - Cyan for Debug -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)), -} - -// WhiteTheme is a color scheme with white colors only -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)), -} - -// NoColorTheme disables color theme -var NoColorTheme = Theme{ - Fatal: (color.New()), - Error: (color.New()), - Info: (color.New()), - Debug: (color.New()), + Size *color.Color + Time *color.Color + File *color.Color + // Reason *color.Color } var ( @@ -117,6 +100,14 @@ func SetTheme(themeName string) error { currThemeName = themeName theme := themesDB[currThemeName] + /* TODO: Uncomment after updating color package + if currThemeName == "nocolor" { + color.NoColor = true + } else { + color.NoColor = false + } + */ + Fatal = func(a ...interface{}) { theme.Fatal.Print(a...); os.Exit(1) } Fatalln = func(a ...interface{}) { theme.Fatal.Println(a...); os.Exit(1) } Fatalf = func(f string, a ...interface{}) { theme.Fatal.Printf(f, a...); os.Exit(1) } @@ -129,6 +120,11 @@ func SetTheme(themeName string) error { Debug = func(a ...interface{}) { theme.Debug.Print(a...) } Debugln = func(a ...interface{}) { theme.Debug.Println(a...) } Debugf = func(f string, a ...interface{}) { theme.Debug.Printf(f, a...) } + + File = theme.File.SprintfFunc() + Size = theme.Size.SprintfFunc() + Time = theme.Time.SprintfFunc() + mutex.Unlock() return nil diff --git a/pkg/console/themes.go b/pkg/console/themes.go new file mode 100644 index 00000000..713c87f7 --- /dev/null +++ b/pkg/console/themes.go @@ -0,0 +1,62 @@ +/* + * Mini Copy, (C) 2015 Minio, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package console + +import "github.com/fatih/color" + +// MiniTheme - Minio's default color theme +/* +var MiniTheme = Theme{ + Debug: (color.New(color.FgWhite, color.Faint, color.Italic)), + Fatal: (color.New(color.FgRed, color.Bold, color.Italic)), + Error: (color.New(color.FgYellow, color.Bold, color.Italic)), + Info: (color.New(color.FgGreen, color.Bold)), + File: (color.New(color.FgCyan, color.Bold)), + Size: (color.New(color.FgYellow, color.Bold)), + Time: (color.New(color.FgGreen, color.Bold)), +}*/ +var MiniTheme = Theme{ + Debug: (color.New(color.FgWhite, color.Faint, color.Italic)), + Fatal: (color.New(color.FgRed, color.Italic)), + Error: (color.New(color.FgYellow, color.Italic)), + Info: (color.New(color.FgGreen, color.Bold)), + File: (color.New(color.FgWhite)), + Size: (color.New(color.FgYellow)), + Time: (color.New(color.FgGreen)), +} + +// WhiteTheme - All white color theme +var WhiteTheme = Theme{ + Debug: (color.New(color.FgWhite, color.Faint, color.Italic)), + Fatal: (color.New(color.FgWhite, color.Bold, color.Italic)), + Error: (color.New(color.FgWhite, color.Bold, color.Italic)), + Info: (color.New(color.FgWhite, color.Bold)), + File: (color.New(color.FgWhite, color.Bold)), + Size: (color.New(color.FgWhite, color.Bold)), + Time: (color.New(color.FgWhite, color.Bold)), +} + +// NoColorTheme - Disables color theme +var NoColorTheme = Theme{ + Debug: (color.New()), + Fatal: (color.New()), + Error: (color.New()), + Info: (color.New()), + File: (color.New()), + Size: (color.New()), + Time: (color.New()), +}