diff --git a/config.go b/config.go index fb325dc..5ea791b 100644 --- a/config.go +++ b/config.go @@ -23,23 +23,30 @@ import ( ) var ( - delim = " ][ " - prefix = "[ " - suffix = " ]" + delim = "] [" + prefix = "[" + suffix = "]" ) var modules = []Module{ { - Func: readers.ReadUptime, + Func: readers.ReadExec("statusbar cpu"), + Interval: 5 * time.Second, + }, + { + Func: readers.ReadExec("statusbar volume"), + Signal: 1, + }, + { + Func: readers.ReadExec("statusbar battery"), + Interval: 60 * time.Second, + }, + { + Func: readers.ReadExec("statusbar date"), Interval: 1 * time.Second, }, { - Func: readers.ReadLoad, + Func: readers.ReadExec("statusbar loadavg"), Interval: 5 * time.Second, }, - { - Func: readers.ReadMemory, - Interval: 5 * time.Second, - Template: "{{.UsedPretty}} / {{.TotalPretty}}", - }, } diff --git a/lib/readers/battery.go b/lib/readers/battery.go index 2b87fc0..02e1c26 100644 --- a/lib/readers/battery.go +++ b/lib/readers/battery.go @@ -116,7 +116,7 @@ func BatteryTechnologyFromStr(technologyStr string) BatteryTechnology { return TechnologyUnknown } -func ReadBattery(batteryName string) (interface{}, error) { +func readBattery(batteryName string) (interface{}, error) { capacityPath := fmt.Sprintf("/sys/class/power_supply/%s/capacity", batteryName) statusPath := fmt.Sprintf("/sys/class/power_supply/%s/status", batteryName) technologyPath := fmt.Sprintf("/sys/class/power_supply/%s/technology", batteryName) @@ -169,3 +169,9 @@ func ReadBattery(batteryName string) (interface{}, error) { Technology: BatteryTechnologyFromStr(batteryTechnology), }, nil } + +func ReadBattery(batteryName string) func() (interface{}, error) { + return func() (interface{}, error) { + return readBattery(batteryName) + } +} diff --git a/lib/readers/cpu_temperature.go b/lib/readers/cpu_temperature.go index 86f632a..ad98f06 100644 --- a/lib/readers/cpu_temperature.go +++ b/lib/readers/cpu_temperature.go @@ -25,7 +25,7 @@ import ( type CpuTemperatureInfo float32 -func ReadCpuTemperature(hwmonName, tempName string) (interface{}, error) { +func readCpuTemperature(hwmonName, tempName string) (interface{}, error) { tempPath := fmt.Sprintf("/sys/class/hwmon/%s/%s_input", hwmonName, tempName) file, err := os.Open(tempPath) @@ -49,3 +49,9 @@ func ReadCpuTemperature(hwmonName, tempName string) (interface{}, error) { return CpuTemperatureInfo(cpuTemperature), nil } + +func ReadCpuTemperature(hwmonName, tempName string) func() (interface{}, error) { + return func() (interface{}, error) { + return readCpuTemperature(hwmonName, tempName) + } +} diff --git a/lib/readers/cpu_usage.go b/lib/readers/cpu_usage.go index c23b0aa..43d72e5 100644 --- a/lib/readers/cpu_usage.go +++ b/lib/readers/cpu_usage.go @@ -34,7 +34,7 @@ func (cu CpuUsageInfo) String() string { return fmt.Sprintf("%d%%", uint(cu.UsagePercent)) } -func ReadCpuUsage() (interface{}, error) { +func readCpuUsage() (interface{}, error) { file, err := os.Open("/proc/stat") if err != nil { return CpuUsageInfo{}, err @@ -81,3 +81,9 @@ func ReadCpuUsage() (interface{}, error) { UsagePercent: float64(cpuInUse) * 100 / float64(cpuTotal), }, nil } + +func ReadCpuUsage() func() (interface{}, error) { + return func() (interface{}, error) { + return readCpuUsage() + } +} diff --git a/lib/readers/exec.go b/lib/readers/exec.go new file mode 100644 index 0000000..09e1af9 --- /dev/null +++ b/lib/readers/exec.go @@ -0,0 +1,53 @@ +// modbot is a system information agregator +// Copyright (C) 2024 frosty +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package readers + +import ( + "bytes" + "os/exec" + "strings" +) + +type ExecInfo string + +func readExec(command string) (interface{}, error) { + args := []string{"sh", "-c", command} + var stdout, stderr bytes.Buffer + + cmd := exec.Command(args[0], args[1:]...) + cmd.Stdout = &stdout + cmd.Stderr = &stderr + + if err := cmd.Run(); err != nil { + return ExecInfo(""), err + } + + outputLines := strings.Split(stdout.String(), "\n") + if len(outputLines) == 0 { + return ExecInfo(""), nil + } + + outputString := outputLines[0] + return ExecInfo(outputString), nil + +} + +func ReadExec(command string) func() (interface{}, error) { + return func() (interface{}, error) { + return readExec(command) + } +} diff --git a/lib/readers/load.go b/lib/readers/load.go index b43c6fd..5db316b 100644 --- a/lib/readers/load.go +++ b/lib/readers/load.go @@ -33,7 +33,7 @@ func (l LoadInfo) String() string { return fmt.Sprintf("%v %v %v", l.OneMinute, l.FiveMinute, l.FifteenMinute) } -func ReadLoad() (interface{}, error) { +func readLoad() (interface{}, error) { const loadPath = "/proc/loadavg" file, err := os.Open(loadPath) @@ -60,3 +60,9 @@ func ReadLoad() (interface{}, error) { FifteenMinute: fields[2], }, nil } + +func ReadLoad() func() (interface{}, error) { + return func() (interface{}, error) { + return readLoad() + } +} diff --git a/lib/readers/memory.go b/lib/readers/memory.go index 60a8cae..34daf47 100644 --- a/lib/readers/memory.go +++ b/lib/readers/memory.go @@ -37,7 +37,7 @@ type MemoryInfo struct { UsedPretty string } -func ReadMemory() (interface{}, error) { +func readMemory() (interface{}, error) { file, err := os.Open("/proc/meminfo") if err != nil { return MemoryInfo{}, err @@ -89,3 +89,9 @@ func ReadMemory() (interface{}, error) { UsedPretty: ui.PrettifyKib(memUsed, 2), }, nil } + +func ReadMemory() func() (interface{}, error) { + return func() (interface{}, error) { + return readMemory() + } +} diff --git a/lib/readers/os.go b/lib/readers/os.go index 9f502e8..b1c1692 100644 --- a/lib/readers/os.go +++ b/lib/readers/os.go @@ -29,7 +29,7 @@ type OsInfo struct { Version string } -func ReadOs() (interface{}, error) { +func readOs() (interface{}, error) { const osPath = "/lib/os-release" file, err := os.Open(osPath) @@ -70,3 +70,9 @@ func ReadOs() (interface{}, error) { Version: osVersion, }, nil } + +func ReadOs() func() (interface{}, error) { + return func() (interface{}, error) { + return readOs() + } +} diff --git a/lib/readers/uptime.go b/lib/readers/uptime.go index a6ff75b..0265ee6 100644 --- a/lib/readers/uptime.go +++ b/lib/readers/uptime.go @@ -65,7 +65,7 @@ func (u UptimeInfo) String() string { return builder.String() } -func ReadUptime() (interface{}, error) { +func readUptime() (interface{}, error) { const uptimePath = "/proc/uptime" file, err := os.Open(uptimePath) @@ -92,3 +92,9 @@ func ReadUptime() (interface{}, error) { return UptimeInfo(uptime), nil } + +func ReadUptime() func() (interface{}, error) { + return func() (interface{}, error) { + return readUptime() + } +}