Skip to content
This repository has been archived by the owner on May 8, 2023. It is now read-only.

Print status without formatting if not to terminal #175

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions ui/terminal/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ func (p *Provider) Status(statuses []ui.ServiceStatus) {
configSet[configPath] = struct{}{}
}

table := tablewriter.NewWriter(os.Stdout)
headings := []string{
"PID",
"Name",
Expand All @@ -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()
Expand Down
47 changes: 47 additions & 0 deletions ui/terminal/table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package terminal

import (
"os"
"strings"
)

type tablePrinter interface {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this could live in the terminal package, and define a statusPrinter rather than being table specific?

SetHeader(header []string)
Append(columns []string)
Render()
}

type plainPrinter struct {
out *os.File
headers []string
rows [][]string
}

func NewPlainPrinter(out *os.File) *plainPrinter {
theothertomelliott marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For versatility, this could be io.Writer

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")
}
}