From 2d87bcb1ab10581d863a425ab3b003303804019c Mon Sep 17 00:00:00 2001 From: Viktar Lukashonak Date: Thu, 24 Mar 2022 15:41:50 +0300 Subject: [PATCH 1/3] https://github.com/Alexays/Waybar/issues/1315. Option to display week number on calendar --- include/modules/clock.hpp | 2 ++ src/modules/clock.cpp | 50 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/include/modules/clock.hpp b/include/modules/clock.hpp index 7c3eb8fc..4472fd0b 100644 --- a/include/modules/clock.hpp +++ b/include/modules/clock.hpp @@ -35,6 +35,8 @@ class Clock : public ALabel { auto weekdays_header(const date::weekday& first_dow, std::ostream& os) -> void; auto first_day_of_week() -> date::weekday; const date::time_zone* current_timezone(); + auto print_iso_weeknum(std::ostream& os, + int weeknum) -> void; bool is_timezone_fixed(); auto timezones_text(std::chrono::system_clock::time_point *now) -> std::string; }; diff --git a/src/modules/clock.cpp b/src/modules/clock.cpp index c80057c5..f91957f0 100644 --- a/src/modules/clock.cpp +++ b/src/modules/clock.cpp @@ -1,4 +1,5 @@ #include "modules/clock.hpp" +#include #include #if FMT_VERSION < 60000 @@ -167,12 +168,32 @@ auto waybar::modules::Clock::calendar_text(const waybar_time& wtime) -> std::str std::stringstream os; const auto first_dow = first_day_of_week(); + int ws{0}; // weeks-pos: side(1 - left, 2 - right) + int wn{0}; // weeknumber + if (config_["week-pos"].isString()) { + if (config_["week-pos"].asString() == "left") { + ws = 1; + // Add paddings before the header + os << std::string(4, ' '); + } else if (config_["week-pos"].asString() == "right") { + ws = 2; + } + } + if (ws > 0){ + wn = (date::sys_days{date::year_month_day{ym / 1}} - date::sys_days{date::year_month_day{ymd.year() / 1 / 1}}).count() / 7 + 1; + } + weekdays_header(first_dow, os); // First week prefixed with spaces if needed. auto wd = date::weekday(ym / 1); auto empty_days = (wd - first_dow).count(); if (empty_days > 0) { + if (ws == 1) { + print_iso_weeknum(os, wn); + os << ' '; + ++wn; + } os << std::string(empty_days * 3 - 1, ' '); } auto last_day = (ym / date::literals::last).day(); @@ -180,7 +201,19 @@ auto waybar::modules::Clock::calendar_text(const waybar_time& wtime) -> std::str if (wd != first_dow) { os << ' '; } else if (unsigned(d) != 1) { + if (ws == 2) { + os << ' '; + print_iso_weeknum(os, wn); + ++wn; + } + os << '\n'; + + if (ws == 1) { + print_iso_weeknum(os, wn); + os << ' '; + ++wn; + } } if (d == curr_day) { if (config_["today-format"].isString()) { @@ -192,6 +225,12 @@ auto waybar::modules::Clock::calendar_text(const waybar_time& wtime) -> std::str } else { os << date::format("%e", d); } + /*Print weeks on the right when the endings with spaces*/ + if (ws == 2 && d == last_day && wd.c_encoding() < 6) { + empty_days = 6 - wd.c_encoding(); + os << std::string(empty_days * 3 + 1, ' '); + print_iso_weeknum(os, wn); + } } auto result = os.str(); @@ -239,6 +278,17 @@ auto waybar::modules::Clock::timezones_text(std::chrono::system_clock::time_poin return os.str(); } +auto waybar::modules::Clock::print_iso_weeknum(std::ostream& os, + int weeknum) -> void { + std::stringstream res; + res << 'W' << std::setfill('0') << std::setw(2) << weeknum; + + if (config_["week-format"].isString()) { + auto week_format = config_["week-format"].asString(); + os << fmt::format(week_format, res.str()); + } else os << res.str(); +} + #ifdef HAVE_LANGINFO_1STDAY template using deleter_from_fn = std::integral_constant; From 1d2a381b5f5ff58932dd7f85e10721f50bffa0a7 Mon Sep 17 00:00:00 2001 From: Viktar Lukashonak Date: Fri, 25 Mar 2022 16:12:11 +0300 Subject: [PATCH 2/3] Waybar. Clock module - weeknum fix for the left side --- src/modules/clock.cpp | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/modules/clock.cpp b/src/modules/clock.cpp index f91957f0..b68b24e1 100644 --- a/src/modules/clock.cpp +++ b/src/modules/clock.cpp @@ -171,6 +171,7 @@ auto waybar::modules::Clock::calendar_text(const waybar_time& wtime) -> std::str int ws{0}; // weeks-pos: side(1 - left, 2 - right) int wn{0}; // weeknumber if (config_["week-pos"].isString()) { + wn = (date::sys_days{date::year_month_day{ym / 1}} - date::sys_days{date::year_month_day{ymd.year() / 1 / 1}}).count() / 7 + 1; if (config_["week-pos"].asString() == "left") { ws = 1; // Add paddings before the header @@ -179,21 +180,17 @@ auto waybar::modules::Clock::calendar_text(const waybar_time& wtime) -> std::str ws = 2; } } - if (ws > 0){ - wn = (date::sys_days{date::year_month_day{ym / 1}} - date::sys_days{date::year_month_day{ymd.year() / 1 / 1}}).count() / 7 + 1; - } - weekdays_header(first_dow, os); - + /* Print weeknumber on the left for the first row*/ + if (ws == 1) { + print_iso_weeknum(os, wn); + os << ' '; + ++wn; + } // First week prefixed with spaces if needed. auto wd = date::weekday(ym / 1); auto empty_days = (wd - first_dow).count(); if (empty_days > 0) { - if (ws == 1) { - print_iso_weeknum(os, wn); - os << ' '; - ++wn; - } os << std::string(empty_days * 3 - 1, ' '); } auto last_day = (ym / date::literals::last).day(); From 4a457648f9754c2164b362f9df26ee210ae3128c Mon Sep 17 00:00:00 2001 From: Viktar Lukashonak Date: Sat, 26 Mar 2022 23:33:15 +0300 Subject: [PATCH 3/3] Waybar. Clock module. Calendar - custom output formats --- src/modules/clock.cpp | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/modules/clock.cpp b/src/modules/clock.cpp index b68b24e1..98a5cf4a 100644 --- a/src/modules/clock.cpp +++ b/src/modules/clock.cpp @@ -170,13 +170,13 @@ auto waybar::modules::Clock::calendar_text(const waybar_time& wtime) -> std::str const auto first_dow = first_day_of_week(); int ws{0}; // weeks-pos: side(1 - left, 2 - right) int wn{0}; // weeknumber - if (config_["week-pos"].isString()) { + if (config_["calendar-weeks-pos"].isString()) { wn = (date::sys_days{date::year_month_day{ym / 1}} - date::sys_days{date::year_month_day{ymd.year() / 1 / 1}}).count() / 7 + 1; - if (config_["week-pos"].asString() == "left") { + if (config_["calendar-weeks-pos"].asString() == "left") { ws = 1; // Add paddings before the header os << std::string(4, ' '); - } else if (config_["week-pos"].asString() == "right") { + } else if (config_["calendar-weeks-pos"].asString() == "right") { ws = 2; } } @@ -219,9 +219,9 @@ auto waybar::modules::Clock::calendar_text(const waybar_time& wtime) -> std::str } else { os << "" << date::format("%e", d) << ""; } - } else { - os << date::format("%e", d); - } + } else if (config_["format-calendar"].isString()) { + os << fmt::format(config_["format-calendar"].asString(), date::format("%e", d)); + } else os << date::format("%e", d); /*Print weeks on the right when the endings with spaces*/ if (ws == 2 && d == last_day && wd.c_encoding() < 6) { empty_days = 6 - wd.c_encoding(); @@ -238,9 +238,10 @@ auto waybar::modules::Clock::calendar_text(const waybar_time& wtime) -> std::str auto waybar::modules::Clock::weekdays_header(const date::weekday& first_dow, std::ostream& os) -> void { + std::stringstream res; auto wd = first_dow; do { - if (wd != first_dow) os << ' '; + if (wd != first_dow) res << ' '; Glib::ustring wd_ustring(date::format(locale_, "%a", wd)); auto clen = ustring_clen(wd_ustring); auto wd_len = wd_ustring.length(); @@ -250,9 +251,13 @@ auto waybar::modules::Clock::weekdays_header(const date::weekday& first_dow, std clen = ustring_clen(wd_ustring); } const std::string pad(2 - clen, ' '); - os << pad << wd_ustring; + res << pad << wd_ustring; } while (++wd != first_dow); - os << "\n"; + res << "\n"; + + if (config_["format-calendar-weekdays"].isString()) { + os << fmt::format(config_["format-calendar-weekdays"].asString(), res.str()); + } else os << res.str(); } auto waybar::modules::Clock::timezones_text(std::chrono::system_clock::time_point *now) -> std::string { @@ -280,9 +285,8 @@ auto waybar::modules::Clock::print_iso_weeknum(std::ostream& os, std::stringstream res; res << 'W' << std::setfill('0') << std::setw(2) << weeknum; - if (config_["week-format"].isString()) { - auto week_format = config_["week-format"].asString(); - os << fmt::format(week_format, res.str()); + if (config_["format-calendar-weeks"].isString()) { + os << fmt::format(config_["format-calendar-weeks"].asString(), res.str()); } else os << res.str(); }