Fix calendar extra padding if there are wide characters

This commit is contained in:
Mateus Eto
2025-06-01 21:27:37 +09:00
parent 0332d2ebf8
commit 05cfd73804

View File

@ -1,5 +1,6 @@
#include "modules/clock.hpp" #include "modules/clock.hpp"
#include <glib.h>
#include <gtkmm/tooltip.h> #include <gtkmm/tooltip.h>
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
@ -358,10 +359,23 @@ auto waybar::modules::Clock::get_calendar(const year_month_day& today, const yea
} }
} }
os << Glib::ustring::format((cldWPos_ != WS::LEFT || line == 0) ? std::left : std::right, // Count wide characters to avoid extra padding
std::setfill(L' '), size_t wideCharCount = 0;
std::setw(cldMonColLen_ + ((line < 2) ? cldWnLen_ : 0)), std::string calendarLine = getCalendarLine(today, ymTmp, line, firstdow, &m_locale_);
getCalendarLine(today, ymTmp, line, firstdow, &m_locale_)); if (line < 2) {
for (gchar *data = calendarLine.data(), *end = data + calendarLine.size();
data != nullptr;) {
gunichar c = g_utf8_get_char_validated(data, end - data);
if (g_unichar_iswide(c)) {
wideCharCount++;
}
data = g_utf8_find_next_char(data, end);
}
}
os << Glib::ustring::format(
(cldWPos_ != WS::LEFT || line == 0) ? std::left : std::right, std::setfill(L' '),
std::setw(cldMonColLen_ + ((line < 2) ? cldWnLen_ - wideCharCount : 0)),
calendarLine);
// Week numbers on the right // Week numbers on the right
if (cldWPos_ == WS::RIGHT && line > 0) { if (cldWPos_ == WS::RIGHT && line > 0) {