2016-06-15 03:32:35 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "common.hpp"
|
|
|
|
#include "components/builder.hpp"
|
|
|
|
#include "components/config.hpp"
|
|
|
|
#include "components/types.hpp"
|
|
|
|
#include "drawtypes/label.hpp"
|
2016-10-25 05:10:03 +00:00
|
|
|
#include "utils/math.hpp"
|
2016-06-15 03:32:35 +00:00
|
|
|
#include "utils/mixins.hpp"
|
|
|
|
|
|
|
|
LEMONBUDDY_NS
|
|
|
|
|
|
|
|
namespace drawtypes {
|
|
|
|
class progressbar : public non_copyable_mixin<progressbar> {
|
|
|
|
public:
|
|
|
|
explicit progressbar(
|
2016-10-25 05:10:03 +00:00
|
|
|
const bar_settings& bar, int width, string format, bool lazy_builder_closing = true)
|
2016-06-15 03:32:35 +00:00
|
|
|
: m_builder(make_unique<builder>(bar, lazy_builder_closing))
|
|
|
|
, m_format(format)
|
|
|
|
, m_width(width) {}
|
|
|
|
|
|
|
|
void set_fill(icon_t&& fill) {
|
|
|
|
m_fill = forward<decltype(fill)>(fill);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_empty(icon_t&& empty) {
|
|
|
|
m_empty = forward<decltype(empty)>(empty);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_indicator(icon_t&& indicator) {
|
2016-10-25 05:10:03 +00:00
|
|
|
if (!m_indicator && indicator.get())
|
|
|
|
m_width--;
|
2016-06-15 03:32:35 +00:00
|
|
|
m_indicator = forward<decltype(indicator)>(indicator);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_gradient(bool mode) {
|
|
|
|
m_gradient = mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_colors(vector<string>&& colors) {
|
|
|
|
m_colors = forward<decltype(colors)>(colors);
|
|
|
|
|
|
|
|
if (m_colors.empty())
|
2016-10-25 05:10:03 +00:00
|
|
|
m_colorstep = 1;
|
|
|
|
else
|
|
|
|
m_colorstep = m_width / m_colors.size();
|
|
|
|
}
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-10-25 05:10:03 +00:00
|
|
|
string output(float percentage) {
|
|
|
|
string output{m_format};
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-10-25 05:10:03 +00:00
|
|
|
// Get fill/empty widths based on percentage
|
|
|
|
unsigned int perc = math_util::cap(percentage, 0.0f, 100.0f);
|
|
|
|
unsigned int fill_width = math_util::percentage_to_value(perc, m_width);
|
|
|
|
unsigned int empty_width = m_width - fill_width;
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-10-25 05:10:03 +00:00
|
|
|
// Output fill icons
|
|
|
|
fill(perc, fill_width);
|
2016-06-15 03:32:35 +00:00
|
|
|
output = string_util::replace_all(output, "%fill%", m_builder->flush());
|
|
|
|
|
2016-10-25 05:10:03 +00:00
|
|
|
// Output indicator icon
|
2016-06-15 03:32:35 +00:00
|
|
|
m_builder->node(m_indicator);
|
|
|
|
output = string_util::replace_all(output, "%indicator%", m_builder->flush());
|
|
|
|
|
2016-10-25 05:10:03 +00:00
|
|
|
// Output empty icons
|
2016-06-15 03:32:35 +00:00
|
|
|
while (empty_width--) m_builder->node(m_empty);
|
|
|
|
output = string_util::replace_all(output, "%empty%", m_builder->flush());
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2016-10-25 05:10:03 +00:00
|
|
|
void fill(unsigned int perc, unsigned int fill_width) {
|
|
|
|
if (m_colors.empty()) {
|
|
|
|
for (size_t i = 0; i < fill_width; i++) {
|
|
|
|
m_builder->node(m_fill);
|
|
|
|
}
|
|
|
|
} else if (m_gradient) {
|
|
|
|
size_t color = 0;
|
|
|
|
for (size_t i = 0; i < fill_width; i++) {
|
|
|
|
if (i % m_colorstep == 0)
|
|
|
|
m_fill->m_foreground = m_colors[color++];
|
|
|
|
m_builder->node(m_fill);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
size_t color = math_util::percentage_to_value<size_t>(perc, m_colors.size() - 1);
|
|
|
|
m_fill->m_foreground = m_colors[color];
|
|
|
|
for (size_t i = 0; i < fill_width; i++) {
|
|
|
|
m_builder->node(m_fill);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-06-15 03:32:35 +00:00
|
|
|
unique_ptr<builder> m_builder;
|
|
|
|
vector<string> m_colors;
|
|
|
|
string m_format;
|
|
|
|
unsigned int m_width;
|
2016-10-25 05:10:03 +00:00
|
|
|
unsigned int m_colorstep = 1;
|
2016-06-15 03:32:35 +00:00
|
|
|
bool m_gradient = false;
|
|
|
|
|
|
|
|
icon_t m_fill;
|
|
|
|
icon_t m_empty;
|
|
|
|
icon_t m_indicator;
|
|
|
|
};
|
|
|
|
|
|
|
|
using progressbar_t = shared_ptr<progressbar>;
|
|
|
|
|
2016-10-25 05:10:03 +00:00
|
|
|
/**
|
|
|
|
* Create a progressbar by loading values
|
|
|
|
* from the configuration
|
|
|
|
*/
|
|
|
|
inline auto load_progressbar(
|
|
|
|
const bar_settings& bar, const config& conf, string section, string name) {
|
|
|
|
// Remove the start and end tag from the name in case a format tag is passed
|
2016-06-15 03:32:35 +00:00
|
|
|
name = string_util::ltrim(string_util::rtrim(name, '>'), '<');
|
|
|
|
|
2016-10-25 05:10:03 +00:00
|
|
|
string format = "%fill%%indicator%%empty%";
|
|
|
|
unsigned int width;
|
|
|
|
|
|
|
|
if ((format = conf.get<decltype(format)>(section, name + "-format", format)).empty())
|
|
|
|
throw application_error("Invalid format defined at [" + conf.build_path(section, name) + "]");
|
|
|
|
if ((width = conf.get<decltype(width)>(section, name + "-width")) < 1)
|
|
|
|
throw application_error("Invalid width defined at [" + conf.build_path(section, name) + "]");
|
|
|
|
|
|
|
|
progressbar_t progressbar{new progressbar_t::element_type(bar, width, format)};
|
|
|
|
progressbar->set_gradient(conf.get<bool>(section, name + "-gradient", true));
|
|
|
|
progressbar->set_colors(conf.get_list<string>(section, name + "-foreground", {}));
|
|
|
|
|
|
|
|
icon_t icon_empty;
|
|
|
|
icon_t icon_fill;
|
|
|
|
icon_t icon_indicator;
|
|
|
|
|
|
|
|
if (format.find("%empty%") != string::npos)
|
|
|
|
icon_empty = load_icon(conf, section, name + "-empty");
|
|
|
|
if (format.find("%fill%") != string::npos)
|
|
|
|
icon_fill = load_icon(conf, section, name + "-fill");
|
|
|
|
if (format.find("%indicator%") != string::npos)
|
|
|
|
icon_indicator = load_icon(conf, section, name + "-indicator");
|
|
|
|
|
|
|
|
// If a foreground/background color is defined for the indicator
|
|
|
|
// but not for the empty icon we use the bar's default colors to
|
|
|
|
// avoid color bleed
|
|
|
|
if (icon_empty && icon_indicator) {
|
|
|
|
if (!icon_indicator->m_background.empty() && icon_empty->m_background.empty())
|
|
|
|
icon_empty->m_background = bar.background.hex();
|
|
|
|
if (!icon_indicator->m_foreground.empty() && icon_empty->m_foreground.empty())
|
|
|
|
icon_empty->m_foreground = bar.foreground.hex();
|
|
|
|
}
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-10-25 05:10:03 +00:00
|
|
|
progressbar->set_empty(move(icon_empty));
|
|
|
|
progressbar->set_fill(move(icon_fill));
|
|
|
|
progressbar->set_indicator(move(icon_indicator));
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-10-25 05:10:03 +00:00
|
|
|
return progressbar;
|
2016-06-15 03:32:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LEMONBUDDY_NS_END
|