diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/filesystem.cpp | 30 | ||||
| -rw-r--r-- | src/filesystem.hpp | 35 | ||||
| -rw-r--r-- | src/main.cpp | 48 |
3 files changed, 100 insertions, 13 deletions
diff --git a/src/filesystem.cpp b/src/filesystem.cpp new file mode 100644 index 0000000..4dfa183 --- /dev/null +++ b/src/filesystem.cpp @@ -0,0 +1,30 @@ +#include "filesystem.hpp" + +#include <boost/iterator/filter_iterator.hpp> + +#include <random> + +namespace wanda +{ + +std::optional<path_list> scan(std::filesystem::path source, bool(filter)(std::filesystem::path const &)) +{ + if (!std::filesystem::is_directory(source)) + { + return std::nullopt; + } + + auto first = boost::make_filter_iterator(filter, std::filesystem::recursive_directory_iterator{source}); + auto last = boost::make_filter_iterator(filter, std::filesystem::recursive_directory_iterator{}); + return path_list{first, last}; +} + +std::filesystem::path random_pick(path_list const &paths) +{ + static auto generator = std::mt19937{std::random_device{}()}; + auto distribution = std::uniform_int_distribution<std::size_t>{0, paths.size() - 1}; + + return paths[distribution(generator)]; +} + +} // namespace wanda
\ No newline at end of file diff --git a/src/filesystem.hpp b/src/filesystem.hpp new file mode 100644 index 0000000..bcc9e5e --- /dev/null +++ b/src/filesystem.hpp @@ -0,0 +1,35 @@ +#ifndef WANDA_FILESYSTEM_HPP +#define WANDA_FILESYSTEM_HPP + +#include <filesystem> +#include <optional> +#include <vector> + +namespace wanda +{ + +/** + * @brief Covenience alias for path lists + */ +using path_list = std::vector<std::filesystem::path>; + +/** + * @brief The default scan filter, allowing only regular files to pass + */ +constexpr inline auto default_filter = [](std::filesystem::path const &path) { + return is_regular_file(path); +}; + +/** + * @brief Scan the given folder for files + */ +std::optional<path_list> scan(std::filesystem::path folder, bool(filter)(std::filesystem::path const &) = default_filter); + +/** + * @brief Pick a random path from the given list + */ +std::filesystem::path random_pick(path_list const &paths); + +} // namespace wanda + +#endif
\ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 38fdf7f..6d1353d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,28 +1,50 @@ +#include "filesystem.hpp" #include "setting.hpp" #include "optional.hpp" #include <iostream> +#include <set> #include <string> -#include <typeinfo> +namespace +{ -int main() +void set_wallpaper(std::filesystem::path wallpaper) { using namespace wanda::literals; using namespace wanda::std_ext; using namespace std::string_literals; - with("org.gnome.desktop.background"_setting, [](auto &setting) { - with(setting["picture-uri"_key], [](auto &value) { - std::visit([](auto &&value) { - using ValueType = std::decay_t<decltype(value)>; - if constexpr (std::is_same_v<ValueType, std::string>) - { - std::cout << value << '\n'; - } - }, - *value); - std::cout << (value = "file:///"s) << '\n'; + with("org.gnome.desktop.background"_setting, [&](auto &setting) { + with(setting["picture-uri"_key], [&](auto &value) { + value = "file://" + wallpaper.native(); }) || [] { std::cerr << "No such key!\n"; }; }) || [] { std::cerr << "No such setting!\n"; }; } + +constexpr auto image_filter = [](auto const &path) { + static auto const extensions = std::set<std::filesystem::path>{ + std::filesystem::path{".jpg"}, + std::filesystem::path{".png"}, + }; + + if (!std::filesystem::is_regular_file(path)) + { + return false; + } + + return extensions.find(path.extension()) != extensions.cend(); +}; + +} // namespace + +int main() +{ + using namespace wanda::std_ext; + + with(wanda::scan({"/usr/share/backgrounds"}, image_filter), [](auto const &list) { + auto wallpaper = wanda::random_pick(list); + std::cout << "changing wallpaper to " << wallpaper << '\n'; + set_wallpaper(wallpaper); + }) || [] { std::cerr << "Directory does not exist\n"; }; +} |
