This commit is contained in:
andrzej 2024-10-27 17:21:28 +01:00
parent 58aba48e3c
commit 9faa121e1f
7 changed files with 86 additions and 48 deletions

24
config.go Normal file
View File

@ -0,0 +1,24 @@
package main
import (
"encoding/json"
"log"
"os"
)
type Config struct {
ImageFilters
Root string
}
func (config *Config) load() {
configRaw, err := os.ReadFile("./config.json")
if err != nil {
log.Fatal("Couldn't open config file!", err)
}
err = json.Unmarshal(configRaw, &config)
if err != nil {
log.Fatal("Couldn't unmarshal config!", err)
}
}

BIN
curr.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.5 MiB

After

Width:  |  Height:  |  Size: 7.5 MiB

View File

@ -1,16 +1,35 @@
package main
import (
// "fmt"
"math/rand"
"os"
"regexp"
)
func getRandomFile(dir string) (string, error) {
func getRandomFile(dir string) (path string, filename string, error error) {
files, err := os.ReadDir(dir)
if err != nil {
return "", err
return "", "", err
}
randomIndex := rand.Intn(len(files))
randomImg := files[randomIndex]
return dir + "/" + randomImg.Name(), nil
// for _, file := range files { fmt.Println(file.Name(), file.IsDir())
// }
var randomIndex int
var randomImg os.DirEntry
for {
randomIndex = rand.Intn(len(files))
randomImg = files[randomIndex]
//re-roll if you get a directory
if files[randomIndex].IsDir() == false {
break
}
//re-roll if not a recognised image type
isImage, _ := regexp.Match(".jpg$|.jpeg$|.png$|.bmp$|.webp$", []byte(randomImg.Name()))
if isImage {
break
}
}
return dir + "/" + randomImg.Name(), randomImg.Name(), nil
}

View File

@ -6,26 +6,30 @@ import (
)
func hyprpaperPreload(path string) error {
var err error
var out []byte
out, err = exec.Command("hyprctl", "hyprpaper", "preload", path).Output()
out, err := exec.Command("hyprctl", "hyprpaper", "preload", path).Output()
if err != nil {
return err
}
str := string(out)
if str != "ok\n" {
return errors.New(str)
}
return nil
return checkHyprctlError(out)
}
func hyprpaperWallpaper(path string) error {
var err error
var out []byte
out, err = exec.Command("hyprctl", "hyprpaper", "wallpaper", ","+path).Output()
out, err := exec.Command("hyprctl", "hyprpaper", "wallpaper", ","+path).Output()
if err != nil {
return err
}
return checkHyprctlError(out)
}
func hyprpaperUnloadAll() error {
out, err := exec.Command("hyprctl", "hyprpaper", "unload", "all").Output()
if err != nil {
return err
}
return checkHyprctlError(out)
}
func checkHyprctlError(out []byte) error {
str := string(out)
if str != "ok\n" {
return errors.New(str)

View File

@ -10,15 +10,15 @@ import (
"image/png"
)
type Config struct {
type ImageFilters struct {
Colorize [3]float32
Contrast int
Gamma float64
Root string
}
func loadImage(filename string) image.Image {
f, err := os.Open(filename)
log.Println("filename: ", filename)
if err != nil {
log.Fatalf("os.Open failed: %v", err)
}

BIN
in.jpg

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.2 MiB

51
main.go
View File

@ -1,7 +1,6 @@
package main
import (
"encoding/json"
"fmt"
"log"
"os"
@ -10,7 +9,9 @@ import (
func main() {
var err error
config := loadConfig()
var config Config
config.load()
fmt.Printf("%+v\n", config)
//get working directory
wd, err := os.Getwd()
@ -18,45 +19,35 @@ func main() {
log.Fatal("couldn't find working directory", err)
}
curr := wd + "/curr.png"
// apply curr.png if already preloaded
err = hyprpaperWallpaper(curr)
if err != nil {
//preload curr.png if not
err = hyprpaperPreload(curr)
if err != nil {
fmt.Println("could not preload (this is fine)", err)
}
}
// // apply curr.png if already preloaded
// err = hyprpaperWallpaper(curr)
// if err != nil {
// //preload curr.png if not
// err = hyprpaperPreload(curr)
// if err != nil {
// fmt.Println("could not preload (this is fine)", err)
// }
// }
dir := config.Root + "/chsck"
randomImg, _ := getRandomFile(dir)
randomImg, filename, _ := getRandomFile(dir)
img := loadImage(randomImg)
dst := processImage(img, config)
curr := wd + "/images/" + filename
log.Println("curr: ", curr)
saveImage(curr, dst)
saveImage("curr.png", dst)
path := wd + "/curr.png"
err = hyprpaperPreload(path)
//BUG: hyprpaper won't preload if it recognises the path. SO we need to use images' original names
err = hyprpaperPreload(curr)
if err != nil {
log.Fatal("preload failed!", err)
}
err = hyprpaperWallpaper(path)
err = hyprpaperWallpaper(curr)
if err != nil {
log.Fatal("set wallpaper failed!", err)
}
}
func loadConfig() Config {
configRaw, err := os.ReadFile("./config.json")
err = hyprpaperUnloadAll()
if err != nil {
log.Fatal("Couldn't open config file!", err)
log.Fatal("unload all failed!", err)
}
var config Config
err = json.Unmarshal(configRaw, &config)
if err != nil {
log.Fatal("Couldn't unmarshal config!", err)
}
return config
}