git.postgresql.org Git - postgresql.git/commitdiff

git projects / postgresql.git / commitdiff
? search:
summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 92b7902)
Improve header output from psql's \watch command.
2016年3月21日 22:18:13 +0000 (18:18 -0400)
2016年3月21日 22:18:13 +0000 (18:18 -0400)
Include the \pset title string if there is one, and shorten the prefab
part of the header to be "timestamp (every Ns)". Per suggestion by
David Johnston.

Michael Paquier and Tom Lane


diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index 8a8580411eee1eaac123639addcdb014a779cd60..385cb599278a4885ad93848ff1d33b3462fa288f 100644 (file)
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -2673,9 +2673,11 @@ testdb=&gt; <userinput>\setenv LESS -imx4F</userinput>
<term><literal>\watch [ <replaceable class="parameter">seconds</replaceable> ]</literal></term>
<listitem>
<para>
- Repeatedly execute the current query buffer (like <literal>\g</>)
+ Repeatedly execute the current query buffer (as <literal>\g</> does)
until interrupted or the query fails. Wait the specified number of
- seconds (default 2) between executions.
+ seconds (default 2) between executions. Each query result is
+ displayed with a header that includes the <literal>\pset title</>
+ string (if any), the time as of query start, and the delay interval.
</para>
</listitem>
</varlistentry>
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index eef6e4bd0bd0ef4dbb044e863815eebc0832bb4f..6319b030c7a61d55eabf30d7c472b3a12154c2c0 100644 (file)
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -3020,7 +3020,10 @@ static bool
do_watch(PQExpBuffer query_buf, long sleep)
{
printQueryOpt myopt = pset.popt;
- char title[50];
+ const char *user_title;
+ char *title;
+ int title_len;
+ int res = 0;
if (!query_buf || query_buf->len <= 0)
{
@@ -3034,19 +3037,38 @@ do_watch(PQExpBuffer query_buf, long sleep)
*/
myopt.topt.pager = 0;
+ /*
+ * If there's a title in the user configuration, make sure we have room
+ * for it in the title buffer.
+ */
+ user_title = myopt.title;
+ title_len = (user_title ? strlen(user_title) : 0) + 100;
+ title = pg_malloc(title_len);
+
for (;;)
{
- int res;
time_t timer;
+ char asctimebuf[64];
long i;
/*
- * Prepare title for output. XXX would it be better to use the time
- * of completion of the command?
+ * Prepare title for output. Note that we intentionally include a
+ * newline at the end of the title; this is somewhat historical but it
+ * makes for reasonably nicely formatted output in simple cases.
*/
timer = time(NULL);
- snprintf(title, sizeof(title), _("Watch every %lds\t%s"),
- sleep, asctime(localtime(&timer)));
+ strlcpy(asctimebuf, asctime(localtime(&timer)), sizeof(asctimebuf));
+ /* strip trailing newline from asctime's output */
+ i = strlen(asctimebuf);
+ while (i > 0 && asctimebuf[--i] == '\n')
+ asctimebuf[i] = '0円';
+
+ if (user_title)
+ snprintf(title, title_len, _("%s\t%s (every %lds)\n"),
+ user_title, asctimebuf, sleep);
+ else
+ snprintf(title, title_len, _("%s (every %lds)\n"),
+ asctimebuf, sleep);
myopt.title = title;
/* Run the query and print out the results */
@@ -3056,10 +3078,8 @@ do_watch(PQExpBuffer query_buf, long sleep)
* PSQLexecWatch handles the case where we can no longer repeat the
* query, and returns 0 or -1.
*/
- if (res == 0)
+ if (res <= 0)
break;
- if (res == -1)
- return false;
/*
* Set up cancellation of 'watch' via SIGINT. We redo this each time
@@ -3084,7 +3104,8 @@ do_watch(PQExpBuffer query_buf, long sleep)
sigint_interrupt_enabled = false;
}
- return true;
+ pg_free(title);
+ return (res >= 0);
}
/*
This is the main PostgreSQL git repository.
RSS Atom

AltStyle によって変換されたページ (->オリジナル) /