@@ -3020,7 +3020,10 @@ static bool
30203020do_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