Handle empty directory and invalid image extension

This commit is contained in:
Vyn 2024-06-04 12:56:46 +02:00
parent fce4fad1fb
commit 90ac4001e7
3 changed files with 104 additions and 73 deletions

View file

@ -2,14 +2,20 @@
#include <algorithm> #include <algorithm>
#include <filesystem> #include <filesystem>
#include <iostream> #include <iostream>
#include <ostream>
#include <stdexcept>
Wallpapers::Wallpapers(const std::string& directoryPath) : directoryPath(directoryPath) { Wallpapers::Wallpapers(const std::string& directoryPath) : directoryPath(directoryPath) {
for (const auto& entry : std::filesystem::directory_iterator(directoryPath)) { for (const auto& entry : std::filesystem::directory_iterator(directoryPath)) {
if (!isValidPath(entry.path())) {
continue;
}
wallpapersPath.push_back(entry.path()); wallpapersPath.push_back(entry.path());
} }
for (const std::string& wallpaperPath : wallpapersPath) { if (wallpapersPath.size() == 0) {
std::cout << wallpaperPath << std::endl; throw std::runtime_error("Directory '" + directoryPath + "' doesn't contain any valid wallapers\n\
Allowed extension are .png, .jpg, .jpeg");
} }
e1 = std::default_random_engine(r()); e1 = std::default_random_engine(r());
uniformDist = std::uniform_int_distribution<int>(0, wallpapersPath.size()); uniformDist = std::uniform_int_distribution<int>(0, wallpapersPath.size());
@ -22,7 +28,21 @@ void Wallpapers::shuffle() {
} }
const std::string& Wallpapers::next() { const std::string& Wallpapers::next() {
if (wallpapersPathQueue.size() == 0) {
throw std::runtime_error("No valid wallpapers found when calling Wallpapers::next method");
}
const std::string& wallpaperPath = wallpapersPathQueue[currentWallpaperIndex]; const std::string& wallpaperPath = wallpapersPathQueue[currentWallpaperIndex];
currentWallpaperIndex = (currentWallpaperIndex + 1) % wallpapersPathQueue.size(); currentWallpaperIndex = (currentWallpaperIndex + 1) % wallpapersPathQueue.size();
return wallpaperPath; return wallpaperPath;
} }
bool Wallpapers::isValidPath(const std::string& path) const {
static std::array validExtensions {"jpg", "jpeg", "png"};
for (const auto& extension : validExtensions) {
if (path.ends_with(extension)) {
return true;
}
}
return false;
}

View file

@ -1,4 +1,5 @@
#pragma once #pragma once
#include <array>
#include <random> #include <random>
#include <string> #include <string>
#include <vector> #include <vector>
@ -14,6 +15,8 @@ class Wallpapers {
private: private:
bool isValidPath(const std::string& path) const;
std::string directoryPath; std::string directoryPath;
std::vector<std::string> wallpapersPath; std::vector<std::string> wallpapersPath;
std::vector<std::string> wallpapersPathQueue; std::vector<std::string> wallpapersPathQueue;

View file

@ -1,84 +1,92 @@
#include "CliArguments.h" #include "CliArguments.h"
#include "Wallpapers.h" #include "Wallpapers.h"
#include "SwaybgProcess.h" #include "SwaybgProcess.h"
#include <exception>
#include <iostream> #include <iostream>
#include <ostream> #include <ostream>
#include <string> #include <string>
int main(int argc, char** argv, char** envp) { int main(int argc, char** argv, char** envp) {
CliArguments::CliArguments arguments{ try {
argc, argv,
{
{"path", {
.description = "The path to the directory containing the wallpapers",
.type = CliArguments::String,
.order = 0,
.direct = true
}},
{"output", {
.description = "The outputs as shown with the command `swaymsg -t get_outputs`",
.type = CliArguments::String,
.order = 1,
.direct = true
}},
{"interval", {
.aliases = {"i"},
.description = "Specify the interval between wallpapers in seconds",
.type = CliArguments::Int,
}},
{"never-random", {
.description = "if set, wallpapers order is based on the names of the files",
.type = CliArguments::Bool,
}},
{"help", {
.aliases = {"h"},
.description = "Show help",
.type = CliArguments::Bool,
}},
}
};
if (!arguments.isValid()) { CliArguments::CliArguments arguments{
std::cout << arguments.error() << std::endl; argc, argv,
arguments.printHelp(); {
{"path", {
.description = "The path to the directory containing the wallpapers",
.type = CliArguments::String,
.order = 0,
.direct = true
}},
{"output", {
.description = "The outputs as shown with the command `swaymsg -t get_outputs`",
.type = CliArguments::String,
.order = 1,
.direct = true
}},
{"interval", {
.aliases = {"i"},
.description = "Specify the interval between wallpapers in seconds",
.type = CliArguments::Int,
}},
{"never-random", {
.description = "if set, wallpapers order is based on the names of the files",
.type = CliArguments::Bool,
}},
{"help", {
.aliases = {"h"},
.description = "Show help",
.type = CliArguments::Bool,
}},
}
};
if (!arguments.isValid()) {
std::cout << arguments.error() << std::endl;
arguments.printHelp();
return 1;
}
if (arguments.exists("help")) {
arguments.printHelp();
return 0;
}
const std::string path = arguments.getString("path");
const std::string output = arguments.getString("output");
Wallpapers wallpapers(path);
if (!arguments.exists("never-random")) {
wallpapers.shuffle();
}
SwaybgProcess swayBgProcess(wallpapers.next(), output);
swayBgProcess.exec();
if (arguments.exists("interval")) {
int interval = arguments.getInt("interval");
while (true) {
sleep(interval);
SwaybgProcess newSwayBgProcess(wallpapers.next(), output);
newSwayBgProcess.exec();
sleep(1);
swayBgProcess.kill();
swayBgProcess = newSwayBgProcess;
}
} else {
char ch;
while (ch != 'q') {
ch = std::cin.get();
}
}
swayBgProcess.kill();
return 0;
} catch (std::exception& e) {
std::cerr << e.what() << std::endl;
return 1; return 1;
} }
if (arguments.exists("help")) {
arguments.printHelp();
return 0;
}
const std::string path = arguments.getString("path");
const std::string output = arguments.getString("output");
Wallpapers wallpapers(path);
if (!arguments.exists("never-random")) {
wallpapers.shuffle();
}
SwaybgProcess swayBgProcess(wallpapers.next(), output);
swayBgProcess.exec();
if (arguments.exists("interval")) {
int interval = arguments.getInt("interval");
while (true) {
sleep(interval);
SwaybgProcess newSwayBgProcess(wallpapers.next(), output);
newSwayBgProcess.exec();
sleep(1);
swayBgProcess.kill();
swayBgProcess = newSwayBgProcess;
}
} else {
char ch;
while (ch != 'q') {
ch = std::cin.get();
}
}
swayBgProcess.kill();
return 0;
} }