From 4491ee1805ea9044747af22a7abba9bac00c671a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20S=C3=B6derqvist?= Date: Tue, 25 Jan 2022 16:15:57 +0100 Subject: [PATCH] redis-cli: Aligned RESP3 maps with multiline value in TTY (#10170) Before: ``` 127.0.0.1:6379> command info get 1) 1) "get" 2) (integer) 2 3) 1~ readonly 2~ fast 4) (integer) 1 5) (integer) 1 6) (integer) 1 7) 1~ @read 2~ @string 3~ @fast 8) (empty set) 9) 1~ 1# "flags" => 1~ RO 2~ access 2# "begin_search" => 1# "type" => "index" 2# "spec" => 1# "index" => (integer) 1 3# "find_keys" => 1# "type" => "range" 2# "spec" => 1# "lastkey" => (integer) 0 2# "keystep" => (integer) 1 3# "limit" => (integer) 0 10) (empty set) ``` After: ``` 127.0.0.1:6379> command info get 1) 1) "get" 2) (integer) 2 3) 1~ readonly 2~ fast 4) (integer) 1 5) (integer) 1 6) (integer) 1 7) 1~ @read 2~ @string 3~ @fast 8) (empty set) 9) 1~ 1# "flags" => 1~ RO 2~ access 2# "begin_search" => 1# "type" => "index" 2# "spec" => 1# "index" => (integer) 1 3# "find_keys" => 1# "type" => "range" 2# "spec" => 1# "lastkey" => (integer) 0 2# "keystep" => (integer) 1 3# "limit" => (integer) 0 10) (empty set) ``` --- src/redis-cli.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/redis-cli.c b/src/redis-cli.c index 8ef67f67a..14d17f8f5 100644 --- a/src/redis-cli.c +++ b/src/redis-cli.c @@ -878,6 +878,24 @@ static sds cliFormatInvalidateTTY(redisReply *r) { return sdscatlen(out, "\n", 1); } +/* Returns non-zero if cliFormatReplyTTY renders the reply in multiple lines. */ +static int cliIsMultilineValueTTY(redisReply *r) { + switch (r->type) { + case REDIS_REPLY_ARRAY: + case REDIS_REPLY_SET: + case REDIS_REPLY_PUSH: + if (r->elements == 0) return 0; + if (r->elements > 1) return 1; + return cliIsMultilineValueTTY(r->element[0]); + case REDIS_REPLY_MAP: + if (r->elements == 0) return 0; + if (r->elements > 2) return 1; + return cliIsMultilineValueTTY(r->element[1]); + default: + return 0; + } +} + static sds cliFormatReplyTTY(redisReply *r, char *prefix) { sds out = sdsempty(); switch (r->type) { @@ -974,6 +992,11 @@ static sds cliFormatReplyTTY(redisReply *r, char *prefix) { i++; sdsrange(out,0,-2); out = sdscat(out," => "); + if (cliIsMultilineValueTTY(r->element[i])) { + /* linebreak before multiline value to fix alignment */ + out = sdscat(out, "\n"); + out = sdscat(out, _prefix); + } tmp = cliFormatReplyTTY(r->element[i],_prefix); out = sdscatlen(out,tmp,sdslen(tmp)); sdsfree(tmp);