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;
|
||||
|
|
|
@ -1,12 +1,16 @@
|
|||
#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) {
|
||||
|
||||
try {
|
||||
|
||||
|
||||
CliArguments::CliArguments arguments{
|
||||
argc, argv,
|
||||
{
|
||||
|
@ -81,4 +85,8 @@ int main(int argc, char** argv, char** envp) {
|
|||
|
||||
swayBgProcess.kill();
|
||||
return 0;
|
||||
} catch (std::exception& e) {
|
||||
std::cerr << e.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue