c++20. clock chrono API. STL + format

Signed-off-by: Viktar Lukashonak <myxabeer@gmail.com>
This commit is contained in:
Viktar Lukashonak
2023-11-10 17:57:26 +03:00
parent c4330a1e36
commit 86a38980e4
8 changed files with 379 additions and 374 deletions

View File

@ -10,6 +10,7 @@ AModule::AModule(const Json::Value& config, const std::string& name, const std::
bool enable_click, bool enable_scroll)
: name_(std::move(name)),
config_(std::move(config)),
isTooltip{config_["tooltip"].isBool() ? config_["tooltip"].asBool() : true},
distance_scrolled_y_(0.0),
distance_scrolled_x_(0.0) {
// Configure module action Map
@ -189,9 +190,7 @@ bool AModule::handleScroll(GdkEventScroll* e) {
return true;
}
bool AModule::tooltipEnabled() {
return config_["tooltip"].isBool() ? config_["tooltip"].asBool() : true;
}
bool AModule::tooltipEnabled() { return isTooltip; }
AModule::operator Gtk::Widget&() { return event_box_; }

View File

@ -1,325 +1,286 @@
#include "modules/clock.hpp"
#include <fmt/chrono.h>
#include <spdlog/spdlog.h>
#include <ctime>
#include <iomanip>
#include <regex>
#include <sstream>
#include <type_traits>
#include "util/ustring_clen.hpp"
#ifdef HAVE_LANGINFO_1STDAY
#include <langinfo.h>
#include <locale.h>
#endif
namespace fmt_lib = waybar::util::date::format;
waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config)
: ALabel(config, "clock", id, "{:%H:%M}", 60, false, false, true),
current_time_zone_idx_{0},
is_calendar_in_tooltip_{false},
is_timezoned_list_in_tooltip_{false} {
locale_{std::locale(config_["locale"].isString() ? config_["locale"].asString() : "")},
tlpFmt_{(config_["tooltip-format"].isString()) ? config_["tooltip-format"].asString() : ""},
cldInTooltip_{tlpFmt_.find("{" + kCldPlaceholder + "}") != std::string::npos},
tzInTooltip_{tlpFmt_.find("{" + kTZPlaceholder + "}") != std::string::npos},
tzCurrIdx_{0} {
tlpText_ = tlpFmt_;
if (config_["timezones"].isArray() && !config_["timezones"].empty()) {
for (const auto& zone_name : config_["timezones"]) {
if (!zone_name.isString()) continue;
if (zone_name.asString().empty())
// local time should be shown
time_zones_.push_back(date::current_zone());
tzList_.push_back(current_zone());
else
try {
time_zones_.push_back(date::locate_zone(zone_name.asString()));
tzList_.push_back(locate_zone(zone_name.asString()));
} catch (const std::exception& e) {
spdlog::warn("Timezone: {0}. {1}", zone_name.asString(), e.what());
}
}
} else if (config_["timezone"].isString()) {
if (config_["timezone"].asString().empty())
time_zones_.push_back(date::current_zone());
// local time should be shown
tzList_.push_back(current_zone());
else
try {
time_zones_.push_back(date::locate_zone(config_["timezone"].asString()));
tzList_.push_back(locate_zone(config_["timezone"].asString()));
} catch (const std::exception& e) {
spdlog::warn("Timezone: {0}. {1}", config_["timezone"].asString(), e.what());
}
}
if (!tzList_.size()) tzList_.push_back(current_zone());
// If all timezones are parsed and no one is good
if (!time_zones_.size()) {
time_zones_.push_back(date::current_zone());
}
// Check if a particular placeholder is present in the tooltip format, to know what to calculate
// on update.
if (config_["tooltip-format"].isString()) {
std::string trimmed_format{config_["tooltip-format"].asString()};
fmtMap_.insert({5, trimmed_format});
trimmed_format.erase(std::remove_if(trimmed_format.begin(), trimmed_format.end(),
[](unsigned char x) { return std::isspace(x); }),
trimmed_format.end());
if (trimmed_format.find("{" + kCalendarPlaceholder + "}") != std::string::npos) {
is_calendar_in_tooltip_ = true;
}
if (trimmed_format.find("{" + KTimezonedTimeListPlaceholder + "}") != std::string::npos) {
is_timezoned_list_in_tooltip_ = true;
}
}
// Calendar configuration
if (is_calendar_in_tooltip_) {
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, "{}"});
if (config_[kCalendarPlaceholder]["format"]["weeks"].isString() &&
cldWPos_ != WeeksSide::HIDDEN) {
fmtMap_.insert(
{4, std::regex_replace(config_[kCalendarPlaceholder]["format"]["weeks"].asString(),
std::regex("\\{\\}"),
(first_day_of_week() == date::Monday) ? "{:%W}" : "{:%U}")});
Glib::ustring tmp{std::regex_replace(fmtMap_[4], std::regex("</?[^>]+>|\\{.*\\}"), "")};
cldWnLen_ += tmp.size();
} else {
if (cldWPos_ != WeeksSide::HIDDEN)
fmtMap_.insert({4, (first_day_of_week() == date::Monday) ? "{:%W}" : "{:%U}"});
else
cldWnLen_ = 0;
}
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()});
cldBaseDay_ =
date::year_month_day{date::floor<date::days>(std::chrono::system_clock::now())}.day();
} 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}};
// Calendar properties
if (cldInTooltip_) {
if (config_[kCldPlaceholder]["mode"].isString()) {
const std::string cfgMode{config_[kCldPlaceholder]["mode"].asString()};
const std::map<std::string_view, 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",
"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;
if (config_[kCldPlaceholder]["weeks-pos"].isString()) {
if (config_[kCldPlaceholder]["weeks-pos"].asString() == "left") cldWPos_ = WS::LEFT;
if (config_[kCldPlaceholder]["weeks-pos"].asString() == "right") cldWPos_ = WS::RIGHT;
}
if (config_[kCldPlaceholder]["format"]["months"].isString())
fmtMap_.insert({0, config_[kCldPlaceholder]["format"]["months"].asString()});
else
fmtMap_.insert({0, "{}"});
if (config_[kCldPlaceholder]["format"]["weekdays"].isString())
fmtMap_.insert({1, config_[kCldPlaceholder]["format"]["weekdays"].asString()});
else
fmtMap_.insert({1, "{}"});
if (config_[kCldPlaceholder]["format"]["days"].isString())
fmtMap_.insert({2, config_[kCldPlaceholder]["format"]["days"].asString()});
else
fmtMap_.insert({2, "{}"});
if (config_[kCldPlaceholder]["format"]["today"].isString()) {
fmtMap_.insert({3, config_[kCldPlaceholder]["format"]["today"].asString()});
cldBaseDay_ =
year_month_day{
floor<days>(zoned_time{current_zone(), system_clock::now()}.get_local_time())}
.day();
} else
fmtMap_.insert({3, "{}"});
if (config_[kCldPlaceholder]["format"]["weeks"].isString() && cldWPos_ != WS::HIDDEN) {
fmtMap_.insert({4, std::regex_replace(config_[kCldPlaceholder]["format"]["weeks"].asString(),
std::regex("\\{\\}"),
(first_day_of_week() == Monday) ? "{:%W}" : "{:%U}")});
Glib::ustring tmp{std::regex_replace(fmtMap_[4], std::regex("</?[^>]+>|\\{.*\\}"), "")};
cldWnLen_ += tmp.size();
} else {
if (cldWPos_ != WS::HIDDEN)
fmtMap_.insert({4, (first_day_of_week() == Monday) ? "{:%W}" : "{:%U}"});
else
cldWnLen_ = 0;
}
if (config_[kCldPlaceholder]["mode-mon-col"].isInt()) {
cldMonCols_ = config_[kCldPlaceholder]["mode-mon-col"].asInt();
if (cldMonCols_ == 0u || (12 % cldMonCols_) != 0u) {
spdlog::warn(
"Clock calendar configuration \"mode-mon-col\" = {0} must be one of [1, 2, 3, 4, 6, "
"12]. Value 3 is using instead",
"Clock calendar configuration mode-mon-col = {0} must be one of [1, 2, 3, 4, 6, 12]. "
"Value 3 is using instead",
cldMonCols_);
cldMonCols_ = 3u;
}
} else
cldMonCols_ = 1;
if (config_[kCalendarPlaceholder]["on-scroll"].isInt()) {
cldShift_ = date::months{config_[kCalendarPlaceholder]["on-scroll"].asInt()};
if (config_[kCldPlaceholder]["on-scroll"].isInt()) {
event_box_.add_events(Gdk::LEAVE_NOTIFY_MASK);
event_box_.signal_leave_notify_event().connect([this](GdkEventCrossing*) {
cldCurrShift_ = date::months{0};
cldCurrShift_ = months{0};
return false;
});
}
}
if (config_["locale"].isString())
locale_ = std::locale(config_["locale"].asString());
else
locale_ = std::locale("");
thread_ = [this] {
dp.emit();
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);
thread_.sleep_for(interval_ - system_clock::now().time_since_epoch() % interval_);
};
}
const date::time_zone* waybar::modules::Clock::current_timezone() {
return time_zones_[current_time_zone_idx_];
}
auto waybar::modules::Clock::update() -> void {
const auto* tz{current_timezone()};
const date::zoned_time now{
tz,
date::floor<std::chrono::seconds>(
std::chrono::system_clock::now())}; // Define local time is based on provided time zone
const date::year_month_day today{
date::floor<date::days>(now.get_local_time())}; // Convert now to year_month_day
const date::year_month_day shiftedDay{today + cldCurrShift_}; // Shift today
// Define shift local time
const auto shiftedNow{date::make_zoned(
tz, date::local_days(shiftedDay) +
(now.get_sys_time() - date::floor<date::days>(now.get_sys_time())))};
auto tz{tzList_[tzCurrIdx_]};
const zoned_time now{tz, floor<seconds>(system_clock::now())};
label_.set_markup(fmt::format(locale_, fmt::runtime(format_), now));
label_.set_markup(fmt_lib::vformat(locale_, format_, fmt_lib::make_format_args(now)));
if (tooltipEnabled()) {
const std::string tz_text{(is_timezoned_list_in_tooltip_) ? timezones_text(now.get_sys_time())
: ""};
const std::string cld_text{(is_calendar_in_tooltip_) ? get_calendar(today, shiftedDay, tz)
: ""};
const year_month_day today{floor<days>(now.get_local_time())};
const auto shiftedDay{today + cldCurrShift_};
const zoned_time shiftedNow{
tz, local_days(shiftedDay) + (now.get_local_time() - floor<days>(now.get_local_time()))};
const std::string text{fmt::format(locale_, fmt::runtime(fmtMap_[5]), shiftedNow,
fmt::arg(KTimezonedTimeListPlaceholder.c_str(), tz_text),
fmt::arg(kCalendarPlaceholder.c_str(), cld_text))};
label_.set_tooltip_markup(text);
if (tzInTooltip_) tzText_ = getTZtext(now.get_sys_time());
if (cldInTooltip_) cldText_ = get_calendar(today, shiftedDay, tz);
if (tzInTooltip_ || cldInTooltip_) {
// std::vformat doesn't support named arguments.
tlpText_ = std::regex_replace(tlpFmt_, std::regex("\\{" + kTZPlaceholder + "\\}"), tzText_);
tlpText_ =
std::regex_replace(tlpText_, std::regex("\\{" + kCldPlaceholder + "\\}"), cldText_);
}
tlpText_ = fmt_lib::vformat(locale_, tlpText_, fmt_lib::make_format_args(shiftedNow));
label_.set_tooltip_markup(tlpText_);
}
// Call parent update
ALabel::update();
}
auto waybar::modules::Clock::doAction(const std::string& name) -> void {
if ((actionMap_[name])) {
(this->*actionMap_[name])();
update();
} else
spdlog::error("Clock. Unsupported action \"{0}\"", name);
auto waybar::modules::Clock::getTZtext(sys_seconds now) -> std::string {
if (tzList_.size() == 1) return "";
std::stringstream os;
for (size_t tz_idx{0}; tz_idx < tzList_.size(); ++tz_idx) {
if (static_cast<int>(tz_idx) == tzCurrIdx_) continue;
auto zt{zoned_time{tzList_[tz_idx], now}};
os << fmt_lib::vformat(locale_, format_, fmt_lib::make_format_args(zt)) << '\n';
}
return os.str();
}
// The number of weeks in calendar month layout plus 1 more for calendar titles
const unsigned cldRowsInMonth(const date::year_month& ym, const date::weekday& firstdow) {
using namespace date;
return static_cast<unsigned>(
ceil<weeks>((weekday{ym / 1} - firstdow) + ((ym / last).day() - day{0})).count()) +
2;
const unsigned cldRowsInMonth(const year_month& ym, const weekday& firstdow) {
return 2u + ceil<weeks>((weekday{ym / 1} - firstdow) + ((ym / last).day() - day{0})).count();
}
auto cldGetWeekForLine(const date::year_month& ym, const date::weekday& 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;
auto cldGetWeekForLine(const year_month& ym, const weekday& firstdow, const unsigned line)
-> const year_month_weekday {
unsigned index{line - 2};
if (weekday{ym / 1} == firstdow) ++index;
return ym / firstdow[index];
}
auto getCalendarLine(const date::year_month_day& currDate, const date::year_month ym,
const unsigned line, const date::weekday& firstdow,
const std::locale* const locale_) -> std::string {
using namespace date::literals;
std::ostringstream res;
auto getCalendarLine(const year_month_day& currDate, const year_month ym, const unsigned line,
const weekday& firstdow, const std::locale* const locale_) -> std::string {
std::ostringstream os;
switch (line) {
// Print month and year title
case 0: {
// Output month and year title
res << date::format(*locale_, "%B %Y", ym);
os << date::format(*locale_, "{:L%B %Y}", ym);
break;
}
// Print weekday names title
case 1: {
// Output weekday names title
auto wd{firstdow};
Glib::ustring wdStr;
Glib::ustring::size_type wdLen{0};
int clen{0};
do {
Glib::ustring wd_ustring{date::format(*locale_, "%a", wd)};
auto clen{ustring_clen(wd_ustring)};
auto wd_len{wd_ustring.length()};
wdStr = date::format(*locale_, "{:L%a}", wd);
clen = ustring_clen(wdStr);
wdLen = wdStr.length();
while (clen > 2) {
wd_ustring = wd_ustring.substr(0, wd_len - 1);
--wd_len;
clen = ustring_clen(wd_ustring);
wdStr = wdStr.substr(0, wdLen - 1);
--wdLen;
clen = ustring_clen(wdStr);
}
const std::string pad(2 - clen, ' ');
if (wd != firstdow) res << ' ';
if (wd != firstdow) os << ' ';
res << pad << wd_ustring;
os << pad << wdStr;
} while (++wd != firstdow);
break;
}
// Print first week prefixed with spaces if necessary
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, ' ');
auto wd{weekday{ym / 1}};
os << std::string((wd - firstdow).count() * 3, ' ');
if (currDate.year() != ym.year() || currDate.month() != ym.month() || currDate != ym / 1_d)
res << date::format("%e", 1_d);
if (currDate != ym / 1d)
os << date::format(*locale_, "{:L%e}", 1d);
else
res << "{today}";
auto d = 2_d;
os << "{today}";
auto d{2d};
while (++wd != firstdow) {
if (currDate.year() != ym.year() || currDate.month() != ym.month() || currDate != ym / d)
res << date::format(" %e", d);
if (currDate != ym / d)
os << date::format(*locale_, " {:L%e}", d);
else
res << " {today}";
os << " {today}";
++d;
}
break;
}
// Print non-first week
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;
auto ymdTmp{cldGetWeekForLine(ym, firstdow, line)};
if (ymdTmp.ok()) {
auto d{year_month_day{ymdTmp}.day()};
const auto dlast{(ym / last).day()};
auto wd{firstdow};
if (currDate.year() != ym.year() || currDate.month() != ym.month() || currDate != ym / d)
res << date::format("%e", d);
if (currDate != ym / d)
os << date::format(*locale_, "{:L%e}", d);
else
res << "{today}";
os << "{today}";
while (++wd != firstdow && ++d <= e) {
if (currDate.year() != ym.year() || currDate.month() != ym.month() || currDate != ym / d)
res << date::format(" %e", d);
while (++wd != firstdow && ++d <= dlast) {
if (currDate != ym / d)
os << date::format(*locale_, " {:L%e}", d);
else
res << " {today}";
os << " {today}";
}
// Append row with spaces if the week did not complete
res << std::string(static_cast<unsigned>((firstdow - wd).count()) * 3, ' ');
// Append row with spaces if the week was not completed
os << std::string((firstdow - wd).count() * 3, ' ');
}
break;
}
}
return res.str();
return os.str();
}
auto waybar::modules::Clock::get_calendar(const date::year_month_day& today,
const date::year_month_day& ymd,
const date::time_zone* tz) -> const std::string {
auto waybar::modules::Clock::get_calendar(const year_month_day& today, const year_month_day& ymd,
const time_zone* tz) -> const std::string {
const auto firstdow{first_day_of_week()};
const auto maxRows{12 / cldMonCols_};
const auto ym{ymd.year() / ymd.month()};
const auto y{ymd.year()};
const auto d{ymd.day()};
const auto firstdow = first_day_of_week();
const auto maxRows{12 / cldMonCols_};
std::ostringstream os;
std::ostringstream tmp;
if (cldMode_ == CldMode::YEAR) {
if (y / date::month{1} / 1 == cldYearShift_)
if (y / month{1} / 1 == cldYearShift_)
if (d == cldBaseDay_ || (uint)cldBaseDay_ == 0u)
return cldYearCached_;
else
cldBaseDay_ = d;
else
cldYearShift_ = y / date::month{1} / 1;
cldYearShift_ = y / month{1} / 1;
}
if (cldMode_ == CldMode::MONTH) {
if (ym == cldMonShift_)
@ -330,67 +291,69 @@ auto waybar::modules::Clock::get_calendar(const date::year_month_day& today,
else
cldMonShift_ = ym;
}
// Pad object
const std::string pads(cldWnLen_, ' ');
// Compute number of lines needed for each calendar month
unsigned ml[12]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
for (auto& m : ml) {
if (cldMode_ == CldMode::YEAR || m == static_cast<unsigned>(ymd.month()))
m = cldRowsInMonth(y / date::month{m}, firstdow);
m = cldRowsInMonth(y / 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_));
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}};
const auto mon{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 << " ";
const year_month ymTmp{y / mon};
if (col != 0 && cldMode_ == CldMode::YEAR) os << std::string(3, ' ');
// Week numbers on the left
if (cldWPos_ == WeeksSide::LEFT && line > 0) {
if (cldWPos_ == WS::LEFT && line > 0) {
if (line > 1) {
if (line < ml[static_cast<unsigned>(ymTmp.month()) - 1u])
os << fmt::format(fmt::runtime(fmtMap_[4]),
(line == 2)
? date::zoned_seconds{tz, date::local_days{ymTmp / 1}}
: date::zoned_seconds{tz, date::local_days{cldGetWeekForLine(
ymTmp, firstdow, line)}})
if (line < ml[(unsigned)ymTmp.month() - 1u])
os << fmt_lib::vformat(
locale_, fmtMap_[4],
fmt_lib::make_format_args(
(line == 2) ? zoned_seconds{tz, local_days{ymTmp / 1}}
: zoned_seconds{tz, local_days{cldGetWeekForLine(
ymTmp, firstdow, line)}}))
<< ' ';
else
os << std::string(cldWnLen_, ' ');
os << pads;
}
}
os << fmt::format(
fmt::runtime((cldWPos_ != WeeksSide::LEFT || line == 0) ? "{:<{}}" : "{:>{}}"),
getCalendarLine(today, ymTmp, line, firstdow, &locale_),
(cldMonColLen_ + ((line < 2) ? cldWnLen_ : 0)));
os << Glib::ustring::format((cldWPos_ != WS::LEFT || line == 0) ? std::left : std::right,
std::setfill(L' '),
std::setw(cldMonColLen_ + ((line < 2) ? cldWnLen_ : 0)),
getCalendarLine(today, ymTmp, line, firstdow, &locale_));
// Week numbers on the right
if (cldWPos_ == WeeksSide ::RIGHT && line > 0) {
if (cldWPos_ == WS::RIGHT && line > 0) {
if (line > 1) {
if (line < ml[static_cast<unsigned>(ymTmp.month()) - 1u])
if (line < ml[(unsigned)ymTmp.month() - 1u])
os << ' '
<< fmt::format(fmt::runtime(fmtMap_[4]),
(line == 2)
? date::zoned_seconds{tz, date::local_days{ymTmp / 1}}
: date::zoned_seconds{tz, date::local_days{cldGetWeekForLine(
ymTmp, firstdow, line)}});
<< fmt_lib::vformat(
locale_, fmtMap_[4],
fmt_lib::make_format_args(
(line == 2) ? zoned_seconds{tz, local_days{ymTmp / 1}}
: zoned_seconds{tz, local_days{cldGetWeekForLine(
ymTmp, firstdow, line)}}));
else
os << std::string(cldWnLen_, ' ');
os << pads;
}
}
}
}
// Apply user formats to calendar
// Apply user's formats
if (line < 2)
tmp << fmt::format(fmt::runtime(fmtMap_[line]), os.str());
tmp << fmt_lib::vformat(locale_, fmtMap_[line], fmt_lib::make_format_args(os.str()));
else
tmp << os.str();
// Clear ostringstream
@ -400,10 +363,10 @@ auto waybar::modules::Clock::get_calendar(const date::year_month_day& today,
if (row + 1u != maxRows && cldMode_ == CldMode::YEAR) tmp << '\n';
}
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", d))));
os << std::regex_replace(
fmt_lib::vformat(locale_, fmtMap_[2], fmt_lib::make_format_args(tmp.str())),
std::regex("\\{today\\}"),
fmt_lib::vformat(locale_, fmtMap_[3], fmt_lib::make_format_args(date::format("{:L%e}", d))));
if (cldMode_ == CldMode::YEAR)
cldYearCached_ = os.str();
@ -413,50 +376,34 @@ auto waybar::modules::Clock::get_calendar(const date::year_month_day& today,
return os.str();
}
/*Clock actions*/
// Actions handler
auto waybar::modules::Clock::doAction(const std::string& name) -> void {
if (actionMap_[name]) {
(this->*actionMap_[name])();
} else
spdlog::error("Clock. Unsupported action \"{0}\"", name);
}
// Module actions
void waybar::modules::Clock::cldModeSwitch() {
cldMode_ = (cldMode_ == CldMode::YEAR) ? CldMode::MONTH : CldMode::YEAR;
}
void waybar::modules::Clock::cldShift_up() {
cldCurrShift_ += ((cldMode_ == CldMode::YEAR) ? 12 : 1) * cldShift_;
cldCurrShift_ += (months)((cldMode_ == CldMode::YEAR) ? 12 : 1);
}
void waybar::modules::Clock::cldShift_down() {
cldCurrShift_ -= ((cldMode_ == CldMode::YEAR) ? 12 : 1) * cldShift_;
cldCurrShift_ -= (months)((cldMode_ == CldMode::YEAR) ? 12 : 1);
}
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;
const auto tzSize{tzList_.size()};
if (tzSize == 1) return;
size_t newIdx{tzCurrIdx_ + 1lu};
tzCurrIdx_ = (newIdx == tzSize) ? 0 : newIdx;
}
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;
}
auto waybar::modules::Clock::timezones_text(std::chrono::system_clock::time_point now)
-> std::string {
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();
}
auto ztime = date::zoned_time{timezone, date::floor<std::chrono::seconds>(now)};
os << fmt::format(locale_, fmt::runtime(format_), ztime) << '\n';
}
return os.str();
const auto tzSize{tzList_.size()};
if (tzSize == 1) return;
tzCurrIdx_ = (tzCurrIdx_ == 0) ? tzSize - 1 : tzCurrIdx_ - 1;
}
#ifdef HAVE_LANGINFO_1STDAY
@ -468,17 +415,16 @@ using deleting_unique_ptr = std::unique_ptr<T, deleter_from_fn<fn>>;
#endif
// Computations done similarly to Linux cal utility.
auto waybar::modules::Clock::first_day_of_week() -> date::weekday {
auto waybar::modules::Clock::first_day_of_week() -> weekday {
#ifdef HAVE_LANGINFO_1STDAY
deleting_unique_ptr<std::remove_pointer<locale_t>::type, freelocale> posix_locale{
newlocale(LC_ALL, locale_.name().c_str(), nullptr)};
if (posix_locale) {
const int i = (std::intptr_t)nl_langinfo_l(_NL_TIME_WEEK_1STDAY, posix_locale.get());
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());
return wd + date::days(j - 1);
const auto i{(int)((std::intptr_t)nl_langinfo_l(_NL_TIME_WEEK_1STDAY, posix_locale.get()))};
const weekday wd{year_month_day{year(i / 10000) / month(i / 100 % 100) / day(i % 100)}};
const auto j{(uint8_t)*nl_langinfo_l(_NL_TIME_FIRST_WEEKDAY, posix_locale.get())};
return wd + days{j - 1};
}
#endif
return date::Sunday;
return Sunday;
}