changeset 23:2a32be5d151b

background: use less classes
author David Demelier <markand@malikania.fr>
date Thu, 26 Apr 2018 17:29:22 +0200
parents cc87d1ed14a5
children 65dabffc3445
files dmenu-background/main.cpp
diffstat 1 files changed, 47 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/dmenu-background/main.cpp	Thu Apr 26 17:19:58 2018 +0200
+++ b/dmenu-background/main.cpp	Thu Apr 26 17:29:22 2018 +0200
@@ -38,64 +38,66 @@
 
 namespace {
 
-class context {
-private:
-    fs::path path_;
-    std::string command_;
-
-    std::vector<std::string> list();
+// {{{ config (class)
 
+class config {
 public:
-    context();
-
-    boost::optional<std::string> select();
-
-    void apply(const std::string& file);
+    fs::path path;
+    std::string command;
 };
 
-std::vector<std::string> context::list()
+// }}}
+
+// {{{ open
+
+config open()
+{
+    const auto section = dmenu::config("background");
+    const auto path = section.get("path").get_value();
+    const auto command = section.get("command").get_value();
+
+    return config{
+        path.empty() ? "/" : path,
+        command.empty() ? "feh --bg-scale" : command
+    };
+}
+
+// }}}
+
+// {{{ select
+
+std::vector<std::string> select_list(const fs::path& path)
 {
     std::vector<std::string> result;
 
-    if (path_.has_parent_path())
+    if (path.has_parent_path())
         result.push_back("..");
-    for (fs::directory_iterator it(path_); it != fs::directory_iterator(); it++)
+    for (fs::directory_iterator it(path); it != fs::directory_iterator(); it++)
         result.push_back(it->path().filename().string());
 
     return result;
 }
 
-context::context()
+std::string select(const fs::path& base)
 {
-    const auto section = dmenu::config("background");
-
-    path_ = section.get("path").get_value();
-    command_ = section.get("command").get_value();
+    auto path = base;
 
-    if (path_.empty())
-        path_ = "/";
-    if (command_.empty())
-        command_ = "feh --bg-scale";
-}
-
-boost::optional<std::string> context::select()
-{
     for (;;) {
-        const auto selection = dmenu::run({ "-l 16" }, list());
+        const auto selection = dmenu::run({ "-l 16" }, select_list(path));
 
         if (selection.empty())
-            return boost::none;
+            return selection;
 
         if (selection == "..") {
-            path_ = path_.parent_path();
+            path = path.parent_path();
             continue;
         }
 
-        const auto item = fs::path(path_) / selection;
+        const auto item = fs::path(path) / selection;
 
         switch (fs::status(item).type()) {
         case fs::directory_file:
-            path_ = item;
+            path = item;
             break;
         case fs::regular_file:
             return item.string();
@@ -104,27 +106,34 @@
         }
     }
 
-    return boost::none;
+    return "";
 }
 
-void context::apply(const std::string& file)
+// }}}
+
+// {{{ apply
+
+void apply(const std::string& command, const std::string& file)
 {
     std::ostringstream oss;
 
-    oss << command_ << " " << file;
+    oss << command << " " << file;
 
     std::system(oss.str().c_str());
 }
 
+// }}}
+
 } // !namespace
 
 int main()
 {
     try {
-        context ctx;
+        const auto conf = open();
+        const auto selection = select(conf.path);
 
-        if (auto selection = ctx.select())
-            ctx.apply(*selection);
+        if (!selection.empty())
+            apply(conf.command, selection);
     } catch (const std::exception& ex) {
         std::cerr << "abort: " << ex.what() << std::endl;
         return 1;