polybar-dwm/src/modules/date.cpp

74 lines
1.6 KiB
C++
Raw Normal View History

2016-11-02 20:22:45 +01:00
#include "modules/date.hpp"
2016-11-20 23:04:31 +01:00
#include "modules/meta/base.inl"
#include "modules/meta/timer_module.inl"
2016-11-19 06:22:44 +01:00
POLYBAR_NS
2016-11-02 20:22:45 +01:00
namespace modules {
2016-11-20 23:04:31 +01:00
template class module<date_module>;
template class timer_module<date_module>;
2016-11-02 20:22:45 +01:00
void date_module::setup() {
2016-11-25 13:55:15 +01:00
if (!m_bar.locale.empty()) {
2016-11-02 20:22:45 +01:00
setlocale(LC_TIME, m_bar.locale.c_str());
2016-11-25 13:55:15 +01:00
}
2016-11-02 20:22:45 +01:00
m_interval = chrono::duration<double>(m_conf.get<float>(name(), "interval", 1));
m_formatter->add(DEFAULT_FORMAT, TAG_DATE, {TAG_DATE});
m_format = m_conf.get<string>(name(), "date");
m_formatalt = m_conf.get<string>(name(), "date-alt", "");
}
bool date_module::update() {
2016-11-25 13:55:15 +01:00
if (!m_formatter->has(TAG_DATE)) {
2016-11-02 20:22:45 +01:00
return false;
2016-11-25 13:55:15 +01:00
}
2016-11-02 20:22:45 +01:00
auto time = std::time(nullptr);
auto date_format = m_toggled ? m_formatalt : m_format;
char buffer[256] = {'\0'};
std::strftime(buffer, sizeof(buffer), date_format.c_str(), std::localtime(&time));
2016-11-25 13:55:15 +01:00
if (std::strncmp(buffer, m_buffer, sizeof(buffer)) == 0) {
2016-11-02 20:22:45 +01:00
return false;
2016-11-25 13:55:15 +01:00
} else {
2016-11-02 20:22:45 +01:00
std::memmove(m_buffer, buffer, sizeof(buffer));
2016-11-25 13:55:15 +01:00
}
2016-11-02 20:22:45 +01:00
return true;
}
2016-11-25 13:55:15 +01:00
bool date_module::build(builder* builder, const string& tag) const {
2016-11-02 20:22:45 +01:00
if (tag != TAG_DATE) {
return false;
}
2016-11-25 13:55:15 +01:00
if (!m_formatalt.empty()) {
2016-11-02 20:22:45 +01:00
m_builder->cmd(mousebtn::LEFT, EVENT_TOGGLE);
2016-11-25 13:55:15 +01:00
}
2016-11-02 20:22:45 +01:00
builder->node(m_buffer);
return true;
}
bool date_module::handle_event(string cmd) {
if (cmd == EVENT_TOGGLE) {
m_toggled = !m_toggled;
wakeup();
}
return cmd == EVENT_TOGGLE;
}
bool date_module::receive_events() const {
return true;
}
}
2016-11-19 06:22:44 +01:00
POLYBAR_NS_END