Refactor NetworkDevices widget to sort interfaces
This commit is contained in:
parent
e4a584f4df
commit
44689ada2c
@ -34,7 +34,7 @@ func GetDevices() map[string]map[string]Device {
|
|||||||
}
|
}
|
||||||
for mac, device := range knownDevices {
|
for mac, device := range knownDevices {
|
||||||
if _, ok := devices[mac]; !ok {
|
if _, ok := devices[mac]; !ok {
|
||||||
devices[mac] = Device{Name: device.Name, IP: device.IP, Interface: "offline", Icon: device.Icon}
|
devices[mac] = Device{Name: device.Name, IP: device.IP, Interface: "_offline_", Icon: device.Icon}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
writeDevices(devices)
|
writeDevices(devices)
|
||||||
|
|||||||
@ -28,7 +28,7 @@ func createNetworkDevicesList(ctx context.Context, _ terminalapi.Terminal, _ int
|
|||||||
go util.Periodic(ctx, 20*time.Second, func() error {
|
go util.Periodic(ctx, 20*time.Second, func() error {
|
||||||
interfaces := networking.GetDevices()
|
interfaces := networking.GetDevices()
|
||||||
list.Reset()
|
list.Reset()
|
||||||
for iface, devices := range interfaces {
|
for _, iface := range sortedInterfaces(interfaces) {
|
||||||
status := cell.ColorGreen
|
status := cell.ColorGreen
|
||||||
if iface == "offline" {
|
if iface == "offline" {
|
||||||
status = cell.ColorGray
|
status = cell.ColorGray
|
||||||
@ -36,17 +36,17 @@ func createNetworkDevicesList(ctx context.Context, _ terminalapi.Terminal, _ int
|
|||||||
if err := list.Write(fmt.Sprintf("=== %s ===\n", iface)); err != nil {
|
if err := list.Write(fmt.Sprintf("=== %s ===\n", iface)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, mac := range sortedKeys(devices) {
|
for _, mac := range sortedKeys(interfaces[iface]) {
|
||||||
if err := list.Write(fmt.Sprintf("|%-15s|", mac), text.WriteCellOpts(cell.FgColor(cell.ColorGray))); err != nil {
|
if err := list.Write(fmt.Sprintf("|%-15s|", mac), text.WriteCellOpts(cell.FgColor(cell.ColorGray))); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := list.Write(fmt.Sprintf("%2s ", devices[mac].Icon), text.WriteCellOpts(cell.FgColor(status))); err != nil {
|
if err := list.Write(fmt.Sprintf("%2s ", interfaces[iface][mac].Icon), text.WriteCellOpts(cell.FgColor(status))); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := list.Write(fmt.Sprintf(" %s", devices[mac].Name), text.WriteCellOpts(cell.FgColor(cell.ColorWhite))); err != nil {
|
if err := list.Write(fmt.Sprintf(" %s", interfaces[iface][mac].Name), text.WriteCellOpts(cell.FgColor(cell.ColorWhite))); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := list.Write(fmt.Sprintf(" (%s)\n", devices[mac].IP), text.WriteCellOpts(cell.FgColor(cell.ColorGray))); err != nil {
|
if err := list.Write(fmt.Sprintf(" (%s)\n", interfaces[iface][mac].IP), text.WriteCellOpts(cell.FgColor(cell.ColorGray))); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -58,6 +58,15 @@ func createNetworkDevicesList(ctx context.Context, _ terminalapi.Terminal, _ int
|
|||||||
return list
|
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 {
|
func sortedKeys(devices map[string]networking.Device) []string {
|
||||||
macs := make([]string, 0, len(devices))
|
macs := make([]string, 0, len(devices))
|
||||||
for mac := range devices {
|
for mac := range devices {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user