diff options
Diffstat (limited to 'cmd/rr/util')
-rw-r--r-- | cmd/rr/util/cprint.go | 28 | ||||
-rw-r--r-- | cmd/rr/util/list.go | 58 |
2 files changed, 86 insertions, 0 deletions
diff --git a/cmd/rr/util/cprint.go b/cmd/rr/util/cprint.go new file mode 100644 index 00000000..0985de62 --- /dev/null +++ b/cmd/rr/util/cprint.go @@ -0,0 +1,28 @@ +package util + +import ( + "fmt" + "github.com/mgutz/ansi" + "regexp" + "strings" +) + +var reg *regexp.Regexp + +func init() { + reg, _ = regexp.Compile(`<([^>]+)>`) +} + +// Printf works identically to fmt.Print but adds `<white+hb>color formatting support for CLI</reset>`. +func Printf(format string, args ...interface{}) { + fmt.Print(Sprintf(format, args...)) +} + +// Sprintf works identically to fmt.Sprintf but adds `<white+hb>color formatting support for CLI</reset>`. +func Sprintf(format string, args ...interface{}) string { + format = reg.ReplaceAllStringFunc(format, func(s string) string { + return ansi.ColorCode(strings.Trim(s, "<>/")) + }) + + return fmt.Sprintf(format, args...) +} diff --git a/cmd/rr/util/list.go b/cmd/rr/util/list.go new file mode 100644 index 00000000..4094ce44 --- /dev/null +++ b/cmd/rr/util/list.go @@ -0,0 +1,58 @@ +package util + +import ( + rrutil "github.com/spiral/roadrunner/util" + "github.com/olekukonko/tablewriter" + "os" + "strconv" + "github.com/dustin/go-humanize" + "time" +) + +// WorkerTable renders table with information about rr server workers. +func WorkerTable(workers []*rrutil.State) *tablewriter.Table { + tw := tablewriter.NewWriter(os.Stdout) + tw.SetHeader([]string{"PID", "Status", "Execs", "Memory", "Created"}) + tw.SetColMinWidth(0, 7) + tw.SetColMinWidth(1, 9) + tw.SetColMinWidth(2, 7) + tw.SetColMinWidth(3, 7) + tw.SetColMinWidth(4, 18) + + for _, w := range workers { + tw.Append([]string{ + strconv.Itoa(w.Pid), + renderStatus(w.Status), + renderJobs(w.NumJobs), + humanize.Bytes(w.MemoryUsage), + renderAlive(time.Unix(0, w.Created)), + }) + } + + return tw +} + +func renderStatus(status string) string { + switch status { + case "inactive": + return Sprintf("<yellow>inactive</reset>") + case "ready": + return Sprintf("<cyan>ready</reset>") + case "working": + return Sprintf("<green>working</reset>") + case "stopped": + return Sprintf("<red>stopped</reset>") + case "errored": + return Sprintf("<red>errored</reset>") + } + + return status +} + +func renderJobs(number int64) string { + return humanize.Comma(int64(number)) +} + +func renderAlive(t time.Time) string { + return humanize.RelTime(t, time.Now(), "ago", "") +} |