code cleanup

This commit is contained in:
Daniel Sommer 2023-11-24 16:52:11 +01:00
parent ff68336c1b
commit acc7d1513b
5 changed files with 53 additions and 73 deletions

2
.vscode/launch.json vendored
View file

@ -10,7 +10,7 @@
"env":{ "env":{
"SLIDESHOW_INTERVAL": "10", "SLIDESHOW_INTERVAL": "10",
"SLIDESHOW_DIRECTORY": "/home/velvettear/images", "SLIDESHOW_DIRECTORY": "/home/velvettear/images",
"SLIDESHOW_SCANINTERVAL": "300", "SLIDESHOW_SCANINTERVAL": "10",
"SLIDESHOW_RESOLUTION": "600x1024", "SLIDESHOW_RESOLUTION": "600x1024",
"SLIDESHOW_LOGLEVEL": "debug", "SLIDESHOW_LOGLEVEL": "debug",
"SLIDESHOW_PALETTE": "/tmp/.slideshow.palette", "SLIDESHOW_PALETTE": "/tmp/.slideshow.palette",

View file

@ -62,23 +62,6 @@ func getColorPaletteRaw(data []byte) ([]color.Color, error) {
return colors, error return colors, error
} }
// extract the given amount of dominant colors of an image
func getColorPalette(image string) ([]color.Color, error) {
var colors []color.Color
if !config.IsPaletteSet() {
return colors, nil
}
timestamp := time.Now().UnixMilli()
amount := config.PaletteColors
colors, error := color_thief.GetPaletteFromFile(image, amount, config.PaletteAlgorithm)
if error != nil {
loggo.ErrorTimed("encountered an error generating the color palette from image", timestamp, "image: "+image, "colors: "+strconv.Itoa(amount))
} else {
loggo.DebugTimed("generated color palette from image", timestamp, "image: "+image, "colors: "+strconv.Itoa(amount))
}
return colors, error
}
// parse rgb color values to hex // parse rgb color values to hex
func rgbToHex(color color.Color) string { func rgbToHex(color color.Color) string {
r, g, b, _ := color.RGBA() r, g, b, _ := color.RGBA()

View file

@ -4,6 +4,7 @@ import (
"errors" "errors"
"image/color" "image/color"
"io" "io"
"os"
"os/exec" "os/exec"
"strconv" "strconv"
"strings" "strings"
@ -22,9 +23,16 @@ func Start() {
loggo.Info("starting the image slideshow...", "interval: "+strconv.FormatFloat(config.Interval.Seconds(), 'f', 0, 64)+" seconds") loggo.Info("starting the image slideshow...", "interval: "+strconv.FormatFloat(config.Interval.Seconds(), 'f', 0, 64)+" seconds")
var sleepTime time.Duration var sleepTime time.Duration
var loopTime time.Duration var loopTime time.Duration
scaleImages := config.IsResolutionSet()
for { for {
loopTimestamp := time.Now() loopTimestamp := time.Now()
for !scanner.HasFoundImages() {
sleepTime := time.Until(scanner.GetNextScan().Add(scanner.GetLastScanDuration()))
if sleepTime <= 0 {
continue
}
loggo.Info("there have no images been found yet, waiting for " + strconv.FormatInt(sleepTime.Milliseconds(), 10) + "ms...")
time.Sleep(sleepTime)
}
var image string var image string
var palette []color.Color var palette []color.Color
var data []byte var data []byte
@ -34,70 +42,39 @@ func Start() {
break break
} }
} }
if scaleImages { if config.IsResolutionSet() {
tmp, error := scale(image) tmp, error := scale(image)
if error != nil { if error != nil {
loggo.Error("encountered an error scaling an image", "image: "+image, error.Error()) loggo.Error("encountered an error scaling an image", "image: "+image, error.Error())
continue continue
} }
data = tmp data = tmp
palette, _ = getColorPaletteRaw(data)
} else { } else {
palette, _ = getColorPalette(image) tmp, error := os.ReadFile(image)
if error != nil {
loggo.Error("encountered an erro reading an image", "image: "+image, error.Error())
continue
}
data = tmp
} }
palette, _ = getColorPaletteRaw(data)
loopTime = time.Since(loopTimestamp) loopTime = time.Since(loopTimestamp)
if sleepTime > 0 { if sleepTime > 0 {
loggo.Debug("sleeping for " + strconv.FormatInt(sleepTime.Milliseconds(), 10) + "ms before next image will be displayed...") loggo.Debug("sleeping for " + strconv.FormatInt(sleepTime.Milliseconds(), 10) + "ms before next image will be displayed...")
time.Sleep(sleepTime) time.Sleep(sleepTime)
} }
go exportPalette(palette) go exportPalette(palette)
if scaleImages { error := setBackground(data)
error := setBackgroundPiped(data) if error != nil {
if error != nil { loggo.Error("encountered an error setting the background image", error.Error())
loggo.Error("encountered an error setting the background via pipe to feh's stdin", error.Error())
}
loggo.Info("set new scaled background image", "image: "+image, "resolution: "+config.Resolution)
} else {
error := setBackgroundImage(image)
if error != nil {
loggo.Error("encountered an error setting the background image", "image: "+image, error.Error())
}
loggo.Info("set new background image", "image: "+image)
} }
loggo.Info("set new background image", "image: "+image)
sleepTime = config.Interval - loopTime sleepTime = config.Interval - loopTime
} }
} }
// set the background image via 'feh'
func setBackgroundImage(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
}
// pipe data to 'feh' via stdin to set the background image // pipe data to 'feh' via stdin to set the background image
func setBackgroundPiped(data []byte) error { func setBackground(data []byte) error {
cmd := exec.Command("feh", "--no-fehbg", "--bg-fill", "-") cmd := exec.Command("feh", "--no-fehbg", "--bg-fill", "-")
stdin, stdinError := cmd.StdinPipe() stdin, stdinError := cmd.StdinPipe()
stdout, stdoutError := cmd.StdoutPipe() stdout, stdoutError := cmd.StdoutPipe()

View file

@ -1,7 +1,6 @@
package main package main
import ( import (
"sync"
"time" "time"
"git.velvettear.de/velvettear/loggo" "git.velvettear.de/velvettear/loggo"
@ -13,11 +12,8 @@ import (
func main() { func main() {
timestamp := time.Now().UnixMilli() timestamp := time.Now().UnixMilli()
loggo.Info("slideshow is starting now...") loggo.Info("slideshow is starting now...")
var waitgroup sync.WaitGroup
waitgroup.Add(1)
config.Initialize() config.Initialize()
scanner.Initialize() scanner.Initialize()
slideshow.Start() slideshow.Start()
waitgroup.Wait()
loggo.InfoTimed("slideshow is shutting down now!", timestamp) loggo.InfoTimed("slideshow is shutting down now!", timestamp)
} }

View file

@ -18,6 +18,12 @@ var images []string
// temporary slice of images // temporary slice of images
var tmpImages []string var tmpImages []string
// time of next scan
var nextScan time.Time
// duration of the last scan
var lastScanDuration time.Duration
// scan the specified directories for images // scan the specified directories for images
func Initialize() { func Initialize() {
directory := config.Directory directory := config.Directory
@ -29,21 +35,39 @@ func GetRandomImage() string {
return images[rand.Intn(len(images))] return images[rand.Intn(len(images))]
} }
// check if images has been found
func HasFoundImages() bool {
return len(images) > 0
}
// get the time of the next scan
func GetNextScan() time.Time {
return nextScan
}
// get the duration of the last scan
func GetLastScanDuration() time.Duration {
return lastScanDuration
}
// scan the specified directory // scan the specified directory
func scan(directory string) { func scan(directory string) {
timestamp := time.Now().UnixMilli() timestamp := time.Now()
loggo.Info("scanning directory for images and subdirectories...", "interval: "+strconv.FormatFloat(config.ScanInterval.Seconds(), 'f', 0, 64)+" seconds", "directory: "+directory) loggo.Info("scanning directory for images and subdirectories...", "interval: "+strconv.FormatFloat(config.ScanInterval.Seconds(), 'f', 0, 64)+" seconds", "directory: "+directory)
filepath.WalkDir(directory, checkDirectory) filepath.WalkDir(directory, checkDirectory)
images = tmpImages images = tmpImages
tmpImages = nil tmpImages = nil
loggo.InfoTimed("found "+strconv.Itoa(len(images))+" image(s)", timestamp) loggo.InfoTimed("found "+strconv.Itoa(len(images))+" image(s)", timestamp.UnixMilli())
go scheduleRescan(directory) lastScanDuration = time.Since(timestamp)
nextScan = time.Now().Add(config.ScanInterval)
go scheduleRescan(directory, nextScan)
} }
// sleep the specified interval and then trigger a rescan of the specified directory // sleep the specified interval and then trigger a rescan of the specified directory
func scheduleRescan(directory string) { func scheduleRescan(directory string, timestamp time.Time) {
loggo.Debug("sleeping for " + strconv.FormatInt(config.ScanInterval.Milliseconds(), 10) + "ms before next scan...") sleepTime := time.Until(timestamp)
time.Sleep(config.ScanInterval) loggo.Debug("sleeping for " + strconv.FormatInt(sleepTime.Milliseconds(), 10) + "ms before next scan...")
time.Sleep(sleepTime)
scan(directory) scan(directory)
} }