Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit dea2b59

Browse files
committed
Improve header output from psql's \watch command.
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
1 parent 92b7902 commit dea2b59

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

doc/src/sgml/ref/psql-ref.sgml

+4-2
Original file line numberDiff line numberDiff line change
@@ -2673,9 +2673,11 @@ testdb=&gt; <userinput>\setenv LESS -imx4F</userinput>
26732673
<term><literal>\watch [ <replaceable class="parameter">seconds</replaceable> ]</literal></term>
26742674
<listitem>
26752675
<para>
2676-
Repeatedly execute the current query buffer (like <literal>\g</>)
2676+
Repeatedly execute the current query buffer (as <literal>\g</> does)
26772677
until interrupted or the query fails. Wait the specified number of
2678-
seconds (default 2) between executions.
2678+
seconds (default 2) between executions. Each query result is
2679+
displayed with a header that includes the <literal>\pset title</>
2680+
string (if any), the time as of query start, and the delay interval.
26792681
</para>
26802682
</listitem>
26812683
</varlistentry>

src/bin/psql/command.c

+31-10
Original file line numberDiff line numberDiff line change
@@ -3020,7 +3020,10 @@ static bool
30203020
do_watch(PQExpBuffer query_buf, long sleep)
30213021
{
30223022
printQueryOpt myopt = pset.popt;
3023-
char title[50];
3023+
const char *user_title;
3024+
char *title;
3025+
int title_len;
3026+
int res = 0;
30243027

30253028
if (!query_buf || query_buf->len <= 0)
30263029
{
@@ -3034,19 +3037,38 @@ do_watch(PQExpBuffer query_buf, long sleep)
30343037
*/
30353038
myopt.topt.pager = 0;
30363039

3040+
/*
3041+
* If there's a title in the user configuration, make sure we have room
3042+
* for it in the title buffer.
3043+
*/
3044+
user_title = myopt.title;
3045+
title_len = (user_title ? strlen(user_title) : 0) + 100;
3046+
title = pg_malloc(title_len);
3047+
30373048
for (;;)
30383049
{
3039-
int res;
30403050
time_t timer;
3051+
char asctimebuf[64];
30413052
long i;
30423053

30433054
/*
3044-
* Prepare title for output. XXX would it be better to use the time
3045-
* of completion of the command?
3055+
* Prepare title for output. Note that we intentionally include a
3056+
* newline at the end of the title; this is somewhat historical but it
3057+
* makes for reasonably nicely formatted output in simple cases.
30463058
*/
30473059
timer = time(NULL);
3048-
snprintf(title, sizeof(title), _("Watch every %lds\t%s"),
3049-
sleep, asctime(localtime(&timer)));
3060+
strlcpy(asctimebuf, asctime(localtime(&timer)), sizeof(asctimebuf));
3061+
/* strip trailing newline from asctime's output */
3062+
i = strlen(asctimebuf);
3063+
while (i > 0 && asctimebuf[--i] == '\n')
3064+
asctimebuf[i] = '\0';
3065+
3066+
if (user_title)
3067+
snprintf(title, title_len, _("%s\t%s (every %lds)\n"),
3068+
user_title, asctimebuf, sleep);
3069+
else
3070+
snprintf(title, title_len, _("%s (every %lds)\n"),
3071+
asctimebuf, sleep);
30503072
myopt.title = title;
30513073

30523074
/* Run the query and print out the results */
@@ -3056,10 +3078,8 @@ do_watch(PQExpBuffer query_buf, long sleep)
30563078
* PSQLexecWatch handles the case where we can no longer repeat the
30573079
* query, and returns 0 or -1.
30583080
*/
3059-
if (res == 0)
3081+
if (res <= 0)
30603082
break;
3061-
if (res == -1)
3062-
return false;
30633083

30643084
/*
30653085
* Set up cancellation of 'watch' via SIGINT. We redo this each time
@@ -3084,7 +3104,8 @@ do_watch(PQExpBuffer query_buf, long sleep)
30843104
sigint_interrupt_enabled = false;
30853105
}
30863106

3087-
return true;
3107+
pg_free(title);
3108+
return (res >= 0);
30883109
}
30893110

30903111
/*

0 commit comments

Comments
 (0)