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