2018-08-08 23:54:58 +02:00
|
|
|
#include "modules/clock.hpp"
|
2020-05-22 18:52:26 +02:00
|
|
|
|
2022-01-08 07:25:15 +01:00
|
|
|
#include <fmt/chrono.h>
|
2022-07-13 07:20:49 +02:00
|
|
|
#include <spdlog/spdlog.h>
|
2020-05-22 18:52:26 +02:00
|
|
|
|
2022-01-08 07:25:15 +01:00
|
|
|
#include <ctime>
|
2022-07-13 07:20:49 +02:00
|
|
|
#include <iomanip>
|
2022-12-10 12:02:15 +01:00
|
|
|
#include <regex>
|
2022-12-10 14:48:22 +01:00
|
|
|
#include <sstream>
|
2020-02-05 20:02:42 +01:00
|
|
|
#include <type_traits>
|
2022-01-08 07:25:15 +01:00
|
|
|
|
2021-01-31 20:53:53 +01:00
|
|
|
#include "util/ustring_clen.hpp"
|
2020-02-04 01:58:18 +01:00
|
|
|
#ifdef HAVE_LANGINFO_1STDAY
|
|
|
|
#include <langinfo.h>
|
2020-02-05 20:02:42 +01:00
|
|
|
#include <locale.h>
|
2020-02-04 01:58:18 +01:00
|
|
|
#endif
|
2020-01-31 17:54:41 +01:00
|
|
|
|
2018-12-18 17:30:54 +01:00
|
|
|
waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config)
|
2022-11-24 12:28:52 +01:00
|
|
|
: ALabel(config, "clock", id, "{:%H:%M}", 60, false, false, true),
|
2021-10-03 18:48:21 +02:00
|
|
|
current_time_zone_idx_(0),
|
2021-10-05 11:46:52 +02:00
|
|
|
is_calendar_in_tooltip_(false),
|
2022-04-06 08:37:19 +02:00
|
|
|
is_timezoned_list_in_tooltip_(false) {
|
2021-10-03 05:27:54 +02:00
|
|
|
if (config_["timezones"].isArray() && !config_["timezones"].empty()) {
|
2022-04-06 08:37:19 +02:00
|
|
|
for (const auto& zone_name : config_["timezones"]) {
|
2021-10-03 18:48:21 +02:00
|
|
|
if (!zone_name.isString() || zone_name.asString().empty()) {
|
|
|
|
time_zones_.push_back(nullptr);
|
|
|
|
continue;
|
|
|
|
}
|
2022-04-06 08:37:19 +02:00
|
|
|
time_zones_.push_back(date::locate_zone(zone_name.asString()));
|
2021-10-03 18:48:21 +02:00
|
|
|
}
|
2021-10-05 12:20:06 +02:00
|
|
|
} else if (config_["timezone"].isString() && !config_["timezone"].asString().empty()) {
|
2022-04-06 08:37:19 +02:00
|
|
|
time_zones_.push_back(date::locate_zone(config_["timezone"].asString()));
|
2021-10-03 05:27:54 +02:00
|
|
|
}
|
2021-10-03 18:48:21 +02:00
|
|
|
|
2023-02-04 21:47:44 +01:00
|
|
|
// If all timezones are parsed and no one is good, add current time zone. nullptr in timezones
|
|
|
|
// vector means that local time should be shown
|
2021-10-05 12:20:06 +02:00
|
|
|
if (!time_zones_.size()) {
|
2023-02-04 21:47:44 +01:00
|
|
|
time_zones_.push_back(date::current_zone());
|
2021-10-05 12:20:06 +02:00
|
|
|
}
|
|
|
|
|
2022-04-06 08:37:19 +02:00
|
|
|
// Check if a particular placeholder is present in the tooltip format, to know what to calculate
|
|
|
|
// on update.
|
2021-10-03 18:48:21 +02:00
|
|
|
if (config_["tooltip-format"].isString()) {
|
2021-10-05 11:55:30 +02:00
|
|
|
std::string trimmed_format = config_["tooltip-format"].asString();
|
2022-04-06 08:37:19 +02:00
|
|
|
trimmed_format.erase(std::remove_if(trimmed_format.begin(), trimmed_format.end(),
|
|
|
|
[](unsigned char x) { return std::isspace(x); }),
|
|
|
|
trimmed_format.end());
|
2021-10-05 11:55:30 +02:00
|
|
|
if (trimmed_format.find("{" + kCalendarPlaceholder + "}") != std::string::npos) {
|
2021-10-03 18:48:21 +02:00
|
|
|
is_calendar_in_tooltip_ = true;
|
|
|
|
}
|
2021-10-05 11:46:52 +02:00
|
|
|
if (trimmed_format.find("{" + KTimezonedTimeListPlaceholder + "}") != std::string::npos) {
|
|
|
|
is_timezoned_list_in_tooltip_ = true;
|
|
|
|
}
|
2021-10-03 18:48:21 +02:00
|
|
|
}
|
|
|
|
|
2023-01-23 16:42:32 +01:00
|
|
|
// Calendar configuration
|
2022-08-06 12:55:20 +02:00
|
|
|
if (is_calendar_in_tooltip_) {
|
2023-01-23 16:42:32 +01:00
|
|
|
if (config_[kCalendarPlaceholder]["weeks-pos"].isString()) {
|
|
|
|
if (config_[kCalendarPlaceholder]["weeks-pos"].asString() == "left") {
|
|
|
|
cldWPos_ = WeeksSide::LEFT;
|
|
|
|
} else if (config_[kCalendarPlaceholder]["weeks-pos"].asString() == "right") {
|
|
|
|
cldWPos_ = WeeksSide::RIGHT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (config_[kCalendarPlaceholder]["format"]["months"].isString())
|
|
|
|
fmtMap_.insert({0, config_[kCalendarPlaceholder]["format"]["months"].asString()});
|
|
|
|
else
|
|
|
|
fmtMap_.insert({0, "{}"});
|
|
|
|
if (config_[kCalendarPlaceholder]["format"]["days"].isString())
|
|
|
|
fmtMap_.insert({2, config_[kCalendarPlaceholder]["format"]["days"].asString()});
|
|
|
|
else
|
|
|
|
fmtMap_.insert({2, "{}"});
|
2023-01-27 13:55:27 +01:00
|
|
|
if (config_[kCalendarPlaceholder]["format"]["weeks"].isString() &&
|
|
|
|
cldWPos_ != WeeksSide::HIDDEN) {
|
2023-01-23 16:42:32 +01:00
|
|
|
fmtMap_.insert(
|
|
|
|
{4, std::regex_replace(config_[kCalendarPlaceholder]["format"]["weeks"].asString(),
|
|
|
|
std::regex("\\{\\}"),
|
|
|
|
(first_day_of_week() == date::Monday) ? "{:%W}" : "{:%U}")});
|
2023-01-27 13:55:27 +01:00
|
|
|
Glib::ustring tmp{std::regex_replace(fmtMap_[4], std::regex("</?[^>]+>|\\{.*\\}"), "")};
|
|
|
|
cldWnLen_ += tmp.size();
|
2023-01-23 16:42:32 +01:00
|
|
|
} else {
|
|
|
|
if (cldWPos_ != WeeksSide::HIDDEN)
|
|
|
|
fmtMap_.insert({4, (first_day_of_week() == date::Monday) ? "{:%W}" : "{:%U}"});
|
2023-01-27 13:55:27 +01:00
|
|
|
else
|
|
|
|
cldWnLen_ = 0;
|
2023-01-23 16:42:32 +01:00
|
|
|
}
|
|
|
|
if (config_[kCalendarPlaceholder]["format"]["weekdays"].isString())
|
|
|
|
fmtMap_.insert({1, config_[kCalendarPlaceholder]["format"]["weekdays"].asString()});
|
|
|
|
else
|
|
|
|
fmtMap_.insert({1, "{}"});
|
|
|
|
if (config_[kCalendarPlaceholder]["format"]["today"].isString())
|
|
|
|
fmtMap_.insert({3, config_[kCalendarPlaceholder]["format"]["today"].asString()});
|
|
|
|
else
|
|
|
|
fmtMap_.insert({3, "{}"});
|
|
|
|
if (config_[kCalendarPlaceholder]["mode"].isString()) {
|
|
|
|
const std::string cfgMode{(config_[kCalendarPlaceholder]["mode"].isString())
|
|
|
|
? config_[kCalendarPlaceholder]["mode"].asString()
|
|
|
|
: "month"};
|
|
|
|
const std::map<std::string, const CldMode&> monthModes{{"month", CldMode::MONTH},
|
|
|
|
{"year", CldMode::YEAR}};
|
|
|
|
if (monthModes.find(cfgMode) != monthModes.end())
|
|
|
|
cldMode_ = monthModes.at(cfgMode);
|
|
|
|
else
|
|
|
|
spdlog::warn(
|
|
|
|
"Clock calendar configuration \"mode\"\"\" \"{0}\" is not recognized. Mode = \"month\" "
|
|
|
|
"is using instead",
|
|
|
|
cfgMode);
|
|
|
|
}
|
|
|
|
if (config_[kCalendarPlaceholder]["mode-mon-col"].isInt()) {
|
|
|
|
cldMonCols_ = config_[kCalendarPlaceholder]["mode-mon-col"].asInt();
|
|
|
|
if (cldMonCols_ == 0u || 12 % cldMonCols_ != 0u) {
|
|
|
|
cldMonCols_ = 3u;
|
|
|
|
spdlog::warn(
|
|
|
|
"Clock calendar configuration \"mode-mon-col\" = {0} must be one of [1, 2, 3, 4, 6, "
|
|
|
|
"12]. Value 3 is using instead",
|
|
|
|
cldMonCols_);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
cldMonCols_ = 1;
|
|
|
|
if (config_[kCalendarPlaceholder]["on-scroll"].isInt()) {
|
|
|
|
cldShift_ = date::months{config_[kCalendarPlaceholder]["on-scroll"].asInt()};
|
2022-12-27 19:23:16 +01:00
|
|
|
event_box_.add_events(Gdk::LEAVE_NOTIFY_MASK);
|
|
|
|
event_box_.signal_leave_notify_event().connect([this](GdkEventCrossing*) {
|
2023-01-23 16:42:32 +01:00
|
|
|
cldCurrShift_ = date::months{0};
|
2022-12-27 19:23:16 +01:00
|
|
|
return false;
|
|
|
|
});
|
2022-08-06 12:55:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-23 16:42:32 +01:00
|
|
|
if (config_["locale"].isString())
|
2022-05-13 21:58:00 +02:00
|
|
|
locale_ = std::locale(config_["locale"].asString());
|
2023-01-23 16:42:32 +01:00
|
|
|
else
|
2022-05-13 21:58:38 +02:00
|
|
|
locale_ = std::locale("");
|
2022-12-10 12:02:15 +01:00
|
|
|
|
2018-11-23 11:57:37 +01:00
|
|
|
thread_ = [this] {
|
2018-08-20 14:50:45 +02:00
|
|
|
dp.emit();
|
2022-03-31 17:14:29 +02:00
|
|
|
auto now = std::chrono::system_clock::now();
|
|
|
|
/* difference with projected wakeup time */
|
|
|
|
auto diff = now.time_since_epoch() % interval_;
|
|
|
|
/* sleep until the next projected time */
|
|
|
|
thread_.sleep_for(interval_ - diff);
|
2018-08-08 23:54:58 +02:00
|
|
|
};
|
2018-08-18 11:43:48 +02:00
|
|
|
}
|
2018-08-08 23:54:58 +02:00
|
|
|
|
2021-10-03 18:48:21 +02:00
|
|
|
const date::time_zone* waybar::modules::Clock::current_timezone() {
|
2022-04-06 08:37:19 +02:00
|
|
|
return time_zones_[current_time_zone_idx_] ? time_zones_[current_time_zone_idx_]
|
|
|
|
: date::current_zone();
|
2021-10-03 18:48:21 +02:00
|
|
|
}
|
2020-05-22 18:52:26 +02:00
|
|
|
|
2021-10-03 18:48:21 +02:00
|
|
|
bool waybar::modules::Clock::is_timezone_fixed() {
|
|
|
|
return time_zones_[current_time_zone_idx_] != nullptr;
|
|
|
|
}
|
2020-01-31 17:54:41 +01:00
|
|
|
|
2021-10-03 18:48:21 +02:00
|
|
|
auto waybar::modules::Clock::update() -> void {
|
2023-01-23 16:42:32 +01:00
|
|
|
const auto* time_zone = current_timezone();
|
2021-10-03 18:48:21 +02:00
|
|
|
auto now = std::chrono::system_clock::now();
|
2023-01-23 16:42:32 +01:00
|
|
|
auto ztime = date::zoned_time{time_zone, date::floor<std::chrono::seconds>(now)};
|
2022-12-27 19:23:16 +01:00
|
|
|
|
2023-01-23 16:42:32 +01:00
|
|
|
auto shifted_date = date::year_month_day{date::floor<date::days>(now)} + cldCurrShift_;
|
2022-12-27 19:23:16 +01:00
|
|
|
auto now_shifted = date::sys_days{shifted_date} + (now - date::floor<date::days>(now));
|
2023-01-23 16:42:32 +01:00
|
|
|
auto shifted_ztime = date::zoned_time{time_zone, date::floor<std::chrono::seconds>(now_shifted)};
|
2022-12-27 19:23:16 +01:00
|
|
|
|
2023-01-23 16:42:32 +01:00
|
|
|
std::string text{""};
|
2021-10-03 18:48:21 +02:00
|
|
|
if (!is_timezone_fixed()) {
|
2020-05-22 18:52:26 +02:00
|
|
|
// As date dep is not fully compatible, prefer fmt
|
|
|
|
tzset();
|
|
|
|
auto localtime = fmt::localtime(std::chrono::system_clock::to_time_t(now));
|
2023-01-23 16:42:32 +01:00
|
|
|
text = fmt::format(locale_, fmt::runtime(format_), localtime);
|
2020-05-22 18:52:26 +02:00
|
|
|
} else {
|
2023-01-23 16:42:32 +01:00
|
|
|
text = fmt::format(locale_, fmt::runtime(format_), ztime);
|
2020-05-22 18:52:26 +02:00
|
|
|
}
|
2022-11-24 12:28:52 +01:00
|
|
|
label_.set_markup(text);
|
2019-04-18 17:52:00 +02:00
|
|
|
|
2019-02-24 09:25:34 +01:00
|
|
|
if (tooltipEnabled()) {
|
|
|
|
if (config_["tooltip-format"].isString()) {
|
2022-08-06 12:55:20 +02:00
|
|
|
std::string calendar_lines{""};
|
|
|
|
std::string timezoned_time_lines{""};
|
2022-12-06 01:47:28 +01:00
|
|
|
if (is_calendar_in_tooltip_) {
|
2023-01-23 16:42:32 +01:00
|
|
|
calendar_lines = get_calendar(ztime, shifted_ztime);
|
2022-12-06 01:47:28 +01:00
|
|
|
}
|
|
|
|
if (is_timezoned_list_in_tooltip_) {
|
|
|
|
timezoned_time_lines = timezones_text(&now);
|
|
|
|
}
|
2021-10-03 18:48:21 +02:00
|
|
|
auto tooltip_format = config_["tooltip-format"].asString();
|
2023-01-23 16:42:32 +01:00
|
|
|
text = fmt::format(locale_, fmt::runtime(tooltip_format), shifted_ztime,
|
2022-12-27 19:23:16 +01:00
|
|
|
fmt::arg(kCalendarPlaceholder.c_str(), calendar_lines),
|
|
|
|
fmt::arg(KTimezonedTimeListPlaceholder.c_str(), timezoned_time_lines));
|
2022-11-24 12:28:52 +01:00
|
|
|
label_.set_tooltip_markup(text);
|
2019-02-24 09:25:34 +01:00
|
|
|
}
|
|
|
|
}
|
2021-10-03 18:48:21 +02:00
|
|
|
|
2020-04-12 18:30:21 +02:00
|
|
|
// Call parent update
|
2022-11-24 12:28:52 +01:00
|
|
|
ALabel::update();
|
2018-08-09 12:05:48 +02:00
|
|
|
}
|
2020-02-02 23:44:26 +01:00
|
|
|
|
2023-02-28 13:32:28 +01:00
|
|
|
auto waybar::modules::Clock::doAction(const std::string& name) -> void {
|
|
|
|
if ((actionMap_[name])) {
|
|
|
|
(this->*actionMap_[name])();
|
|
|
|
update();
|
2023-01-23 16:42:32 +01:00
|
|
|
} else
|
2023-02-28 13:32:28 +01:00
|
|
|
spdlog::error("Clock. Unsupported action \"{0}\"", name);
|
2020-08-13 04:46:51 +02:00
|
|
|
}
|
|
|
|
|
2023-01-23 16:42:32 +01:00
|
|
|
// The number of weeks in calendar month layout plus 1 more for calendar titles
|
|
|
|
unsigned cldRowsInMonth(date::year_month const ym, date::weekday const firstdow) {
|
|
|
|
using namespace date;
|
|
|
|
return static_cast<unsigned>(
|
|
|
|
ceil<weeks>((weekday{ym / 1} - firstdow) + ((ym / last).day() - day{0})).count()) +
|
|
|
|
2;
|
|
|
|
}
|
2022-08-06 12:55:20 +02:00
|
|
|
|
2023-01-23 16:42:32 +01:00
|
|
|
auto cldGetWeekForLine(date::year_month const ym, date::weekday const firstdow, unsigned const line)
|
|
|
|
-> const date::year_month_weekday {
|
|
|
|
unsigned index = line - 2;
|
|
|
|
auto sd = date::sys_days{ym / 1};
|
|
|
|
if (date::weekday{sd} == firstdow) ++index;
|
|
|
|
auto ymdw = ym / firstdow[index];
|
|
|
|
return ymdw;
|
|
|
|
}
|
2020-02-02 23:44:26 +01:00
|
|
|
|
2023-01-23 16:42:32 +01:00
|
|
|
auto getCalendarLine(date::year_month_day const currDate, date::year_month const ym,
|
2023-01-27 13:55:27 +01:00
|
|
|
unsigned const line, date::weekday const firstdow,
|
2023-01-23 16:42:32 +01:00
|
|
|
const std::locale* const locale_) -> std::string {
|
|
|
|
using namespace date::literals;
|
|
|
|
std::ostringstream res;
|
|
|
|
|
|
|
|
switch (line) {
|
|
|
|
case 0: {
|
|
|
|
// Output month and year title
|
2023-02-02 11:26:05 +01:00
|
|
|
res << date::format(*locale_, "%B %Y", ym);
|
2023-01-23 16:42:32 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 1: {
|
|
|
|
// Output weekday names title
|
|
|
|
auto wd{firstdow};
|
|
|
|
do {
|
|
|
|
Glib::ustring wd_ustring{date::format(*locale_, "%a", wd)};
|
|
|
|
auto clen{ustring_clen(wd_ustring)};
|
|
|
|
auto wd_len{wd_ustring.length()};
|
|
|
|
while (clen > 2) {
|
|
|
|
wd_ustring = wd_ustring.substr(0, wd_len - 1);
|
|
|
|
--wd_len;
|
|
|
|
clen = ustring_clen(wd_ustring);
|
|
|
|
}
|
|
|
|
const std::string pad(2 - clen, ' ');
|
|
|
|
|
|
|
|
if (wd != firstdow) res << ' ';
|
|
|
|
|
|
|
|
res << pad << wd_ustring;
|
|
|
|
} while (++wd != firstdow);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 2: {
|
|
|
|
// Output first week prefixed with spaces if necessary
|
|
|
|
auto wd = date::weekday{ym / 1};
|
|
|
|
res << std::string(static_cast<unsigned>((wd - firstdow).count()) * 3, ' ');
|
2020-01-31 17:54:41 +01:00
|
|
|
|
2023-01-23 16:42:32 +01:00
|
|
|
if (currDate.year() != ym.year() || currDate.month() != ym.month() || currDate != ym / 1_d)
|
|
|
|
res << date::format("%e", 1_d);
|
|
|
|
else
|
|
|
|
res << "{today}";
|
2022-04-10 11:54:50 +02:00
|
|
|
|
2023-01-23 16:42:32 +01:00
|
|
|
auto d = 2_d;
|
|
|
|
|
|
|
|
while (++wd != firstdow) {
|
|
|
|
if (currDate.year() != ym.year() || currDate.month() != ym.month() || currDate != ym / d)
|
|
|
|
res << date::format(" %e", d);
|
|
|
|
else
|
|
|
|
res << " {today}";
|
|
|
|
|
|
|
|
++d;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
// Output a non-first week:
|
|
|
|
auto ymdw{cldGetWeekForLine(ym, firstdow, line)};
|
|
|
|
if (ymdw.ok()) {
|
|
|
|
auto d = date::year_month_day{ymdw}.day();
|
|
|
|
auto const e = (ym / last).day();
|
|
|
|
auto wd = firstdow;
|
|
|
|
|
|
|
|
if (currDate.year() != ym.year() || currDate.month() != ym.month() || currDate != ym / d)
|
|
|
|
res << date::format("%e", d);
|
|
|
|
else
|
|
|
|
res << "{today}";
|
|
|
|
|
|
|
|
while (++wd != firstdow && ++d <= e) {
|
|
|
|
if (currDate.year() != ym.year() || currDate.month() != ym.month() || currDate != ym / d)
|
|
|
|
res << date::format(" %e", d);
|
|
|
|
else
|
|
|
|
res << " {today}";
|
|
|
|
}
|
|
|
|
// Append row with spaces if the week did not complete
|
|
|
|
res << std::string(static_cast<unsigned>((firstdow - wd).count()) * 3, ' ');
|
2023-01-27 13:55:27 +01:00
|
|
|
}
|
2023-01-23 16:42:32 +01:00
|
|
|
break;
|
2022-03-24 13:41:50 +01:00
|
|
|
}
|
|
|
|
}
|
2022-04-15 14:39:13 +02:00
|
|
|
|
2023-01-23 16:42:32 +01:00
|
|
|
return res.str();
|
|
|
|
}
|
2022-06-24 15:44:06 +02:00
|
|
|
|
2023-01-23 16:42:32 +01:00
|
|
|
auto waybar::modules::Clock::get_calendar(const date::zoned_seconds& now,
|
|
|
|
const date::zoned_seconds& wtime) -> std::string {
|
|
|
|
auto daypoint = date::floor<date::days>(wtime.get_local_time());
|
|
|
|
const auto ymd{date::year_month_day{daypoint}};
|
|
|
|
const auto ym{ymd.year() / ymd.month()};
|
|
|
|
const auto y{ymd.year()};
|
|
|
|
const auto firstdow = first_day_of_week();
|
|
|
|
const auto maxRows{12 / cldMonCols_};
|
|
|
|
std::ostringstream os;
|
|
|
|
std::ostringstream tmp;
|
|
|
|
// get currdate
|
|
|
|
daypoint = date::floor<date::days>(now.get_local_time());
|
|
|
|
const auto currDate{date::year_month_day{daypoint}};
|
|
|
|
|
|
|
|
if (cldMode_ == CldMode::YEAR) {
|
|
|
|
if (y / date::month{1} / 1 == cldYearShift_)
|
|
|
|
return cldYearCached_;
|
|
|
|
else
|
|
|
|
cldYearShift_ = y / date::month{1} / 1;
|
2022-03-25 14:12:11 +01:00
|
|
|
}
|
2023-01-23 16:42:32 +01:00
|
|
|
if (cldMode_ == CldMode::MONTH) {
|
|
|
|
if (ym == cldMonShift_)
|
|
|
|
return cldMonCached_;
|
|
|
|
else
|
|
|
|
cldMonShift_ = ym;
|
2020-01-31 17:54:41 +01:00
|
|
|
}
|
2022-12-10 12:02:15 +01:00
|
|
|
|
2023-01-23 16:42:32 +01:00
|
|
|
// Compute number of lines needed for each calendar month
|
|
|
|
unsigned ml[12]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
|
2022-12-10 12:02:15 +01:00
|
|
|
|
2023-01-23 16:42:32 +01:00
|
|
|
for (auto& m : ml) {
|
|
|
|
if (cldMode_ == CldMode::YEAR || m == static_cast<unsigned>(ymd.month()))
|
|
|
|
m = cldRowsInMonth(y / date::month{m}, firstdow);
|
|
|
|
else
|
|
|
|
m = 0u;
|
|
|
|
}
|
|
|
|
for (auto row{0u}; row < maxRows; ++row) {
|
|
|
|
const auto lines = *std::max_element(std::begin(ml) + (row * cldMonCols_),
|
|
|
|
std::begin(ml) + ((row + 1) * cldMonCols_));
|
|
|
|
for (auto line{0u}; line < lines; ++line) {
|
|
|
|
for (auto col{0u}; col < cldMonCols_; ++col) {
|
|
|
|
const auto mon{date::month{row * cldMonCols_ + col + 1}};
|
|
|
|
if (cldMode_ == CldMode::YEAR || y / mon == ym) {
|
|
|
|
date::year_month ymTmp{y / mon};
|
|
|
|
if (col != 0 && cldMode_ == CldMode::YEAR) os << " ";
|
|
|
|
|
|
|
|
// Week numbers on the left
|
|
|
|
if (cldWPos_ == WeeksSide::LEFT && line > 0) {
|
2023-01-27 13:55:27 +01:00
|
|
|
if (line > 1) {
|
|
|
|
if (line < ml[static_cast<unsigned>(ymTmp.month()) - 1u])
|
|
|
|
os << fmt::format(fmt::runtime(fmtMap_[4]),
|
|
|
|
(line == 2)
|
|
|
|
? date::sys_days{ymTmp / 1}
|
|
|
|
: date::sys_days{cldGetWeekForLine(ymTmp, firstdow, line)})
|
|
|
|
<< ' ';
|
|
|
|
else
|
|
|
|
os << std::string(cldWnLen_, ' ');
|
|
|
|
}
|
2023-01-23 16:42:32 +01:00
|
|
|
}
|
|
|
|
|
2023-01-27 13:55:27 +01:00
|
|
|
os << fmt::format(
|
2023-02-02 11:26:05 +01:00
|
|
|
fmt::runtime((cldWPos_ != WeeksSide::LEFT || line == 0) ? "{:<{}}" : "{:>{}}"),
|
2023-01-27 13:55:27 +01:00
|
|
|
getCalendarLine(currDate, ymTmp, line, firstdow, &locale_),
|
|
|
|
(cldMonColLen_ + ((line < 2) ? cldWnLen_ : 0)));
|
2023-01-23 16:42:32 +01:00
|
|
|
|
|
|
|
// Week numbers on the right
|
|
|
|
if (cldWPos_ == WeeksSide ::RIGHT && line > 0) {
|
2023-01-27 13:55:27 +01:00
|
|
|
if (line > 1) {
|
|
|
|
if (line < ml[static_cast<unsigned>(ymTmp.month()) - 1u])
|
|
|
|
os << ' '
|
|
|
|
<< fmt::format(fmt::runtime(fmtMap_[4]),
|
|
|
|
(line == 2)
|
|
|
|
? date::sys_days{ymTmp / 1}
|
|
|
|
: date::sys_days{cldGetWeekForLine(ymTmp, firstdow, line)});
|
|
|
|
else
|
|
|
|
os << std::string(cldWnLen_, ' ');
|
|
|
|
}
|
2023-01-23 16:42:32 +01:00
|
|
|
}
|
|
|
|
}
|
2022-03-24 13:41:50 +01:00
|
|
|
}
|
|
|
|
|
2023-01-23 16:42:32 +01:00
|
|
|
// Apply user formats to calendar
|
|
|
|
if (line < 2)
|
|
|
|
tmp << fmt::format(fmt::runtime(fmtMap_[line]), os.str());
|
|
|
|
else
|
|
|
|
tmp << os.str();
|
|
|
|
// Clear ostringstream
|
|
|
|
std::ostringstream().swap(os);
|
|
|
|
if (line + 1u != lines || (row + 1u != maxRows && cldMode_ == CldMode::YEAR)) tmp << '\n';
|
2020-02-02 23:55:37 +01:00
|
|
|
}
|
2023-01-23 16:42:32 +01:00
|
|
|
if (row + 1u != maxRows && cldMode_ == CldMode::YEAR) tmp << '\n';
|
|
|
|
}
|
2022-12-10 12:02:15 +01:00
|
|
|
|
2023-01-23 16:42:32 +01:00
|
|
|
os << fmt::format( // Apply days format
|
|
|
|
fmt::runtime(fmt::format(fmt::runtime(fmtMap_[2]), tmp.str())),
|
|
|
|
// Apply today format
|
|
|
|
fmt::arg("today", fmt::format(fmt::runtime(fmtMap_[3]), date::format("%e", ymd.day()))));
|
2022-12-10 12:02:15 +01:00
|
|
|
|
2023-01-23 16:42:32 +01:00
|
|
|
if (cldMode_ == CldMode::YEAR)
|
|
|
|
cldYearCached_ = os.str();
|
|
|
|
else
|
|
|
|
cldMonCached_ = os.str();
|
2020-01-31 17:54:41 +01:00
|
|
|
|
2023-01-23 16:42:32 +01:00
|
|
|
return os.str();
|
2020-01-31 17:54:41 +01:00
|
|
|
}
|
|
|
|
|
2023-02-28 13:32:28 +01:00
|
|
|
/*Clock actions*/
|
2023-01-23 16:42:32 +01:00
|
|
|
void waybar::modules::Clock::cldModeSwitch() {
|
|
|
|
cldMode_ = (cldMode_ == CldMode::YEAR) ? CldMode::MONTH : CldMode::YEAR;
|
2020-01-31 17:54:41 +01:00
|
|
|
}
|
2023-02-28 13:32:28 +01:00
|
|
|
void waybar::modules::Clock::cldShift_up() {
|
|
|
|
cldCurrShift_ += ((cldMode_ == CldMode::YEAR) ? 12 : 1) * cldShift_;
|
|
|
|
}
|
|
|
|
void waybar::modules::Clock::cldShift_down() {
|
|
|
|
cldCurrShift_ -= ((cldMode_ == CldMode::YEAR) ? 12 : 1) * cldShift_;
|
|
|
|
}
|
|
|
|
void waybar::modules::Clock::tz_up() {
|
|
|
|
auto nr_zones = time_zones_.size();
|
|
|
|
|
|
|
|
if (nr_zones == 1) return;
|
|
|
|
|
|
|
|
size_t new_idx = current_time_zone_idx_ + 1;
|
|
|
|
current_time_zone_idx_ = new_idx == nr_zones ? 0 : new_idx;
|
|
|
|
}
|
|
|
|
void waybar::modules::Clock::tz_down() {
|
|
|
|
auto nr_zones = time_zones_.size();
|
|
|
|
|
|
|
|
if (nr_zones == 1) return;
|
|
|
|
|
|
|
|
current_time_zone_idx_ = current_time_zone_idx_ == 0 ? nr_zones - 1 : current_time_zone_idx_ - 1;
|
|
|
|
}
|
2018-08-08 23:54:58 +02:00
|
|
|
|
2022-04-06 08:37:19 +02:00
|
|
|
auto waybar::modules::Clock::timezones_text(std::chrono::system_clock::time_point* now)
|
|
|
|
-> std::string {
|
2021-10-05 11:46:52 +02:00
|
|
|
if (time_zones_.size() == 1) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
std::stringstream os;
|
|
|
|
for (size_t time_zone_idx = 0; time_zone_idx < time_zones_.size(); ++time_zone_idx) {
|
|
|
|
if (static_cast<int>(time_zone_idx) == current_time_zone_idx_) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const date::time_zone* timezone = time_zones_[time_zone_idx];
|
|
|
|
if (!timezone) {
|
|
|
|
timezone = date::current_zone();
|
|
|
|
}
|
2023-01-23 16:42:32 +01:00
|
|
|
auto ztime = date::zoned_time{timezone, date::floor<std::chrono::seconds>(*now)};
|
|
|
|
os << fmt::format(locale_, fmt::runtime(format_), ztime) << '\n';
|
2021-10-05 11:46:52 +02:00
|
|
|
}
|
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
|
2020-02-05 20:02:42 +01:00
|
|
|
#ifdef HAVE_LANGINFO_1STDAY
|
|
|
|
template <auto fn>
|
|
|
|
using deleter_from_fn = std::integral_constant<decltype(fn), fn>;
|
2020-01-21 23:48:16 +01:00
|
|
|
|
2020-02-05 20:02:42 +01:00
|
|
|
template <typename T, auto fn>
|
|
|
|
using deleting_unique_ptr = std::unique_ptr<T, deleter_from_fn<fn>>;
|
|
|
|
#endif
|
2019-04-18 17:52:00 +02:00
|
|
|
|
2020-02-05 20:02:42 +01:00
|
|
|
// Computations done similarly to Linux cal utility.
|
|
|
|
auto waybar::modules::Clock::first_day_of_week() -> date::weekday {
|
|
|
|
#ifdef HAVE_LANGINFO_1STDAY
|
2020-05-22 18:52:26 +02:00
|
|
|
deleting_unique_ptr<std::remove_pointer<locale_t>::type, freelocale> posix_locale{
|
|
|
|
newlocale(LC_ALL, locale_.name().c_str(), nullptr)};
|
2020-02-05 20:02:42 +01:00
|
|
|
if (posix_locale) {
|
2020-05-22 18:52:26 +02:00
|
|
|
const int i = (std::intptr_t)nl_langinfo_l(_NL_TIME_WEEK_1STDAY, posix_locale.get());
|
2022-04-06 08:37:19 +02:00
|
|
|
auto ymd = date::year(i / 10000) / (i / 100 % 100) / (i % 100);
|
|
|
|
auto wd = date::weekday(ymd);
|
|
|
|
uint8_t j = *nl_langinfo_l(_NL_TIME_FIRST_WEEKDAY, posix_locale.get());
|
2020-02-05 20:02:42 +01:00
|
|
|
return wd + date::days(j - 1);
|
2019-02-24 09:25:34 +01:00
|
|
|
}
|
2020-02-05 20:02:42 +01:00
|
|
|
#endif
|
|
|
|
return date::Sunday;
|
2018-08-09 12:05:48 +02:00
|
|
|
}
|