From 75b4325df3047f7b77b73853e4898ebfae85f059 Mon Sep 17 00:00:00 2001 From: velvettear Date: Thu, 16 Nov 2023 15:29:55 +0100 Subject: [PATCH] fixed .gitignore --- .gitignore | 2 +- internal/slideshow/scale.go | 47 +++++++++++++++++++ internal/slideshow/slideshow.go | 81 +++++++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 internal/slideshow/scale.go create mode 100644 internal/slideshow/slideshow.go diff --git a/.gitignore b/.gitignore index a796a8b..2834c10 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -slideshow +./slideshow __debug_bin* \ No newline at end of file diff --git a/internal/slideshow/scale.go b/internal/slideshow/scale.go new file mode 100644 index 0000000..fac223d --- /dev/null +++ b/internal/slideshow/scale.go @@ -0,0 +1,47 @@ +package slideshow + +import ( + "errors" + "io" + "os" + "os/exec" + "path/filepath" + "strconv" + "strings" + "time" + + "git.velvettear.de/velvettear/loggo" + "git.velvettear.de/velvettear/slideshow/internal/config" +) + +// scale an image +func scale(image string) (string, error) { + timestamp := time.Now().UnixMilli() + scaledImage := filepath.Join(os.TempDir(), strconv.FormatInt(timestamp, 10)+"_"+filepath.Base(image)) + arguments := []string{image, "-resize", config.Resolution + "^", "-gravity", "center", "-extent", config.Resolution, scaledImage} + cmd := exec.Command("convert", arguments...) + stdout, stdoutError := cmd.StdoutPipe() + stderr, stderrError := cmd.StderrPipe() + cmd.Start() + if stdoutError != nil { + return scaledImage, stdoutError + } + if stderrError != nil { + return scaledImage, stdoutError + } + _, stdoutError = io.ReadAll(stdout) + if stdoutError != nil { + return scaledImage, stdoutError + } + errorBytes, stderrError := io.ReadAll(stderr) + if stderrError != nil { + return scaledImage, stdoutError + } + cmd.Wait() + errorMessage := strings.TrimSpace(string(errorBytes)) + if len(errorMessage) > 0 { + return scaledImage, errors.New(errorMessage) + } + loggo.DebugTimed("successfully scaled image", timestamp, "image: "+image, "scaled image: "+scaledImage, "resolution: "+config.Resolution) + return scaledImage, nil +} diff --git a/internal/slideshow/slideshow.go b/internal/slideshow/slideshow.go new file mode 100644 index 0000000..70feecf --- /dev/null +++ b/internal/slideshow/slideshow.go @@ -0,0 +1,81 @@ +package slideshow + +import ( + "errors" + "io" + "os" + "os/exec" + "strconv" + "strings" + "time" + + "git.velvettear.de/velvettear/loggo" + "git.velvettear.de/velvettear/slideshow/internal/config" + "git.velvettear.de/velvettear/slideshow/internal/watcher" +) + +// internal counter +var counter int64 + +// start the slideshow +func Start() { + loggo.Info("starting the image slideshow...", "interval: "+strconv.FormatFloat(config.Interval.Seconds(), 'f', 0, 64)+" seconds") + for { + var sleepTime time.Duration + if counter > 0 { + sleepTime = time.Until(time.Now().Add(config.Interval)) + } + var scaledImage string + image := watcher.GetRandomImage() + if len(config.Resolution) > 0 { + tmp, error := scale(image) + if error != nil { + loggo.Fatal("encountered an error scaling an image", "image: "+image, error.Error()) + } + scaledImage = tmp + } + if sleepTime > 0 { + time.Sleep(sleepTime) + } + error := setBackground(image) + if error != nil { + loggo.Error("encountered an error setting the background image", "image: "+image, error.Error()) + } + counter++ + loggo.Info("set new background image", "image: "+image) + if len(scaledImage) > 0 { + error = os.Remove(image) + if error != nil { + loggo.Error("encountered an error deleting a scaled image", "image: "+scaledImage) + } + } + } +} + +// set the background image via 'feh' +func setBackground(image string) error { + cmd := exec.Command("feh", "--no-fehbg", "--bg-fill", image) + stdout, stdoutError := cmd.StdoutPipe() + stderr, stderrError := cmd.StderrPipe() + cmd.Start() + if stdoutError != nil { + return stdoutError + } + if stderrError != nil { + return stderrError + } + _, stdoutError = io.ReadAll(stdout) + if stdoutError != nil { + return stdoutError + } + errorBytes, stderrError := io.ReadAll(stderr) + if stderrError != nil { + return stderrError + } + cmd.Wait() + error := strings.TrimSpace(string(errorBytes)) + if len(error) > 0 { + return errors.New(error) + } + return nil +}