From 598e93c9d267e61e1fad20c37a3833f05af32a89 Mon Sep 17 00:00:00 2001 From: Arindy Date: Sun, 28 Dec 2025 12:49:18 +0100 Subject: [PATCH] Refactor NetworkDevices widget and `GetDevices` API to simplify device handling and remove interface-based grouping. --- apis/networking/main.go | 16 ++++--------- widgets/networkdevices.go | 50 ++++++++++++++------------------------- 2 files changed, 23 insertions(+), 43 deletions(-) diff --git a/apis/networking/main.go b/apis/networking/main.go index 1ab6ffe..a3c4631 100644 --- a/apis/networking/main.go +++ b/apis/networking/main.go @@ -21,35 +21,29 @@ type Device struct { Online bool `json:"online"` } -func GetDevices() map[string]map[string]Device { - result := make(map[string]map[string]Device) +func GetDevices() map[string]Device { + result := make(map[string]Device) devices := make(map[string]Device) arpDevices := arpDevices() knownDevices := knownDevices() for mac, device := range arpDevices { - if strings.HasPrefix(device.Interface, "macvlan") { - continue - } if strings.HasPrefix(device.Interface, "br") { continue } if known, ok := knownDevices[mac]; ok { - devices[mac] = Device{Name: known.Name, IP: device.IP, Interface: device.Interface, Icon: known.Icon} + devices[mac] = Device{Name: known.Name, IP: device.IP, Icon: known.Icon} } else { devices[mac] = device } } for mac, device := range knownDevices { if _, ok := devices[mac]; !ok { - devices[mac] = Device{Name: device.Name, IP: device.IP, Interface: "_unknown_", Icon: device.Icon} + devices[mac] = Device{Name: device.Name, IP: device.IP, Icon: device.Icon} } } writeDevices(devices) for mac, device := range devices { - if _, ok := result[device.Interface]; !ok { - result[device.Interface] = make(map[string]Device) - } if device.Icon == "" { device.Icon = "\U000F0C8A" } @@ -63,7 +57,7 @@ func GetDevices() map[string]map[string]Device { } } } - result[device.Interface][mac] = device + result[mac] = device } return result diff --git a/widgets/networkdevices.go b/widgets/networkdevices.go index 3616ae2..cb32650 100644 --- a/widgets/networkdevices.go +++ b/widgets/networkdevices.go @@ -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 {