generalize

This commit is contained in:
abs3nt 2023-10-24 11:45:46 -07:00
parent 2341d2c349
commit 592ed82593
Signed by: abs3nt
GPG Key ID: A7BD96A8BAB04C09

View File

@ -2,8 +2,6 @@ package cmd
import ( import (
"fmt" "fmt"
"io"
"log"
"math/rand" "math/rand"
"os" "os"
"os/exec" "os/exec"
@ -63,14 +61,6 @@ func init() {
5, 5,
"number of pages to randomly choose wallpaper from.", "number of pages to randomly choose wallpaper from.",
) )
setCmd.PersistentFlags().
BoolVarP(
&localPath,
"localPath",
"l",
false,
"set if the argument is to a directory or an image file.",
)
setCmd.PersistentFlags(). setCmd.PersistentFlags().
StringSliceVar( StringSliceVar(
&setRatios, &setRatios,
@ -85,6 +75,20 @@ func init() {
"2560x1440", "2560x1440",
"minimum resolution for results.", "minimum resolution for results.",
) )
setCmd.PersistentFlags().StringVarP(
&setScript,
"script",
"t",
"",
"script to run after downloading the wallpaper",
)
setCmd.PersistentFlags().StringVarP(
&setPath,
"download-path",
"d",
"",
"script to run after downloading the wallpaper",
)
} }
var ( var (
@ -94,9 +98,10 @@ var (
setSorting string setSorting string
setOrder string setOrder string
setAtLeast string setAtLeast string
setScript string
setPath string
setRatios []string setRatios []string
setPage int setPage int
localPath bool
setCmd = &cobra.Command{ setCmd = &cobra.Command{
Use: "set", Use: "set",
Aliases: []string{"s"}, Aliases: []string{"s"},
@ -109,25 +114,6 @@ var (
) )
func set(args []string) error { func set(args []string) error {
if localPath {
if len(args) == 0 {
return fmt.Errorf("you must provide a path to an image or directory of images to use this option")
}
filePath := args[0]
fileInfo, err := os.Stat(filePath)
if err != nil {
return err
}
if fileInfo.IsDir() {
files, err := os.ReadDir(filePath)
if err != nil {
return err
}
file := files[rand.Intn(len(files))]
return setWallPaperAndRestartStuff(file.Name())
}
return setWallPaperAndRestartStuff(filePath)
}
seed := rand.NewSource(time.Now().UnixNano()) seed := rand.NewSource(time.Now().UnixNano())
r := rand.New(seed) r := rand.New(seed)
s := &wallhaven.Search{ s := &wallhaven.Search{
@ -140,7 +126,6 @@ func set(args []string) error {
Ratios: setRatios, Ratios: setRatios,
Page: r.Intn(setPage) + 1, Page: r.Intn(setPage) + 1,
} }
log.Println(args)
if len(args) > 0 { if len(args) > 0 {
s.Query = wallhaven.Q{ s.Query = wallhaven.Q{
Tags: []string{args[0]}, Tags: []string{args[0]},
@ -150,64 +135,41 @@ func set(args []string) error {
if err != nil { if err != nil {
return err return err
} }
result, err := getOrDownload(results, r) resultPath, err := getOrDownload(results, r)
if err != nil { if err != nil {
return err return err
} }
err = setWallPaperAndRestartStuff(result.Path) if setScript != "" {
if err != nil { err = runScript(resultPath, setScript)
return err
}
return nil
}
func getOrDownload(results *wallhaven.SearchResults, r *rand.Rand) (wallhaven.Wallpaper, error) {
if len(results.Data) == 0 {
return wallhaven.Wallpaper{}, fmt.Errorf("no wallpapers found")
}
homedir, _ := os.UserHomeDir()
result := results.Data[r.Intn(len(results.Data))]
if _, err := os.Stat(path.Join(homedir, "Pictures/Wallpapers", path.Base(result.Path))); err != nil {
err = result.Download(path.Join(homedir, "Pictures/Wallpapers"))
if err != nil { if err != nil {
return wallhaven.Wallpaper{}, err return err
} }
} }
return result, nil return nil
} }
func setWallPaperAndRestartStuff(result string) error { func getOrDownload(results *wallhaven.SearchResults, r *rand.Rand) (string, error) {
if len(results.Data) == 0 {
return "", fmt.Errorf("no wallpapers found")
}
homedir, _ := os.UserHomeDir() homedir, _ := os.UserHomeDir()
_, err := exec.Command("wal", "--cols16", "-i", path.Join(homedir, "Pictures/Wallpapers", path.Base(result)), "-n", "-a", "85"). downloadPath := path.Join(homedir, "Pictures/Wallpapers")
Output() if setPath != "" {
if err != nil { downloadPath = setPath
return err
} }
_, err = exec.Command("swww", "img", path.Join(homedir, "/Pictures/Wallpapers", path.Base(result))). result := results.Data[r.Intn(len(results.Data))]
Output() fullPath := path.Join(downloadPath, path.Base(result.Path))
if err != nil { if _, err := os.Stat(fullPath); err != nil {
return err err = result.Download(path.Join(downloadPath))
if err != nil {
return "", err
}
} }
_, err = exec.Command("restart_dunst"). return fullPath, nil
Output() }
if err != nil {
return err func runScript(imgPath, script string) error {
} _, err := exec.Command(script, imgPath).Output()
_, err = exec.Command("pywalfox", "update").
Output()
if err != nil {
return err
}
source, err := os.Open(path.Join(homedir, ".cache/wal/discord-wal.theme.css"))
if err != nil {
return err
}
defer source.Close()
destination, err := os.Create(path.Join(homedir, ".config/Vencord/themes/discord-wal.theme.css"))
if err != nil {
return err
}
_, err = io.Copy(destination, source)
if err != nil { if err != nil {
return err return err
} }