Skip to content

Commit

Permalink
New tabular and plain text output format
Browse files Browse the repository at this point in the history
  • Loading branch information
xitonix authored Jun 19, 2020
1 parent 8912d77 commit b2ea02a
Show file tree
Hide file tree
Showing 31 changed files with 1,018 additions and 522 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ I would also like to mention some of the amazing libraries and packages I used f

- [kingpin](https://github.com/alecthomas/kingpin) and [chroma](https://github.com/alecthomas/chroma) by Alec Thomas

- [tablewriter](https://github.com/olekukonko/tablewriter) by Oleku Konko
- [go-pretty](https://github.com/jedib0t/go-pretty) by Naveen Mahalingam

- [diskv](https://github.com/peterbourgon/diskv) by Peter Bourgon

Expand Down
9 changes: 1 addition & 8 deletions application.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"io/ioutil"
"os"
"runtime"

"gopkg.in/alecthomas/kingpin.v2"

Expand Down Expand Up @@ -39,13 +38,7 @@ func newApplication() error {
}

func bindAppFlags(app *kingpin.Application, global *commands.GlobalParameters) {
colorFlag := app.Flag("colour", "Enables colours in the standard output. To disable, use --no-colour (Disabled by default on Windows).")

if runtime.GOOS == "windows" {
colorFlag.Default("false")
} else {
colorFlag.Default("true")
}
colorFlag := app.Flag("colour", "Enables colours in the standard output. To disable, use --no-colour.").Default("true")

colorFlag.BoolVar(&global.EnableColor)

Expand Down
41 changes: 27 additions & 14 deletions commands/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import (
"strings"
"syscall"

"github.com/olekukonko/tablewriter"
"gopkg.in/alecthomas/kingpin.v2"

"github.com/xitonix/trubka/internal/output"
"github.com/xitonix/trubka/internal"
"github.com/xitonix/trubka/internal/output/format"
"github.com/xitonix/trubka/internal/output/format/list"
"github.com/xitonix/trubka/internal/output/format/tabular"
"github.com/xitonix/trubka/kafka"
)

Expand Down Expand Up @@ -66,27 +68,38 @@ func AddFormatFlag(c *kingpin.CmdClause, format *string) {
}

func PrintConfigTable(entries []*kafka.ConfigEntry) {
output.WithCount("Configurations", len(entries))
table := output.InitStaticTable(os.Stdout,
output.H("Name", tablewriter.ALIGN_LEFT),
output.H("Value", tablewriter.ALIGN_LEFT),
table := tabular.NewTable(true,
tabular.C("Name").Align(tabular.AlignLeft).MaxWidth(100),
tabular.C("Value").Align(tabular.AlignLeft).FAlign(tabular.AlignRight).MaxWidth(100),
)
table.SetTitle(format.WithCount("Configurations", len(entries)))
for _, config := range entries {
table.Append([]string{
config.Name,
config.Value,
})
parts := strings.Split(config.Value, ",")
table.AddRow(config.Name, strings.Join(parts, "\n"))
}
table.SetFooter([]string{" ", fmt.Sprintf("Total: %d", len(entries))})
table.SetFooterAlignment(tablewriter.ALIGN_RIGHT)
table.AddFooter("", fmt.Sprintf("Total: %d", len(entries)))
table.Render()
}

func PrintConfigPlain(entries []*kafka.ConfigEntry) {
output.UnderlineWithCount("Configurations", len(entries))
b := list.NewBullet()
b.SetTitle(format.WithCount("Configurations", len(entries)))
for _, config := range entries {
fmt.Printf(" - %s: %s\n", config.Name, config.Value)
parts := strings.Split(config.Value, ",")
if len(parts) == 1 {
b.AddItem(fmt.Sprintf("%s: %v", config.Name, config.Value))
continue
}
b.AddItem(config.Name)
b.Intend()
for _, val := range parts {
if !internal.IsEmpty(val) {
b.AddItem(val)
}
}
b.UnIntend()
}
b.Render()
}

// AskForConfirmation asks the user for confirmation. The user must type in "yes/y", "no/n" or "exit/quit/q"
Expand Down
23 changes: 10 additions & 13 deletions commands/consume/interactive_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
"strconv"
"strings"

"github.com/olekukonko/tablewriter"

"github.com/xitonix/trubka/commands"
"github.com/xitonix/trubka/internal"
"github.com/xitonix/trubka/internal/output"
"github.com/xitonix/trubka/internal/output/format/tabular"
"github.com/xitonix/trubka/kafka"
"github.com/xitonix/trubka/protobuf"
)
Expand Down Expand Up @@ -282,24 +282,21 @@ func askForStartingOffset(topic string, defaultCP *kafka.PartitionCheckpoints) (
}

func confirmConsumerStart(topics map[string]*kafka.PartitionCheckpoints, contracts map[string]string) bool {
table := tablewriter.NewWriter(os.Stdout)
headers := []string{"Topic"}
headers := []*tabular.Column{tabular.C("Topic")}
isProto := len(contracts) != 0
if isProto {
headers = append(headers, "Contract")
headers = append(headers, tabular.C("Contract"))
}
headers = append(headers, "Offset")
table.SetHeader(headers)
table.SetRowLine(true)
headers = append(headers, tabular.C("Offset"))
table := tabular.NewTable(false, headers...)
for topic, cp := range topics {
row := []string{topic}
if isProto {
row = append(row, contracts[topic])
table.AddRow(topic, contracts[topic], cp.OriginalFromValue())
continue
}
row = append(row, cp.OriginalFromValue())
table.Append(row)
table.AddRow(topic, cp.OriginalFromValue())
}
fmt.Println()
output.NewLines(1)
table.Render()
return commands.AskForConfirmation("Start consuming")
}
Expand Down
Loading

0 comments on commit b2ea02a

Please sign in to comment.