Add IPType provider

This commit is contained in:
akp 2025-02-01 16:21:59 +00:00
parent d3f8ba3be5
commit a132c1de0c
No known key found for this signature in database
GPG key ID: CF8D58F3DEB20755
3 changed files with 108 additions and 2 deletions

View file

@ -23,7 +23,8 @@ This interacts with i3 using the [i3bar input protocol](https://i3wm.org/docs/i3
* `CPU` - show CPU load and provide alerts if it leaves set boundaries
* `DateTime` - show the current date and time
* `Disk` - show the current usage of a disk
* `IPAddress` - show the current local IPv4 address
* `IPAddress` - show the current local IPv4/IPv6 address
* `IPType` - show the current available IP versions
* `Memory` - show the current memory usage and provide alerts it if leaves set boundaries
* `PlainText`
* `PulseaudioVolume` - show the current volume of a PulseAudio sink and control that using the scroll wheel

View file

@ -53,7 +53,8 @@ func run() error {
// providers.NewDisk("/", 30, 10),
providers.NewBattery("BAT0", 80, 30, 20),
providers.NewWiFi("wlp0s20f3", 75),
providers.NewIPAddress("wlp0s20f3"),
providers.NewIPType("wlp0s20f3"),
// providers.NewIPAddress("wlp0s20f3"),
providers.NewAudioPlayer(32),
)

View file

@ -0,0 +1,104 @@
package providers
import (
"fmt"
"strings"
"github.com/codemicro/bar/internal/i3bar"
"github.com/samber/lo"
)
type IPType struct {
Adapter string
name string
}
func NewIPType(adapter string) i3bar.BlockGenerator {
return &IPType{
Adapter: adapter,
name: "ipAddr",
}
}
func (g *IPType) Frequency() uint8 {
return 5
}
func (g *IPType) getAdapterIPTypes() ([]string, error) {
// call ifconfig
output, err := runCommand("ifconfig")
if err != nil {
return nil, err
}
adapters := lo.Filter(
strings.Split(string(output), "\n\n"),
func(x string, _ int) bool {
return x != ""
},
)
var ipTypes []string
// parse output
// split by \n\n
for _, adapter := range adapters {
fields := strings.Fields(adapter)
if !strings.EqualFold(
strings.TrimSuffix(fields[0], ":"), g.Adapter,
) {
continue
}
for _, field := range fields {
switch field {
case "inet":
ipTypes = append(ipTypes, "ipv4")
case "inet6":
ipTypes = append(ipTypes, "ipv6")
}
}
}
knownTypes := make(map[string]struct{})
n := 0
for _, v := range ipTypes {
if _, known := knownTypes[v]; !known {
knownTypes[v] = struct{}{}
ipTypes[n] = v
n += 1
}
}
ipTypes = ipTypes[:n]
return ipTypes, nil
}
func (g *IPType) Block(colors *i3bar.ColorSet) (*i3bar.Block, error) {
ipAddrs, err := g.getAdapterIPTypes()
if err != nil {
return nil, err
}
block := &i3bar.Block{
Name: g.name,
Instance: g.Adapter,
}
if len(ipAddrs) == 0 {
block.TextColor = colors.Bad
block.FullText = fmt.Sprintf("%s no IP", g.Adapter)
block.ShortText = "no IP"
} else {
block.TextColor = colors.Good
block.FullText = strings.Join(ipAddrs, "+")
}
return block, nil
}
func (g *IPType) GetNameAndInstance() (string, string) {
return g.name, g.Adapter
}