From ea91f5488b3a3637a2a884d21119267eb1173eb3 Mon Sep 17 00:00:00 2001 From: Jesse Sharps Date: Mon, 18 Mar 2019 19:35:12 -0400 Subject: [PATCH 1/2] Print status without formatting if not to terminal Enables other services to read the output of edward status --- ui/terminal/status.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/ui/terminal/status.go b/ui/terminal/status.go index cf3599bc..655076ab 100644 --- a/ui/terminal/status.go +++ b/ui/terminal/status.go @@ -50,6 +50,14 @@ func (p *Provider) Status(statuses []ui.ServiceStatus) { table.SetHeader(headings) table.SetAlignment(tablewriter.ALIGN_LEFT) + outputInfo, _ := os.Stdout.Stat() + if (outputInfo.Mode() & os.ModeCharDevice) != os.ModeCharDevice { + table.SetBorder(false) + table.SetRowLine(false) + table.SetHeaderLine(false) + table.SetColumnSeparator("\t") + } + for index, serviceStatus := range statuses { service := serviceStatus.Service() status := serviceStatus.Status() From 8c9331d296a5439baad9d61216660c96dc32538b Mon Sep 17 00:00:00 2001 From: Jesse Sharps Date: Mon, 18 Mar 2019 21:11:38 -0400 Subject: [PATCH 2/2] Format using an interface instead --- ui/terminal/status.go | 17 ++++++++-------- ui/terminal/table.go | 47 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 9 deletions(-) create mode 100644 ui/terminal/table.go diff --git a/ui/terminal/status.go b/ui/terminal/status.go index 655076ab..ebc3ce2e 100644 --- a/ui/terminal/status.go +++ b/ui/terminal/status.go @@ -30,7 +30,6 @@ func (p *Provider) Status(statuses []ui.ServiceStatus) { configSet[configPath] = struct{}{} } - table := tablewriter.NewWriter(os.Stdout) headings := []string{ "PID", "Name", @@ -47,17 +46,17 @@ func (p *Provider) Status(statuses []ui.ServiceStatus) { headings = append(headings, "Config File") } - table.SetHeader(headings) - table.SetAlignment(tablewriter.ALIGN_LEFT) - outputInfo, _ := os.Stdout.Stat() - if (outputInfo.Mode() & os.ModeCharDevice) != os.ModeCharDevice { - table.SetBorder(false) - table.SetRowLine(false) - table.SetHeaderLine(false) - table.SetColumnSeparator("\t") + var table tablePrinter + if (outputInfo.Mode() & os.ModeCharDevice) == os.ModeCharDevice { + tableWriter := tablewriter.NewWriter(os.Stdout) + tableWriter.SetAlignment(tablewriter.ALIGN_LEFT) + table = tableWriter + } else { + table = NewPlainPrinter(os.Stdout) } + table.SetHeader(headings) for index, serviceStatus := range statuses { service := serviceStatus.Service() status := serviceStatus.Status() diff --git a/ui/terminal/table.go b/ui/terminal/table.go new file mode 100644 index 00000000..ddb5b605 --- /dev/null +++ b/ui/terminal/table.go @@ -0,0 +1,47 @@ +package terminal + +import ( + "os" + "strings" +) + +type tablePrinter interface { + SetHeader(header []string) + Append(columns []string) + Render() +} + +type plainPrinter struct { + out *os.File + headers []string + rows [][]string +} + +func NewPlainPrinter(out *os.File) *plainPrinter { + p := new(plainPrinter) + p.out = out + return p +} + +func (p *plainPrinter) SetHeader(header []string) { + p.headers = header +} + +func (p *plainPrinter) Append(columns []string) { + p.rows = append(p.rows, columns) +} + +func (p *plainPrinter) Render() { + for _, col := range p.headers { + p.out.WriteString(strings.ToUpper(col)) + p.out.WriteString("\t") + } + p.out.WriteString("\n") + for _, row := range p.rows { + for _, col := range row { + p.out.WriteString(col) + p.out.WriteString("\t") + } + p.out.WriteString("\n") + } +}