diff --git a/ui/terminal/status.go b/ui/terminal/status.go index cf3599bc..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,9 +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() + 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") + } +}