feat: Add basename support
Now, the user can decide to show only the program name, instead of the full program path. It is useful, when you have a very long hierarchy of directories, which, with the full path name, the user might not see the program name.
This commit is contained in:
committed by
Matheus Rambo
parent
82a30bb644
commit
b94cd9f227
@@ -19,6 +19,7 @@ nethogs \- Net top tool grouping bandwidth per process
|
|||||||
.RB [ "\-l" ]
|
.RB [ "\-l" ]
|
||||||
.RB [ "\-f filter" ]
|
.RB [ "\-f filter" ]
|
||||||
.RB [ "\-C" ]
|
.RB [ "\-C" ]
|
||||||
|
.RB [ "\-b" ]
|
||||||
.RB [ "\-g period" ]
|
.RB [ "\-g period" ]
|
||||||
.RI [device(s)]
|
.RI [device(s)]
|
||||||
.SH DESCRIPTION
|
.SH DESCRIPTION
|
||||||
@@ -62,6 +63,9 @@ monitor all devices, even loopback/stopped ones.
|
|||||||
\fB-C\fP
|
\fB-C\fP
|
||||||
capture TCP and UDP.
|
capture TCP and UDP.
|
||||||
.TP
|
.TP
|
||||||
|
\fB-b\fP
|
||||||
|
Display the program basename.
|
||||||
|
.TP
|
||||||
\fB-g\fP
|
\fB-g\fP
|
||||||
garbage collection period in number of refresh. default is 50.
|
garbage collection period in number of refresh. default is 50.
|
||||||
.TP
|
.TP
|
||||||
@@ -86,6 +90,9 @@ sort by RECEIVED traffic
|
|||||||
l
|
l
|
||||||
display command line
|
display command line
|
||||||
.TP
|
.TP
|
||||||
|
b
|
||||||
|
display the program basename
|
||||||
|
.TP
|
||||||
m
|
m
|
||||||
switch between total (KB, B, MB) and throughput (KB/s, MB/s, GB/s) mode
|
switch between total (KB, B, MB) and throughput (KB/s, MB/s, GB/s) mode
|
||||||
.RE
|
.RE
|
||||||
|
|||||||
14
src/cui.cpp
14
src/cui.cpp
@@ -26,6 +26,7 @@
|
|||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <pwd.h>
|
#include <pwd.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <strings.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
#include "nethogs.h"
|
#include "nethogs.h"
|
||||||
@@ -45,6 +46,7 @@ extern bool sortRecv;
|
|||||||
|
|
||||||
extern int viewMode;
|
extern int viewMode;
|
||||||
extern bool showcommandline;
|
extern bool showcommandline;
|
||||||
|
extern bool showBasename;
|
||||||
|
|
||||||
extern unsigned refreshlimit;
|
extern unsigned refreshlimit;
|
||||||
extern unsigned refreshcount;
|
extern unsigned refreshcount;
|
||||||
@@ -67,6 +69,8 @@ const char *COLUMN_FORMAT_RECEIVED = "%11.3f";
|
|||||||
const char *const desc_view_mode[VIEWMODE_COUNT] = {
|
const char *const desc_view_mode[VIEWMODE_COUNT] = {
|
||||||
"KB/sec", "KB ", "B ", "MB ", "MB/sec", "GB/sec"};
|
"KB/sec", "KB ", "B ", "MB ", "MB/sec", "GB/sec"};
|
||||||
|
|
||||||
|
constexpr char FILE_SEPARATOR = '/';
|
||||||
|
|
||||||
class Line {
|
class Line {
|
||||||
public:
|
public:
|
||||||
Line(const char *name, const char *cmdline, double n_recv_value,
|
Line(const char *name, const char *cmdline, double n_recv_value,
|
||||||
@@ -152,6 +156,12 @@ static void mvaddstr_truncate_trailing(int row, int col, const char *str,
|
|||||||
static void mvaddstr_truncate_cmdline(int row, int col, const char *progname,
|
static void mvaddstr_truncate_cmdline(int row, int col, const char *progname,
|
||||||
const char *cmdline,
|
const char *cmdline,
|
||||||
std::size_t max_len) {
|
std::size_t max_len) {
|
||||||
|
if (showBasename) {
|
||||||
|
if (index(progname, FILE_SEPARATOR) != NULL) {
|
||||||
|
progname = rindex(progname, FILE_SEPARATOR) + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
std::size_t proglen = strlen(progname);
|
std::size_t proglen = strlen(progname);
|
||||||
std::size_t max_cmdlen;
|
std::size_t max_cmdlen;
|
||||||
|
|
||||||
@@ -305,6 +315,10 @@ void ui_tick() {
|
|||||||
/* switch mode: total vs kb/s */
|
/* switch mode: total vs kb/s */
|
||||||
viewMode = (viewMode + 1) % VIEWMODE_COUNT;
|
viewMode = (viewMode + 1) % VIEWMODE_COUNT;
|
||||||
break;
|
break;
|
||||||
|
case 'b':
|
||||||
|
/* show only the process basename */
|
||||||
|
showBasename = !showBasename;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ static void help(bool iserror) {
|
|||||||
// output << "usage: nethogs [-V] [-b] [-d seconds] [-t] [-p] [-f (eth|ppp))]
|
// output << "usage: nethogs [-V] [-b] [-d seconds] [-t] [-p] [-f (eth|ppp))]
|
||||||
// [device [device [device ...]]]\n";
|
// [device [device [device ...]]]\n";
|
||||||
output << "usage: nethogs [-V] [-h] [-x] [-d seconds] [-v mode] [-c count] "
|
output << "usage: nethogs [-V] [-h] [-x] [-d seconds] [-v mode] [-c count] "
|
||||||
"[-t] [-p] [-s] [-a] [-l] [-f filter] [-C]"
|
"[-t] [-p] [-s] [-a] [-l] [-f filter] [-C] [-b]"
|
||||||
"[device [device [device ...]]]\n";
|
"[device [device [device ...]]]\n";
|
||||||
output << " -V : prints version.\n";
|
output << " -V : prints version.\n";
|
||||||
output << " -h : prints this help.\n";
|
output << " -h : prints this help.\n";
|
||||||
@@ -48,6 +48,7 @@ static void help(bool iserror) {
|
|||||||
output << " -C : capture TCP and UDP.\n";
|
output << " -C : capture TCP and UDP.\n";
|
||||||
output << " -g : garbage collection period in number of refresh. "
|
output << " -g : garbage collection period in number of refresh. "
|
||||||
"default is 50.\n";
|
"default is 50.\n";
|
||||||
|
output << " -b : Short program name. Displays only the program name.\n";
|
||||||
output << " -f : EXPERIMENTAL: specify string pcap filter (like "
|
output << " -f : EXPERIMENTAL: specify string pcap filter (like "
|
||||||
"tcpdump)."
|
"tcpdump)."
|
||||||
" This may be removed or changed in a future version.\n";
|
" This may be removed or changed in a future version.\n";
|
||||||
@@ -59,6 +60,7 @@ static void help(bool iserror) {
|
|||||||
output << " s: sort by SENT traffic\n";
|
output << " s: sort by SENT traffic\n";
|
||||||
output << " r: sort by RECEIVED traffic\n";
|
output << " r: sort by RECEIVED traffic\n";
|
||||||
output << " l: display command line\n";
|
output << " l: display command line\n";
|
||||||
|
output << " b: display the program basename instead of the fullpath\n";
|
||||||
output << " m: switch between total (KB, B, MB) and throughput (KB/s, MB/s, "
|
output << " m: switch between total (KB, B, MB) and throughput (KB/s, MB/s, "
|
||||||
"GB/s) mode\n";
|
"GB/s) mode\n";
|
||||||
}
|
}
|
||||||
@@ -145,7 +147,7 @@ int main(int argc, char **argv) {
|
|||||||
int garbage_collection_period = 50;
|
int garbage_collection_period = 50;
|
||||||
|
|
||||||
int opt;
|
int opt;
|
||||||
while ((opt = getopt(argc, argv, "Vhxtpsd:v:c:laf:Cg:")) != -1) {
|
while ((opt = getopt(argc, argv, "Vhxtpsd:v:c:laf:Cbg:")) != -1) {
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 'V':
|
case 'V':
|
||||||
versiondisplay();
|
versiondisplay();
|
||||||
@@ -187,6 +189,9 @@ int main(int argc, char **argv) {
|
|||||||
case 'C':
|
case 'C':
|
||||||
catchall = true;
|
catchall = true;
|
||||||
break;
|
break;
|
||||||
|
case 'b':
|
||||||
|
showBasename = true;
|
||||||
|
break;
|
||||||
case 'g':
|
case 'g':
|
||||||
garbage_collection_period = (time_t)atoi(optarg);
|
garbage_collection_period = (time_t)atoi(optarg);
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -60,6 +60,7 @@ bool bughuntmode = false;
|
|||||||
// sort on sent or received?
|
// sort on sent or received?
|
||||||
bool sortRecv = true;
|
bool sortRecv = true;
|
||||||
bool showcommandline = false;
|
bool showcommandline = false;
|
||||||
|
bool showBasename = false;
|
||||||
// viewMode: kb/s or total
|
// viewMode: kb/s or total
|
||||||
int viewMode = VIEWMODE_KBPS;
|
int viewMode = VIEWMODE_KBPS;
|
||||||
const char version[] = " version " VERSION;
|
const char version[] = " version " VERSION;
|
||||||
|
|||||||
Reference in New Issue
Block a user