Clock. Formating simplify

Signed-off-by: Viktar Lukashonak <myxabeer@gmail.com>
This commit is contained in:
Viktar Lukashonak 2023-01-27 15:55:27 +03:00
parent 47cdd6f988
commit 5d8f9a8273
No known key found for this signature in database
GPG Key ID: 08A413AA87200A6F
2 changed files with 35 additions and 35 deletions

View File

@ -49,7 +49,7 @@ class Clock : public ALabel {
CldMode cldMode_{CldMode::MONTH}; CldMode cldMode_{CldMode::MONTH};
uint cldMonCols_{3}; // Count of the month in the row uint cldMonCols_{3}; // Count of the month in the row
int cldMonColLen_{20}; // Length of the month column int cldMonColLen_{20}; // Length of the month column
int cldWnLen_{2}; // Length of the week number int cldWnLen_{3}; // Length of the week number
date::year_month_day cldYearShift_; date::year_month_day cldYearShift_;
date::year_month cldMonShift_; date::year_month cldMonShift_;
date::months cldCurrShift_{0}; date::months cldCurrShift_{0};

View File

@ -70,23 +70,19 @@ waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config)
fmtMap_.insert({2, config_[kCalendarPlaceholder]["format"]["days"].asString()}); fmtMap_.insert({2, config_[kCalendarPlaceholder]["format"]["days"].asString()});
else else
fmtMap_.insert({2, "{}"}); fmtMap_.insert({2, "{}"});
if (config_[kCalendarPlaceholder]["format"]["weeks"].isString()) { if (config_[kCalendarPlaceholder]["format"]["weeks"].isString() &&
cldWPos_ != WeeksSide::HIDDEN) {
fmtMap_.insert( fmtMap_.insert(
{4, std::regex_replace(config_[kCalendarPlaceholder]["format"]["weeks"].asString(), {4, std::regex_replace(config_[kCalendarPlaceholder]["format"]["weeks"].asString(),
std::regex("\\{\\}"), std::regex("\\{\\}"),
(first_day_of_week() == date::Monday) ? "{:%W}" : "{:%U}")}); (first_day_of_week() == date::Monday) ? "{:%W}" : "{:%U}")});
Glib::ustring tmp{std::regex_replace(fmtMap_[4], std::regex("</?[^>]+>|\\{.*\\}"), "")};
if (cldWPos_ == WeeksSide::HIDDEN) cldWnLen_ += tmp.size();
cldWnLen_ = 0;
else {
// tmp contains full length of the weeks including user characters
Glib::ustring tmp{std::regex_replace(fmtMap_[4], std::regex("</?[^>]+>|\\{.*\\}"), "")};
cldWnLen_ += (tmp.size() + 1);
cldMonColLen_ += cldWnLen_;
}
} else { } else {
if (cldWPos_ != WeeksSide::HIDDEN) if (cldWPos_ != WeeksSide::HIDDEN)
fmtMap_.insert({4, (first_day_of_week() == date::Monday) ? "{:%W}" : "{:%U}"}); fmtMap_.insert({4, (first_day_of_week() == date::Monday) ? "{:%W}" : "{:%U}"});
else
cldWnLen_ = 0;
} }
if (config_[kCalendarPlaceholder]["format"]["weekdays"].isString()) if (config_[kCalendarPlaceholder]["format"]["weekdays"].isString())
fmtMap_.insert({1, config_[kCalendarPlaceholder]["format"]["weekdays"].asString()}); fmtMap_.insert({1, config_[kCalendarPlaceholder]["format"]["weekdays"].asString()});
@ -277,7 +273,7 @@ auto cldGetWeekForLine(date::year_month const ym, date::weekday const firstdow,
} }
auto getCalendarLine(date::year_month_day const currDate, date::year_month const ym, auto getCalendarLine(date::year_month_day const currDate, date::year_month const ym,
unsigned const line, date::weekday const firstdow, int rowLen, unsigned const line, date::weekday const firstdow,
const std::locale* const locale_) -> std::string { const std::locale* const locale_) -> std::string {
using namespace date::literals; using namespace date::literals;
std::ostringstream res; std::ostringstream res;
@ -285,8 +281,8 @@ auto getCalendarLine(date::year_month_day const currDate, date::year_month const
switch (line) { switch (line) {
case 0: { case 0: {
// Output month and year title // Output month and year title
Glib::ustring wd_ustring{Glib::ustring::format( Glib::ustring wd_ustring{
std::left, std::setw(rowLen), date::format(*locale_, "%B %Y", ym), std::right)}; Glib::ustring::format(std::left, date::format(*locale_, "%B %Y", ym), std::right)};
res << wd_ustring; res << wd_ustring;
break; break;
} }
@ -308,7 +304,6 @@ auto getCalendarLine(date::year_month_day const currDate, date::year_month const
res << pad << wd_ustring; res << pad << wd_ustring;
} while (++wd != firstdow); } while (++wd != firstdow);
break; break;
} }
case 2: { case 2: {
@ -354,9 +349,7 @@ auto getCalendarLine(date::year_month_day const currDate, date::year_month const
} }
// Append row with spaces if the week did not complete // Append row with spaces if the week did not complete
res << std::string(static_cast<unsigned>((firstdow - wd).count()) * 3, ' '); res << std::string(static_cast<unsigned>((firstdow - wd).count()) * 3, ' ');
} else // Otherwise not a valid week, output a blank row }
res << std::string(rowLen, ' ');
break; break;
} }
} }
@ -412,28 +405,35 @@ auto waybar::modules::Clock::get_calendar(const date::zoned_seconds& now,
// Week numbers on the left // Week numbers on the left
if (cldWPos_ == WeeksSide::LEFT && line > 0) { if (cldWPos_ == WeeksSide::LEFT && line > 0) {
if (line == 1 && cldWnLen_ > 0) os << std::string(cldWnLen_, ' '); if (line > 1) {
if (line < ml[static_cast<unsigned>(ymTmp.month()) - 1u])
if (line > 1 && line < ml[static_cast<unsigned>(ymTmp.month()) - 1u]) os << fmt::format(fmt::runtime(fmtMap_[4]),
os << fmt::format(fmt::runtime(fmtMap_[4]), (line == 2)
(line == 2) ? date::sys_days{ymTmp / 1}
? date::sys_days{ymTmp / 1} : date::sys_days{cldGetWeekForLine(ymTmp, firstdow, line)})
: date::sys_days{cldGetWeekForLine(ymTmp, firstdow, line)}) << ' ';
<< ' '; else
os << std::string(cldWnLen_, ' ');
}
} }
os << getCalendarLine(currDate, ymTmp, line, firstdow, cldMonColLen_, &locale_); os << fmt::format(
fmt::runtime((cldWPos_ == WeeksSide::RIGHT || line == 0) ? "{:<{}}" : "{:>{}}"),
getCalendarLine(currDate, ymTmp, line, firstdow, &locale_),
(cldMonColLen_ + ((line < 2) ? cldWnLen_ : 0)));
// Week numbers on the right // Week numbers on the right
if (cldWPos_ == WeeksSide ::RIGHT && line > 0) { if (cldWPos_ == WeeksSide ::RIGHT && line > 0) {
if (line == 1 && cldWnLen_ > 0) os << std::string(cldWnLen_, ' '); if (line > 1) {
if (line < ml[static_cast<unsigned>(ymTmp.month()) - 1u])
if (line > 1 && line < ml[static_cast<unsigned>(ymTmp.month()) - 1u]) os << ' '
os << ' ' << fmt::format(fmt::runtime(fmtMap_[4]),
<< fmt::format(fmt::runtime(fmtMap_[4]), (line == 2)
(line == 2) ? date::sys_days{ymTmp / 1}
? date::sys_days{ymTmp / 1} : date::sys_days{cldGetWeekForLine(ymTmp, firstdow, line)});
: date::sys_days{cldGetWeekForLine(ymTmp, firstdow, line)}); else
os << std::string(cldWnLen_, ' ');
}
} }
} }
} }