2016-11-02 19:22:45 +00:00
|
|
|
#include "modules/date.hpp"
|
2016-12-05 03:45:01 +00:00
|
|
|
#include "drawtypes/label.hpp"
|
2016-11-02 19:22:45 +00:00
|
|
|
|
2016-11-20 22:04:31 +00:00
|
|
|
#include "modules/meta/base.inl"
|
|
|
|
|
2016-11-19 05:22:44 +00:00
|
|
|
POLYBAR_NS
|
2016-11-02 19:22:45 +00:00
|
|
|
|
|
|
|
namespace modules {
|
2016-11-20 22:04:31 +00:00
|
|
|
template class module<date_module>;
|
|
|
|
|
2016-12-21 07:00:09 +00:00
|
|
|
date_module::date_module(const bar_settings& bar, string name_) : timer_module<date_module>(bar, move(name_)) {
|
2016-11-25 12:55:15 +00:00
|
|
|
if (!m_bar.locale.empty()) {
|
2016-11-02 19:22:45 +00:00
|
|
|
setlocale(LC_TIME, m_bar.locale.c_str());
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
|
2016-12-05 03:45:01 +00:00
|
|
|
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", ""), '"');
|
2016-11-02 19:22:45 +00:00
|
|
|
|
2016-12-05 03:45:01 +00:00
|
|
|
if (m_dateformat.empty() && m_timeformat.empty()) {
|
|
|
|
throw module_error("No date or time format specified");
|
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
|
2016-12-05 03:45:01 +00:00
|
|
|
m_interval = chrono::duration<double>(m_conf.get<double>(name(), "interval", 1.0));
|
2016-11-02 19:22:45 +00:00
|
|
|
|
2016-12-05 03:45:01 +00:00
|
|
|
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);
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
|
2016-12-05 03:45:01 +00:00
|
|
|
if (m_formatter->has(TAG_LABEL)) {
|
|
|
|
m_label = load_optional_label(m_conf, name(), "label", "%date%");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool date_module::update() {
|
2016-11-02 19:22:45 +00:00
|
|
|
auto time = std::time(nullptr);
|
|
|
|
|
2016-12-05 03:45:01 +00:00
|
|
|
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};
|
2016-11-02 19:22:45 +00:00
|
|
|
|
2016-12-05 03:45:01 +00:00
|
|
|
if (!date_changed && !time_changed) {
|
2016-11-02 19:22:45 +00:00
|
|
|
return false;
|
2016-12-05 03:45:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_date = string{date_buffer};
|
|
|
|
m_time = string{time_buffer};
|
|
|
|
|
|
|
|
if (m_label) {
|
|
|
|
m_label->reset_tokens();
|
|
|
|
m_label->replace_token("%date%", m_date);
|
|
|
|
m_label->replace_token("%time%", m_time);
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-25 12:55:15 +00:00
|
|
|
bool date_module::build(builder* builder, const string& tag) const {
|
2016-12-05 03:45:01 +00:00
|
|
|
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 {
|
2016-11-02 19:22:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-21 07:38:44 +00:00
|
|
|
bool date_module::on(const input_event_t& evt) {
|
|
|
|
string cmd{*evt.data()};
|
|
|
|
|
2016-12-05 03:45:01 +00:00
|
|
|
if (cmd != EVENT_TOGGLE) {
|
|
|
|
return false;
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
2016-12-05 03:45:01 +00:00
|
|
|
m_toggled = !m_toggled;
|
|
|
|
wakeup();
|
2016-11-02 19:22:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-19 05:22:44 +00:00
|
|
|
POLYBAR_NS_END
|