From d2a1f4175052f47ee618feaf32e7034bea63f6d3 Mon Sep 17 00:00:00 2001 From: nullobsi Date: Sun, 31 Jan 2021 11:53:53 -0800 Subject: [PATCH] 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