mirror of
https://github.com/aristocratos/btop.git
synced 2024-05-19 03:43:45 +12:00
Fixed: Alignment of columns in proc box when wide UTF8 characters are used
This commit is contained in:
parent
951423dba5
commit
31555d8a7e
1491
include/widechar_width.hpp
Normal file
1491
include/widechar_width.hpp
Normal file
File diff suppressed because it is too large
Load diff
|
@ -1399,7 +1399,7 @@ namespace Proc {
|
||||||
out += Mv::to(y+2+lc, x+1)
|
out += Mv::to(y+2+lc, x+1)
|
||||||
+ g_color + rjust(to_string(p.pid), 8) + ' '
|
+ g_color + rjust(to_string(p.pid), 8) + ' '
|
||||||
+ c_color + ljust(p.name, prog_size, true) + ' ' + end
|
+ 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
|
//? Tree view line
|
||||||
else {
|
else {
|
||||||
|
@ -1412,10 +1412,10 @@ namespace Proc {
|
||||||
width_left -= (ulen(p.name) + 1);
|
width_left -= (ulen(p.name) + 1);
|
||||||
}
|
}
|
||||||
if (width_left > 7 and p.short_cmd != p.name) {
|
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);
|
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
|
//? Common end of line
|
||||||
string cpu_str = to_string(p.cpu_p);
|
string cpu_str = to_string(p.cpu_p);
|
||||||
|
|
|
@ -25,6 +25,7 @@ tab-size = 4
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <ranges>
|
#include <ranges>
|
||||||
#include <robin_hood.h>
|
#include <robin_hood.h>
|
||||||
|
#include <widechar_width.hpp>
|
||||||
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
|
@ -166,17 +167,47 @@ namespace Fx {
|
||||||
|
|
||||||
namespace Tools {
|
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) {
|
string uresize(string str, const size_t len, const bool wide) {
|
||||||
if (len < 1 or str.empty()) return "";
|
if (len < 1 or str.empty()) return "";
|
||||||
for (size_t x = 0, i = 0; i < str.size(); i++) {
|
if (wide) {
|
||||||
if (wide and static_cast<unsigned char>(str.at(i)) > 0xef) x += 2;
|
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
|
||||||
else if ((static_cast<unsigned char>(str.at(i)) & 0xC0) != 0x80) x++;
|
auto w_str = conv.from_bytes(str.c_str());
|
||||||
if (x >= len + 1) {
|
while (wide_ulen(w_str) > len)
|
||||||
str.resize(i);
|
w_str.pop_back();
|
||||||
str.shrink_to_fit();
|
str = conv.to_bytes(w_str);
|
||||||
break;
|
}
|
||||||
|
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;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -132,10 +132,12 @@ namespace Term {
|
||||||
namespace Tools {
|
namespace Tools {
|
||||||
constexpr auto SSmax = std::numeric_limits<std::streamsize>::max();
|
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) {
|
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; })
|
return (wide ? wide_ulen(str) : 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//* Resize a string consisting of UTF8 characters (only reduces size)
|
//* Resize a string consisting of UTF8 characters (only reduces size)
|
||||||
|
|
Loading…
Reference in a new issue