Handle empty directory and invalid image extension
This commit is contained in:
parent
fce4fad1fb
commit
90ac4001e7
3 changed files with 104 additions and 73 deletions
|
@ -2,14 +2,20 @@
|
|||
#include <algorithm>
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
#include <stdexcept>
|
||||
|
||||
Wallpapers::Wallpapers(const std::string& directoryPath) : directoryPath(directoryPath) {
|
||||
|
||||
for (const auto& entry : std::filesystem::directory_iterator(directoryPath)) {
|
||||
if (!isValidPath(entry.path())) {
|
||||
continue;
|
||||
}
|
||||
wallpapersPath.push_back(entry.path());
|
||||
}
|
||||
for (const std::string& wallpaperPath : wallpapersPath) {
|
||||
std::cout << wallpaperPath << std::endl;
|
||||
if (wallpapersPath.size() == 0) {
|
||||
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());
|
||||
uniformDist = std::uniform_int_distribution<int>(0, wallpapersPath.size());
|
||||
|
@ -22,7 +28,21 @@ void Wallpapers::shuffle() {
|
|||
}
|
||||
|
||||
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];
|
||||
currentWallpaperIndex = (currentWallpaperIndex + 1) % wallpapersPathQueue.size();
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#pragma once
|
||||
#include <array>
|
||||
#include <random>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
@ -14,6 +15,8 @@ class Wallpapers {
|
|||
|
||||
private:
|
||||
|
||||
bool isValidPath(const std::string& path) const;
|
||||
|
||||
std::string directoryPath;
|
||||
std::vector<std::string> wallpapersPath;
|
||||
std::vector<std::string> wallpapersPathQueue;
|
||||
|
|
150
src/main.cpp
150
src/main.cpp
|
@ -1,84 +1,92 @@
|
|||
#include "CliArguments.h"
|
||||
#include "Wallpapers.h"
|
||||
#include "SwaybgProcess.h"
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
|
||||
int main(int argc, char** argv, char** envp) {
|
||||
|
||||
CliArguments::CliArguments arguments{
|
||||
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,
|
||||
}},
|
||||
}
|
||||
};
|
||||
try {
|
||||
|
||||
|
||||
if (!arguments.isValid()) {
|
||||
std::cout << arguments.error() << std::endl;
|
||||
arguments.printHelp();
|
||||
CliArguments::CliArguments arguments{
|
||||
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()) {
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue