Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion include/modules/clock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace waybar::modules {
const std::string kCldPlaceholder{"calendar"};
const std::string kTZPlaceholder{"tz_list"};
const std::string kOrdPlaceholder{"ordinal_date"};
const std::string kMonthPlaceholder{"month_name"};

enum class CldMode { MONTH, YEAR };
enum class WS { LEFT, RIGHT, HIDDEN };
Expand Down Expand Up @@ -67,11 +68,16 @@ class Clock final : public ALabel {
std::string tzText_{""}; // time zones text to print
std::string tzTooltipFormat_{""}; // optional timezone tooltip format
util::SleeperThread thread_;

// ordinal date in tooltip
const bool ordInTooltip_;
std::string ordText_{""};

const bool monInTooltip_; // month in tooltip
std::string monText_{""}; // month text to print

auto get_ordinal_date(const date::year_month_day& today) -> std::string;
auto get_month_name(const date::year_month& ym) -> std::string;

auto getTZtext(date::sys_seconds now) -> std::string;
auto first_day_of_week() -> date::weekday;
Expand Down
4 changes: 4 additions & 0 deletions man/waybar-clock.5.scd
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ $XDG_CONFIG_HOME/waybar/config ++
:[ string
:[ same as format
:[ Tooltip on hover
|[ *month_name*
:[ string
:[ Placeholder
:[ This is intended to add an ability to use a modifier %OB as chrono does not support them, in calendar
|[ *menu*
:[ string
:[
Expand Down
21 changes: 19 additions & 2 deletions src/modules/clock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config)
tzTooltipFormat_{config_["timezone-tooltip-format"].isString()
? config_["timezone-tooltip-format"].asString()
: ""},
ordInTooltip_{m_tlpFmt_.find("{" + kOrdPlaceholder + "}") != std::string::npos} {
ordInTooltip_{m_tlpFmt_.find("{" + kOrdPlaceholder + "}") != std::string::npos},
monInTooltip_{m_tlpFmt_.find("{" + kMonthPlaceholder + "}") != std::string::npos} {
m_tlpText_ = m_tlpFmt_;

if (config_["timezones"].isArray() && !config_["timezones"].empty()) {
Expand Down Expand Up @@ -219,9 +220,10 @@ auto waybar::modules::Clock::update() -> void {

if (tzInTooltip_) tzText_ = getTZtext(now.get_sys_time());
if (cldInTooltip_) cldText_ = get_calendar(today, shiftedDay, tz);
if (monInTooltip_) monText_ = get_month_name(shiftedDay.year() / shiftedDay.month());
if (ordInTooltip_) ordText_ = get_ordinal_date(shiftedDay);
try {
if (tzInTooltip_ || cldInTooltip_ || ordInTooltip_) {
if (tzInTooltip_ || cldInTooltip_ || ordInTooltip_ || monInTooltip_) {
// std::vformat doesn't support named arguments.
m_tlpText_ =
std::regex_replace(m_tlpFmt_, std::regex("\\{" + kTZPlaceholder + "\\}"), tzText_);
Expand All @@ -230,6 +232,8 @@ auto waybar::modules::Clock::update() -> void {
fmt_lib::vformat(m_locale_, cldText_, fmt_lib::make_format_args(shiftedNow)));
m_tlpText_ =
std::regex_replace(m_tlpText_, std::regex("\\{" + kOrdPlaceholder + "\\}"), ordText_);
m_tlpText_ =
std::regex_replace(m_tlpText_, std::regex("\\{" + kMonthPlaceholder + "\\}"), monText_);
} else {
m_tlpText_ = m_tlpFmt_;
}
Expand Down Expand Up @@ -679,3 +683,16 @@ auto waybar::modules::Clock::get_ordinal_date(const year_month_day& today) -> st
}
return res.str();
}

/* Falling back to use strftime() for %OB specifier support that missing on chrono/date C++20 library */
auto waybar::modules::Clock::get_month_name(const date::year_month& ym) -> std::string {
const auto tp = date::sys_days{ym / date::day{1}};
const std::time_t t = std::chrono::system_clock::to_time_t(tp);
std::tm tm{};
gmtime_r(&t, &tm);

std::ostringstream oss;
oss.imbue(m_locale_);
oss << std::put_time(&tm, "%OB");
return oss.str();
}