2016-11-02 15:22:45 -04:00
|
|
|
#include "modules/date.hpp"
|
|
|
|
|
2020-05-15 13:59:08 -04:00
|
|
|
#include "drawtypes/label.hpp"
|
2016-11-20 17:04:31 -05:00
|
|
|
#include "modules/meta/base.inl"
|
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS
|
2016-11-02 15:22:45 -04:00
|
|
|
|
|
|
|
namespace modules {
|
2016-11-20 17:04:31 -05:00
|
|
|
template class module<date_module>;
|
|
|
|
|
2016-12-21 02:00:09 -05:00
|
|
|
date_module::date_module(const bar_settings& bar, string name_) : timer_module<date_module>(bar, move(name_)) {
|
2016-11-25 07:55:15 -05:00
|
|
|
if (!m_bar.locale.empty()) {
|
2017-09-15 08:21:38 -04:00
|
|
|
datetime_stream.imbue(std::locale(m_bar.locale.c_str()));
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
2016-11-02 15:22:45 -04:00
|
|
|
|
2018-07-29 11:38:01 -04:00
|
|
|
m_dateformat = m_conf.get(name(), "date", ""s);
|
|
|
|
m_dateformat_alt = m_conf.get(name(), "date-alt", ""s);
|
|
|
|
m_timeformat = m_conf.get(name(), "time", ""s);
|
|
|
|
m_timeformat_alt = m_conf.get(name(), "time-alt", ""s);
|
2016-11-02 15:22:45 -04:00
|
|
|
|
2016-12-04 22:45:01 -05:00
|
|
|
if (m_dateformat.empty() && m_timeformat.empty()) {
|
|
|
|
throw module_error("No date or time format specified");
|
|
|
|
}
|
2016-11-02 15:22:45 -04:00
|
|
|
|
2020-12-05 16:58:38 -05:00
|
|
|
set_interval(1s);
|
2016-11-02 15:22:45 -04:00
|
|
|
|
2016-12-04 22:45:01 -05: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 07:55:15 -05:00
|
|
|
}
|
2016-11-02 15:22:45 -04:00
|
|
|
|
2016-12-04 22:45:01 -05:00
|
|
|
if (m_formatter->has(TAG_LABEL)) {
|
|
|
|
m_label = load_optional_label(m_conf, name(), "label", "%date%");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool date_module::update() {
|
2016-11-02 15:22:45 -04:00
|
|
|
auto time = std::time(nullptr);
|
|
|
|
|
2016-12-04 22:45:01 -05:00
|
|
|
auto date_format = m_toggled ? m_dateformat_alt : m_dateformat;
|
2017-09-15 08:21:38 -04:00
|
|
|
// Clear stream contents
|
|
|
|
datetime_stream.str("");
|
|
|
|
datetime_stream << std::put_time(localtime(&time), date_format.c_str());
|
|
|
|
auto date_string = datetime_stream.str();
|
2016-12-04 22:45:01 -05:00
|
|
|
|
|
|
|
auto time_format = m_toggled ? m_timeformat_alt : m_timeformat;
|
2017-09-15 08:21:38 -04:00
|
|
|
// Clear stream contents
|
|
|
|
datetime_stream.str("");
|
|
|
|
datetime_stream << std::put_time(localtime(&time), time_format.c_str());
|
|
|
|
auto time_string = datetime_stream.str();
|
2016-12-04 22:45:01 -05:00
|
|
|
|
2017-09-15 18:09:26 -04:00
|
|
|
if (m_date == date_string && m_time == time_string) {
|
2016-11-02 15:22:45 -04:00
|
|
|
return false;
|
2016-12-04 22:45:01 -05:00
|
|
|
}
|
|
|
|
|
2017-09-15 08:13:35 -04:00
|
|
|
m_date = date_string;
|
|
|
|
m_time = time_string;
|
2016-12-04 22:45:01 -05:00
|
|
|
|
|
|
|
if (m_label) {
|
|
|
|
m_label->reset_tokens();
|
|
|
|
m_label->replace_token("%date%", m_date);
|
|
|
|
m_label->replace_token("%time%", m_time);
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
2016-11-02 15:22:45 -04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-25 07:55:15 -05:00
|
|
|
bool date_module::build(builder* builder, const string& tag) const {
|
2016-12-04 22:45:01 -05:00
|
|
|
if (tag == TAG_LABEL) {
|
|
|
|
if (!m_dateformat_alt.empty() || !m_timeformat_alt.empty()) {
|
2020-05-23 18:36:16 -04:00
|
|
|
builder->action(mousebtn::LEFT, *this, EVENT_TOGGLE, "", m_label);
|
2016-12-04 22:45:01 -05:00
|
|
|
} else {
|
|
|
|
builder->node(m_label);
|
|
|
|
}
|
|
|
|
} else {
|
2016-11-02 15:22:45 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-31 15:11:36 -04:00
|
|
|
bool date_module::input(const string& action, const string&) {
|
2020-05-24 12:35:12 -04:00
|
|
|
if (action != EVENT_TOGGLE) {
|
2016-12-04 22:45:01 -05:00
|
|
|
return false;
|
2016-11-02 15:22:45 -04:00
|
|
|
}
|
2016-12-04 22:45:01 -05:00
|
|
|
m_toggled = !m_toggled;
|
|
|
|
wakeup();
|
2016-11-02 15:22:45 -04:00
|
|
|
return true;
|
|
|
|
}
|
2020-05-15 13:59:08 -04:00
|
|
|
} // namespace modules
|
2016-11-02 15:22:45 -04:00
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS_END
|