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 package main
import ( import (
// "fmt"
"math/rand" "math/rand"
"os" "os"
"regexp"
) )
func getRandomFile(dir string) (string, error) { func getRandomFile(dir string) (path string, filename string, error error) {
files, err := os.ReadDir(dir) files, err := os.ReadDir(dir)
if err != nil { if err != nil {
return "", err return "", "", err
} }
randomIndex := rand.Intn(len(files))
randomImg := files[randomIndex] // for _, file := range files { fmt.Println(file.Name(), file.IsDir())
return dir + "/" + randomImg.Name(), nil // }
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 { func hyprpaperPreload(path string) error {
var err error out, err := exec.Command("hyprctl", "hyprpaper", "preload", path).Output()
var out []byte
out, err = exec.Command("hyprctl", "hyprpaper", "preload", path).Output()
if err != nil { if err != nil {
return err return err
} }
str := string(out) return checkHyprctlError(out)
if str != "ok\n" {
return errors.New(str)
}
return nil
} }
func hyprpaperWallpaper(path string) error { func hyprpaperWallpaper(path string) error {
var err error out, err := exec.Command("hyprctl", "hyprpaper", "wallpaper", ","+path).Output()
var out []byte
out, err = exec.Command("hyprctl", "hyprpaper", "wallpaper", ","+path).Output()
if err != nil { if err != nil {
return err 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) str := string(out)
if str != "ok\n" { if str != "ok\n" {
return errors.New(str) return errors.New(str)

View File

@ -10,15 +10,15 @@ import (
"image/png" "image/png"
) )
type Config struct { type ImageFilters struct {
Colorize [3]float32 Colorize [3]float32
Contrast int Contrast int
Gamma float64 Gamma float64
Root string
} }
func loadImage(filename string) image.Image { func loadImage(filename string) image.Image {
f, err := os.Open(filename) f, err := os.Open(filename)
log.Println("filename: ", filename)
if err != nil { if err != nil {
log.Fatalf("os.Open failed: %v", err) 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 package main
import ( import (
"encoding/json"
"fmt" "fmt"
"log" "log"
"os" "os"
@ -10,7 +9,9 @@ import (
func main() { func main() {
var err error var err error
config := loadConfig() var config Config
config.load()
fmt.Printf("%+v\n", config)
//get working directory //get working directory
wd, err := os.Getwd() wd, err := os.Getwd()
@ -18,45 +19,35 @@ func main() {
log.Fatal("couldn't find working directory", err) log.Fatal("couldn't find working directory", err)
} }
curr := wd + "/curr.png" // // apply curr.png if already preloaded
// apply curr.png if already preloaded // err = hyprpaperWallpaper(curr)
err = hyprpaperWallpaper(curr) // if err != nil {
if err != nil { // //preload curr.png if not
//preload curr.png if not // err = hyprpaperPreload(curr)
err = hyprpaperPreload(curr) // if err != nil {
if err != nil { // fmt.Println("could not preload (this is fine)", err)
fmt.Println("could not preload (this is fine)", err) // }
} // }
}
dir := config.Root + "/chsck" dir := config.Root + "/chsck"
randomImg, _ := getRandomFile(dir) randomImg, filename, _ := getRandomFile(dir)
img := loadImage(randomImg) img := loadImage(randomImg)
dst := processImage(img, config) dst := processImage(img, config)
curr := wd + "/images/" + filename
log.Println("curr: ", curr)
saveImage(curr, dst)
saveImage("curr.png", dst) //BUG: hyprpaper won't preload if it recognises the path. SO we need to use images' original names
err = hyprpaperPreload(curr)
path := wd + "/curr.png"
err = hyprpaperPreload(path)
if err != nil { if err != nil {
log.Fatal("preload failed!", err) log.Fatal("preload failed!", err)
} }
err = hyprpaperWallpaper(path) err = hyprpaperWallpaper(curr)
if err != nil { if err != nil {
log.Fatal("set wallpaper failed!", err) log.Fatal("set wallpaper failed!", err)
} }
} err = hyprpaperUnloadAll()
func loadConfig() Config {
configRaw, err := os.ReadFile("./config.json")
if err != nil { 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
} }