From 29ac2402418c6629338f5c2a85692c181acf5907 Mon Sep 17 00:00:00 2001 From: AKP Date: Fri, 27 May 2022 11:47:59 +0100 Subject: [PATCH] Update definition of i3bar.BlockGenerator Signed-off-by: AKP --- internal/i3bar/i3bar.go | 1 + internal/providers/audioPlayer.go | 9 ++++++++- internal/providers/battery.go | 24 +++++++++++++++--------- internal/providers/cpu.go | 9 ++++++++- internal/providers/datetime.go | 11 +++++++++-- internal/providers/disk.go | 10 +++++++++- internal/providers/ipAddr.go | 12 +++++++++++- internal/providers/memory.go | 9 ++++++++- internal/providers/plainText.go | 9 ++++++++- internal/providers/pulseaudioVolume.go | 12 ++++++++++-- internal/providers/wifi.go | 13 ++++++++++++- 11 files changed, 99 insertions(+), 20 deletions(-) diff --git a/internal/i3bar/i3bar.go b/internal/i3bar/i3bar.go index 57f7aa9..d7d5d3c 100644 --- a/internal/i3bar/i3bar.go +++ b/internal/i3bar/i3bar.go @@ -144,4 +144,5 @@ type Block struct { type BlockGenerator interface { Block(*ColorSet) (*Block, error) + GetNameAndInstance() (name, instance string) } diff --git a/internal/providers/audioPlayer.go b/internal/providers/audioPlayer.go index bfd11c0..e8fcb56 100644 --- a/internal/providers/audioPlayer.go +++ b/internal/providers/audioPlayer.go @@ -21,11 +21,14 @@ const ( type AudioPlayer struct { ShowTextOnPause bool MaxLabelLen int + + name string } func NewAudioPlayer(maxLabelLength int) *AudioPlayer { return &AudioPlayer{ MaxLabelLen: maxLabelLength, + name: "audioPlayer", } } @@ -103,7 +106,7 @@ func (g *AudioPlayer) Block(colors *i3bar.ColorSet) (*i3bar.Block, error) { } b := new(i3bar.Block) - b.Name = "audioPlayer" + b.Name = g.name b.FullText = musicNoteString b.ShortText = musicNoteString @@ -124,3 +127,7 @@ func (g *AudioPlayer) Block(colors *i3bar.ColorSet) (*i3bar.Block, error) { return b, nil } + +func (g *AudioPlayer) GetNameAndInstance() (string, string) { + return g.name, "" +} \ No newline at end of file diff --git a/internal/providers/battery.go b/internal/providers/battery.go index dd1fd53..ceaba7d 100644 --- a/internal/providers/battery.go +++ b/internal/providers/battery.go @@ -11,10 +11,10 @@ import ( ) const ( - batteryStateFull = "FULL" + batteryStateFull = "FULL" batteryStateDischarging = "BAT" - batteryStateCharging = "CHR" - batteryStateUnknown = "UNK" + batteryStateCharging = "CHR" + batteryStateUnknown = "UNK" ) type Battery struct { @@ -22,23 +22,25 @@ type Battery struct { OkThreshold float32 WarningThreshold float32 - Name string + DeviceName string UseDesignMaxEnergy bool + name string previousWasBackgroundWarning bool } -func NewBattery(name string, fullThreshold, okThreshold, warningThreshold float32) i3bar.BlockGenerator { +func NewBattery(deviceName string, fullThreshold, okThreshold, warningThreshold float32) i3bar.BlockGenerator { return &Battery{ - Name: name, + DeviceName: deviceName, FullThreshold: fullThreshold, OkThreshold: okThreshold, WarningThreshold: warningThreshold, + name: "battery", } } func (g *Battery) infoPath() string { - return path.Join("/sys/class/power_supply", g.Name) + return path.Join("/sys/class/power_supply", g.DeviceName) } func (g *Battery) getPercentage() (float32, error) { @@ -114,8 +116,8 @@ func (g *Battery) Block(colors *i3bar.ColorSet) (*i3bar.Block, error) { } block := &i3bar.Block{ - Name: "battery", - Instance: g.Name, + Name: g.name, + Instance: g.DeviceName, FullText: fmt.Sprintf("%s %.1f%%", state, percentage), ShortText: fmt.Sprintf("%.1f%%", percentage), } @@ -152,3 +154,7 @@ func (g *Battery) Block(colors *i3bar.ColorSet) (*i3bar.Block, error) { return block, nil } + +func (g *Battery) GetNameAndInstance() (string, string) { + return g.name, g.DeviceName +} diff --git a/internal/providers/cpu.go b/internal/providers/cpu.go index a1e00b9..1b8ace8 100644 --- a/internal/providers/cpu.go +++ b/internal/providers/cpu.go @@ -16,12 +16,15 @@ type CPU struct { idle0, total0 uint64 idle1, total1 uint64 + + name string } func NewCPU(okThreshold, warningThreshold float32) i3bar.BlockGenerator { m := &CPU{ OkThreshold: okThreshold, WarningThreshold: warningThreshold, + name: "cpu", } _ = m.doSample() return m @@ -74,7 +77,7 @@ func (g *CPU) Block(colors *i3bar.ColorSet) (*i3bar.Block, error) { p := g.getPercentage() block := &i3bar.Block{ - Name: "cpu", + Name: g.name, FullText: fmt.Sprintf("CPU: %.1f%%", p), ShortText: fmt.Sprintf("C: %.1f%%", p), } @@ -87,3 +90,7 @@ func (g *CPU) Block(colors *i3bar.ColorSet) (*i3bar.Block, error) { return block, nil } + +func (g *CPU) GetNameAndInstance() (string, string) { + return g.name, "" +} \ No newline at end of file diff --git a/internal/providers/datetime.go b/internal/providers/datetime.go index e74e7f9..64af3c1 100644 --- a/internal/providers/datetime.go +++ b/internal/providers/datetime.go @@ -8,18 +8,25 @@ import ( type DateTime struct { // TODO: 12 hour mode? + name string } func NewDateTime() i3bar.BlockGenerator { - return new(DateTime) + return &DateTime{ + name: "datetime", + } } func (g *DateTime) Block(*i3bar.ColorSet) (*i3bar.Block, error) { cTime := time.Now().Local() return &i3bar.Block{ - Name: "datetime", + Name: g.name, FullText: cTime.Format("2006-01-02 15:04:05"), ShortText: cTime.Format("15:04:05"), }, nil } + +func (g *DateTime) GetNameAndInstance() (string, string) { + return g.name, "" +} \ No newline at end of file diff --git a/internal/providers/disk.go b/internal/providers/disk.go index d90f2aa..3c63b74 100644 --- a/internal/providers/disk.go +++ b/internal/providers/disk.go @@ -14,6 +14,8 @@ type Disk struct { WarningThreshold float32 MountPath string + + name string } func NewDisk(mountPath string, okThreshold, warningThreshold float32) i3bar.BlockGenerator { @@ -21,6 +23,7 @@ func NewDisk(mountPath string, okThreshold, warningThreshold float32) i3bar.Bloc OkThreshold: okThreshold, WarningThreshold: warningThreshold, MountPath: mountPath, + name: "disk", } } @@ -46,7 +49,8 @@ func (g *Disk) Block(colors *i3bar.ColorSet) (*i3bar.Block, error) { } block := &i3bar.Block{ - Name: "disk", + Name: g.name, + Instance: g.MountPath, FullText: fmt.Sprintf("Disk avail: %.1fGB", da), ShortText: fmt.Sprintf("D: %.1fGB", da), } @@ -59,3 +63,7 @@ func (g *Disk) Block(colors *i3bar.ColorSet) (*i3bar.Block, error) { return block, nil } + +func (g *Disk) GetNameAndInstance() (string, string) { + return g.name, g.MountPath +} \ No newline at end of file diff --git a/internal/providers/ipAddr.go b/internal/providers/ipAddr.go index c96b53f..eb1d614 100644 --- a/internal/providers/ipAddr.go +++ b/internal/providers/ipAddr.go @@ -10,11 +10,14 @@ import ( type IPAddress struct { Adapter string + + name string } func NewIPAddress(adapter string) i3bar.BlockGenerator { return &IPAddress{ Adapter: adapter, + name: "ipAddr", } } @@ -62,7 +65,10 @@ func (g *IPAddress) Block(colors *i3bar.ColorSet) (*i3bar.Block, error) { return nil, err } - block := new(i3bar.Block) + block := &i3bar.Block{ + Name: g.name, + Instance: g.Adapter, + } if ipAddr == "" { block.TextColor = colors.Bad @@ -75,3 +81,7 @@ func (g *IPAddress) Block(colors *i3bar.ColorSet) (*i3bar.Block, error) { return block, nil } + +func (g *IPAddress) GetNameAndInstance() (string, string) { + return g.name, g.Adapter +} \ No newline at end of file diff --git a/internal/providers/memory.go b/internal/providers/memory.go index b63ffe0..9842501 100644 --- a/internal/providers/memory.go +++ b/internal/providers/memory.go @@ -13,12 +13,15 @@ import ( type Memory struct { OkThreshold float32 WarningThreshold float32 + + name string } func NewMemory(okThreshold, warningThreshold float32) i3bar.BlockGenerator { return &Memory{ OkThreshold: okThreshold, WarningThreshold: warningThreshold, + name: "memory", } } @@ -69,7 +72,7 @@ func (g *Memory) Block(colors *i3bar.ColorSet) (*i3bar.Block, error) { // TODO: Make the readout change between KB/MB/GB block := &i3bar.Block{ - Name: "memory", + Name: g.name, FullText: fmt.Sprintf("Mem: %.1fGB of %.1fGB", used, total), ShortText: fmt.Sprintf("M: %.1fGB", used), } @@ -82,3 +85,7 @@ func (g *Memory) Block(colors *i3bar.ColorSet) (*i3bar.Block, error) { return block, nil } + +func (g *Memory) GetNameAndInstance() (string, string) { + return g.name, "" +} \ No newline at end of file diff --git a/internal/providers/plainText.go b/internal/providers/plainText.go index 60cec64..0cb9e9a 100644 --- a/internal/providers/plainText.go +++ b/internal/providers/plainText.go @@ -4,17 +4,24 @@ import "github.com/codemicro/bar/internal/i3bar" type PlainText struct { Text string + + name string } func NewPlainText(text string) i3bar.BlockGenerator { return &PlainText{ Text: text, + name: "plaintext", } } func (g *PlainText) Block(*i3bar.ColorSet) (*i3bar.Block, error) { return &i3bar.Block{ - Name: "plaintext", + Name: g.name, FullText: g.Text, }, nil } + +func (g *PlainText) GetNameAndInstance() (string, string) { + return g.name, "" +} \ No newline at end of file diff --git a/internal/providers/pulseaudioVolume.go b/internal/providers/pulseaudioVolume.go index 831e3d1..a93a95c 100644 --- a/internal/providers/pulseaudioVolume.go +++ b/internal/providers/pulseaudioVolume.go @@ -14,10 +14,14 @@ type PulseaudioVolume struct { // Sink is the target sink name to look for in Pulseaudio. Leave blank // to use the default sink. Sink string + + name string } func NewPulseaudioVolume() i3bar.BlockGenerator { - return new(PulseaudioVolume) + return &PulseaudioVolume{ + name: "pulseaudioVolume", + } } func (g *PulseaudioVolume) getInfo() (string, error) { @@ -97,7 +101,7 @@ func (g *PulseaudioVolume) Block(colors *i3bar.ColorSet) (*i3bar.Block, error) { } block := new(i3bar.Block) - block.Name = "pulseaudioVolume" + block.Name = g.name block.Instance = g.Sink if v.Muted { @@ -114,3 +118,7 @@ func (g *PulseaudioVolume) Block(colors *i3bar.ColorSet) (*i3bar.Block, error) { return block, nil } + +func (g *PulseaudioVolume) GetNameAndInstance() (string, string) { + return g.name, g.Sink +} \ No newline at end of file diff --git a/internal/providers/wifi.go b/internal/providers/wifi.go index 5d44d56..158ccc0 100644 --- a/internal/providers/wifi.go +++ b/internal/providers/wifi.go @@ -13,12 +13,16 @@ import ( type WiFi struct { Adapter string OkThreshold float32 + + name string } func NewWiFi(adapter string, okThreshold float32) i3bar.BlockGenerator { return &WiFi{ Adapter: adapter, OkThreshold: okThreshold, + + name: "wifi", } } @@ -108,7 +112,10 @@ func (g *WiFi) Block(colors *i3bar.ColorSet) (*i3bar.Block, error) { return nil, err } - block := new(i3bar.Block) + block := &i3bar.Block{ + Name: g.name, + Instance: g.Adapter, + } if ssid == "" { block.TextColor = colors.Bad @@ -126,3 +133,7 @@ func (g *WiFi) Block(colors *i3bar.ColorSet) (*i3bar.Block, error) { return block, nil } + +func (g *WiFi) GetNameAndInstance() (string, string) { + return g.name, g.Adapter +} \ No newline at end of file