Created
March 8, 2019 16:48
-
-
Save nehayward/45249e163912331e55975482ef1cb13b to your computer and use it in GitHub Desktop.
Change Sublime Theme at Sunrise/Sunset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
// /Users/nehayward/Library/Application Support/Sublime Text 3/Packages/User | |
//`"color_scheme": "Packages/Color Scheme - Default/Solarized (Light).tmTheme",` | |
import ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"strings" | |
"time" | |
) | |
type SunriseSunsetAPIResponse struct { | |
Results struct { | |
AstronomicalTwilightBegin string `json:"astronomical_twilight_begin"` | |
AstronomicalTwilightEnd string `json:"astronomical_twilight_end"` | |
CivilTwilightBegin string `json:"civil_twilight_begin"` | |
CivilTwilightEnd string `json:"civil_twilight_end"` | |
DayLength int `json:"day_length"` | |
NauticalTwilightBegin string `json:"nautical_twilight_begin"` | |
NauticalTwilightEnd string `json:"nautical_twilight_end"` | |
SolarNoon string `json:"solar_noon"` | |
Sunrise string `json:"sunrise"` | |
Sunset string `json:"sunset"` | |
} `json:"results"` | |
Status string `json:"status"` | |
} | |
func main() { | |
// t := time.Now() | |
getSunrise() | |
} | |
func getSunrise() { | |
//http://api.sunrise-sunset.org/json?lat=30.2672&lng=-97.7431 | |
resp, err := http.Get("http://api.sunrise-sunset.org/json?lat=30.16&lng=-97.45&formatted=0") | |
if err != nil { | |
panic(err.Error()) | |
} | |
body, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
panic(err.Error()) | |
} | |
s, err := getStations([]byte(body)) | |
if err == nil { | |
fmt.Println(s.Results.Sunrise) | |
fmt.Println(s.Results.Sunset) | |
t, err := time.Parse(time.RFC3339Nano, s.Results.Sunrise) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(t) | |
b, err := time.Parse(time.RFC3339Nano, s.Results.Sunset) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(t) | |
s := time.Now().UTC() | |
fmt.Println("Time is ", s) | |
if inTimeSpan(t, b, s) { | |
changeSublimeThemeDay() | |
fmt.Println(s, "is between", t, "and", b, ".") | |
} else { | |
fmt.Println("Sunset") | |
changeSublimeThemeNight() | |
} | |
} | |
} | |
func inTimeSpan(start, end, check time.Time) bool { | |
return check.After(start) && check.Before(end) | |
} | |
var countryTz = map[string]string{ | |
"Hungary": "Europe/Budapest", | |
"Egypt": "Africa/Cairo", | |
"Austin": "America/Chicago", | |
} | |
func timeIn(name string) time.Time { | |
loc, err := time.LoadLocation(countryTz[name]) | |
if err != nil { | |
panic(err) | |
} | |
return time.Now().In(loc) | |
} | |
func getStations(body []byte) (*SunriseSunsetAPIResponse, error) { | |
var s = new(SunriseSunsetAPIResponse) | |
err := json.Unmarshal(body, &s) | |
if err != nil { | |
fmt.Println("whoops:", err) | |
} | |
return s, err | |
} | |
func getThemeFile() []byte { | |
input, err := ioutil.ReadFile("/Users/nehayward/Library/Application Support/Sublime Text 3/Packages/User/Preferences.sublime-settings") | |
if err != nil { | |
log.Fatalln(err) | |
} | |
return input | |
} | |
func changeSublimeThemeDay() { | |
input := getThemeFile() | |
lines := strings.Split(string(input), "\n") | |
for i, line := range lines { | |
if strings.Contains(line, "color_scheme") { | |
lines[i] = `"color_scheme": "Packages/Boxy Theme/schemes/Boxy Yesterday.tmTheme",` | |
} | |
} | |
output := strings.Join(lines, "\n") | |
err := ioutil.WriteFile("/Users/nehayward/Library/Application Support/Sublime Text 3/Packages/User/Preferences.sublime-settings", []byte(output), 0644) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
} | |
func changeSublimeThemeNight() { | |
input := getThemeFile() | |
lines := strings.Split(string(input), "\n") | |
for i, line := range lines { | |
if strings.Contains(line, "color_scheme") { | |
lines[i] = `"color_scheme": "Packages/Boxy Theme/schemes/Boxy Tomorrow.tmTheme",` | |
} | |
} | |
output := strings.Join(lines, "\n") | |
err := ioutil.WriteFile("/Users/nehayward/Library/Application Support/Sublime Text 3/Packages/User/Preferences.sublime-settings", []byte(output), 0644) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
fmt.Println("Done") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment