Use g_unichar_iswide to properly align calendar on CJK locales

This commit is contained in:
nullobsi 2021-01-31 11:53:53 -08:00
parent 69a366dced
commit d2a1f41750
No known key found for this signature in database
GPG Key ID: 4698F7ECCC7E2005
4 changed files with 25 additions and 5 deletions

View File

@ -0,0 +1,5 @@
#pragma once
#include <glibmm/ustring.h>
// calculate column width of ustring
int ustring_clen(const Glib::ustring &str);

View File

@ -147,6 +147,7 @@ src_files = files(
'src/main.cpp', 'src/main.cpp',
'src/bar.cpp', 'src/bar.cpp',
'src/client.cpp', 'src/client.cpp',
'src/util/ustring_clen.cpp'
) )
if is_linux if is_linux

View File

@ -5,6 +5,7 @@
#include <sstream> #include <sstream>
#include <type_traits> #include <type_traits>
#include "util/ustring_clen.hpp"
#ifdef HAVE_LANGINFO_1STDAY #ifdef HAVE_LANGINFO_1STDAY
#include <langinfo.h> #include <langinfo.h>
#include <locale.h> #include <locale.h>
@ -154,12 +155,16 @@ auto waybar::modules::Clock::weekdays_header(const date::weekday& first_dow, std
do { do {
if (wd != first_dow) os << ' '; if (wd != first_dow) os << ' ';
Glib::ustring wd_ustring(date::format(locale_, "%a", wd)); Glib::ustring wd_ustring(date::format(locale_, "%a", wd));
auto clen = ustring_clen(wd_ustring);
auto wd_len = wd_ustring.length(); auto wd_len = wd_ustring.length();
if (wd_len > 2) { fmt::print("{} {}\n", clen, wd_len);
wd_ustring = wd_ustring.substr(0, 2); while (clen > 2) {
wd_len = 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; os << pad << wd_ustring;
} while (++wd != first_dow); } while (++wd != first_dow);
os << "\n"; os << "\n";

View File

@ -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;
}