Commit: 82c0e997702b895185ccf3447ec91b13773edbb2 Author: Vi Grey Date: 2023-08-22 09:40 UTC Summary: Add apparent solar time display src/config.go | 14 ++++++++++---- src/moon.go | 1 + src/solartime.go | 31 +++++++++++++++++++++++++++++++ src/timer.go | 2 ++ src/topbar.go | 3 ++- 5 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/config.go b/src/config.go index 82e127f..fb85f7a 100644 --- a/src/config.go +++ b/src/config.go @@ -11,10 +11,11 @@ var ( ) type Config struct { - WeatherStation string `json:"weather-station"` - Latitude float32 `json:"latitude"` - Longitude float32 `json:"longitude"` - Email []ConfigEmail `json email` + WeatherStation string `json:"weather-station"` + Latitude float32 `json:"latitude"` + Longitude float32 `json:"longitude"` + SolarTime ConfigSolarTime `json:"solar-time"` + Email []ConfigEmail `json:"email"` } type ConfigEmail struct { @@ -22,6 +23,11 @@ type ConfigEmail struct { SpamPath string `json:"spam-path"` } +type ConfigSolarTime struct { + Enabled bool `json:"enabled"` + TwelveHourTime bool `json:"twelve-hour-time"` +} + var ( configPaths = []string{ filepath.Join(os.Getenv("XDG_CONFIG_HOME"), "topbar", "config"), diff --git a/src/moon.go b/src/moon.go index d3eb6ea..c2ce777 100644 --- a/src/moon.go +++ b/src/moon.go @@ -11,6 +11,7 @@ type Syzygy struct { MoonIllumination float64 `json:"moonIllumination"` MoonPhase float64 `json:"moonPhase"` SunAngle float64 `json:"sunAngle"` + SunEarthPhase float64 `json:"sunEarthPhase"` } func getSyzygyData() (s Syzygy) { diff --git a/src/solartime.go b/src/solartime.go new file mode 100644 index 0000000..c1a5cb3 --- /dev/null +++ b/src/solartime.go @@ -0,0 +1,31 @@ +package main + +import ( + "fmt" + "math" +) + +func getSolarTime(sy Syzygy) (s string) { + if configData.SolarTime.Enabled { + hourData := (math.Mod(sy.SunEarthPhase+360, 360) - 180) / 15 + hour, hourFrac := math.Modf(hourData) + seconds := int(hourFrac * 3600) + minute := seconds / 60 + second := seconds % 60 + hourPadding := "" + ampm := "" + if configData.SolarTime.TwelveHourTime { + if hour >= 12 { + ampm += "PM" + } else { + ampm += "AM" + } + } else { + if hour < 10 { + hourPadding = "0" + } + } + s = fmt.Sprintf("%s%d:%02d:%02d%s | ", hourPadding, int(hour), minute, second, ampm) + } + return +} diff --git a/src/timer.go b/src/timer.go index a104a01..e42e7d2 100644 --- a/src/timer.go +++ b/src/timer.go @@ -61,6 +61,7 @@ func moonTimer() { s := getSyzygyData() moonStr = getMoon(s) runRedshift(s) + solarTimeStr = getSolarTime(s) ticker := time.NewTicker(1000 * time.Millisecond) for { select { @@ -68,6 +69,7 @@ func moonTimer() { s := getSyzygyData() moonStr = getMoon(s) runRedshift(s) + solarTimeStr = getSolarTime(s) } } } diff --git a/src/topbar.go b/src/topbar.go index 1fb5c2d..6ead579 100644 --- a/src/topbar.go +++ b/src/topbar.go @@ -10,6 +10,7 @@ var ( streamingStr string emailStr, diceStr, updateCheckStr string limerickDateStr string + solarTimeStr string rebootStr string twitchIcon string twitchBlinkTimer int @@ -17,7 +18,7 @@ var ( func printBarLine() { barline := "" - //barline += streamingStr + barline += solarTimeStr barline += limerickDateStr barline += updateCheckStr barline += pomodoroStr