diff --git a/include/modules/clock.hpp b/include/modules/clock.hpp index d6aabaa0..8b597c4e 100644 --- a/include/modules/clock.hpp +++ b/include/modules/clock.hpp @@ -8,6 +8,7 @@ namespace waybar::modules { const std::string kCldPlaceholder{"calendar"}; const std::string kTZPlaceholder{"tz_list"}; +const std::string kOrdPlaceholder{"ordinal_date"}; enum class CldMode { MONTH, YEAR }; enum class WS { LEFT, RIGHT, HIDDEN }; @@ -57,6 +58,11 @@ class Clock final : public ALabel { std::string tzText_{""}; // time zones text to print util::SleeperThread thread_; + // ordinal date in tooltip + const bool ordInTooltip_; + std::string ordText_{""}; + auto get_ordinal_date(const year_month_day& today) -> std::string; + auto getTZtext(sys_seconds now) -> std::string; auto first_day_of_week() -> weekday; // Module actions diff --git a/man/waybar-clock.5.scd b/man/waybar-clock.5.scd index fc079338..e8ef7bed 100644 --- a/man/waybar-clock.5.scd +++ b/man/waybar-clock.5.scd @@ -157,6 +157,7 @@ View all valid format options in *strftime(3)* or have a look https://en.cpprefe - *{calendar}*: Current month calendar - *{tz_list}*: List of time in the rest timezones, if more than one timezone is set in the config +- *{ordinal_date}*: The current day in (English) ordinal form, e.g. 21st # EXAMPLES diff --git a/src/modules/clock.cpp b/src/modules/clock.cpp index 495dfab3..e83cbef0 100644 --- a/src/modules/clock.cpp +++ b/src/modules/clock.cpp @@ -2,8 +2,10 @@ #include +#include #include #include +#include #include "util/ustring_clen.hpp" @@ -20,7 +22,8 @@ waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config) 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} { + tzCurrIdx_{0}, + ordInTooltip_{tlpFmt_.find("{" + kOrdPlaceholder + "}") != std::string::npos} { tlpText_ = tlpFmt_; if (config_["timezones"].isArray() && !config_["timezones"].empty()) { @@ -126,6 +129,7 @@ waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config) }; } + auto waybar::modules::Clock::update() -> void { auto tz{tzList_[tzCurrIdx_] ?: current_zone()}; const zoned_time now{tz, floor(system_clock::now())}; @@ -140,11 +144,13 @@ auto waybar::modules::Clock::update() -> void { if (tzInTooltip_) tzText_ = getTZtext(now.get_sys_time()); if (cldInTooltip_) cldText_ = get_calendar(today, shiftedDay, tz); - if (tzInTooltip_ || cldInTooltip_) { + if (ordInTooltip_) ordText_ = get_ordinal_date(shiftedDay); + if (tzInTooltip_ || cldInTooltip_ || ordInTooltip_) { // 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_ = std::regex_replace(tlpText_, std::regex("\\{" + kOrdPlaceholder + "\\}"), ordText_); } tlpText_ = fmt_lib::vformat(locale_, tlpText_, fmt_lib::make_format_args(shiftedNow)); @@ -437,3 +443,28 @@ auto waybar::modules::Clock::first_day_of_week() -> weekday { #endif return Sunday; } + +auto waybar::modules::Clock::get_ordinal_date(const year_month_day& today) -> std::string { + auto day = static_cast(today.day()); + std::stringstream res; + res << day; + if (day >= 11 && day <= 13) { + res << "th"; + return res.str(); + } + + switch (day % 10) { + case 1: + res << "st"; + break; + case 2: + res << "nd"; + break; + case 3: + res << "rd"; + break; + default: + res << "th"; + } + return res.str(); +} \ No newline at end of file