wlan.go (1151B)
1 package wlan 2 3 import ( 4 "bufio" 5 "bytes" 6 "context" 7 "fmt" 8 "os/exec" 9 "strings" 10 ) 11 12 // Config holds the configuration for the wlan block. 13 type Config struct { 14 Iface string `json:"iface"` 15 } 16 17 type Block struct { 18 iface string 19 } 20 21 func New(iface string) *Block { 22 return &Block{iface: iface} 23 } 24 25 func (b *Block) Name() string { 26 return "wlan" 27 } 28 29 // TODO: signal strength icon. 30 func (b *Block) Render(ctx context.Context) (string, error) { 31 out, err := exec.CommandContext( 32 ctx, "iwctl", "station", b.iface, "show", 33 ).Output() 34 if err != nil { 35 return "", fmt.Errorf("exec iwctl failed: %v", err) 36 } 37 38 var ( 39 scanner = bufio.NewScanner(bytes.NewReader(out)) 40 41 state, network, ip, rssi string 42 ) 43 for scanner.Scan() { 44 fields := strings.Fields(scanner.Text()) 45 if len(fields) < 2 { 46 continue 47 } 48 49 switch { 50 case fields[0] == "State": 51 state = fields[1] 52 case fields[0] == "Connected": 53 network = fields[2] 54 case fields[0] == "IPv4": 55 ip = fields[2] 56 case fields[0] == "RSSI": 57 rssi = fields[1] + fields[2] 58 } 59 } 60 61 if state == "disconnected" { 62 return " ", nil 63 } 64 return fmt.Sprintf(" %s %s %s", network, ip, rssi), nil 65 }