Skip to content

Commit

Permalink
highlight --help
Browse files Browse the repository at this point in the history
  • Loading branch information
lwalkin committed Sep 20, 2016
1 parent 90c95e1 commit 56907d9
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 13 deletions.
26 changes: 18 additions & 8 deletions src/tcpkali.c
Original file line number Diff line number Diff line change
Expand Up @@ -1012,10 +1012,10 @@ parse_percentile_values(const char *option, char *str,
*/
static void
usage_long(char *argv0, struct tcpkali_config *conf) {
fprintf(stderr, "Usage: %s [OPTIONS] [-l <port>] [<host:port>...]\n",
fprintf(stdout, "Usage: %s [OPTIONS] [-l <port>] [<host:port>...]\n",
basename(argv0));
/* clang-format off */
fprintf(stderr,
fprintf(stdout,
"Where OPTIONS are:\n"
" -h Print short help screen, then exit\n"
" --help Print this help screen, then exit\n"
Expand Down Expand Up @@ -1080,14 +1080,17 @@ usage_long(char *argv0, struct tcpkali_config *conf) {

static void
usage_short(char *argv0) {
fprintf(stderr, "Usage: %s [OPTIONS] [-l <port>] [<host:port>...]\n",
if(isatty(fileno(stdout))) {
tcpkali_init_terminal();
}

fprintf(stdout, "Usage: %s [OPTIONS] [-l <port>] [<host:port>...]\n",
basename(argv0));
fprintf(
stderr,
fprintf(stdout,
/* clang-format off */
"Where some OPTIONS are:\n"
" -h Print this help screen, then exit\n"
" --help Print long help screen, then exit\n"
" %s--help%s Print long help screen, then exit\n"
" -d Dump i/o data for a single connection\n"
"\n"
" -c <N> Connections to keep open to the destinations\n"
Expand All @@ -1105,7 +1108,14 @@ usage_short(char *argv0) {
" <Time>: ms, s, m, h, d (milliseconds, seconds, minutes, hours, days)\n"
" <Rate> and <Time> can be fractional values, such as 0.25.\n"
"\n"
"Use `%s --help` or `man tcpkali` for a full set of supported options.\n",
basename(argv0));
"Use `%s %s--help%s` or `man tcpkali` for a %sfull set%s of supported options.\n",
tk_attr(TKA_HIGHLIGHT),
tk_attr(TKA_NORMAL),
basename(argv0),
tk_attr(TKA_HIGHLIGHT),
tk_attr(TKA_NORMAL),
tk_attr(TKA_HIGHLIGHT),
tk_attr(TKA_NORMAL)
);
/* clang-format on */
}
37 changes: 33 additions & 4 deletions src/tcpkali_terminfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,28 @@
#include "tcpkali_common.h"
#include "tcpkali_terminfo.h"

static int terminal_initialized = 0;
static int int_utf8 = 0;
static int terminal_width = 80;
static const char *str_clear_eol = ""; // ANSI terminal code: "\033[K";
static char tka_sndbrace[16];
static char tka_rcvbrace[16];
static char tka_warn[16];
static char tka_highlight[16];
static char tka_normal[16];

const char *
tk_attr(enum tk_attribute tka) {
if(!terminal_initialized)
return "";

switch(tka) {
case TKA_NORMAL:
return tka_normal;
case TKA_WARNING:
return tka_warn;
case TKA_HIGHLIGHT:
return tka_highlight;
case TKA_SndBrace:
return tka_sndbrace;
case TKA_RcvBrace:
Expand Down Expand Up @@ -106,11 +113,27 @@ tcpkali_terminal_width(void) {
return terminal_width;
}

void
tcpkali_disable_cursor(void) {
if(tcpkali_init_terminal() == 0) {
/* Disable cursor */
printf("%s", cap("vi"));
atexit(enable_cursor);
}
}

int
tcpkali_init_terminal(void) {
const int NOT_INITIALIZED = 1;
static int terminal_init_response = NOT_INITIALIZED;
int errret = 0;

if(terminal_init_response != NOT_INITIALIZED) {
return terminal_init_response;
}

if(setupterm(NULL, 1, &errret) == ERR) {
terminal_init_response = -1;
return -1;
} else {
setvbuf(stdout, 0, _IONBF, 0);
Expand All @@ -125,10 +148,6 @@ tcpkali_init_terminal(void) {
/* Obtain the clear end of line string */
str_clear_eol = cap("ce");

/* Disable cursor */
printf("%s", cap("vi"));
atexit(enable_cursor);

const char *bold = cap("md");

snprintf(tka_warn, sizeof(tka_warn),
Expand All @@ -138,6 +157,8 @@ tcpkali_init_terminal(void) {
"%s", bold);
#endif

snprintf(tka_highlight, sizeof(tka_highlight), "%s", bold);

snprintf(tka_sndbrace, sizeof(tka_sndbrace),
#if NCURSES_TPARM_VARARGS
"%s", cap("AF") ? tparm(cap("AF"), COLOR_RED) ?: bold : bold);
Expand All @@ -154,15 +175,23 @@ tcpkali_init_terminal(void) {

snprintf(tka_normal, sizeof(tka_normal), "%s", cap("me"));

terminal_init_response = 0;
terminal_initialized = 1;
return 0;
}

#else /* !HAVE_LIBNCURSES */

int
tcpkali_init_terminal(void) {
return -1;
}

void
tcpkali_disable_cursor(void) {
return;
}

int
tcpkali_terminal_width(void) {
return terminal_width;
Expand Down
4 changes: 3 additions & 1 deletion src/tcpkali_terminfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
*/
int tcpkali_init_terminal(void);

void tcpkali_disable_cursor(void);

/*
* Capability "clr_eol":
* Return a string which clears the line until the end of it.
Expand All @@ -53,7 +55,7 @@ int tcpkali_terminal_width();
* Get an escape sequence for special terminal output
* attributes used by tcpkali.
*/
enum tk_attribute { TKA_NORMAL, TKA_WARNING, TKA_SndBrace, TKA_RcvBrace };
enum tk_attribute { TKA_NORMAL, TKA_WARNING, TKA_HIGHLIGHT, TKA_SndBrace, TKA_RcvBrace };
const char *tk_attr(enum tk_attribute);

#endif /* TCPKALI_TERMINFO_H */

0 comments on commit 56907d9

Please sign in to comment.