Skip to content

Commit 4d6515f

Browse files
committed
Change board list watcher ouptut formatting
1 parent 61eae53 commit 4d6515f

File tree

3 files changed

+87
-21
lines changed

3 files changed

+87
-21
lines changed

Diff for: cli/board/list.go

+77-21
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func runListCommand(cmd *cobra.Command, args []string) {
6363
feedback.Errorf("Error detecting boards: %v", err)
6464
os.Exit(errorcodes.ErrGeneric)
6565
}
66-
watchList(inst)
66+
watchList(cmd, inst)
6767
os.Exit(0)
6868
}
6969

@@ -89,45 +89,45 @@ func runListCommand(cmd *cobra.Command, args []string) {
8989
feedback.PrintResult(result{ports})
9090
}
9191

92-
func watchList(inst *rpc.Instance) {
92+
func watchList(cmd *cobra.Command, inst *rpc.Instance) {
9393
pm := commands.GetPackageManager(inst.Id)
9494
eventsChan, err := commands.WatchListBoards(pm)
9595
if err != nil {
9696
feedback.Errorf("Error detecting boards: %v", err)
9797
os.Exit(errorcodes.ErrNetwork)
9898
}
9999

100-
boardPorts := map[string]*commands.BoardPort{}
100+
// This is done to avoid printing the header each time a new event is received
101+
if feedback.GetFormat() == feedback.Text {
102+
t := table.New()
103+
t.SetHeader("Port", "Type", "Event", "Board Name", "FQBN", "Core")
104+
feedback.Print(t.Render())
105+
}
106+
101107
for event := range eventsChan {
102-
switch event.Type {
103-
case "add":
104-
boardPorts[event.Port.Address] = &commands.BoardPort{
108+
boards := []*rpc.BoardListItem{}
109+
if event.Type == "add" {
110+
boards, err = board.Identify(pm, &commands.BoardPort{
105111
Address: event.Port.Address,
106112
Label: event.Port.AddressLabel,
107113
Prefs: event.Port.Properties,
108114
IdentificationPrefs: event.Port.IdentificationProperties,
109115
Protocol: event.Port.Protocol,
110116
ProtocolLabel: event.Port.ProtocolLabel,
111-
}
112-
case "remove":
113-
delete(boardPorts, event.Port.Address)
114-
}
115-
116-
ports := []*rpc.DetectedPort{}
117-
for _, p := range boardPorts {
118-
boardList, err := board.Identify(pm, p)
117+
})
119118
if err != nil {
120119
feedback.Errorf("Error identifying board: %v", err)
121120
os.Exit(errorcodes.ErrNetwork)
122121
}
123-
ports = append(ports, &rpc.DetectedPort{
124-
Address: p.Address,
125-
Protocol: p.Protocol,
126-
ProtocolLabel: p.ProtocolLabel,
127-
Boards: boardList,
128-
})
129122
}
130-
feedback.PrintResult(result{ports})
123+
124+
feedback.PrintResult(watchEvent{
125+
Type: event.Type,
126+
Address: event.Port.Address,
127+
Protocol: event.Port.Protocol,
128+
ProtocolLabel: event.Port.ProtocolLabel,
129+
Boards: boards,
130+
})
131131
}
132132
}
133133

@@ -191,3 +191,59 @@ func (dr result) String() string {
191191
}
192192
return t.Render()
193193
}
194+
195+
type watchEvent struct {
196+
Type string `json:"type"`
197+
Address string `json:"address,omitempty"`
198+
Protocol string `json:"protocol,omitempty"`
199+
ProtocolLabel string `json:"protocol_label,omitempty"`
200+
Boards []*rpc.BoardListItem `json:"boards,omitempty"`
201+
}
202+
203+
func (dr watchEvent) Data() interface{} {
204+
return dr
205+
}
206+
207+
func (dr watchEvent) String() string {
208+
t := table.New()
209+
210+
event := map[string]string{
211+
"add": "Connected",
212+
"remove": "Disconnected",
213+
}[dr.Type]
214+
215+
address := fmt.Sprintf("%s://%s", dr.Protocol, dr.Address)
216+
if dr.Protocol == "serial" || dr.Protocol == "" {
217+
address = dr.Address
218+
}
219+
protocol := dr.ProtocolLabel
220+
if boards := dr.Boards; len(boards) > 0 {
221+
sort.Slice(boards, func(i, j int) bool {
222+
x, y := boards[i], boards[j]
223+
return x.GetName() < y.GetName() || (x.GetName() == y.GetName() && x.GetFQBN() < y.GetFQBN())
224+
})
225+
for _, b := range boards {
226+
board := b.GetName()
227+
228+
// to improve the user experience, show on a dedicated column
229+
// the name of the core supporting the board detected
230+
var coreName = ""
231+
fqbn, err := cores.ParseFQBN(b.GetFQBN())
232+
if err == nil {
233+
coreName = fmt.Sprintf("%s:%s", fqbn.Package, fqbn.PlatformArch)
234+
}
235+
236+
t.AddRow(address, protocol, event, board, fqbn, coreName)
237+
238+
// reset address and protocol, we only show them on the first row
239+
address = ""
240+
protocol = ""
241+
}
242+
} else {
243+
board := ""
244+
fqbn := ""
245+
coreName := ""
246+
t.AddRow(address, protocol, event, board, fqbn, coreName)
247+
}
248+
return t.Render()
249+
}

Diff for: cli/feedback/exported.go

+5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ func SetFormat(f OutputFormat) {
3434
fb.SetFormat(f)
3535
}
3636

37+
// GetFormat returns the currently set output format
38+
func GetFormat() OutputFormat {
39+
return fb.GetFormat()
40+
}
41+
3742
// OutputWriter returns the underlying io.Writer to be used when the Print*
3843
// api is not enough
3944
func OutputWriter() io.Writer {

Diff for: cli/feedback/feedback.go

+5
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ func (fb *Feedback) SetFormat(f OutputFormat) {
6868
fb.format = f
6969
}
7070

71+
// GetFormat returns the output format currently set
72+
func (fb *Feedback) GetFormat() OutputFormat {
73+
return fb.format
74+
}
75+
7176
// OutputWriter returns the underlying io.Writer to be used when the Print*
7277
// api is not enough.
7378
func (fb *Feedback) OutputWriter() io.Writer {

0 commit comments

Comments
 (0)