add reading from config file

This commit is contained in:
sudacode 2025-05-04 01:45:51 -07:00
parent ae201f1be0
commit 8f3b4f14d8
Signed by: sudacode
SSH Key Fingerprint: SHA256:lT5C2bB398DcX6daCF/gYFNSTK3y+Du3oTGUnYzfTEw

@ -20,7 +20,11 @@ const (
wallpaperDir = "Pictures/wallpapers/wallhaven" wallpaperDir = "Pictures/wallpapers/wallhaven"
) )
var topics = []string{ type Config struct {
Topics []string `json:"topics"`
}
var defaultTopics = []string{
"132262 - Mobuseka", "132262 - Mobuseka",
"konosuba", "konosuba",
"bunny girl senpai", "bunny girl senpai",
@ -30,6 +34,24 @@ var topics = []string{
"eminence in shadow", "eminence in shadow",
} }
func loadConfig() []string {
homeDir, err := os.UserHomeDir()
if err != nil {
return defaultTopics
}
configPath := filepath.Join(homeDir, ".config", "change-wallpaper", "config.json")
file, err := os.Open(configPath)
if err != nil {
return defaultTopics
}
defer file.Close()
var cfg Config
if err := json.NewDecoder(file).Decode(&cfg); err != nil || len(cfg.Topics) == 0 {
return defaultTopics
}
return cfg.Topics
}
type WallhavenResponse struct { type WallhavenResponse struct {
Data []struct { Data []struct {
Path string `json:"path"` Path string `json:"path"`
@ -40,6 +62,9 @@ func main() {
// Initialize random source // Initialize random source
r := rand.New(rand.NewSource(time.Now().UnixNano())) r := rand.New(rand.NewSource(time.Now().UnixNano()))
// Load topics from config or use defaults
topics := loadConfig()
// Check if a file path was provided as argument // Check if a file path was provided as argument
if len(os.Args) > 1 { if len(os.Args) > 1 {
imgPath := os.Args[1] imgPath := os.Args[1]
@ -63,7 +88,7 @@ func main() {
} }
// Download and set new wallpaper // Download and set new wallpaper
newWallpaper, topic := downloadRandomWallpaper(wallpaperPath, r) newWallpaper, topic := downloadRandomWallpaper(wallpaperPath, r, topics)
if newWallpaper != "" { if newWallpaper != "" {
changeWallpaper(newWallpaper, topic) changeWallpaper(newWallpaper, topic)
} else { } else {
@ -72,7 +97,7 @@ func main() {
} }
} }
func downloadRandomWallpaper(wallpaperPath string, r *rand.Rand) (string, string) { func downloadRandomWallpaper(wallpaperPath string, r *rand.Rand, topics []string) (string, string) {
// Select random topic // Select random topic
topic := topics[r.Intn(len(topics))] topic := topics[r.Intn(len(topics))]
var query string var query string