From 69bfdcf7d4cffcc0c11814e3236e7600aaaa246b Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Sun, 6 Mar 2011 21:10:50 +0100 Subject: [PATCH] Show database number in cli prompt when non-zero --- src/redis-cli.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/redis-cli.c b/src/redis-cli.c index 5710c3d1d..f0c712e74 100644 --- a/src/redis-cli.c +++ b/src/redis-cli.c @@ -65,6 +65,7 @@ static struct config { char *historyfile; int raw_output; /* output mode per command */ sds mb_delim; + char prompt[32]; } config; static void usage(); @@ -85,6 +86,13 @@ static long long mstime(void) { return mst; } +static void cliRefreshPrompt(void) { + if (config.dbnum == 0) + snprintf(config.prompt,sizeof(config.prompt),"redis> "); + else + snprintf(config.prompt,sizeof(config.prompt),"redis:%d> ",config.dbnum); +} + /*------------------------------------------------------------------------------ * Help functions *--------------------------------------------------------------------------- */ @@ -492,8 +500,10 @@ static int cliSendCommand(int argc, char **argv, int repeat) { return REDIS_ERR; } else { /* Store database number when SELECT was successfully executed. */ - if (!strcasecmp(command,"select") && argc == 2) + if (!strcasecmp(command,"select") && argc == 2) { config.dbnum = atoi(argv[1]); + cliRefreshPrompt(); + } } } @@ -622,7 +632,8 @@ static void repl() { config.interactive = 1; linenoiseSetCompletionCallback(completionCallback); - while((line = linenoise(context ? "redis> " : "not connected> ")) != NULL) { + cliRefreshPrompt(); + while((line = linenoise(context ? config.prompt : "not connected> ")) != NULL) { if (line[0] != '\0') { argv = sdssplitargs(line,&argc); linenoiseHistoryAdd(line); -- GitLab