Refactor NetworkDevices widget and GetDevices API to simplify device handling and remove interface-based grouping.
This commit is contained in:
@@ -26,33 +26,28 @@ func NetworkDevices() NetworkDevicesOptions {
|
||||
func createNetworkDevicesList(ctx context.Context, _ terminalapi.Terminal, _ interface{}) widgetapi.Widget {
|
||||
list := util.PanicOnErrorWithResult(text.New())
|
||||
go util.Periodic(ctx, 20*time.Second, func() error {
|
||||
interfaces := networking.GetDevices()
|
||||
devices := networking.GetDevices()
|
||||
list.Reset()
|
||||
for _, iface := range sortedInterfaces(interfaces) {
|
||||
if err := list.Write(fmt.Sprintf("=== %s ===\n", iface)); err != nil {
|
||||
for _, mac := range sortedKeys(devices) {
|
||||
status := cell.ColorGray
|
||||
if devices[mac].Online {
|
||||
status = cell.ColorGreen
|
||||
}
|
||||
if err := list.Write(fmt.Sprintf("|%-15s|", mac), text.WriteCellOpts(cell.FgColor(cell.ColorGray))); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := list.Write(fmt.Sprintf("%2s ", devices[mac].Icon), text.WriteCellOpts(cell.BgColor(status), cell.FgColor(cell.ColorBlack))); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := list.Write(fmt.Sprint("|"), text.WriteCellOpts(cell.FgColor(cell.ColorGray))); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, mac := range sortedKeys(interfaces[iface]) {
|
||||
status := cell.ColorGray
|
||||
if interfaces[iface][mac].Online {
|
||||
status = cell.ColorGreen
|
||||
}
|
||||
if err := list.Write(fmt.Sprintf("|%-15s|", mac), text.WriteCellOpts(cell.FgColor(cell.ColorGray))); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := list.Write(fmt.Sprintf("%2s ", interfaces[iface][mac].Icon), text.WriteCellOpts(cell.BgColor(status), cell.FgColor(cell.ColorBlack))); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := list.Write(fmt.Sprint("|"), text.WriteCellOpts(cell.FgColor(cell.ColorGray))); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := list.Write(fmt.Sprintf(" %s", interfaces[iface][mac].Name), text.WriteCellOpts(cell.FgColor(cell.ColorWhite))); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := list.Write(fmt.Sprintf(" (%s)\n", interfaces[iface][mac].IP), text.WriteCellOpts(cell.FgColor(cell.ColorGray))); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := list.Write(fmt.Sprintf(" %s", devices[mac].Name), text.WriteCellOpts(cell.FgColor(cell.ColorWhite))); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := list.Write(fmt.Sprintf(" (%s)\n", devices[mac].IP), text.WriteCellOpts(cell.FgColor(cell.ColorGray))); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,15 +57,6 @@ func createNetworkDevicesList(ctx context.Context, _ terminalapi.Terminal, _ int
|
||||
return list
|
||||
}
|
||||
|
||||
func sortedInterfaces(interfaces map[string]map[string]networking.Device) []string {
|
||||
keys := make([]string, 0, len(interfaces))
|
||||
for key := range interfaces {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
return keys
|
||||
}
|
||||
|
||||
func sortedKeys(devices map[string]networking.Device) []string {
|
||||
macs := make([]string, 0, len(devices))
|
||||
for mac := range devices {
|
||||
|
||||
Reference in New Issue
Block a user