From e8cb754c27aac806da45c9f54c06fd048d6c3e94 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Thu, 30 Jun 2016 05:18:53 +0200 Subject: [PATCH] builder: Get bar options lazily --- include/services/builder.hpp | 6 +++--- src/bar.cpp | 2 +- src/services/builder.cpp | 23 ++++++++++++++++------- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/include/services/builder.hpp b/include/services/builder.hpp index 2c7047c8..72d2d9e5 100644 --- a/include/services/builder.hpp +++ b/include/services/builder.hpp @@ -3,7 +3,6 @@ #include #include -#include "bar.hpp" #include "drawtypes/animation.hpp" #include "drawtypes/bar.hpp" #include "drawtypes/icon.hpp" @@ -13,6 +12,7 @@ #define DEFAULT_SPACING -1 class Lemonbuddy; +struct Options; class Builder { @@ -43,11 +43,11 @@ class Builder void align_right(); std::shared_ptr opts; + std::shared_ptr& get_opts(); public: explicit Builder(bool lazy_closing = true) - : lazy_closing(lazy_closing) - , opts(bar_opts()) {} + : lazy_closing(lazy_closing) {} void set_lazy_closing(bool mode) { this->lazy_closing = mode; } diff --git a/src/bar.cpp b/src/bar.cpp index 294cad04..ada2e3e9 100644 --- a/src/bar.cpp +++ b/src/bar.cpp @@ -36,7 +36,7 @@ std::shared_ptr get_bar() } std::shared_ptr bar_opts() { - return bar->opts; + return get_bar()->opts; } /** diff --git a/src/services/builder.cpp b/src/services/builder.cpp index e09af090..c3027f4f 100644 --- a/src/services/builder.cpp +++ b/src/services/builder.cpp @@ -4,14 +4,23 @@ #include #include +#include "bar.hpp" #include "config.hpp" #include "exception.hpp" #include "services/builder.hpp" #include "utils/string.hpp" #include "utils/math.hpp" + // Private +std::shared_ptr& Builder::get_opts() +{ + if (!this->opts) + this->opts = bar_opts(); + return this->opts; +} + void Builder::tag_open(char tag, std::string value) { this->append("%{"+ std::string({tag}) + value +"}"); } @@ -93,12 +102,12 @@ void Builder::append_module_output(Alignment alignment, std::string module_outpu int margin; - if (margin_left && (margin= this->opts->module_margin_left) > 0) + if (margin_left && (margin= this->get_opts()->module_margin_left) > 0) this->output += std::string(margin, ' '); this->append(module_output); - if (margin_right && (margin = this->opts->module_margin_right) > 0) + if (margin_right && (margin = this->get_opts()->module_margin_right) > 0) this->output += std::string(margin, ' '); } @@ -271,7 +280,7 @@ void Builder::offset(int pixels) void Builder::space(int width) { - if (width == DEFAULT_SPACING) width = this->opts->spacing; + if (width == DEFAULT_SPACING) width = this->get_opts()->spacing; if (width <= 0) return; std::string str(width, ' '); this->append(str); @@ -279,7 +288,7 @@ void Builder::space(int width) void Builder::remove_trailing_space(int width) { - if (width == DEFAULT_SPACING) width = this->opts->spacing; + if (width == DEFAULT_SPACING) width = this->get_opts()->spacing; if (width <= 0) return; std::string::size_type spacing = width; std::string str(spacing, ' '); @@ -323,7 +332,7 @@ void Builder::background(std::string color_) if (color.length() == 2 || (color.find("#") == 0 && color.length() == 3)) { color = "#"+ color.substr(color.length()-2); - auto bg = this->opts->background; + auto bg = this->get_opts()->background; color += bg.substr(bg.length()-(bg.length() < 6 ? 3 : 6)); } else if (color.length() >= 7 && color == "#"+ std::string(color.length()-1, color[1])) { color = color.substr(0, 4); @@ -352,7 +361,7 @@ void Builder::color(std::string color_) auto color(color_); if (color.length() == 2 || (color.find("#") == 0 && color.length() == 3)) { color = "#"+ color.substr(color.length()-2); - auto bg = this->opts->foreground; + auto bg = this->get_opts()->foreground; color += bg.substr(bg.length()-(bg.length() < 6 ? 3 : 6)); } else if (color.length() >= 7 && color == "#"+ std::string(color.length()-1, color[1])) { color = color.substr(0, 4); @@ -370,7 +379,7 @@ void Builder::color(std::string color_) void Builder::color_alpha(std::string alpha_) { auto alpha(alpha_); - std::string val = this->opts->foreground; + std::string val = this->get_opts()->foreground; if (alpha.find("#") == 0) { if (alpha.size() == 3) this->color(alpha.substr(0, 3) + val.substr(val.size() - 6));