Skip to content

Commit

Permalink
Colorize status output
Browse files Browse the repository at this point in the history
  • Loading branch information
cleishm committed May 24, 2017
1 parent 080f99d commit bcb17b6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
10 changes: 10 additions & 0 deletions src/bin/colorization.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ static struct help_colorization _ansi_help_colorization =
.arg = { ANSI_COLOR_GREEN, ANSI_COLOR_RESET },
.dsc = { ANSI_COLOR_BLUE, ANSI_COLOR_RESET } };

static struct status_colorization _no_status_colorization =
{ .url = { "", "" },
.wrn = { "", "" } };

static struct status_colorization _ansi_status_colorization =
{ .url = { ANSI_COLOR_BOLD, ANSI_COLOR_RESET },
.wrn = { ANSI_COLOR_RED, ANSI_COLOR_RESET } };

static struct options_colorization _no_options_colorization =
{ .opt = { "", "" },
.val = { "", "" },
Expand All @@ -72,11 +80,13 @@ static struct exports_colorization _ansi_exports_colorization =
static struct shell_colorization _no_shell_colorization =
{ .error = &_no_error_colorization,
.help = &_no_help_colorization,
.status = &_no_status_colorization,
.options = &_no_options_colorization,
.exports = &_no_exports_colorization };

static struct shell_colorization _ansi_shell_colorization =
{ .error = &_ansi_error_colorization,
.status = &_ansi_status_colorization,
.help = &_ansi_help_colorization,
.options = &_ansi_options_colorization,
.exports = &_ansi_exports_colorization };
Expand Down
8 changes: 8 additions & 0 deletions src/bin/colorization.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ struct help_colorization
};


struct status_colorization
{
const char *url[2];
const char *wrn[2];
};


struct options_colorization
{
const char *opt[2];
Expand All @@ -55,6 +62,7 @@ struct shell_colorization
{
struct error_colorization *error;
struct help_colorization *help;
struct status_colorization *status;
struct options_colorization *options;
struct exports_colorization *exports;
};
Expand Down
23 changes: 17 additions & 6 deletions src/bin/state.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,10 @@ void shell_state_unexport(shell_state_t *state, neo4j_value_t name)

void display_status(FILE* stream, shell_state_t *state)
{
struct status_colorization *colors = state->colorize->status;
if (state->connection == NULL)
{
fprintf(stream, "Not connected\n");
fprintf(stream, "%sNot connected%s\n", colors->url[0], colors->url[1]);
}
else
{
Expand All @@ -301,14 +302,24 @@ void display_status(FILE* stream, shell_state_t *state)
unsigned int port = neo4j_connection_port(state->connection);
bool secure = neo4j_connection_is_secure(state->connection);
const char *server_id = neo4j_server_id(state->connection);
fprintf(stream, "Connected to 'neo4j://%s%s%s%s%s:%u'%s%s%s%s\n",

fprintf(stream, "Connected to '%sneo4j://%s%s%s%s%s:%u%s'",
colors->url[0],
(username != NULL)? username : "",
(username != NULL)? "@" : "",
ipv6? "[" : "", hostname, ipv6? "]" : "",
port,
secure? "" : " (insecure)",
(server_id != NULL)? " [" : "",
(server_id != NULL)? server_id : "",
(server_id != NULL)? "]" : "");
colors->url[1]);

if (!secure)
{
fprintf(stream, " (%sinsecure%s)", colors->wrn[0], colors->wrn[1]);
}

if (server_id != NULL)
{
fprintf(stream, " [%s]", server_id);
}
fputc('\n', stream);
}
}

0 comments on commit bcb17b6

Please sign in to comment.