breto

A status bar written in Go.
git clone git://git.swab.dev/breto.git
Log | Files | Refs | README | LICENSE

commit cac365d8b4d0118a8fb3e300a3107e33d6310a95
parent 107702ca5889ace331d0fd36e7450b4fa24f7553
Author: Jaron Swab <jaron@swab.dev>
Date:   Wed,  7 Apr 2021 21:37:48 -0400

added format package

Diffstat:
MCHANGELOG.md | 1+
Aformat/format.go | 65+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mmain.go | 101+++++++++++++++++++------------------------------------------------------------
Mstats/stats.go | 5+++++
4 files changed, 95 insertions(+), 77 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - stats package for info struct +- format package for formatting text and defining flags ## [0.12.0] - January 12, 2020 diff --git a/format/format.go b/format/format.go @@ -0,0 +1,65 @@ +package format + +import ( + "flag" + "fmt" + + "git.swab.dev/breto.git/blocks" + "git.swab.dev/breto.git/icons" + "git.swab.dev/breto.git/stats" +) + +// CLI flag variables +type Options struct { + Dwm, Battery, Clock, Audio, Memory, DiskSpace, Temperature, Tray, Emoji bool +} + +func (o *Options) ParseFlags() *Options { + // Setup and define cli flags + flag.BoolVar(&o.Dwm, "dwm", false, "Used to enable output for DWM's status bar.\n Example: --dwm=true") + flag.BoolVar(&o.Battery, "battery", false, "Used to enable battery module.\n Example: --battery=true") + flag.BoolVar(&o.Clock, "dateTime", true, "Used to disable the date and time module.\n Example: --dateTime=false") + flag.BoolVar(&o.Audio, "volume", true, "Used to disable the volume module.\n Example: --volume=false") + flag.BoolVar(&o.Memory, "ram", true, "Used to disable the RAM module.\n Example: --ram=false") + flag.BoolVar(&o.DiskSpace, "storage", true, "Used to disable the home directory storage module.\n Example: --storage=false") + flag.BoolVar(&o.Temperature, "temp", true, "Used to disable the temperature module.\n Example: --temp=false") + flag.BoolVar(&o.Tray, "tray", true, "Used to disable the custom tray module.\n Example: --tray=false") + flag.BoolVar(&o.Emoji, "emoji", false, "Used to enable Openmoji icons instead of Awesome Font.\n Example: --emoji=true") + + flag.Parse() + + return o +} + +func (o *Options) Output(status string, info *stats.Info, ico *icons.Symbols, bat *stats.Battery) string { + if o.Temperature { + status = fmt.Sprintf("%s %s%s ", status, icons.Temp(o.Emoji), info.Weather) + } + if o.DiskSpace { + status = fmt.Sprintf("%s %s%s ", status, icons.Dir(o.Emoji), info.HomeSpace) + } + if o.Memory { + status = fmt.Sprintf("%s %s%s ", status, icons.Mem(o.Emoji), info.RamFree) + } + if o.Audio { + info.VolText, _ = blocks.VolumeText() + ico.VolIcon, _ = icons.Volume(o.Emoji) + status = fmt.Sprintf("%s %s%s ", status, ico.VolIcon, info.VolText) + } + if o.Battery { + if bat.FiveMins == 0 || bat.Passed < 10 { + info.Power, _ = blocks.Battery() + } + status = fmt.Sprintf("%s %s%s ", status, icons.Power(o.Emoji), info.Power) + } + if o.Clock { + status = fmt.Sprintf("%s %s ", status, info.HTime) + } + if o.Tray { + ico.RShift, _ = icons.Redshift(o.Emoji) + ico.Dropbox, _ = icons.Dropbox(o.Emoji) + ico.Syncthing, _ = icons.Syncthing(o.Emoji) + status = fmt.Sprintf("%s %s%s%s", status, ico.Dropbox, ico.Syncthing, ico.RShift) + } + return status +} diff --git a/main.go b/main.go @@ -1,79 +1,24 @@ package main import ( - "flag" - "fmt" "log" "math" "time" "git.swab.dev/breto.git/blocks" + "git.swab.dev/breto.git/format" "git.swab.dev/breto.git/icons" "git.swab.dev/breto.git/stats" "git.swab.dev/breto.git/ui" ) -// CLI flag variables -var dwm, battery, clock, audio, memory, diskSpace, temperature, tray, emoji bool - -func init() { - // Setup and define cli flags - flag.BoolVar(&dwm, "dwm", false, "Used to enable output for DWM's status bar.\n Example: --dwm=true") - flag.BoolVar(&battery, "battery", false, "Used to enable battery module.\n Example: --battery=true") - flag.BoolVar(&clock, "dateTime", true, "Used to disable the date and time module.\n Example: --dateTime=false") - flag.BoolVar(&audio, "volume", true, "Used to disable the volume module.\n Example: --volume=false") - flag.BoolVar(&memory, "ram", true, "Used to disable the RAM module.\n Example: --ram=false") - flag.BoolVar(&diskSpace, "storage", true, "Used to disable the home directory storage module.\n Example: --storage=false") - flag.BoolVar(&temperature, "temp", true, "Used to disable the temperature module.\n Example: --temp=false") - flag.BoolVar(&tray, "tray", true, "Used to disable the custom tray module.\n Example: --tray=false") - flag.BoolVar(&emoji, "emoji", false, "Used to enable Openmoji icons instead of Awesome Font.\n Example: --emoji=true") -} - -func formatOutput(status string, stats *stats.Info, ico *icons.Symbols, baty batInfo) string { - if temperature { - status = fmt.Sprintf("%s %s%s ", status, icons.Temp(emoji), stats.Weather) - } - if diskSpace { - status = fmt.Sprintf("%s %s%s ", status, icons.Dir(emoji), stats.HomeSpace) - } - if memory { - status = fmt.Sprintf("%s %s%s ", status, icons.Mem(emoji), stats.RamFree) - } - if audio { - stats.VolText, _ = blocks.VolumeText() - ico.VolIcon, _ = icons.Volume(emoji) - status = fmt.Sprintf("%s %s%s ", status, ico.VolIcon, stats.VolText) - } - if battery { - if baty.fiveMins == 0 || baty.passed < 10 { - stats.Power, _ = blocks.Battery() - } - status = fmt.Sprintf("%s %s%s ", status, icons.Power(emoji), stats.Power) - } - if clock { - status = fmt.Sprintf("%s %s ", status, stats.HTime) - } - if tray { - ico.RShift, _ = icons.Redshift(emoji) - ico.Dropbox, _ = icons.Dropbox(emoji) - ico.Syncthing, _ = icons.Syncthing(emoji) - status = fmt.Sprintf("%s %s%s%s", status, ico.Dropbox, ico.Syncthing, ico.RShift) - } - return status -} - -type batInfo struct { - passed float64 - fiveMins float64 -} - func main() { - flag.Parse() var ( - stats = new(stats.Info) + opt = new(format.Options) + info = new(stats.Info) ico = new(icons.Symbols) - baty = batInfo{} + bat = new(stats.Battery) cWttr = make(chan string) eWttr = make(chan error) cRAM = make(chan string) @@ -82,17 +27,19 @@ func main() { eHomeDisk = make(chan error) ) + o := opt.ParseFlags() + // Each Go routine has it's own timer to delay the execution of the command. // A Go routine will run unless it's CLI flag is set to false. - if temperature { + if o.Temperature { go blocks.Wttr(cWttr, eWttr) } - if memory { + if o.Memory { go blocks.FreeRam(cRAM, eRAM) } - if diskSpace { + if o.DiskSpace { go blocks.HomeDisk(cHomeDisk, eHomeDisk) } @@ -100,32 +47,32 @@ func main() { ticker := time.NewTicker(time.Second) for range ticker.C { // add year & seconds with "Jan 02, 2006 15:04:05" - stats.HTime = time.Now().Format("Jan 02 15:04") + info.HTime = time.Now().Format("Jan 02 15:04") - if battery { - baty.passed = time.Since(start).Seconds() - baty.fiveMins = math.Floor(math.Remainder(baty.passed, 300)) + if o.Battery { + bat.Passed = time.Since(start).Seconds() + bat.FiveMins = math.Floor(math.Remainder(bat.Passed, 300)) } select { // updates the go routine channels as they send data - case stats.Weather = <-cWttr: - case stats.WttrErr = <-eWttr: - log.Println(stats.WttrErr.Error()) - case stats.RamFree = <-cRAM: - case stats.RamErr = <-eRAM: - log.Println(stats.RamErr.Error()) - case stats.HomeSpace = <-cHomeDisk: - case stats.HomeErr = <-eHomeDisk: - log.Println(stats.HomeErr.Error()) + case info.Weather = <-cWttr: + case info.WttrErr = <-eWttr: + log.Println(info.WttrErr.Error()) + case info.RamFree = <-cRAM: + case info.RamErr = <-eRAM: + log.Println(info.RamErr.Error()) + case info.HomeSpace = <-cHomeDisk: + case info.HomeErr = <-eHomeDisk: + log.Println(info.HomeErr.Error()) default: } // Status bar information as defined by the CLI flags. // reset status on every run. - finalStatus := formatOutput("", stats, ico, baty) + finalStatus := o.Output("", info, ico, bat) // Output methods as specified by CLI flags. - if dwm { + if o.Dwm { ui.Dwm(finalStatus) } else { ui.Default(finalStatus) diff --git a/stats/stats.go b/stats/stats.go @@ -12,3 +12,8 @@ type Info struct { RamErr error HomeErr error } + +type Battery struct { + Passed float64 + FiveMins float64 +}