changeset 38:b8ee1540e065

misc: use C++17 if-initializers
author David Demelier <markand@malikania.fr>
date Fri, 04 May 2018 10:33:55 +0200
parents 76d774822dac
children fa72d1db4fe4
files CMakeLists.txt dmenu-background/main.cpp dmenu-mpc/main.cpp dmenu-ssh/main.cpp libdmenu/CMakeLists.txt
diffstat 5 files changed, 45 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Tue May 01 12:36:51 2018 +0200
+++ b/CMakeLists.txt	Fri May 04 10:33:55 2018 +0200
@@ -19,13 +19,14 @@
 cmake_minimum_required(VERSION 3.5)
 project(dmenutools)
 
-set(CMAKE_CXX_STANDARD 14)
+set(CMAKE_CXX_STANDARD 17)
 set(CMAKE_CXX_STANDARD_REQUIRED On)
 
 include(cmake/function/DefineExecutable.cmake)
 
 find_package(Boost REQUIRED COMPONENTS filesystem system)
 find_package(PkgConfig REQUIRED)
+find_package(Threads REQUIRED)
 
 add_subdirectory(libdmenu)
 add_subdirectory(dmenu-background)
--- a/dmenu-background/main.cpp	Tue May 01 12:36:51 2018 +0200
+++ b/dmenu-background/main.cpp	Fri May 04 10:33:55 2018 +0200
@@ -69,12 +69,11 @@
 auto select_args() -> std::vector<std::string>
 {
     const auto section = dmenu::config("background");
-    const auto it = section.find("args");
 
-    if (it == section.end())
-        return { "-p background", "-l 16" };
+    if (const auto it = section.find("args"); it != section.end())
+        return *it;
 
-    return *it;
+    return { "-p background", "-l 16" };
 }
 
 auto select_list(const fs::path& path) -> std::vector<std::string> 
@@ -122,7 +121,6 @@
 
 // }}}
 
-
 // {{{ apply
 
 void apply(const std::string& command, const std::string& file)
@@ -142,9 +140,8 @@
 {
     try {
         const auto conf = open();
-        const auto selection = select(conf.path);
 
-        if (!selection.empty())
+        if (const auto selection = select(conf.path); !selection.empty())
             apply(conf.command, selection);
     } catch (const std::exception& ex) {
         dmenu::error("background", ex.what());
--- a/dmenu-mpc/main.cpp	Tue May 01 12:36:51 2018 +0200
+++ b/dmenu-mpc/main.cpp	Fri May 04 10:33:55 2018 +0200
@@ -140,12 +140,11 @@
 auto args() -> std::vector<std::string>
 {
     const auto section = dmenu::config("mpc");
-    const auto it = section.find("args");
 
-    if (it == section.end())
-        return { "-p mpc", "-l 16" };
+    if (const auto it = section.find("args"); it != section.end())
+        return *it;
 
-    return *it;
+    return { "-p mpc", "-l 16" };
 }
 
 // }}}
@@ -307,12 +306,10 @@
         "clear"
     };
 
-    const auto selection = dmenu::run(args(), lines);
+    if (const auto selection = dmenu::run(args(), lines); !selection.empty())
+        return selection;
 
-    if (selection.empty())
-        return boost::none;
-
-    return selection;
+    return boost::none;
 }
 
 // }}} menu
--- a/dmenu-ssh/main.cpp	Tue May 01 12:36:51 2018 +0200
+++ b/dmenu-ssh/main.cpp	Fri May 04 10:33:55 2018 +0200
@@ -33,26 +33,33 @@
 
 namespace {
 
+// {{{ path
+
 auto path() -> std::string
 {
-    const auto home = std::getenv("HOME");
+    if (const auto home = std::getenv("HOME"); home)
+        return std::string(home) + "/.ssh/config";
 
-    if (!home)
-        throw std::runtime_error("could not get HOME");
+    throw std::runtime_error("could not get HOME");
+}
 
-    return std::string(home) + "/.ssh/config";
-}
+// }}}
+
+// {{{ args
 
 auto args() -> std::vector<std::string>
 {
     const auto section = dmenu::config("ssh");
-    const auto it = section.find("args");
+
+    if (const auto it = section.find("args"); it != section.end())
+        return *it;
 
-    if (it == section.end())
-        return { "-p ssh" };
+    return { "-p ssh" };
+}
 
-    return *it;
-}
+// }}}
+
+// {{{ list
 
 auto list() -> std::vector<std::string>
 {
@@ -76,6 +83,10 @@
     return lines;
 }
 
+// }}}
+
+// {{{ terminal
+
 auto terminal() -> std::string
 {
     const auto section = dmenu::config("ssh");
@@ -84,11 +95,19 @@
     return terminal.empty() ? "xterm" : terminal;
 }
 
+// }}}
+
+// {{{ menu
+
 auto menu() -> std::string
 {
     return dmenu::run(args(), list());
 }
 
+// }}}
+
+// {{{ exec
+
 void exec(const std::string& host)
 {
     const auto term = terminal();
@@ -97,14 +116,14 @@
     execvp(term.c_str(), (char**)list);
 }
 
+// }}}
+
 } // !namespace
 
 int main()
 {
     try {
-        const auto selection = menu();
-
-        if (!selection.empty())
+        if (const auto selection = menu(); !selection.empty())
             exec(selection);
     } catch (const std::exception& ex) {
         dmenu::error("ssh", ex.what());
--- a/libdmenu/CMakeLists.txt	Tue May 01 12:36:51 2018 +0200
+++ b/libdmenu/CMakeLists.txt	Fri May 04 10:33:55 2018 +0200
@@ -52,6 +52,7 @@
     libdmenu
     Boost::filesystem
     Boost::system
+    Threads::Threads
     $<$<BOOL:${HAVE_NOTIFY}>:PkgConfig::libnotify>
 )