From 1d9dd719bca8c83344a6f42c1fe327d421a9942b Mon Sep 17 00:00:00 2001
From: patrick96 <p.ziegler96@gmail.com>
Date: Tue, 2 Jan 2018 14:05:14 +0100
Subject: [PATCH] refactor(script): Loop over buttons and replace tokens

---
 src/modules/script.cpp | 41 +++++++++++------------------------------
 1 file changed, 11 insertions(+), 30 deletions(-)

diff --git a/src/modules/script.cpp b/src/modules/script.cpp
index 0f0dfa20..669e9a8e 100644
--- a/src/modules/script.cpp
+++ b/src/modules/script.cpp
@@ -174,39 +174,20 @@ namespace modules {
     string cnt{to_string(m_counter)};
     string output{module::get_output()};
 
-    if (!m_actions[mousebtn::LEFT].empty()) {
-      m_builder->cmd(mousebtn::LEFT, string_util::replace_all(m_actions[mousebtn::LEFT], "%counter%", cnt));
+    for (auto btn : {mousebtn::LEFT, mousebtn::MIDDLE, mousebtn::RIGHT, mousebtn::SCROLL_UP, mousebtn::SCROLL_DOWN}) {
 
-      if(m_tail && m_command && m_command->is_running()) {
-        m_builder->cmd(mousebtn::LEFT, string_util::replace_all(m_actions[mousebtn::LEFT], "%pid%", std::to_string(m_command->get_pid())));
-      }
-    }
-    if (!m_actions[mousebtn::MIDDLE].empty()) {
-      m_builder->cmd(mousebtn::MIDDLE, string_util::replace_all(m_actions[mousebtn::MIDDLE], "%counter%", cnt));
+      auto action = m_actions[btn];
 
-      if(m_tail && m_command && m_command->is_running()) {
-        m_builder->cmd(mousebtn::MIDDLE, string_util::replace_all(m_actions[mousebtn::MIDDLE], "%pid%", std::to_string(m_command->get_pid())));
-      }
-    }
-    if (!m_actions[mousebtn::RIGHT].empty()) {
-      m_builder->cmd(mousebtn::RIGHT, string_util::replace_all(m_actions[mousebtn::RIGHT], "%counter%", cnt));
+      if (!action.empty()) {
+        m_builder->cmd(btn, string_util::replace_all(action, "%counter%", cnt));
 
-      if(m_tail && m_command && m_command->is_running()) {
-        m_builder->cmd(mousebtn::RIGHT, string_util::replace_all(m_actions[mousebtn::RIGHT], "%pid%", std::to_string(m_command->get_pid())));
-      }
-    }
-    if (!m_actions[mousebtn::SCROLL_UP].empty()) {
-      m_builder->cmd(mousebtn::SCROLL_UP, string_util::replace_all(m_actions[mousebtn::SCROLL_UP], "%counter%", cnt));
-
-      if(m_tail && m_command && m_command->is_running()) {
-        m_builder->cmd(mousebtn::SCROLL_UP, string_util::replace_all(m_actions[mousebtn::SCROLL_UP], "%pid%", std::to_string(m_command->get_pid())));
-      }
-    }
-    if (!m_actions[mousebtn::SCROLL_DOWN].empty()) {
-      m_builder->cmd( mousebtn::SCROLL_DOWN, string_util::replace_all(m_actions[mousebtn::SCROLL_DOWN], "%counter%", cnt));
-
-      if(m_tail && m_command && m_command->is_running()) {
-        m_builder->cmd(mousebtn::SCROLL_DOWN, string_util::replace_all(m_actions[mousebtn::SCROLL_DOWN], "%pid%", std::to_string(m_command->get_pid())));
+        /*
+         * The pid token is only for tailed commands.
+         * If the command is not specified or running, replacement is unnecessary as well
+         */
+        if(m_tail && m_command && m_command->is_running()) {
+          m_builder->cmd(btn, string_util::replace_all(action, "%pid%", to_string(m_command->get_pid())));
+        }
       }
     }