From 3a51bff0358c38162bc925ab25661e6090cf1161 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 4 Aug 2010 17:46:56 +0200 Subject: [PATCH] Change output format for non-tty redis-cli execution --- src/redis-cli.c | 19 ++++++++++++------- tests/integration/redis-cli.tcl | 22 ++++++++++++++++++++++ 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/redis-cli.c b/src/redis-cli.c index 6878d283..fc2238d4 100644 --- a/src/redis-cli.c +++ b/src/redis-cli.c @@ -61,6 +61,7 @@ static struct config { int pubsub_mode; int raw_output; /* output mode per command */ int tty; /* flag for default output format */ + char mb_sep; char *auth; char *historyfile; } config; @@ -108,7 +109,7 @@ static int cliReadSingleLineReply(int fd, int quiet) { if (reply == NULL) return 1; if (!quiet) - printf("%s\n", reply); + printf("%s", reply); sdsfree(reply); return 0; } @@ -162,7 +163,6 @@ static int cliReadBulkReply(int fd) { /* If you are producing output for the standard output we want * a more interesting output with quoted characters and so forth */ printStringRepr(reply,bulklen); - printf("\n"); } zfree(reply); return 0; @@ -183,8 +183,9 @@ static int cliReadMultiBulkReply(int fd) { printf("(empty list or set)\n"); } while(elements--) { - printf("%d. ", c); + if (config.tty) printf("%d. ", c); if (cliReadReply(fd)) return 1; + if (elements) printf("%c",config.mb_sep); c++; } return 0; @@ -199,13 +200,13 @@ static int cliReadReply(int fd) { } switch(type) { case '-': - printf("(error) "); + if (config.tty) printf("(error) "); cliReadSingleLineReply(fd,0); return 1; case '+': return cliReadSingleLineReply(fd,0); case ':': - printf("(integer) "); + if (config.tty) printf("(integer) "); return cliReadSingleLineReply(fd,0); case '$': return cliReadBulkReply(fd); @@ -275,7 +276,7 @@ static int cliSendCommand(int argc, char **argv, int repeat) { printf("Reading messages... (press Ctrl-c to quit)\n"); while (1) { cliReadReply(fd); - printf("\n"); + printf("\n\n"); } } @@ -283,6 +284,9 @@ static int cliSendCommand(int argc, char **argv, int repeat) { if (retval) { return retval; } + if (!config.raw_output && config.tty) { + printf("\n"); + } } return 0; } @@ -493,6 +497,7 @@ int main(int argc, char **argv) { config.auth = NULL; config.historyfile = NULL; config.tty = 1; + config.mb_sep = '\n'; if (getenv("HOME") != NULL) { config.historyfile = malloc(256); @@ -517,7 +522,7 @@ int main(int argc, char **argv) { repl(); } - config.tty = isatty(stdout) || (getenv("FAKETTY") != NULL); + config.tty = isatty(fileno(stdout)) || (getenv("FAKETTY") != NULL); argvcopy = convertToSds(argc+1, argv); if (config.argn_from_stdin) { sds lastarg = readArgFromStdin(); diff --git a/tests/integration/redis-cli.tcl b/tests/integration/redis-cli.tcl index b9c4f8e1..c4954304 100644 --- a/tests/integration/redis-cli.tcl +++ b/tests/integration/redis-cli.tcl @@ -131,4 +131,26 @@ start_server {tags {"cli"}} { r rpush list bar assert_equal "1. \"foo\"\n2. \"bar\"\n" [run_tty_cli lrange list 0 -1] } + + test_nontty_cli "Status reply" { + assert_equal "OK" [run_nontty_cli set key bar] + assert_equal "bar" [r get key] + } + + test_nontty_cli "Integer reply" { + r del counter + assert_equal "1" [run_nontty_cli incr counter] + } + + test_nontty_cli "Bulk reply" { + r set key "tab\tnewline\n" + assert_equal "tab\tnewline\n" [run_nontty_cli get key] + } + + test_nontty_cli "Multi-bulk reply" { + r del list + r rpush list foo + r rpush list bar + assert_equal "foo\nbar" [run_nontty_cli lrange list 0 -1] + } } -- 2.47.2