@@ -3020,7 +3020,10 @@ static bool
3020
3020
do_watch (PQExpBuffer query_buf , long sleep )
3021
3021
{
3022
3022
printQueryOpt myopt = pset .popt ;
3023
- char title [50 ];
3023
+ const char * user_title ;
3024
+ char * title ;
3025
+ int title_len ;
3026
+ int res = 0 ;
3024
3027
3025
3028
if (!query_buf || query_buf -> len <= 0 )
3026
3029
{
@@ -3034,19 +3037,38 @@ do_watch(PQExpBuffer query_buf, long sleep)
3034
3037
*/
3035
3038
myopt .topt .pager = 0 ;
3036
3039
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
+
3037
3048
for (;;)
3038
3049
{
3039
- int res ;
3040
3050
time_t timer ;
3051
+ char asctimebuf [64 ];
3041
3052
long i ;
3042
3053
3043
3054
/*
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.
3046
3058
*/
3047
3059
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 );
3050
3072
myopt .title = title ;
3051
3073
3052
3074
/* Run the query and print out the results */
@@ -3056,10 +3078,8 @@ do_watch(PQExpBuffer query_buf, long sleep)
3056
3078
* PSQLexecWatch handles the case where we can no longer repeat the
3057
3079
* query, and returns 0 or -1.
3058
3080
*/
3059
- if (res = = 0 )
3081
+ if (res < = 0 )
3060
3082
break ;
3061
- if (res == -1 )
3062
- return false;
3063
3083
3064
3084
/*
3065
3085
* Set up cancellation of 'watch' via SIGINT. We redo this each time
@@ -3084,7 +3104,8 @@ do_watch(PQExpBuffer query_buf, long sleep)
3084
3104
sigint_interrupt_enabled = false;
3085
3105
}
3086
3106
3087
- return true;
3107
+ pg_free (title );
3108
+ return (res >= 0 );
3088
3109
}
3089
3110
3090
3111
/*
0 commit comments