From 3b25b5bc0254c7462555c9911c67266627428795 Mon Sep 17 00:00:00 2001 From: Jackson Taylor Date: Fri, 2 Oct 2020 23:10:52 -0400 Subject: Create modules package --- .gitignore | 1 + bar.go | 7 +++++-- batteryModule.go | 30 ------------------------------ config.go | 12 ++++++++---- dateModule.go | 17 ----------------- main.go | 1 + module.go | 6 ------ modules/batteryModule.go | 29 +++++++++++++++++++++++++++++ modules/dateModule.go | 17 +++++++++++++++++ modules/module.go | 6 ++++++ modules/timeModule.go | 10 ++++++++++ timeModule.go | 10 ---------- 12 files changed, 77 insertions(+), 69 deletions(-) delete mode 100644 batteryModule.go delete mode 100644 dateModule.go delete mode 100644 module.go create mode 100644 modules/batteryModule.go create mode 100644 modules/dateModule.go create mode 100644 modules/module.go create mode 100644 modules/timeModule.go delete mode 100644 timeModule.go diff --git a/.gitignore b/.gitignore index 77dc878..6369cac 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ jasbr +# For windows *.exe diff --git a/bar.go b/bar.go index 510d292..a2a9d3c 100644 --- a/bar.go +++ b/bar.go @@ -1,10 +1,13 @@ package main -import "fmt" +import ( + "fmt" + "github.com/HelixBePraised/jasbr/modules" +) // Bar is the struct that holds each of the modules and displays the data from them type Bar struct { - Modules []Module + Modules []modules.Module } func (b Bar) Display() string { diff --git a/batteryModule.go b/batteryModule.go deleted file mode 100644 index 81fdef0..0000000 --- a/batteryModule.go +++ /dev/null @@ -1,30 +0,0 @@ -package main - -import ( - "io/ioutil" -) - -// TODO: Move this to a central spot, like a config.h -const ( - batteryPath = "/sys/class/power_supply/BAT0/capacity" - identifier = "BAT" -) - -// BatteryModule is the module that controls finding out the battery -// percentage, mainly useful in laptops -type BatteryModule struct { -} - -func (_ BatteryModule) GetInfo() (string, error) { - return readBatteryLevel(batteryPath) -} - -func readBatteryLevel(path string) (string, error) { - dat, err := ioutil.ReadFile(path) - - if err != nil { - return "NULL", err - } - - return (string(dat[:len(dat)-1]) + "%"), nil -} diff --git a/config.go b/config.go index 27badc9..ca73a6c 100644 --- a/config.go +++ b/config.go @@ -1,13 +1,17 @@ package main +import ( + "github.com/HelixBePraised/jasbr/modules" +) + // This is the main configuration file for jasbr // You can comment out any modules you don't want to use // you can also add any new ones here that you make // Order does matter // Modules that are displayed in the bar -var BarModules = []Module{ - DateModule{}, - BatteryModule{}, - TimeModule{}, +var BarModules = []modules.Module{ + modules.DateModule{}, + modules.BatteryModule{}, + modules.TimeModule{}, } diff --git a/dateModule.go b/dateModule.go deleted file mode 100644 index 1fe60fa..0000000 --- a/dateModule.go +++ /dev/null @@ -1,17 +0,0 @@ -package main - -import ( - "fmt" - "strconv" - "time" -) - -type DateModule struct { -} - -// getDate formats a string for the status bar -func (_ DateModule) GetInfo() (string, error) { - now := time.Now() - // return now.Weekday().String() + " " + now.Month().String()[0:3] + " " + now.Year(), nil - return fmt.Sprintf("%s %s %s %s", now.Weekday(), now.Month().String()[0:3], strconv.Itoa(now.Day()), strconv.Itoa(now.Year())), nil -} diff --git a/main.go b/main.go index 87ca582..6ebbad4 100644 --- a/main.go +++ b/main.go @@ -16,5 +16,6 @@ func main() { } // Display the bar info to stdout + // TODO: Change this to interact with Xorg directly? fmt.Println(main.Display()) } diff --git a/module.go b/module.go deleted file mode 100644 index 7370452..0000000 --- a/module.go +++ /dev/null @@ -1,6 +0,0 @@ -package main - -// Module is the interface for you to extend what your bar does. -type Module interface { - GetInfo() (string, error) -} diff --git a/modules/batteryModule.go b/modules/batteryModule.go new file mode 100644 index 0000000..d4593ff --- /dev/null +++ b/modules/batteryModule.go @@ -0,0 +1,29 @@ +package modules + +import ( + "io/ioutil" +) + +// TODO: Move this to a central spot, like a config.h +const ( + batteryPath = "/sys/class/power_supply/BAT0/capacity" +) + +// BatteryModule is the module that controls finding out the battery +// percentage, mainly useful in laptops +type BatteryModule struct { +} + +func (_ BatteryModule) GetInfo() (string, error) { + return readBatteryLevel(batteryPath) +} + +func readBatteryLevel(path string) (string, error) { + dat, err := ioutil.ReadFile(path) + + if err != nil { + return "NULL", err + } + + return (string(dat[:len(dat)-1]) + "%"), nil +} diff --git a/modules/dateModule.go b/modules/dateModule.go new file mode 100644 index 0000000..9dec1dd --- /dev/null +++ b/modules/dateModule.go @@ -0,0 +1,17 @@ +package modules + +import ( + "fmt" + "strconv" + "time" +) + +type DateModule struct { +} + +// getDate formats a string for the status bar +func (_ DateModule) GetInfo() (string, error) { + now := time.Now() + // return now.Weekday().String() + " " + now.Month().String()[0:3] + " " + now.Year(), nil + return fmt.Sprintf("%s %s %s %s", now.Weekday(), now.Month().String()[0:3], strconv.Itoa(now.Day()), strconv.Itoa(now.Year())), nil +} diff --git a/modules/module.go b/modules/module.go new file mode 100644 index 0000000..69e8337 --- /dev/null +++ b/modules/module.go @@ -0,0 +1,6 @@ +package modules + +// Module is the interface for you to extend what your bar does. +type Module interface { + GetInfo() (string, error) +} diff --git a/modules/timeModule.go b/modules/timeModule.go new file mode 100644 index 0000000..501b2fe --- /dev/null +++ b/modules/timeModule.go @@ -0,0 +1,10 @@ +package modules + +import "time" + +type TimeModule struct { +} + +func (_ TimeModule) GetInfo() (string, error) { + return time.Now().Format(time.Kitchen), nil +} diff --git a/timeModule.go b/timeModule.go deleted file mode 100644 index da9522b..0000000 --- a/timeModule.go +++ /dev/null @@ -1,10 +0,0 @@ -package main - -import "time" - -type TimeModule struct { -} - -func (_ TimeModule) GetInfo() (string, error) { - return time.Now().Format(time.Kitchen), nil -} -- cgit v1.2.3