From 26fc0b9730f3151c8ef745889a7523eae2fed8f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Vojt=C3=A1=C5=A1ek?= Date: Thu, 3 Feb 2022 11:08:01 +0100 Subject: [PATCH] load latitude & longitude from config file --- README.md | 14 ++++++++++++- go.mod | 1 + go.sum | 2 ++ main.go | 62 +++++++++++++++++++++++++++++++++++++++---------------- 4 files changed, 60 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index c9a1fcd..a749015 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,19 @@ A lightweight version of https://github.com/vojty/sunset-app +## Setup + +1. Create `.sunset-time.toml` file in your home directory + +``` +# ~/.sunset-time.toml + +latitude = 50.073658 +longitude = 14.41854 +``` + +2. Run the app + ## Work in progress 🚧 -- make geolocation configurable (hardcoded latitude & longitude for Prague for now) - add github action for an automatic release diff --git a/go.mod b/go.mod index bf14ebe..d3e62a4 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module sunset-time go 1.13 require ( + github.com/BurntSushi/toml v1.0.0 github.com/kermieisinthehouse/systray v1.2.3 github.com/sixdouglas/suncalc v0.0.0-20210131155613-475bb71c60c4 github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 diff --git a/go.sum b/go.sum index f86f67c..7d294bf 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/BurntSushi/toml v1.0.0 h1:dtDWrepsVPfW9H/4y7dDgFc2MBUSeJhlaDtK13CxFlU= +github.com/BurntSushi/toml v1.0.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/kermieisinthehouse/systray v1.2.3 h1:tawLahcam/Ccs/F2n6EOQo8qJnSTD2hLzOYqTGsUsbA= github.com/kermieisinthehouse/systray v1.2.3/go.mod h1:axh6C/jNuSyC0QGtidZJURc9h+h41HNoMySoLVrhVR4= github.com/sixdouglas/suncalc v0.0.0-20210131155613-475bb71c60c4 h1:bOMqPNYguS4t9tLAIuDTRx7nIW1maiXX+ZIt8Bl79ss= diff --git a/main.go b/main.go index 90ab948..66a5a39 100644 --- a/main.go +++ b/main.go @@ -2,23 +2,48 @@ package main import ( "fmt" + "os" + "path/filepath" "time" + "github.com/BurntSushi/toml" "github.com/kermieisinthehouse/systray" "github.com/sixdouglas/suncalc" "github.com/skratchdot/open-golang/open" ) -var latitude = 50.073658 -var longitude = 14.41854 +type Config struct { + Latitude float64 + Longitude float64 +} + +// Prague +var defaultConfig = Config{ + Latitude: 50.073658, + Longitude: 14.41854, +} var icons = map[suncalc.DayTimeName][]byte{ suncalc.Sunrise: sunriseIcon, suncalc.Sunset: sunsetIcon, } -func getTimes(timestamp time.Time, latitude float64, longitude float64) map[suncalc.DayTimeName]suncalc.DayTime { - times := suncalc.GetTimes(timestamp, latitude, longitude) +func getConfig() Config { + homeDir, err := os.UserHomeDir() + if err != nil { + return defaultConfig + } + configPath := filepath.FromSlash(homeDir + "/.sunset-time.toml") + + var loaded Config + if _, err := toml.DecodeFile(configPath, &loaded); err != nil { + return defaultConfig + } + return loaded +} + +func getTimes(timestamp time.Time, config *Config) map[suncalc.DayTimeName]suncalc.DayTime { + times := suncalc.GetTimes(timestamp, config.Latitude, config.Longitude) return times } @@ -28,13 +53,13 @@ type event struct { name suncalc.DayTimeName } -func getNextEvent() event { +func getNextEvent(config *Config) event { now := time.Now() - times := getTimes(now, latitude, longitude) + times := getTimes(now, config) // today's sunset has already happened -> recompute for tomorrow if times[suncalc.Sunset].Time.Sub(now) < 0 { - times := getTimes(now.AddDate(0, 0, 1), latitude, longitude) + times := getTimes(now.AddDate(0, 0, 1), config) return event{time: times[suncalc.Sunrise].Time, name: suncalc.Sunrise} } @@ -45,29 +70,30 @@ func formatTime(t time.Time) string { return fmt.Sprintf("%02d:%02d", t.Hour(), t.Minute()) } -func updateTray() { - event := getNextEvent() +func updateTray(config *Config) { + event := getNextEvent(config) systray.SetTemplateIcon(icons[event.name], icons[event.name]) systray.SetTitle(formatTime(event.time)) systray.SetTooltip(fmt.Sprintf("Next %s @ %s", event.name, formatTime(event.time))) } -func updateMenuDayItem(dateItem *systray.MenuItem, timesItem *systray.MenuItem, time time.Time) { +func updateMenuDayItem(config *Config, dateItem *systray.MenuItem, timesItem *systray.MenuItem, time time.Time) { dateItem.SetTitle(fmt.Sprintf("%02d.%02d.%d", time.Day(), time.Month(), time.Year())) dateItem.Disable() - times := getTimes(time, latitude, longitude) + times := getTimes(time, config) timesItem.SetTitle(fmt.Sprintf("↗ %s ↘ %s", formatTime(times[suncalc.Sunrise].Time), formatTime(times[suncalc.Sunset].Time))) timesItem.Disable() } -func updateMenu(mTodayDate *systray.MenuItem, mTodayTimes *systray.MenuItem, mTomorrowDate *systray.MenuItem, mTomorrowTimes *systray.MenuItem) { - updateMenuDayItem(mTodayDate, mTodayTimes, time.Now()) - updateMenuDayItem(mTomorrowDate, mTomorrowTimes, time.Now().AddDate(0, 0, 1)) +func updateMenu(config *Config, mTodayDate *systray.MenuItem, mTodayTimes *systray.MenuItem, mTomorrowDate *systray.MenuItem, mTomorrowTimes *systray.MenuItem) { + updateMenuDayItem(config, mTodayDate, mTodayTimes, time.Now()) + updateMenuDayItem(config, mTomorrowDate, mTomorrowTimes, time.Now().AddDate(0, 0, 1)) } func onReady() { - updateTray() + config := getConfig() + updateTray(&config) systray.AddMenuItem("Today", "Today").Disable() mTodayDate := systray.AddMenuItem("", "") @@ -79,7 +105,7 @@ func onReady() { mTomorrowTimes := systray.AddMenuItem("", "") systray.AddSeparator() - updateMenu(mTodayDate, mTodayTimes, mTomorrowDate, mTomorrowTimes) + updateMenu(&config, mTodayDate, mTodayTimes, mTomorrowDate, mTomorrowTimes) mAbout := systray.AddMenuItem("About", "About") mQuit := systray.AddMenuItem("Quit", "Quit") @@ -105,8 +131,8 @@ func onReady() { for { select { case <-ticker.C: - updateTray() - updateMenu(mTodayDate, mTodayTimes, mTomorrowDate, mTomorrowTimes) + updateTray(&config) + updateMenu(&config, mTodayDate, mTodayTimes, mTomorrowDate, mTomorrowTimes) } } }()