fixed .gitignore

This commit is contained in:
Daniel Sommer 2023-11-16 15:29:55 +01:00
parent 543e9ad755
commit 75b4325df3
3 changed files with 129 additions and 1 deletions

2
.gitignore vendored
View file

@ -1,2 +1,2 @@
slideshow ./slideshow
__debug_bin* __debug_bin*

View file

@ -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
}

View file

@ -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
}