ISSUE#1874

1. Calendar. Weeks. Fix right paddings when first days of the week is
Monday
2. Fix small perfomrance penalty(avoid of defining parameter in the
month loop)
3. Small name convention for format string variables
This commit is contained in:
Viktar Lukashonak 2022-12-14 16:43:23 +03:00
parent b5c686c0dd
commit 0079092699
No known key found for this signature in database
GPG Key ID: 08A413AA87200A6F
2 changed files with 21 additions and 15 deletions

View File

@ -33,8 +33,9 @@ class Clock : public ALabel {
bool handleScroll(GdkEventScroll* e); bool handleScroll(GdkEventScroll* e);
std::string weeks_format_; std::string fmt_str_weeks_;
int weeks_format_left_gaps{0}; std::string fmt_str_calendar_;
int fmt_weeks_left_pad_{0};
auto calendar_text(const waybar_time& wtime) -> std::string; auto calendar_text(const waybar_time& wtime) -> std::string;
auto weekdays_header(const date::weekday& first_dow, std::ostream& os) -> void; auto weekdays_header(const date::weekday& first_dow, std::ostream& os) -> void;
auto first_day_of_week() -> date::weekday; auto first_day_of_week() -> date::weekday;

View File

@ -76,13 +76,19 @@ waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config)
} }
if (config_["format-calendar-weeks"].isString()) { if (config_["format-calendar-weeks"].isString()) {
weeks_format_ = fmt_str_weeks_ =
std::regex_replace(config_["format-calendar-weeks"].asString(), std::regex("\\{\\}"), std::regex_replace(config_["format-calendar-weeks"].asString(), std::regex("\\{\\}"),
(first_day_of_week() == date::Monday) ? "{:%V}" : "{:%U}"); (first_day_of_week() == date::Monday) ? "{:%V}" : "{:%U}");
weeks_format_left_gaps = fmt_weeks_left_pad_ =
std::regex_replace(weeks_format_, std::regex("</?[^>]+>|\\{.*\\}"), "").length(); std::regex_replace(fmt_str_weeks_, std::regex("</?[^>]+>|\\{.*\\}"), "").length();
} else { } else {
weeks_format_ = ""; fmt_str_weeks_ = "";
}
if (config_["format-calendar"].isString()) {
fmt_str_calendar_ = config_["format-calendar"].asString();
} else {
fmt_str_calendar_ = "{}";
} }
thread_ = [this] { thread_ = [this] {
@ -206,7 +212,7 @@ auto waybar::modules::Clock::calendar_text(const waybar_time& wtime) -> std::str
if (config_["calendar-weeks-pos"].asString() == "left") { if (config_["calendar-weeks-pos"].asString() == "left") {
weeks_pos = WeeksSide::LEFT; weeks_pos = WeeksSide::LEFT;
// Add paddings before the header // Add paddings before the header
os << std::string(3 + weeks_format_left_gaps, ' '); os << std::string(3 + fmt_weeks_left_pad_, ' ');
} else if (config_["calendar-weeks-pos"].asString() == "right") { } else if (config_["calendar-weeks-pos"].asString() == "right") {
weeks_pos = WeeksSide::RIGHT; weeks_pos = WeeksSide::RIGHT;
} }
@ -221,7 +227,7 @@ auto waybar::modules::Clock::calendar_text(const waybar_time& wtime) -> std::str
/* Print weeknumber on the left for the first row*/ /* Print weeknumber on the left for the first row*/
if (weeks_pos == WeeksSide::LEFT) { if (weeks_pos == WeeksSide::LEFT) {
os << fmt::format(weeks_format_, print_wd) << ' '; os << fmt::format(fmt_str_weeks_, print_wd) << ' ';
} }
if (empty_days > 0) { if (empty_days > 0) {
@ -235,7 +241,7 @@ auto waybar::modules::Clock::calendar_text(const waybar_time& wtime) -> std::str
os << ' '; os << ' ';
} else if (unsigned(d) != 1) { } else if (unsigned(d) != 1) {
if (weeks_pos == WeeksSide::RIGHT) { if (weeks_pos == WeeksSide::RIGHT) {
os << ' ' << fmt::format(weeks_format_, print_wd); os << ' ' << fmt::format(fmt_str_weeks_, print_wd);
} }
os << '\n'; os << '\n';
@ -243,7 +249,7 @@ auto waybar::modules::Clock::calendar_text(const waybar_time& wtime) -> std::str
print_wd = (ym / d); print_wd = (ym / d);
if (weeks_pos == WeeksSide::LEFT) { if (weeks_pos == WeeksSide::LEFT) {
os << fmt::format(weeks_format_, print_wd) << ' '; os << fmt::format(fmt_str_weeks_, print_wd) << ' ';
} }
} }
@ -254,19 +260,18 @@ auto waybar::modules::Clock::calendar_text(const waybar_time& wtime) -> std::str
} else { } else {
os << "<b><u>" << date::format("%e", d) << "</u></b>"; os << "<b><u>" << date::format("%e", d) << "</u></b>";
} }
} else if (config_["format-calendar"].isString()) {
os << fmt::format(config_["format-calendar"].asString(), date::format("%e", d));
} else { } else {
os << date::format("%e", d); os << fmt::format(fmt_str_calendar_, date::format("%e", d));
} }
/*Print weeks on the right when the endings with spaces*/ /*Print weeks on the right when the endings with spaces*/
if (weeks_pos == WeeksSide::RIGHT && d == last_day) { if (weeks_pos == WeeksSide::RIGHT && d == last_day) {
empty_days = 6 - (wd.c_encoding() - first_dow.c_encoding()); empty_days = 6 - (wd.c_encoding() - first_dow.c_encoding());
if (empty_days > 0) { if (empty_days > 0 &&
empty_days < 7) {
os << std::string(empty_days * 3, ' '); os << std::string(empty_days * 3, ' ');
} }
os << ' ' << fmt::format(weeks_format_, print_wd); os << ' ' << fmt::format(fmt_str_weeks_, print_wd);
} }
} }