refactor(builder): Cleanup

This commit is contained in:
Michael Carlberg 2016-12-26 17:06:28 +01:00
parent b7f16672ff
commit 7b780a3201
2 changed files with 62 additions and 73 deletions

View File

@ -3,13 +3,12 @@
#include <map> #include <map>
#include "common.hpp" #include "common.hpp"
#include "components/types.hpp"
POLYBAR_NS POLYBAR_NS
using std::map; using std::map;
#define DEFAULT_SPACING -1
// fwd decl // fwd decl
namespace drawtypes { namespace drawtypes {
class label; class label;
@ -19,27 +18,22 @@ namespace drawtypes {
} }
using namespace drawtypes; using namespace drawtypes;
enum class alignment : uint8_t;
enum class attribute : uint8_t;
enum class edge : uint8_t;
enum class syntaxtag : uint8_t;
enum class mousebtn : uint8_t;
struct bar_settings;
class builder { class builder {
public: public:
explicit builder(const bar_settings& bar); explicit builder(const bar_settings bar);
string flush(); string flush();
void append(const string& text); void append(string text);
void node(string str, bool add_space = false); void node(string str, bool add_space = false);
void node(string str, int font_index, bool add_space = false); void node(string str, int font_index, bool add_space = false);
void node(const label_t& label, bool add_space = false); void node(const label_t& label, bool add_space = false);
void node_repeat(const string& str, size_t n, bool add_space = false); void node_repeat(const string& str, size_t n, bool add_space = false);
void node_repeat(const label_t& label, size_t n, bool add_space = false); void node_repeat(const label_t& label, size_t n, bool add_space = false);
void offset(int pixels = 0); void offset(int pixels = 0);
void space(int width = DEFAULT_SPACING); void space(size_t width);
void remove_trailing_space(int width = DEFAULT_SPACING); void space();
void remove_trailing_space(size_t len);
void remove_trailing_space();
void font(int index); void font(int index);
void font_close(); void font_close();
void background(string color); void background(string color);
@ -70,17 +64,17 @@ class builder {
void tag_close(attribute attr); void tag_close(attribute attr);
private: private:
const bar_settings& m_bar; const bar_settings m_bar;
string m_output; string m_output;
map<syntaxtag, int> m_tags; map<syntaxtag, int> m_tags{};
map<syntaxtag, string> m_colors; map<syntaxtag, string> m_colors{};
uint8_t m_attributes; uint8_t m_attributes{0};
uint8_t m_fontindex; uint8_t m_fontindex{0};
string m_background; string m_background{};
string m_foreground; string m_foreground{};
}; };
POLYBAR_NS_END POLYBAR_NS_END

View File

@ -1,29 +1,29 @@
#include <utility> #include <utility>
#include "components/builder.hpp" #include "components/builder.hpp"
#include "components/types.hpp"
#include "drawtypes/label.hpp" #include "drawtypes/label.hpp"
#include "utils/color.hpp"
#include "utils/math.hpp" #include "utils/math.hpp"
#include "utils/string.hpp" #include "utils/string.hpp"
#include "utils/color.hpp" #include "utils/time.hpp"
POLYBAR_NS POLYBAR_NS
#ifndef BUILDER_SPACE_TOKEN #ifndef BUILDER_SPACE_TOKEN
#define BUILDER_SPACE_TOKEN "%__" #define BUILDER_SPACE_TOKEN "%__"
#endif #endif
builder::builder(const bar_settings& bar) : m_bar(bar), m_attributes{static_cast<uint8_t>(attribute::NONE)} { builder::builder(const bar_settings bar) : m_bar(bar) {
m_tags[syntaxtag::A] = 0; m_tags[syntaxtag::A] = 1;
m_tags[syntaxtag::B] = 0; m_tags[syntaxtag::B] = 2;
m_tags[syntaxtag::F] = 0; m_tags[syntaxtag::F] = 3;
m_tags[syntaxtag::T] = 0; m_tags[syntaxtag::T] = 9;
m_tags[syntaxtag::u] = 0; m_tags[syntaxtag::o] = 7;
m_tags[syntaxtag::o] = 0; m_tags[syntaxtag::u] = 8;
m_colors[syntaxtag::B] = "";
m_colors[syntaxtag::F] = ""; m_colors[syntaxtag::B] = string();
m_colors[syntaxtag::u] = ""; m_colors[syntaxtag::F] = string();
m_colors[syntaxtag::o] = ""; m_colors[syntaxtag::o] = string();
m_colors[syntaxtag::u] = string();
} }
/** /**
@ -72,8 +72,9 @@ string builder::flush() {
/** /**
* Insert raw text string * Insert raw text string
*/ */
void builder::append(const string& text) { void builder::append(string text) {
m_output += text; m_output.reserve(text.size());
m_output += move(text);
} }
/** /**
@ -264,6 +265,7 @@ void builder::node(const label_t& label, bool add_space) {
*/ */
void builder::node_repeat(const string& str, size_t n, bool add_space) { void builder::node_repeat(const string& str, size_t n, bool add_space) {
string text; string text;
text.reserve(str.size() * n);
while (n--) { while (n--) {
text += str; text += str;
} }
@ -275,8 +277,10 @@ void builder::node_repeat(const string& str, size_t n, bool add_space) {
*/ */
void builder::node_repeat(const label_t& label, size_t n, bool add_space) { void builder::node_repeat(const label_t& label, size_t n, bool add_space) {
string text; string text;
string label_text{label->get()};
text.reserve(label_text.size() * n);
while (n--) { while (n--) {
text += label->get(); text += label_text;
} }
label_t tmp{new label_t::element_type{text}}; label_t tmp{new label_t::element_type{text}};
tmp->replace_defined_values(label); tmp->replace_defined_values(label);
@ -296,32 +300,25 @@ void builder::offset(int pixels) {
/** /**
* Insert spaces * Insert spaces
*/ */
void builder::space(int width) { void builder::space(size_t width) {
if (width == DEFAULT_SPACING) { m_output.append(width, ' ');
width = m_bar.spacing; }
} void builder::space() {
if (width <= 0) { m_output.append(m_bar.spacing, ' ');
return;
}
string str(width, ' ');
append(str);
} }
/** /**
* Remove trailing space * Remove trailing space
*/ */
void builder::remove_trailing_space(int width) { void builder::remove_trailing_space(size_t len) {
if (width == DEFAULT_SPACING) { if (len == 0_z || len > m_output.size()) {
width = m_bar.spacing;
}
if (width <= 0) {
return; return;
} else if (string(m_output.rbegin() + len, m_output.rbegin()) == string(len, ' ')) {
m_output.erase(m_output.size() - len);
} }
string::size_type spacing = width; }
string str(spacing, ' '); void builder::remove_trailing_space() {
if (m_output.length() >= spacing && m_output.substr(m_output.length() - spacing) == str) { remove_trailing_space(m_bar.spacing);
m_output = m_output.substr(0, m_output.length() - spacing);
}
} }
/** /**
@ -364,7 +361,7 @@ void builder::background(string color) {
* Insert tag to reset the background color * Insert tag to reset the background color
*/ */
void builder::background_close() { void builder::background_close() {
m_colors[syntaxtag::B] = ""; m_colors[syntaxtag::B].clear();
tag_close(syntaxtag::B); tag_close(syntaxtag::B);
} }
@ -372,10 +369,12 @@ void builder::background_close() {
* Insert tag to alter the current foreground color * Insert tag to alter the current foreground color
*/ */
void builder::color(string color) { void builder::color(string color) {
if (color.length() == 2 || (color.find('#') == 0 && color.length() == 3)) { if (color.length() == 2 || (color[0] == '#' && color.length() == 3)) {
string fg{foreground_hex()}; string fg{foreground_hex()};
color = "#" + color.substr(color.length() - 2); if (!fg.empty()) {
color += fg.substr(fg.length() - (fg.length() < 6 ? 3 : 6)); color = "#" + color.substr(color.length() - 2);
color += fg.substr(fg.length() - (fg.length() < 6 ? 3 : 6));
}
} else if (color.length() >= 7 && color == "#" + string(color.length() - 1, color[1])) { } else if (color.length() >= 7 && color == "#" + string(color.length() - 1, color[1])) {
color = color.substr(0, 4); color = color.substr(0, 4);
} }
@ -389,29 +388,25 @@ void builder::color(string color) {
* Insert tag to alter the alpha value of the default foreground color * Insert tag to alter the alpha value of the default foreground color
*/ */
void builder::color_alpha(string alpha) { void builder::color_alpha(string alpha) {
string val{foreground_hex()};
if (alpha.find('#') == string::npos) { if (alpha.find('#') == string::npos) {
alpha = "#" + alpha; alpha = "#" + alpha;
} }
if (alpha.size() == 4) { if (alpha.size() == 4) {
color(alpha); color(alpha);
return; } else {
string val{foreground_hex()};
if (val.size() < 6 && val.size() > 2) {
val.append(val.substr(val.size() - 3));
}
color((alpha.substr(0, 3) + val.substr(val.size() - 6)).substr(0, 9));
} }
if (val.size() < 6 && val.size() > 2) {
val.append(val.substr(val.size() - 3));
}
color((alpha.substr(0, 3) + val.substr(val.size() - 6)).substr(0, 9));
} }
/** /**
* Insert tag to reset the foreground color * Insert tag to reset the foreground color
*/ */
void builder::color_close() { void builder::color_close() {
m_colors[syntaxtag::F] = ""; m_colors[syntaxtag::F].clear();
tag_close(syntaxtag::F); tag_close(syntaxtag::F);
} }
@ -445,7 +440,7 @@ void builder::overline_color(string color) {
* Close underline color tag * Close underline color tag
*/ */
void builder::overline_color_close() { void builder::overline_color_close() {
m_colors[syntaxtag::o] = ""; m_colors[syntaxtag::o].clear();
tag_close(syntaxtag::o); tag_close(syntaxtag::o);
} }
@ -464,7 +459,7 @@ void builder::underline_color(string color) {
*/ */
void builder::underline_color_close() { void builder::underline_color_close() {
tag_close(syntaxtag::u); tag_close(syntaxtag::u);
m_colors[syntaxtag::u] = ""; m_colors[syntaxtag::u].clear();
} }
/** /**