From a5bd08487ff22156cbaae9286ab8820172b35df7 Mon Sep 17 00:00:00 2001
From: antirez <antirez@gmail.com>
Date: Wed, 22 Feb 2012 16:07:06 +0100
Subject: [PATCH] Fix for issue #306, thanks to tchajed (on github) for the
 pull request. The original patch was reworked a bit.

---
 src/redis-cli.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/src/redis-cli.c b/src/redis-cli.c
index 692736c7..827e4061 100644
--- a/src/redis-cli.c
+++ b/src/redis-cli.c
@@ -68,7 +68,7 @@ static struct config {
     char *auth;
     int raw_output; /* output mode per command */
     sds mb_delim;
-    char prompt[32];
+    char prompt[128];
     char *eval;
 } config;
 
@@ -91,12 +91,19 @@ static long long mstime(void) {
 }
 
 static void cliRefreshPrompt(void) {
-    if (config.dbnum == 0)
-        snprintf(config.prompt,sizeof(config.prompt),"redis %s:%d> ",
-            config.hostip, config.hostport);
+    int len;
+
+    if (config.hostsocket != NULL)
+        len = snprintf(config.prompt,sizeof(config.prompt),"redis %s",
+                       config.hostsocket);
     else
-        snprintf(config.prompt,sizeof(config.prompt),"redis %s:%d[%d]> ",
-            config.hostip, config.hostport, config.dbnum);
+        len = snprintf(config.prompt,sizeof(config.prompt),"redis %s:%d",
+                       config.hostip, config.hostport);
+    /* Add [dbnum] if needed */
+    if (config.dbnum != 0)
+        len += snprintf(config.prompt+len,sizeof(config.prompt)-len,"[%d]",
+            config.dbnum);
+    snprintf(config.prompt+len,sizeof(config.prompt)-len,"> ");
 }
 
 /*------------------------------------------------------------------------------
-- 
2.47.2