Change output format for non-tty redis-cli execution

This commit is contained in:
Pieter Noordhuis 2010-08-04 17:46:56 +02:00
parent 123a10f7a5
commit 3a51bff035
2 changed files with 34 additions and 7 deletions

View File

@ -61,6 +61,7 @@ static struct config {
int pubsub_mode; int pubsub_mode;
int raw_output; /* output mode per command */ int raw_output; /* output mode per command */
int tty; /* flag for default output format */ int tty; /* flag for default output format */
char mb_sep;
char *auth; char *auth;
char *historyfile; char *historyfile;
} config; } config;
@ -108,7 +109,7 @@ static int cliReadSingleLineReply(int fd, int quiet) {
if (reply == NULL) return 1; if (reply == NULL) return 1;
if (!quiet) if (!quiet)
printf("%s\n", reply); printf("%s", reply);
sdsfree(reply); sdsfree(reply);
return 0; return 0;
} }
@ -162,7 +163,6 @@ static int cliReadBulkReply(int fd) {
/* If you are producing output for the standard output we want /* If you are producing output for the standard output we want
* a more interesting output with quoted characters and so forth */ * a more interesting output with quoted characters and so forth */
printStringRepr(reply,bulklen); printStringRepr(reply,bulklen);
printf("\n");
} }
zfree(reply); zfree(reply);
return 0; return 0;
@ -183,8 +183,9 @@ static int cliReadMultiBulkReply(int fd) {
printf("(empty list or set)\n"); printf("(empty list or set)\n");
} }
while(elements--) { while(elements--) {
printf("%d. ", c); if (config.tty) printf("%d. ", c);
if (cliReadReply(fd)) return 1; if (cliReadReply(fd)) return 1;
if (elements) printf("%c",config.mb_sep);
c++; c++;
} }
return 0; return 0;
@ -199,13 +200,13 @@ static int cliReadReply(int fd) {
} }
switch(type) { switch(type) {
case '-': case '-':
printf("(error) "); if (config.tty) printf("(error) ");
cliReadSingleLineReply(fd,0); cliReadSingleLineReply(fd,0);
return 1; return 1;
case '+': case '+':
return cliReadSingleLineReply(fd,0); return cliReadSingleLineReply(fd,0);
case ':': case ':':
printf("(integer) "); if (config.tty) printf("(integer) ");
return cliReadSingleLineReply(fd,0); return cliReadSingleLineReply(fd,0);
case '$': case '$':
return cliReadBulkReply(fd); 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"); printf("Reading messages... (press Ctrl-c to quit)\n");
while (1) { while (1) {
cliReadReply(fd); cliReadReply(fd);
printf("\n"); printf("\n\n");
} }
} }
@ -283,6 +284,9 @@ static int cliSendCommand(int argc, char **argv, int repeat) {
if (retval) { if (retval) {
return retval; return retval;
} }
if (!config.raw_output && config.tty) {
printf("\n");
}
} }
return 0; return 0;
} }
@ -493,6 +497,7 @@ int main(int argc, char **argv) {
config.auth = NULL; config.auth = NULL;
config.historyfile = NULL; config.historyfile = NULL;
config.tty = 1; config.tty = 1;
config.mb_sep = '\n';
if (getenv("HOME") != NULL) { if (getenv("HOME") != NULL) {
config.historyfile = malloc(256); config.historyfile = malloc(256);
@ -517,7 +522,7 @@ int main(int argc, char **argv) {
repl(); repl();
} }
config.tty = isatty(stdout) || (getenv("FAKETTY") != NULL); config.tty = isatty(fileno(stdout)) || (getenv("FAKETTY") != NULL);
argvcopy = convertToSds(argc+1, argv); argvcopy = convertToSds(argc+1, argv);
if (config.argn_from_stdin) { if (config.argn_from_stdin) {
sds lastarg = readArgFromStdin(); sds lastarg = readArgFromStdin();

View File

@ -131,4 +131,26 @@ start_server {tags {"cli"}} {
r rpush list bar r rpush list bar
assert_equal "1. \"foo\"\n2. \"bar\"\n" [run_tty_cli lrange list 0 -1] 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]
}
} }