提交 123a10f7 编写于 作者: P Pieter Noordhuis

Let the output mode depend on having a tty or not

上级 07242c0c
...@@ -60,6 +60,7 @@ static struct config { ...@@ -60,6 +60,7 @@ static struct config {
int monitor_mode; int monitor_mode;
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 */
char *auth; char *auth;
char *historyfile; char *historyfile;
} config; } config;
...@@ -152,7 +153,7 @@ static int cliReadBulkReply(int fd) { ...@@ -152,7 +153,7 @@ static int cliReadBulkReply(int fd) {
reply = zmalloc(bulklen); reply = zmalloc(bulklen);
anetRead(fd,reply,bulklen); anetRead(fd,reply,bulklen);
anetRead(fd,crlf,2); anetRead(fd,crlf,2);
if (config.raw_output) { if (config.raw_output || !config.tty) {
if (bulklen && fwrite(reply,bulklen,1,stdout) == 0) { if (bulklen && fwrite(reply,bulklen,1,stdout) == 0) {
zfree(reply); zfree(reply);
return 1; return 1;
...@@ -491,6 +492,7 @@ int main(int argc, char **argv) { ...@@ -491,6 +492,7 @@ int main(int argc, char **argv) {
config.raw_output = 0; config.raw_output = 0;
config.auth = NULL; config.auth = NULL;
config.historyfile = NULL; config.historyfile = NULL;
config.tty = 1;
if (getenv("HOME") != NULL) { if (getenv("HOME") != NULL) {
config.historyfile = malloc(256); config.historyfile = malloc(256);
...@@ -515,6 +517,7 @@ int main(int argc, char **argv) { ...@@ -515,6 +517,7 @@ int main(int argc, char **argv) {
repl(); repl();
} }
config.tty = isatty(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();
......
...@@ -41,7 +41,7 @@ start_server {tags {"cli"}} { ...@@ -41,7 +41,7 @@ start_server {tags {"cli"}} {
close_cli $fd close_cli $fd
} }
proc run_cli {args} { proc run_nontty_cli {args} {
set fd [open [format "|src/redis-cli -p %d -n 9 $args" [srv port]] "r"] set fd [open [format "|src/redis-cli -p %d -n 9 $args" [srv port]] "r"]
fconfigure $fd -buffering none fconfigure $fd -buffering none
fconfigure $fd -translation binary fconfigure $fd -translation binary
...@@ -50,8 +50,19 @@ start_server {tags {"cli"}} { ...@@ -50,8 +50,19 @@ start_server {tags {"cli"}} {
set _ $resp set _ $resp
} }
proc test_noninteractive_cli {name code} { proc test_nontty_cli {name code} {
test "Non-interactive CLI: $name" $code test "Non-interactive non-TTY CLI: $name" $code
}
proc run_tty_cli {args} {
set ::env(FAKETTY) 1
set resp [run_nontty_cli {*}$args]
unset ::env(FAKETTY)
set _ $resp
}
proc test_tty_cli {name code} {
test "Non-interactive TTY CLI: $name" $code
} }
test_interactive_cli "INFO response should be printed raw" { test_interactive_cli "INFO response should be printed raw" {
...@@ -99,25 +110,25 @@ start_server {tags {"cli"}} { ...@@ -99,25 +110,25 @@ start_server {tags {"cli"}} {
assert_equal "bar" [r get key] assert_equal "bar" [r get key]
} }
test_noninteractive_cli "Status reply" { test_tty_cli "Status reply" {
assert_equal "OK\n" [run_cli set key bar] assert_equal "OK\n" [run_tty_cli set key bar]
assert_equal "bar" [r get key] assert_equal "bar" [r get key]
} }
test_noninteractive_cli "Integer reply" { test_tty_cli "Integer reply" {
r del counter r del counter
assert_equal "(integer) 1\n" [run_cli incr counter] assert_equal "(integer) 1\n" [run_tty_cli incr counter]
} }
test_noninteractive_cli "Bulk reply" { test_tty_cli "Bulk reply" {
r set key "tab\tnewline\n" r set key "tab\tnewline\n"
assert_equal "\"tab\\tnewline\\n\"\n" [run_cli get key] assert_equal "\"tab\\tnewline\\n\"\n" [run_tty_cli get key]
} }
test_noninteractive_cli "Multi-bulk reply" { test_tty_cli "Multi-bulk reply" {
r del list r del list
r rpush list foo r rpush list foo
r rpush list bar r rpush list bar
assert_equal "1. \"foo\"\n2. \"bar\"\n" [run_cli lrange list 0 -1] assert_equal "1. \"foo\"\n2. \"bar\"\n" [run_tty_cli lrange list 0 -1]
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册