2016-05-30 23:58:58 -04:00
|
|
|
#pragma once
|
2016-05-19 10:41:06 -04:00
|
|
|
|
2016-06-14 23:32:35 -04:00
|
|
|
#include "modules/meta.hpp"
|
2016-05-19 10:41:06 -04:00
|
|
|
|
2016-06-14 23:32:35 -04:00
|
|
|
LEMONBUDDY_NS
|
2016-05-19 10:41:06 -04:00
|
|
|
|
2016-06-14 23:32:35 -04:00
|
|
|
namespace modules {
|
|
|
|
class date_module : public timer_module<date_module> {
|
|
|
|
public:
|
|
|
|
using timer_module::timer_module;
|
|
|
|
|
|
|
|
void setup() {
|
2016-10-12 10:48:14 -04:00
|
|
|
if (!m_bar.locale.empty())
|
|
|
|
setlocale(LC_TIME, m_bar.locale.c_str());
|
|
|
|
|
2016-06-14 23:32:35 -04: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");
|
2016-10-11 23:19:29 -04:00
|
|
|
m_formatalt = m_conf.get<string>(name(), "date-alt", "");
|
2016-06-14 23:32:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool update() {
|
|
|
|
if (!m_formatter->has(TAG_DATE))
|
|
|
|
return false;
|
2016-05-19 10:41:06 -04:00
|
|
|
|
2016-06-14 23:32:35 -04:00
|
|
|
auto time = std::time(nullptr);
|
2016-10-11 23:19:29 -04:00
|
|
|
auto date_format = m_toggled ? m_formatalt : m_format;
|
2016-06-14 23:32:35 -04:00
|
|
|
char buffer[256] = {'\0'};
|
2016-05-19 10:41:06 -04:00
|
|
|
|
2016-06-14 23:32:35 -04:00
|
|
|
std::strftime(buffer, sizeof(buffer), date_format.c_str(), std::localtime(&time));
|
2016-05-19 10:41:06 -04:00
|
|
|
|
2016-06-14 23:32:35 -04:00
|
|
|
if (std::strncmp(buffer, m_buffer, sizeof(buffer)) == 0)
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
std::memmove(m_buffer, buffer, sizeof(buffer));
|
2016-05-19 10:41:06 -04:00
|
|
|
|
2016-06-14 23:32:35 -04:00
|
|
|
return true;
|
|
|
|
}
|
2016-05-19 10:41:06 -04:00
|
|
|
|
2016-06-14 23:32:35 -04:00
|
|
|
string get_output() {
|
2016-10-11 23:19:29 -04:00
|
|
|
if (!m_formatalt.empty())
|
2016-06-14 23:32:35 -04:00
|
|
|
m_builder->cmd(mousebtn::LEFT, EVENT_TOGGLE);
|
|
|
|
m_builder->node(timer_module::get_output());
|
|
|
|
return m_builder->flush();
|
|
|
|
}
|
2016-05-19 10:41:06 -04:00
|
|
|
|
2016-10-18 19:26:17 -04:00
|
|
|
bool build(builder* builder, string tag) const {
|
2016-06-14 23:32:35 -04:00
|
|
|
if (tag == TAG_DATE)
|
|
|
|
builder->node(m_buffer);
|
|
|
|
return tag == TAG_DATE;
|
|
|
|
}
|
2016-06-29 05:06:33 -04:00
|
|
|
|
2016-10-10 12:05:58 -04:00
|
|
|
bool handle_event(string cmd) {
|
|
|
|
if (cmd == EVENT_TOGGLE) {
|
2016-10-11 23:19:29 -04:00
|
|
|
m_toggled = !m_toggled;
|
2016-10-10 12:05:58 -04:00
|
|
|
wakeup();
|
|
|
|
}
|
|
|
|
return cmd == EVENT_TOGGLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool receive_events() const {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-14 23:32:35 -04:00
|
|
|
private:
|
|
|
|
static constexpr auto TAG_DATE = "<date>";
|
|
|
|
|
|
|
|
static constexpr auto EVENT_TOGGLE = "datetoggle";
|
|
|
|
|
|
|
|
string m_format;
|
2016-10-11 23:19:29 -04:00
|
|
|
string m_formatalt;
|
2016-06-14 23:32:35 -04:00
|
|
|
|
|
|
|
char m_buffer[256] = {'\0'};
|
2016-10-11 23:19:29 -04:00
|
|
|
stateflag m_toggled{false};
|
2016-05-19 10:41:06 -04:00
|
|
|
};
|
|
|
|
}
|
2016-06-14 23:32:35 -04:00
|
|
|
|
|
|
|
LEMONBUDDY_NS_END
|