From a4c098ac0b5b6d83ca7e0a1eaaa167e78ef53176 Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Sun, 19 Apr 2015 16:20:00 -0700 Subject: [PATCH] Format of debora output --- cmd/debora/main.go | 11 ++++-- common/colors.go | 84 ++++++++++++++++++++++++++++++++++++++++++++++ common/string.go | 17 ++++++++++ 3 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 common/colors.go diff --git a/cmd/debora/main.go b/cmd/debora/main.go index 47d0fe4b1..714f80917 100644 --- a/cmd/debora/main.go +++ b/cmd/debora/main.go @@ -177,9 +177,14 @@ func cliListProcesses(c *cli.Context) { } else { fmt.Printf("%v processes:\n", remote) for _, proc := range response.Processes { - fmt.Printf(" \"%v\" => `%v` (%v) start:%v end:%v output:%v\n", - proc.Label, proc.ExecPath, proc.Pid, - proc.StartTime, proc.EndTime, proc.OutputPath) + startTimeStr := Green(proc.StartTime.String()) + endTimeStr := proc.EndTime.String() + if !proc.EndTime.IsZero() { + endTimeStr = Red(endTimeStr) + } + fmt.Printf(" %v start:%v end:%v output:%v\n", + RightPadString(Fmt("\"%v\" => `%v` (%v)", proc.Label, proc.ExecPath, proc.Pid), 40), + startTimeStr, endTimeStr, proc.OutputPath) } } } diff --git a/common/colors.go b/common/colors.go new file mode 100644 index 000000000..776b22e2e --- /dev/null +++ b/common/colors.go @@ -0,0 +1,84 @@ +package common + +import ( + "fmt" + "strings" +) + +const ( + ANSIReset = "\x1b[0m" + ANSIBright = "\x1b[1m" + ANSIDim = "\x1b[2m" + ANSIUnderscore = "\x1b[4m" + ANSIBlink = "\x1b[5m" + ANSIReverse = "\x1b[7m" + ANSIHidden = "\x1b[8m" + + ANSIFgBlack = "\x1b[30m" + ANSIFgRed = "\x1b[31m" + ANSIFgGreen = "\x1b[32m" + ANSIFgYellow = "\x1b[33m" + ANSIFgBlue = "\x1b[34m" + ANSIFgMagenta = "\x1b[35m" + ANSIFgCyan = "\x1b[36m" + ANSIFgWhite = "\x1b[37m" + + ANSIBgBlack = "\x1b[40m" + ANSIBgRed = "\x1b[41m" + ANSIBgGreen = "\x1b[42m" + ANSIBgYellow = "\x1b[43m" + ANSIBgBlue = "\x1b[44m" + ANSIBgMagenta = "\x1b[45m" + ANSIBgCyan = "\x1b[46m" + ANSIBgWhite = "\x1b[47m" +) + +// color the string s with color 'color' +// unless s is already colored +func treat(s string, color string) string { + if len(s) > 2 && s[:2] == "\x1b[" { + return s + } else { + return color + s + ANSIReset + } +} + +func treatAll(color string, args ...interface{}) string { + var parts []string + for _, arg := range args { + parts = append(parts, treat(fmt.Sprintf("%v", arg), color)) + } + return strings.Join(parts, "") +} + +func Black(args ...interface{}) string { + return treatAll(ANSIFgBlack, args...) +} + +func Red(args ...interface{}) string { + return treatAll(ANSIFgRed, args...) +} + +func Green(args ...interface{}) string { + return treatAll(ANSIFgGreen, args...) +} + +func Yellow(args ...interface{}) string { + return treatAll(ANSIFgYellow, args...) +} + +func Blue(args ...interface{}) string { + return treatAll(ANSIFgBlue, args...) +} + +func Magenta(args ...interface{}) string { + return treatAll(ANSIFgMagenta, args...) +} + +func Cyan(args ...interface{}) string { + return treatAll(ANSIFgCyan, args...) +} + +func White(args ...interface{}) string { + return treatAll(ANSIFgWhite, args...) +} diff --git a/common/string.go b/common/string.go index f786a2a4f..a4d221b74 100644 --- a/common/string.go +++ b/common/string.go @@ -2,6 +2,23 @@ package common import ( "fmt" + "strings" ) var Fmt = fmt.Sprintf + +func RightPadString(s string, totalLength int) string { + remaining := totalLength - len(s) + if remaining > 0 { + s = s + strings.Repeat(" ", remaining) + } + return s +} + +func LeftPadString(s string, totalLength int) string { + remaining := totalLength - len(s) + if remaining > 0 { + s = strings.Repeat(" ", remaining) + s + } + return s +}