From 8b030930af781b9816bbf207d366e555a7b0d4b5 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Tue, 28 Jun 2016 00:39:33 +0200 Subject: [PATCH] refactor: Better handling of spaces between format tags Ignore spaces if the previous tag was not outputted --- include/modules/base.hpp | 32 +++++++++++++++++--------------- src/modules/base.cpp | 2 +- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/include/modules/base.hpp b/include/modules/base.hpp index 846b0fb9..ca73773d 100644 --- a/include/modules/base.hpp +++ b/include/modules/base.hpp @@ -52,14 +52,14 @@ class ModuleFormatter }; std::string module_name; - std::map> formats; + std::map> formats; public: explicit ModuleFormatter(std::string module_name) : module_name(module_name) {} void add(std::string name, std::string fallback, std::vector&& tags, std::vector&& whitelist = {}); - std::unique_ptr& get(std::string format_name); + std::shared_ptr get(std::string format_name); bool has(std::string tag, std::string format_name); bool has(std::string tag); }; @@ -207,31 +207,34 @@ namespace modules { std::lock_guard lck(this->output_lock); - log_trace(ConstCastModule(ModuleImpl).name()); - if (!this->enabled()) { log_trace(ConstCastModule(ModuleImpl).name() +" is disabled"); return ""; + } else { + log_trace(ConstCastModule(ModuleImpl).name()); } auto format_name = CastModule(ModuleImpl)->get_format(); - auto &&format = this->formatter->get(format_name); + auto format = this->formatter->get(format_name); int i = 0; + bool tag_built = true; + for (auto tag : string::split(format->value, ' ')) { - if ((i > 0 && !tag.empty()) || tag.empty()) { - this->builder->space(format->spacing); - } + bool is_blankspace = tag.empty(); if (tag[0] == '<' && tag[tag.length()-1] == '>') { - if (!CastModule(ModuleImpl)->build(this->builder.get(), tag)) { + if (i > 0) + this->builder->space(format->spacing); + if (!(tag_built = CastModule(ModuleImpl)->build(this->builder.get(), tag)) && i > 0) this->builder->remove_trailing_space(format->spacing); - } - } else { + if (tag_built) + i++; + } else if (is_blankspace && tag_built) { + this->builder->node(" "); + } else if (!is_blankspace) { this->builder->node(tag); } - - i++; } return format->decorate(this->builder.get(), this->builder->flush()); @@ -250,8 +253,7 @@ namespace modules this->threads.emplace_back(std::thread(&StaticModule::broadcast, this)); } - bool build(Builder *builder, std::string tag) - { + bool build(Builder *builder, std::string tag) { return true; } }; diff --git a/src/modules/base.cpp b/src/modules/base.cpp index 1382d2dc..a045fa3f 100644 --- a/src/modules/base.cpp +++ b/src/modules/base.cpp @@ -59,7 +59,7 @@ void ModuleFormatter::add(std::string name, std::string fallback, std::vectorformats.insert(std::make_pair(name, std::move(format))); } -std::unique_ptr& ModuleFormatter::get(std::string format_name) +std::shared_ptr ModuleFormatter::get(std::string format_name) { auto format = this->formats.find(format_name); if (format == this->formats.end())