From d2a1f4175052f47ee618feaf32e7034bea63f6d3 Mon Sep 17 00:00:00 2001 From: nullobsi Date: Sun, 31 Jan 2021 11:53:53 -0800 Subject: [PATCH 1/2] Use g_unichar_iswide to properly align calendar on CJK locales --- include/util/ustring_clen.hpp | 5 +++++ meson.build | 1 + src/modules/clock.cpp | 15 ++++++++++----- src/util/ustring_clen.cpp | 9 +++++++++ 4 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 include/util/ustring_clen.hpp create mode 100644 src/util/ustring_clen.cpp diff --git a/include/util/ustring_clen.hpp b/include/util/ustring_clen.hpp new file mode 100644 index 00000000..cddd2e1a --- /dev/null +++ b/include/util/ustring_clen.hpp @@ -0,0 +1,5 @@ +#pragma once +#include + +// calculate column width of ustring +int ustring_clen(const Glib::ustring &str); \ No newline at end of file diff --git a/meson.build b/meson.build index 49e71f26..373ec86b 100644 --- a/meson.build +++ b/meson.build @@ -147,6 +147,7 @@ src_files = files( 'src/main.cpp', 'src/bar.cpp', 'src/client.cpp', + 'src/util/ustring_clen.cpp' ) if is_linux diff --git a/src/modules/clock.cpp b/src/modules/clock.cpp index 5b2c3f43..0aade690 100644 --- a/src/modules/clock.cpp +++ b/src/modules/clock.cpp @@ -5,6 +5,7 @@ #include #include +#include "util/ustring_clen.hpp" #ifdef HAVE_LANGINFO_1STDAY #include #include @@ -154,12 +155,16 @@ auto waybar::modules::Clock::weekdays_header(const date::weekday& first_dow, std do { if (wd != first_dow) os << ' '; Glib::ustring wd_ustring(date::format(locale_, "%a", wd)); - auto wd_len = wd_ustring.length(); - if (wd_len > 2) { - wd_ustring = wd_ustring.substr(0, 2); - wd_len = 2; + auto clen = ustring_clen(wd_ustring); + auto wd_len = wd_ustring.length(); + fmt::print("{} {}\n", clen, wd_len); + while (clen > 2) { + wd_ustring = wd_ustring.substr(0, wd_len-1); + wd_len--; + clen = ustring_clen(wd_ustring); } - const std::string pad(2 - wd_len, ' '); + fmt::print("{} {}", clen, wd_len); + const std::string pad(2 - clen, ' '); os << pad << wd_ustring; } while (++wd != first_dow); os << "\n"; diff --git a/src/util/ustring_clen.cpp b/src/util/ustring_clen.cpp new file mode 100644 index 00000000..cd7d9cf5 --- /dev/null +++ b/src/util/ustring_clen.cpp @@ -0,0 +1,9 @@ +#include "util/ustring_clen.hpp" + +int ustring_clen(const Glib::ustring &str){ + int total = 0; + for (auto i = str.begin(); i != str.end(); ++i) { + total += g_unichar_iswide(*i) + 1; + } + return total; +} \ No newline at end of file From ecba117dc05b6677c951c4b1c3f1dd2442494aab Mon Sep 17 00:00:00 2001 From: nullobsi Date: Sun, 31 Jan 2021 11:56:25 -0800 Subject: [PATCH 2/2] remove unnessecary logging --- src/modules/clock.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/modules/clock.cpp b/src/modules/clock.cpp index 0aade690..22bedc78 100644 --- a/src/modules/clock.cpp +++ b/src/modules/clock.cpp @@ -157,13 +157,11 @@ auto waybar::modules::Clock::weekdays_header(const date::weekday& first_dow, std Glib::ustring wd_ustring(date::format(locale_, "%a", wd)); auto clen = ustring_clen(wd_ustring); auto wd_len = wd_ustring.length(); - fmt::print("{} {}\n", clen, wd_len); while (clen > 2) { wd_ustring = wd_ustring.substr(0, wd_len-1); wd_len--; clen = ustring_clen(wd_ustring); } - fmt::print("{} {}", clen, wd_len); const std::string pad(2 - clen, ' '); os << pad << wd_ustring; } while (++wd != first_dow);