Skip to content

Commit 12d92e0

Browse files
author
Massimiliano Pippi
committed
re-add table in a dedicated package
1 parent ebe42c2 commit 12d92e0

File tree

9 files changed

+210
-114
lines changed

9 files changed

+210
-114
lines changed

cli/board/details.go

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ package board
2020
import (
2121
"context"
2222
"os"
23-
"text/tabwriter"
2423

2524
"github.com/arduino/arduino-cli/cli/errorcodes"
2625
"github.com/arduino/arduino-cli/cli/feedback"
2726
"github.com/arduino/arduino-cli/cli/globals"
2827
"github.com/arduino/arduino-cli/cli/instance"
2928
"github.com/arduino/arduino-cli/commands/board"
3029
rpc "github.com/arduino/arduino-cli/rpc/commands"
31-
"github.com/cheynewallace/tabby"
30+
"github.com/arduino/arduino-cli/table"
31+
"github.com/fatih/color"
3232
"github.com/spf13/cobra"
3333
)
3434

@@ -60,7 +60,7 @@ func runDetailsCommand(cmd *cobra.Command, args []string) {
6060
}
6161

6262
func outputDetailsResp(details *rpc.BoardDetailsResp) {
63-
// Table is 5 columns wide:
63+
// Table is 4 columns wide:
6464
// | | | | |
6565
// Board name: Arduino Nano
6666
//
@@ -69,33 +69,40 @@ func outputDetailsResp(details *rpc.BoardDetailsResp) {
6969
// arduino:arduinoOTA 1.2.1
7070
//
7171
// Option: Processor cpu
72-
// ATmega328P * cpu=atmega328
72+
// ATmega328P cpu=atmega328
7373
// ATmega328P (Old Bootloader) cpu=atmega328old
7474
// ATmega168 cpu=atmega168
75-
w := tabwriter.NewWriter(feedback.OutputWriter(), 0, 0, 2, ' ', 0)
76-
table := tabby.NewCustom(w)
77-
78-
table.AddLine("Board name:", details.Name, "")
75+
t := table.New()
76+
t.SetColumnWidthMode(1, table.Average)
77+
t.AddRow("Board name:", details.Name)
7978

8079
for i, tool := range details.RequiredTools {
8180
if i == 0 {
82-
table.AddLine("", "", "", "") // get some space from above
83-
table.AddLine("Required tools:", tool.Packager+":"+tool.Name, "", tool.Version)
81+
t.AddRow() // get some space from above
82+
t.AddRow("Required tools:", tool.Packager+":"+tool.Name, "", tool.Version)
8483
continue
8584
}
86-
table.AddLine("", tool.Packager+":"+tool.Name, "", tool.Version)
85+
t.AddRow("", tool.Packager+":"+tool.Name, "", tool.Version)
8786
}
8887

8988
for _, option := range details.ConfigOptions {
90-
table.AddLine("", "", "", "") // get some space from above
91-
table.AddLine("Option:", option.OptionLabel, "", option.Option)
89+
t.AddRow() // get some space from above
90+
t.AddRow("Option:", option.OptionLabel, "", option.Option)
9291
for _, value := range option.Values {
93-
checked := ""
92+
green := color.New(color.FgGreen)
9493
if value.Selected {
95-
checked = "✔"
94+
t.AddRow("",
95+
table.NewCell(value.ValueLabel, green),
96+
table.NewCell("✔", green),
97+
table.NewCell(option.Option+"="+value.Value, green))
98+
} else {
99+
t.AddRow("",
100+
value.ValueLabel,
101+
"",
102+
option.Option+"="+value.Value)
96103
}
97-
table.AddLine("", value.ValueLabel, checked, option.Option+"="+value.Value)
98104
}
99105
}
100-
table.Print()
106+
107+
feedback.Print(t.Render())
101108
}

cli/board/list.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ package board
2020
import (
2121
"os"
2222
"sort"
23-
"text/tabwriter"
2423
"time"
2524

2625
"github.com/arduino/arduino-cli/cli/errorcodes"
@@ -29,7 +28,7 @@ import (
2928
"github.com/arduino/arduino-cli/cli/instance"
3029
"github.com/arduino/arduino-cli/commands/board"
3130
rpc "github.com/arduino/arduino-cli/rpc/commands"
32-
"github.com/cheynewallace/tabby"
31+
"github.com/arduino/arduino-cli/table"
3332
"github.com/spf13/cobra"
3433
)
3534

@@ -85,9 +84,8 @@ func outputListResp(ports []*rpc.DetectedPort) {
8584
(x.GetProtocol() == y.GetProtocol() && x.GetAddress() < y.GetAddress())
8685
})
8786

88-
w := tabwriter.NewWriter(feedback.OutputWriter(), 0, 0, 2, ' ', 0)
89-
table := tabby.NewCustom(w)
90-
table.AddHeader("Port", "Type", "Board Name", "FQBN")
87+
t := table.New()
88+
t.SetHeader("Port", "Type", "Board Name", "FQBN")
9189
for _, port := range ports {
9290
address := port.GetProtocol() + "://" + port.GetAddress()
9391
if port.GetProtocol() == "serial" {
@@ -102,16 +100,16 @@ func outputListResp(ports []*rpc.DetectedPort) {
102100
for _, b := range boards {
103101
board := b.GetName()
104102
fqbn := b.GetFQBN()
105-
table.AddLine(address, protocol, board, fqbn)
103+
t.AddRow(address, protocol, board, fqbn)
106104
// show address and protocol only on the first row
107105
address = ""
108106
protocol = ""
109107
}
110108
} else {
111109
board := "Unknown"
112110
fqbn := ""
113-
table.AddLine(address, protocol, board, fqbn)
111+
t.AddRow(address, protocol, board, fqbn)
114112
}
115113
}
116-
table.Print()
114+
feedback.Print(t.Render())
117115
}

cli/board/listall.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,14 @@ import (
2121
"context"
2222
"os"
2323
"sort"
24-
"text/tabwriter"
2524

2625
"github.com/arduino/arduino-cli/cli/errorcodes"
2726
"github.com/arduino/arduino-cli/cli/feedback"
2827
"github.com/arduino/arduino-cli/cli/globals"
2928
"github.com/arduino/arduino-cli/cli/instance"
3029
"github.com/arduino/arduino-cli/commands/board"
3130
rpc "github.com/arduino/arduino-cli/rpc/commands"
32-
"github.com/cheynewallace/tabby"
31+
"github.com/arduino/arduino-cli/table"
3332
"github.com/spf13/cobra"
3433
)
3534

@@ -71,11 +70,10 @@ func outputBoardListAll(list *rpc.BoardListAllResp) {
7170
return list.Boards[i].GetName() < list.Boards[j].GetName()
7271
})
7372

74-
w := tabwriter.NewWriter(feedback.OutputWriter(), 0, 0, 2, ' ', 0)
75-
table := tabby.NewCustom(w)
76-
table.AddHeader("Board Name", "FQBN")
73+
t := table.New()
74+
t.SetHeader("Board Name", "FQBN")
7775
for _, item := range list.GetBoards() {
78-
table.AddLine(item.GetName(), item.GetFQBN())
76+
t.AddRow(item.GetName(), item.GetFQBN())
7977
}
80-
table.Print()
78+
feedback.Print(t.Render())
8179
}

cli/core/list.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@ package core
2020
import (
2121
"os"
2222
"sort"
23-
"text/tabwriter"
2423

2524
"github.com/arduino/arduino-cli/arduino/cores"
2625
"github.com/arduino/arduino-cli/cli/errorcodes"
2726
"github.com/arduino/arduino-cli/cli/feedback"
2827
"github.com/arduino/arduino-cli/cli/globals"
2928
"github.com/arduino/arduino-cli/cli/instance"
3029
"github.com/arduino/arduino-cli/commands/core"
31-
"github.com/cheynewallace/tabby"
30+
"github.com/arduino/arduino-cli/table"
3231
"github.com/sirupsen/logrus"
3332
"github.com/spf13/cobra"
3433
)
@@ -72,15 +71,14 @@ func outputInstalledCores(platforms []*cores.PlatformRelease) {
7271
return
7372
}
7473

75-
w := tabwriter.NewWriter(feedback.OutputWriter(), 0, 0, 2, ' ', 0)
76-
table := tabby.NewCustom(w)
77-
table.AddHeader("ID", "Installed", "Latest", "Name")
74+
t := table.New()
75+
t.SetHeader("ID", "Installed", "Latest", "Name")
7876
sort.Slice(platforms, func(i, j int) bool {
7977
return platforms[i].Platform.String() < platforms[j].Platform.String()
8078
})
8179
for _, p := range platforms {
82-
table.AddLine(p.Platform.String(), p.Version.String(), p.Platform.GetLatestRelease().Version.String(), p.Platform.Name)
80+
t.AddRow(p.Platform.String(), p.Version.String(), p.Platform.GetLatestRelease().Version.String(), p.Platform.Name)
8381
}
8482

85-
table.Print()
83+
feedback.Print(t.Render())
8684
}

cli/core/search.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,14 @@ import (
2222
"os"
2323
"sort"
2424
"strings"
25-
"text/tabwriter"
2625

2726
"github.com/arduino/arduino-cli/cli/errorcodes"
2827
"github.com/arduino/arduino-cli/cli/feedback"
2928
"github.com/arduino/arduino-cli/cli/globals"
3029
"github.com/arduino/arduino-cli/cli/instance"
3130
"github.com/arduino/arduino-cli/commands/core"
3231
rpc "github.com/arduino/arduino-cli/rpc/commands"
33-
"github.com/cheynewallace/tabby"
32+
"github.com/arduino/arduino-cli/table"
3433
"github.com/sirupsen/logrus"
3534
"github.com/spf13/cobra"
3635
)
@@ -73,16 +72,15 @@ func runSearchCommand(cmd *cobra.Command, args []string) {
7372

7473
func outputSearchCores(cores []*rpc.Platform) {
7574
if len(cores) > 0 {
76-
w := tabwriter.NewWriter(feedback.OutputWriter(), 0, 0, 2, ' ', 0)
77-
table := tabby.NewCustom(w)
78-
table.AddHeader("ID", "Version", "Name")
75+
t := table.New()
76+
t.SetHeader("ID", "Version", "Name")
7977
sort.Slice(cores, func(i, j int) bool {
8078
return cores[i].ID < cores[j].ID
8179
})
8280
for _, item := range cores {
83-
table.AddLine(item.GetID(), item.GetLatest(), item.GetName())
81+
t.AddRow(item.GetID(), item.GetLatest(), item.GetName())
8482
}
85-
table.Print()
83+
feedback.Print(t.Render())
8684
} else {
8785
feedback.Print("No platforms matching your search.")
8886
}

cli/lib/list.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,14 @@ package lib
1919

2020
import (
2121
"os"
22-
"text/tabwriter"
2322

2423
"github.com/arduino/arduino-cli/cli/errorcodes"
2524
"github.com/arduino/arduino-cli/cli/feedback"
2625
"github.com/arduino/arduino-cli/cli/globals"
2726
"github.com/arduino/arduino-cli/cli/instance"
2827
"github.com/arduino/arduino-cli/commands/lib"
2928
rpc "github.com/arduino/arduino-cli/rpc/commands"
30-
"github.com/cheynewallace/tabby"
29+
"github.com/arduino/arduino-cli/table"
3130
"github.com/sirupsen/logrus"
3231
"github.com/spf13/cobra"
3332
"golang.org/x/net/context"
@@ -82,9 +81,8 @@ func outputListLibrary(il []*rpc.InstalledLibrary) {
8281
return
8382
}
8483

85-
w := tabwriter.NewWriter(feedback.OutputWriter(), 0, 0, 2, ' ', 0)
86-
table := tabby.NewCustom(w)
87-
table.AddHeader("Name", "Installed", "Available", "Location")
84+
t := table.New()
85+
t.SetHeader("Name", "Installed", "Available", "Location")
8886

8987
lastName := ""
9088
for _, libMeta := range il {
@@ -104,12 +102,12 @@ func outputListLibrary(il []*rpc.InstalledLibrary) {
104102
if libMeta.GetRelease() != nil {
105103
available := libMeta.GetRelease().GetVersion()
106104
if available != "" {
107-
table.AddLine(name, lib.Version, available, location)
105+
t.AddRow(name, lib.Version, available, location)
108106
} else {
109-
table.AddLine(name, lib.Version, "-", location)
107+
t.AddRow(name, lib.Version, "-", location)
110108
}
111109
}
112110
}
113111

114-
table.Print()
112+
feedback.Print(t.Render())
115113
}

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ require (
99
github.com/arduino/go-properties-orderedmap v0.0.0-20181003091528-89278049acd3
1010
github.com/arduino/go-timeutils v0.0.0-20171220113728-d1dd9e313b1b
1111
github.com/arduino/go-win32-utils v0.0.0-20180330194947-ed041402e83b
12-
github.com/cheynewallace/tabby v1.1.0
1312
github.com/cmaglie/pb v1.0.27
1413
github.com/codeclysm/cc v1.2.2 // indirect
1514
github.com/codeclysm/extract v2.2.0+incompatible

table/cell.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to modify or
12+
// otherwise use the software for commercial activities involving the Arduino
13+
// software without disclosing the source code of your own applications. To purchase
14+
// a commercial license, send an email to license@arduino.cc.
15+
16+
package table
17+
18+
import (
19+
"fmt"
20+
"unicode/utf8"
21+
22+
"github.com/fatih/color"
23+
)
24+
25+
// JustifyMode is used to configure text alignment on cells
26+
type JustifyMode int
27+
28+
// Justify mode enumeration
29+
const (
30+
JustifyLeft JustifyMode = iota
31+
JustifyCenter
32+
JustifyRight
33+
)
34+
35+
// Cell represents a Table cell
36+
type Cell struct {
37+
clean string
38+
raw string
39+
justify JustifyMode
40+
}
41+
42+
// NewCell creates a new cell. Color can be nil.
43+
func NewCell(text string, c *color.Color) *Cell {
44+
styled := text
45+
if c != nil {
46+
styled = c.SprintFunc()(text)
47+
}
48+
49+
return &Cell{
50+
raw: styled,
51+
clean: text,
52+
justify: JustifyLeft,
53+
}
54+
}
55+
56+
// Len returns the size of the cell, taking into account color codes
57+
func (t *Cell) Len() int {
58+
return utf8.RuneCountInString(t.clean)
59+
}
60+
61+
// Justify sets text justification
62+
func (t *Cell) Justify(mode JustifyMode) {
63+
t.justify = mode
64+
}
65+
66+
// Pad sets the cell padding
67+
func (t *Cell) Pad(totalLen int) string {
68+
delta := totalLen - t.Len()
69+
switch t.justify {
70+
case 0:
71+
return t.raw + spaces(delta)
72+
case 1:
73+
return spaces(delta/2) + t.raw + spaces(delta-delta/2)
74+
case 2:
75+
return spaces(delta) + t.raw
76+
}
77+
panic(fmt.Sprintf("internal error: invalid justify %d", t.justify))
78+
}
79+
80+
func spaces(n int) string {
81+
res := ""
82+
for n > 0 {
83+
res += " "
84+
n--
85+
}
86+
return res
87+
}

0 commit comments

Comments
 (0)