Truncate user names when necessary (fixes #28) and fix alignment of sent/received fields

This commit is contained in:
Corbin Hughes
2016-03-01 00:15:38 -06:00
parent 425f667e15
commit 9eb98e0507

90
cui.cpp
View File

@@ -52,6 +52,16 @@ extern unsigned refreshcount;
#define PID_MAX 4194303 #define PID_MAX 4194303
const int COLUMN_WIDTH_PID = 7;
const int COLUMN_WIDTH_USER = 8;
const int COLUMN_WIDTH_DEV = 5;
const int COLUMN_WIDTH_SENT = 11;
const int COLUMN_WIDTH_RECEIVED = 11;
const char * COLUMN_FORMAT_PID = "%7d";
const char * COLUMN_FORMAT_SENT = "%11.3f";
const char * COLUMN_FORMAT_RECEIVED = "%11.3f";
class Line class Line
{ {
public: public:
@@ -109,47 +119,85 @@ std::string uid2username (uid_t uid)
return std::string(pwd->pw_name); return std::string(pwd->pw_name);
} }
/**
* Render the provided text at the specified location, truncating if the length of the text exceeds a maximum. If the
* text must be truncated, the string ".." will be rendered, followed by max_len - 2 characters of the provided text.
*/
static void mvaddstr_truncate_leading(int row, int col, const char* str, std::size_t str_len, std::size_t max_len)
{
if (str_len < max_len) {
mvaddstr(row, col, str);
} else {
mvaddstr(row, col, "..");
addnstr(str + 2, max_len - 2);
}
}
/**
* Render the provided text at the specified location, truncating if the length of the text exceeds a maximum. If the
* text must be truncated, the text will be rendered up to max_len - 2 characters and then ".." will be rendered.
*/
static void mvaddstr_truncate_trailing(int row, int col, const char* str, std::size_t str_len, std::size_t max_len)
{
if (str_len < max_len) {
mvaddstr(row, col, str);
} else {
mvaddnstr(row, col, str, max_len - 2);
addstr("..");
}
}
void Line::show (int row, unsigned int proglen) void Line::show (int row, unsigned int proglen)
{ {
assert (m_pid >= 0); assert (m_pid >= 0);
assert (m_pid <= PID_MAX); assert (m_pid <= PID_MAX);
const int column_offset_pid = 0;
const int column_offset_user = column_offset_pid + COLUMN_WIDTH_PID + 1;
const int column_offset_program = column_offset_user + COLUMN_WIDTH_USER + 1;
const int column_offset_dev = column_offset_program + proglen + 2;
const int column_offset_sent = column_offset_dev + COLUMN_WIDTH_DEV + 1;
const int column_offset_received = column_offset_sent + COLUMN_WIDTH_SENT + 1;
const int column_offset_unit = column_offset_received + COLUMN_WIDTH_RECEIVED + 1;
// PID column
if (m_pid == 0) if (m_pid == 0)
mvprintw (row, 6, "?"); mvaddch (row, column_offset_pid + COLUMN_WIDTH_PID - 1, '?');
else else
mvprintw (row, 0, "%7d", m_pid); mvprintw (row, column_offset_pid, COLUMN_FORMAT_PID, m_pid);
// USER column
std::string username = uid2username(m_uid); std::string username = uid2username(m_uid);
mvprintw (row, 8, "%s", username.c_str()); mvaddstr_truncate_trailing (row, column_offset_user, username.c_str(), username.size(), COLUMN_WIDTH_USER);
if (strlen (m_name) > proglen) {
// truncate oversized names // PROGRAM column
char * tmp = strdup(m_name); mvaddstr_truncate_leading (row, column_offset_program, m_name, strlen (m_name), proglen);
char * start = tmp + strlen (m_name) - proglen;
start[0] = '.'; // DEV column
start[1] = '.'; mvaddstr (row, column_offset_dev, devicename);
mvprintw (row, 8 + 9, "%s", start);
free (tmp); // SENT column
} else { mvprintw (row, column_offset_sent, COLUMN_FORMAT_SENT, sent_value);
mvprintw (row, 8 + 9, "%s", m_name);
} // RECEIVED column
mvprintw (row, 8 + 9 + proglen + 2, "%s", devicename); mvprintw (row, column_offset_received, COLUMN_FORMAT_RECEIVED, recv_value);
mvprintw (row, 8 + 9 + proglen + 2 + 6, "%10.3f", sent_value);
mvprintw (row, 8 + 9 + proglen + 2 + 6 + 9 + 3, "%10.3f", recv_value); // Unit column
if (viewMode == VIEWMODE_KBPS) if (viewMode == VIEWMODE_KBPS)
{ {
mvprintw (row, 8 + 9 + proglen + 2 + 6 + 9 + 3 + 11, "KB/sec"); mvaddstr (row, column_offset_unit, "KB/sec");
} }
else if (viewMode == VIEWMODE_TOTAL_MB) else if (viewMode == VIEWMODE_TOTAL_MB)
{ {
mvprintw (row, 8 + 9 + proglen + 2 + 6 + 9 + 3 + 11, "MB "); mvaddstr (row, column_offset_unit, "MB ");
} }
else if (viewMode == VIEWMODE_TOTAL_KB) else if (viewMode == VIEWMODE_TOTAL_KB)
{ {
mvprintw (row, 8 + 9 + proglen + 2 + 6 + 9 + 3 + 11, "KB "); mvaddstr (row, column_offset_unit, "KB ");
} }
else if (viewMode == VIEWMODE_TOTAL_B) else if (viewMode == VIEWMODE_TOTAL_B)
{ {
mvprintw (row, 8 + 9 + proglen + 2 + 6 + 9 + 3 + 11, "B "); mvaddstr (row, column_offset_unit, "B ");
} }
} }