Fixed: Alignment of columns in proc box when wide UTF8 characters are used

This commit is contained in:
aristocratos 2022-02-11 19:30:06 +01:00
parent 951423dba5
commit 31555d8a7e
4 changed files with 1537 additions and 13 deletions

1491
include/widechar_width.hpp Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1399,7 +1399,7 @@ namespace Proc {
out += Mv::to(y+2+lc, x+1)
+ g_color + rjust(to_string(p.pid), 8) + ' '
+ c_color + ljust(p.name, prog_size, true) + ' ' + end
+ (cmd_size > 0 ? g_color + ljust(p.cmd, cmd_size, true, true) + ' ' : "");
+ (cmd_size > 0 ? g_color + ljust(p.cmd, cmd_size, true, true) + Mv::to(y+2+lc, x+11+prog_size+cmd_size) + ' ' : "");
}
//? Tree view line
else {
@ -1412,10 +1412,10 @@ namespace Proc {
width_left -= (ulen(p.name) + 1);
}
if (width_left > 7 and p.short_cmd != p.name) {
out += g_color + '(' + uresize(p.short_cmd, width_left - 3, true) + ") ";
out += g_color + '(' + uresize(p.short_cmd, width_left - 3, true) + ')';
width_left -= (ulen(p.short_cmd, true) + 3);
}
out += string(max(0, width_left), ' ');
out += Mv::to(y+2+lc, x+2+tree_size);
}
//? Common end of line
string cpu_str = to_string(p.cpu_p);

View File

@ -25,6 +25,7 @@ tab-size = 4
#include <utility>
#include <ranges>
#include <robin_hood.h>
#include <widechar_width.hpp>
#include <unistd.h>
#include <termios.h>
@ -166,17 +167,47 @@ namespace Fx {
namespace Tools {
size_t wide_ulen(const string& str) {
unsigned int chars = 0;
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
auto w_str = conv.from_bytes(str.c_str());
for (auto c : w_str) {
chars += utf8::wcwidth(c);
}
return chars;
}
size_t wide_ulen(const std::wstring& w_str) {
unsigned int chars = 0;
for (auto c : w_str) {
chars += utf8::wcwidth(c);
}
return chars;
}
string uresize(string str, const size_t len, const bool wide) {
if (len < 1 or str.empty()) return "";
for (size_t x = 0, i = 0; i < str.size(); i++) {
if (wide and static_cast<unsigned char>(str.at(i)) > 0xef) x += 2;
else if ((static_cast<unsigned char>(str.at(i)) & 0xC0) != 0x80) x++;
if (x >= len + 1) {
str.resize(i);
str.shrink_to_fit();
break;
if (wide) {
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
auto w_str = conv.from_bytes(str.c_str());
while (wide_ulen(w_str) > len)
w_str.pop_back();
str = conv.to_bytes(w_str);
}
else {
for (size_t x = 0, i = 0; i < str.size(); i++) {
if ((static_cast<unsigned char>(str.at(i)) & 0xC0) != 0x80) x++;
if (x >= len + 1) {
str.resize(i);
break;
}
}
}
str.shrink_to_fit();
return str;
}

View File

@ -132,10 +132,12 @@ namespace Term {
namespace Tools {
constexpr auto SSmax = std::numeric_limits<std::streamsize>::max();
//* Return number of UTF8 characters in a string (wide=true counts UTF-8 characters with a width > 1 as 2 characters)
size_t wide_ulen(const string& str);
size_t wide_ulen(const std::wstring& w_str);
//* Return number of UTF8 characters in a string (wide=true for column size needed on terminal)
inline size_t ulen(const string& str, const bool wide=false) {
return std::ranges::count_if(str, [](char c) { return (static_cast<unsigned char>(c) & 0xC0) != 0x80; })
+ (wide ? std::ranges::count_if(str, [](char c) { return (static_cast<unsigned char>(c) > 0xef); }) : 0);
return (wide ? wide_ulen(str) : std::ranges::count_if(str, [](char c) { return (static_cast<unsigned char>(c) & 0xC0) != 0x80; }));
}
//* Resize a string consisting of UTF8 characters (only reduces size)