code cleanup
This commit is contained in:
parent
ff68336c1b
commit
acc7d1513b
5 changed files with 53 additions and 73 deletions
2
.vscode/launch.json
vendored
2
.vscode/launch.json
vendored
|
@ -10,7 +10,7 @@
|
|||
"env":{
|
||||
"SLIDESHOW_INTERVAL": "10",
|
||||
"SLIDESHOW_DIRECTORY": "/home/velvettear/images",
|
||||
"SLIDESHOW_SCANINTERVAL": "300",
|
||||
"SLIDESHOW_SCANINTERVAL": "10",
|
||||
"SLIDESHOW_RESOLUTION": "600x1024",
|
||||
"SLIDESHOW_LOGLEVEL": "debug",
|
||||
"SLIDESHOW_PALETTE": "/tmp/.slideshow.palette",
|
||||
|
|
|
@ -62,23 +62,6 @@ func getColorPaletteRaw(data []byte) ([]color.Color, 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
|
||||
func rgbToHex(color color.Color) string {
|
||||
r, g, b, _ := color.RGBA()
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"errors"
|
||||
"image/color"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
@ -22,9 +23,16 @@ func Start() {
|
|||
loggo.Info("starting the image slideshow...", "interval: "+strconv.FormatFloat(config.Interval.Seconds(), 'f', 0, 64)+" seconds")
|
||||
var sleepTime time.Duration
|
||||
var loopTime time.Duration
|
||||
scaleImages := config.IsResolutionSet()
|
||||
for {
|
||||
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 palette []color.Color
|
||||
var data []byte
|
||||
|
@ -34,70 +42,39 @@ func Start() {
|
|||
break
|
||||
}
|
||||
}
|
||||
if scaleImages {
|
||||
if config.IsResolutionSet() {
|
||||
tmp, error := scale(image)
|
||||
if error != nil {
|
||||
loggo.Error("encountered an error scaling an image", "image: "+image, error.Error())
|
||||
continue
|
||||
}
|
||||
data = tmp
|
||||
palette, _ = getColorPaletteRaw(data)
|
||||
} 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)
|
||||
if sleepTime > 0 {
|
||||
loggo.Debug("sleeping for " + strconv.FormatInt(sleepTime.Milliseconds(), 10) + "ms before next image will be displayed...")
|
||||
time.Sleep(sleepTime)
|
||||
}
|
||||
go exportPalette(palette)
|
||||
if scaleImages {
|
||||
error := setBackgroundPiped(data)
|
||||
error := setBackground(data)
|
||||
if error != nil {
|
||||
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.Error("encountered an error setting the background image", error.Error())
|
||||
}
|
||||
loggo.Info("set new background image", "image: "+image)
|
||||
}
|
||||
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
|
||||
func setBackgroundPiped(data []byte) error {
|
||||
func setBackground(data []byte) error {
|
||||
cmd := exec.Command("feh", "--no-fehbg", "--bg-fill", "-")
|
||||
stdin, stdinError := cmd.StdinPipe()
|
||||
stdout, stdoutError := cmd.StdoutPipe()
|
||||
|
|
4
main.go
4
main.go
|
@ -1,7 +1,6 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.velvettear.de/velvettear/loggo"
|
||||
|
@ -13,11 +12,8 @@ import (
|
|||
func main() {
|
||||
timestamp := time.Now().UnixMilli()
|
||||
loggo.Info("slideshow is starting now...")
|
||||
var waitgroup sync.WaitGroup
|
||||
waitgroup.Add(1)
|
||||
config.Initialize()
|
||||
scanner.Initialize()
|
||||
slideshow.Start()
|
||||
waitgroup.Wait()
|
||||
loggo.InfoTimed("slideshow is shutting down now!", timestamp)
|
||||
}
|
||||
|
|
|
@ -18,6 +18,12 @@ var images []string
|
|||
// temporary slice of images
|
||||
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
|
||||
func Initialize() {
|
||||
directory := config.Directory
|
||||
|
@ -29,21 +35,39 @@ func GetRandomImage() string {
|
|||
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
|
||||
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)
|
||||
filepath.WalkDir(directory, checkDirectory)
|
||||
images = tmpImages
|
||||
tmpImages = nil
|
||||
loggo.InfoTimed("found "+strconv.Itoa(len(images))+" image(s)", timestamp)
|
||||
go scheduleRescan(directory)
|
||||
loggo.InfoTimed("found "+strconv.Itoa(len(images))+" image(s)", timestamp.UnixMilli())
|
||||
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
|
||||
func scheduleRescan(directory string) {
|
||||
loggo.Debug("sleeping for " + strconv.FormatInt(config.ScanInterval.Milliseconds(), 10) + "ms before next scan...")
|
||||
time.Sleep(config.ScanInterval)
|
||||
func scheduleRescan(directory string, timestamp time.Time) {
|
||||
sleepTime := time.Until(timestamp)
|
||||
loggo.Debug("sleeping for " + strconv.FormatInt(sleepTime.Milliseconds(), 10) + "ms before next scan...")
|
||||
time.Sleep(sleepTime)
|
||||
scan(directory)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue