@@ -63,7 +63,7 @@ func runListCommand(cmd *cobra.Command, args []string) {
63
63
feedback .Errorf ("Error detecting boards: %v" , err )
64
64
os .Exit (errorcodes .ErrGeneric )
65
65
}
66
- watchList (inst )
66
+ watchList (cmd , inst )
67
67
os .Exit (0 )
68
68
}
69
69
@@ -89,45 +89,46 @@ func runListCommand(cmd *cobra.Command, args []string) {
89
89
feedback .PrintResult (result {ports })
90
90
}
91
91
92
- func watchList (inst * rpc.Instance ) {
92
+ func watchList (cmd * cobra. Command , inst * rpc.Instance ) {
93
93
pm := commands .GetPackageManager (inst .Id )
94
94
eventsChan , err := commands .WatchListBoards (pm )
95
95
if err != nil {
96
96
feedback .Errorf ("Error detecting boards: %v" , err )
97
97
os .Exit (errorcodes .ErrNetwork )
98
98
}
99
99
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
+ t .SetColumnWidthMode ()
105
+ feedback .Print (t .Render ())
106
+ }
107
+
101
108
for event := range eventsChan {
102
- switch event . Type {
103
- case "add" :
104
- boardPorts [ event . Port . Address ] = & commands.BoardPort {
109
+ boards := [] * rpc. BoardListItem {}
110
+ if event . Type == "add" {
111
+ boards , err = board . Identify ( pm , & commands.BoardPort {
105
112
Address : event .Port .Address ,
106
113
Label : event .Port .AddressLabel ,
107
114
Prefs : event .Port .Properties ,
108
115
IdentificationPrefs : event .Port .IdentificationProperties ,
109
116
Protocol : event .Port .Protocol ,
110
117
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 )
118
+ })
119
119
if err != nil {
120
120
feedback .Errorf ("Error identifying board: %v" , err )
121
121
os .Exit (errorcodes .ErrNetwork )
122
122
}
123
- ports = append (ports , & rpc.DetectedPort {
124
- Address : p .Address ,
125
- Protocol : p .Protocol ,
126
- ProtocolLabel : p .ProtocolLabel ,
127
- Boards : boardList ,
128
- })
129
123
}
130
- feedback .PrintResult (result {ports })
124
+
125
+ feedback .PrintResult (watchEvent {
126
+ Type : event .Type ,
127
+ Address : event .Port .Address ,
128
+ Protocol : event .Port .Protocol ,
129
+ ProtocolLabel : event .Port .ProtocolLabel ,
130
+ Boards : boards ,
131
+ })
131
132
}
132
133
}
133
134
@@ -191,3 +192,59 @@ func (dr result) String() string {
191
192
}
192
193
return t .Render ()
193
194
}
195
+
196
+ type watchEvent struct {
197
+ Type string `json:"type"`
198
+ Address string `json:"address,omitempty"`
199
+ Protocol string `json:"protocol,omitempty"`
200
+ ProtocolLabel string `json:"protocol_label,omitempty"`
201
+ Boards []* rpc.BoardListItem `json:"boards,omitempty"`
202
+ }
203
+
204
+ func (dr watchEvent ) Data () interface {} {
205
+ return dr
206
+ }
207
+
208
+ func (dr watchEvent ) String () string {
209
+ t := table .New ()
210
+
211
+ event := map [string ]string {
212
+ "add" : "Connected" ,
213
+ "remove" : "Disconnected" ,
214
+ }[dr .Type ]
215
+
216
+ address := fmt .Sprintf ("%s://%s" , dr .Protocol , dr .Address )
217
+ if dr .Protocol == "serial" || dr .Protocol == "" {
218
+ address = dr .Address
219
+ }
220
+ protocol := dr .ProtocolLabel
221
+ if boards := dr .Boards ; len (boards ) > 0 {
222
+ sort .Slice (boards , func (i , j int ) bool {
223
+ x , y := boards [i ], boards [j ]
224
+ return x .GetName () < y .GetName () || (x .GetName () == y .GetName () && x .GetFQBN () < y .GetFQBN ())
225
+ })
226
+ for _ , b := range boards {
227
+ board := b .GetName ()
228
+
229
+ // to improve the user experience, show on a dedicated column
230
+ // the name of the core supporting the board detected
231
+ var coreName = ""
232
+ fqbn , err := cores .ParseFQBN (b .GetFQBN ())
233
+ if err == nil {
234
+ coreName = fmt .Sprintf ("%s:%s" , fqbn .Package , fqbn .PlatformArch )
235
+ }
236
+
237
+ t .AddRow (address , protocol , event , board , fqbn , coreName )
238
+
239
+ // reset address and protocol, we only show them on the first row
240
+ address = ""
241
+ protocol = ""
242
+ }
243
+ } else {
244
+ board := ""
245
+ fqbn := ""
246
+ coreName := ""
247
+ t .AddRow (address , protocol , event , board , fqbn , coreName )
248
+ }
249
+ return t .Render ()
250
+ }
0 commit comments