refactor(date): Add label and date/time tokens

Ref #225
This commit is contained in:
Michael Carlberg 2016-12-05 04:45:01 +01:00
parent d561b9cb2d
commit 7d1092db04
3 changed files with 81 additions and 35 deletions

View File

@ -231,12 +231,20 @@ label-disconnected-foreground = ${colors.foreground-alt}
[module/date] [module/date]
type = internal/date type = internal/date
date = %H:%M
interval = 5 interval = 5
format-prefix = " "
date =
date-alt = " %Y-%m-%d"
time = %H:%M
time-alt = %H:%M:%S
format-prefix =
format-prefix-foreground = ${colors.foreground-alt} format-prefix-foreground = ${colors.foreground-alt}
format-underline = #0a6cf5 format-underline = #0a6cf5
label = %date% %time%
[module/volume] [module/volume]
type = internal/volume type = internal/volume

View File

@ -13,16 +13,27 @@ namespace modules {
bool update(); bool update();
bool build(builder* builder, const string& tag) const; bool build(builder* builder, const string& tag) const;
bool handle_event(string cmd); bool handle_event(string cmd);
bool receive_events() const; bool receive_events() const {
return true;
}
private: private:
static constexpr auto TAG_DATE = "<date>"; static constexpr auto TAG_LABEL = "<label>";
static constexpr auto EVENT_TOGGLE = "datetoggle"; static constexpr auto EVENT_TOGGLE = "datetoggle";
string m_format; // @deprecated: Use <label>
string m_formatalt; static constexpr auto TAG_DATE = "<date>";
label_t m_label;
string m_dateformat;
string m_dateformat_alt;
string m_timeformat;
string m_timeformat_alt;
string m_date;
string m_time;
char m_buffer[256] = {'\0'};
stateflag m_toggled{false}; stateflag m_toggled{false};
}; };
} }

View File

@ -1,4 +1,5 @@
#include "modules/date.hpp" #include "modules/date.hpp"
#include "drawtypes/label.hpp"
#include "modules/meta/base.inl" #include "modules/meta/base.inl"
#include "modules/meta/timer_module.inl" #include "modules/meta/timer_module.inl"
@ -14,58 +15,84 @@ namespace modules {
setlocale(LC_TIME, m_bar.locale.c_str()); setlocale(LC_TIME, m_bar.locale.c_str());
} }
m_interval = chrono::duration<double>(m_conf.get<float>(name(), "interval", 1)); m_dateformat = string_util::trim(m_conf.get<string>(name(), "date", ""), '"');
m_dateformat_alt = string_util::trim(m_conf.get<string>(name(), "date-alt", ""), '"');
m_timeformat = string_util::trim(m_conf.get<string>(name(), "time", ""), '"');
m_timeformat_alt = string_util::trim(m_conf.get<string>(name(), "time-alt", ""), '"');
m_formatter->add(DEFAULT_FORMAT, TAG_DATE, {TAG_DATE}); if (m_dateformat.empty() && m_timeformat.empty()) {
throw module_error("No date or time format specified");
}
m_format = m_conf.get<string>(name(), "date"); m_interval = chrono::duration<double>(m_conf.get<double>(name(), "interval", 1.0));
m_formatalt = m_conf.get<string>(name(), "date-alt", "");
m_formatter->add(DEFAULT_FORMAT, TAG_LABEL, {TAG_LABEL, TAG_DATE});
if (m_formatter->has(TAG_DATE)) {
m_log.warn("%s: The format tag `<date>` is deprecated, use `<label>` instead.", name());
m_formatter->get(DEFAULT_FORMAT)->value =
string_util::replace_all(m_formatter->get(DEFAULT_FORMAT)->value, TAG_DATE, TAG_LABEL);
}
if (m_formatter->has(TAG_LABEL)) {
m_label = load_optional_label(m_conf, name(), "label", "%date%");
}
} }
bool date_module::update() { bool date_module::update() {
if (!m_formatter->has(TAG_DATE)) { auto time = std::time(nullptr);
auto date_format = m_toggled ? m_dateformat_alt : m_dateformat;
char date_buffer[64]{'\0'};
strftime(date_buffer, sizeof(date_buffer), date_format.c_str(), localtime(&time));
auto time_format = m_toggled ? m_timeformat_alt : m_timeformat;
char time_buffer[64]{'\0'};
strftime(time_buffer, sizeof(time_buffer), time_format.c_str(), localtime(&time));
bool date_changed{strncmp(date_buffer, m_date.c_str(), sizeof(date_buffer)) != 0};
bool time_changed{strncmp(time_buffer, m_time.c_str(), sizeof(time_buffer)) != 0};
if (!date_changed && !time_changed) {
return false; return false;
} }
auto time = std::time(nullptr); m_date = string{date_buffer};
auto date_format = m_toggled ? m_formatalt : m_format; m_time = string{time_buffer};
char buffer[256] = {'\0'};
std::strftime(buffer, sizeof(buffer), date_format.c_str(), std::localtime(&time)); if (m_label) {
m_label->reset_tokens();
if (std::strncmp(buffer, m_buffer, sizeof(buffer)) == 0) { m_label->replace_token("%date%", m_date);
return false; m_label->replace_token("%time%", m_time);
} else {
std::memmove(m_buffer, buffer, sizeof(buffer));
} }
return true; return true;
} }
bool date_module::build(builder* builder, const string& tag) const { bool date_module::build(builder* builder, const string& tag) const {
if (tag != TAG_DATE) { if (tag == TAG_LABEL) {
if (!m_dateformat_alt.empty() || !m_timeformat_alt.empty()) {
builder->cmd(mousebtn::LEFT, EVENT_TOGGLE);
builder->node(m_label);
builder->cmd_close();
} else {
builder->node(m_label);
}
} else {
return false; return false;
} }
if (!m_formatalt.empty()) {
m_builder->cmd(mousebtn::LEFT, EVENT_TOGGLE);
}
builder->node(m_buffer);
return true; return true;
} }
bool date_module::handle_event(string cmd) { bool date_module::handle_event(string cmd) {
if (cmd == EVENT_TOGGLE) { if (cmd != EVENT_TOGGLE) {
return false;
}
m_toggled = !m_toggled; m_toggled = !m_toggled;
wakeup(); wakeup();
}
return cmd == EVENT_TOGGLE;
}
bool date_module::receive_events() const {
return true; return true;
} }
} }