mirror of
https://github.com/fluencelabs/redis
synced 2025-04-13 12:46:03 +00:00
Add proper numbering for multi bulk replies in redis-cli
This commit is contained in:
parent
7fc4ce13ed
commit
cfcd5d6d43
@ -157,22 +157,18 @@ static sds cliFormatReply(redisReply *r, char *prefix) {
|
|||||||
sds out = sdsempty();
|
sds out = sdsempty();
|
||||||
switch (r->type) {
|
switch (r->type) {
|
||||||
case REDIS_REPLY_ERROR:
|
case REDIS_REPLY_ERROR:
|
||||||
out = sdscat(out,prefix);
|
|
||||||
if (config.tty) out = sdscat(out,"(error) ");
|
if (config.tty) out = sdscat(out,"(error) ");
|
||||||
out = sdscatprintf(out,"%s\n", r->str);
|
out = sdscatprintf(out,"%s\n", r->str);
|
||||||
break;
|
break;
|
||||||
case REDIS_REPLY_STATUS:
|
case REDIS_REPLY_STATUS:
|
||||||
out = sdscat(out,prefix);
|
|
||||||
out = sdscat(out,r->str);
|
out = sdscat(out,r->str);
|
||||||
out = sdscat(out,"\n");
|
out = sdscat(out,"\n");
|
||||||
break;
|
break;
|
||||||
case REDIS_REPLY_INTEGER:
|
case REDIS_REPLY_INTEGER:
|
||||||
out = sdscat(out,prefix);
|
|
||||||
if (config.tty) out = sdscat(out,"(integer) ");
|
if (config.tty) out = sdscat(out,"(integer) ");
|
||||||
out = sdscatprintf(out,"%lld\n",r->integer);
|
out = sdscatprintf(out,"%lld\n",r->integer);
|
||||||
break;
|
break;
|
||||||
case REDIS_REPLY_STRING:
|
case REDIS_REPLY_STRING:
|
||||||
out = sdscat(out,prefix);
|
|
||||||
if (config.raw_output || !config.tty) {
|
if (config.raw_output || !config.tty) {
|
||||||
out = sdscatlen(out,r->str,r->len);
|
out = sdscatlen(out,r->str,r->len);
|
||||||
} else {
|
} else {
|
||||||
@ -183,23 +179,44 @@ static sds cliFormatReply(redisReply *r, char *prefix) {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case REDIS_REPLY_NIL:
|
case REDIS_REPLY_NIL:
|
||||||
out = sdscat(out,prefix);
|
|
||||||
out = sdscat(out,"(nil)\n");
|
out = sdscat(out,"(nil)\n");
|
||||||
break;
|
break;
|
||||||
case REDIS_REPLY_ARRAY:
|
case REDIS_REPLY_ARRAY:
|
||||||
if (r->elements == 0) {
|
if (r->elements == 0) {
|
||||||
out = sdscat(out,prefix);
|
|
||||||
out = sdscat(out,"(empty list or set)\n");
|
out = sdscat(out,"(empty list or set)\n");
|
||||||
} else {
|
} else {
|
||||||
unsigned int i;
|
unsigned int i, idxlen = 0;
|
||||||
|
char _prefixlen[16];
|
||||||
|
char _prefixfmt[16];
|
||||||
|
sds _prefix;
|
||||||
sds tmp;
|
sds tmp;
|
||||||
|
|
||||||
|
/* Calculate chars needed to represent the largest index */
|
||||||
|
i = r->elements;
|
||||||
|
do {
|
||||||
|
idxlen++;
|
||||||
|
i /= 10;
|
||||||
|
} while(i);
|
||||||
|
|
||||||
|
/* Prefix for nested multi bulks should grow with idxlen+2 spaces */
|
||||||
|
memset(_prefixlen,' ',idxlen+2);
|
||||||
|
_prefixlen[idxlen+2] = '\0';
|
||||||
|
_prefix = sdscat(sdsnew(prefix),_prefixlen);
|
||||||
|
|
||||||
|
/* Setup prefix format for every entry */
|
||||||
|
snprintf(_prefixfmt,sizeof(_prefixfmt),"%%s%%%dd) ",idxlen);
|
||||||
|
|
||||||
for (i = 0; i < r->elements; i++) {
|
for (i = 0; i < r->elements; i++) {
|
||||||
tmp = cliFormatReply(r->element[i],prefix);
|
/* Don't use the prefix for the first element, as the parent
|
||||||
out = sdscat(out,prefix);
|
* caller already prepended the index number. */
|
||||||
|
out = sdscatprintf(out,_prefixfmt,i == 0 ? "" : prefix,i+1);
|
||||||
|
|
||||||
|
/* Format the multi bulk entry */
|
||||||
|
tmp = cliFormatReply(r->element[i],_prefix);
|
||||||
out = sdscatlen(out,tmp,sdslen(tmp));
|
out = sdscatlen(out,tmp,sdslen(tmp));
|
||||||
sdsfree(tmp);
|
sdsfree(tmp);
|
||||||
}
|
}
|
||||||
|
sdsfree(_prefix);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user