diff --git a/main.go b/main.go index c69eb193..64262985 100644 --- a/main.go +++ b/main.go @@ -88,6 +88,7 @@ func registerBefore(ctx *cli.Context) error { console.Errorf("Invalid theme, please choose from the following list: %s.\n", console.GetThemeNames()) return errInvalidTheme{Theme: themeName} } + if err := console.SetTheme(themeName); err != nil { console.Errorf("Failed to set theme ā€˜%s’.", themeName) errorIf(err) diff --git a/pkg/console/console.go b/pkg/console/console.go index dd1c80d9..57d7ed95 100644 --- a/pkg/console/console.go +++ b/pkg/console/console.go @@ -209,19 +209,60 @@ var ( } // Time helper to print Time theme - Time = themesDB[currThemeName].Time.SprintfFunc() + Time = func(format string, data ...interface{}) string { + if isatty.IsTerminal(os.Stdout.Fd()) { + return themesDB[currThemeName].Time.SprintfFunc()(format, data...) + } + return fmt.Sprintf(format, data...) + } + // Size helper to print Size theme - Size = themesDB[currThemeName].Size.SprintfFunc() + Size = func(format string, data ...interface{}) string { + if isatty.IsTerminal(os.Stdout.Fd()) { + return themesDB[currThemeName].Size.SprintfFunc()(format, data...) + } + return fmt.Sprintf(format, data...) + } + // File helper to print File theme - File = themesDB[currThemeName].File.SprintfFunc() + File = func(format string, data ...interface{}) string { + if isatty.IsTerminal(os.Stdout.Fd()) { + return themesDB[currThemeName].File.SprintfFunc()(format, data...) + } + return fmt.Sprintf(format, data...) + } + // Dir helper to print Dir theme - Dir = themesDB[currThemeName].Dir.SprintfFunc() + Dir = func(format string, data ...interface{}) string { + if isatty.IsTerminal(os.Stdout.Fd()) { + return themesDB[currThemeName].Dir.SprintfFunc()(format, data...) + } + return fmt.Sprintf(format, data...) + } + // Command helper to print command theme - Command = themesDB[currThemeName].Command.SprintfFunc() + Command = func(format string, data ...interface{}) string { + if isatty.IsTerminal(os.Stdout.Fd()) { + return themesDB[currThemeName].Command.SprintfFunc()(format, data...) + } + return fmt.Sprintf(format, data...) + } + // SessionID helper to print sessionid theme - SessionID = themesDB[currThemeName].SessionID.SprintfFunc() + SessionID = func(format string, data ...interface{}) string { + if isatty.IsTerminal(os.Stdout.Fd()) { + return themesDB[currThemeName].SessionID.SprintfFunc()(format, data...) + } + return fmt.Sprintf(format, data...) + } + // JSON helper to print json strings - JSON = themesDB[currThemeName].JSON.SprintfFunc() + JSON = func(format string, data ...interface{}) string { + if isatty.IsTerminal(os.Stdout.Fd()) { + return themesDB[currThemeName].JSON.SprintfFunc()(format, data...) + } + return fmt.Sprintf(format, data...) + } ) var (