so 11. 4. 2020 v 8:54 odesÃlatel Pavel Stehule <pavel.steh...@gmail.com> napsal:
> Hi > > Now, the content of redirect output has two parts > > 1. tabular output > 2. cmd tags > > There is a problem with command tags, because it is specific kind of > information and can be nice if can be redirected to stdout every time like > \h output. There can be new psql variable like REDIRECTED_OUTPUT with > possibilities (all, tabular) > > What do you think about this? > or different method - set target of status row - with result (default) or stdout (terminal) patch assigned When I pin status rows just to stdout, then redirected output contains only query results Regards Pavel > Pavel >
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index 621a33f7e8..2b722eb2de 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1072,17 +1072,20 @@ static void PrintQueryStatus(PGresult *results) { char buf[16]; + FILE *fp; + + fp = pset.status_target == PSQL_STATUS_RESULT ? pset.queryFout : stdout; if (!pset.quiet) { if (pset.popt.topt.format == PRINT_HTML) { - fputs("<p>", pset.queryFout); - html_escaped_print(PQcmdStatus(results), pset.queryFout); - fputs("</p>\n", pset.queryFout); + fputs("<p>", fp); + html_escaped_print(PQcmdStatus(results), fp); + fputs("</p>\n", fp); } else - fprintf(pset.queryFout, "%s\n", PQcmdStatus(results)); + fprintf(fp, "%s\n", PQcmdStatus(results)); } if (pset.logfile) diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h index 97941aa10c..2299cb2b6d 100644 --- a/src/bin/psql/settings.h +++ b/src/bin/psql/settings.h @@ -70,6 +70,12 @@ typedef enum hctl_ignoreboth = hctl_ignorespace | hctl_ignoredups } HistControl; +typedef enum +{ + PSQL_STATUS_STDOUT, + PSQL_STATUS_RESULT +} PSQL_STATUS_TARGET; + enum trivalue { TRI_DEFAULT, @@ -135,6 +141,7 @@ typedef struct _psqlSettings PSQL_ECHO_HIDDEN echo_hidden; PSQL_ERROR_ROLLBACK on_error_rollback; PSQL_COMP_CASE comp_case; + PSQL_STATUS_TARGET status_target; HistControl histcontrol; const char *prompt1; const char *prompt2; diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index 3302bd4dd3..e6b0ea91aa 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -1076,6 +1076,30 @@ histcontrol_hook(const char *newval) return true; } +static char * +status_target_substitute_hook(char *newVal) +{ + if (newVal == NULL) + newVal = pg_strdup("result"); + return newVal; +} + +static bool +status_target_hook(const char *newVal) +{ + Assert(newVal != NULL); + if (pg_strcasecmp(newVal, "stdout") == 0) + pset.status_target = PSQL_STATUS_STDOUT; + else if (pg_strcasecmp(newVal, "result") == 0) + pset.status_target = PSQL_STATUS_RESULT; + else + { + PsqlVarEnumError("STATUS_TARGET", newVal, "result, stdout"); + return false; + } + return true; +} + static bool prompt1_hook(const char *newval) { @@ -1228,4 +1252,7 @@ EstablishVariableSpace(void) SetVariableHooks(pset.vars, "HIDE_TABLEAM", bool_substitute_hook, hide_tableam_hook); + SetVariableHooks(pset.vars, "STATUS_TARGET", + status_target_substitute_hook, + status_target_hook); } diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index 0e7a373caf..882baf553e 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -3879,6 +3879,8 @@ psql_completion(const char *text, int start, int end) COMPLETE_WITH_CS("on", "off", "interactive"); else if (TailMatchesCS("SHOW_CONTEXT")) COMPLETE_WITH_CS("never", "errors", "always"); + else if (TailMatchesCS("STATUS_TARGET")) + COMPLETE_WITH_CS("result", "stdout"); else if (TailMatchesCS("VERBOSITY")) COMPLETE_WITH_CS("default", "verbose", "terse", "sqlstate"); }