Search Postgresql Archives

Re: Request - repeat value of \pset title during \watch interations

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Jan 12, 2016 at 1:15 AM, David G. Johnston
<david.g.johnston@xxxxxxxxx> wrote:
> On Mon, Jan 11, 2016 at 9:03 AM, Tom Lane <tgl@xxxxxxxxxxxxx> wrote:
>>
>> "David G. Johnston" <david.g.johnston@xxxxxxxxx> writes:
>> > On Mon, Jan 11, 2016 at 8:14 AM, Tom Lane <tgl@xxxxxxxxxxxxx> wrote:
>> >> Perhaps we should replace the "Watch every Ns" text by the user-given
>> >> title if a title has been set?  That would conserve screen space.
>>
>> > The extra line doesn't both me and given the length of the timestamp I
>> > suspect many titles would cause the combined line length to exceed
>> > terminal
>> > width and cause wrapping anyway.  In my specific case it would though I
>> > am
>> > using an abnormally narrow width.
>>
>> You speak as though the title will be chosen without any regard for the
>> context it's used in, which I rather doubt.  Wouldn't people pick the
>> title for a \watch query so that it fits?  (In any case they could
>> force the issue by including a \n in their title...)
>>
>
> True that.
>
> I don't have a strong opinion either way.  Having a single, condensed, title
> line would be nice though using two in order to not be cryptic has its own
> appeal.

Just looking at that I just hacked the attached that enforces \n at
the end of the user-defined title (that's easily changeable):
=# \pset title 'foo bar'
Title is "foo bar".
=# select 1;
 foo bar
 ?column?
----------
        1
(1 row)
=# \watch 1
foo bar
Watch every 1s    Tue Jan 12 13:54:04 2016

 ?column?
----------
        1
(1 row)
-- 
Michael
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 9750a5b..9612c8a 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -3020,7 +3020,8 @@ static bool
 do_watch(PQExpBuffer query_buf, long sleep)
 {
 	printQueryOpt myopt = pset.popt;
-	char		title[50];
+	char		 *title;
+	bool		 *head_title = NULL;
 
 	if (!query_buf || query_buf->len <= 0)
 	{
@@ -3034,6 +3035,18 @@ do_watch(PQExpBuffer query_buf, long sleep)
 	 */
 	myopt.topt.pager = 0;
 
+	/*
+	 * Take into account any title present in the user setup as a part of
+	 * what is printed for each iteration by using it as a header.
+	 */
+	if (myopt.title)
+	{
+		title = pg_malloc(strlen(myopt.title) + 50);
+		head_title = pg_strdup(myopt.title);
+	}
+	else
+		title = pg_malloc(50);
+
 	for (;;)
 	{
 		int			res;
@@ -3045,8 +3058,13 @@ do_watch(PQExpBuffer query_buf, long sleep)
 		 * of completion of the command?
 		 */
 		timer = time(NULL);
-		snprintf(title, sizeof(title), _("Watch every %lds\t%s"),
-				 sleep, asctime(localtime(&timer)));
+		if (head_title)
+			snprintf(title, strlen(myopt.title) + 50,
+					 _("%s\nWatch every %lds\t%s"),
+					 head_title, sleep, asctime(localtime(&timer)));
+		else
+			snprintf(title, 50, _("Watch every %lds\t%s"),
+					 sleep, asctime(localtime(&timer)));
 		myopt.title = title;
 
 		/* Run the query and print out the results */
@@ -3059,7 +3077,11 @@ do_watch(PQExpBuffer query_buf, long sleep)
 		if (res == 0)
 			break;
 		if (res == -1)
+		{
+			pg_free(title);
+			pg_free(head_title);
 			return false;
+		}
 
 		/*
 		 * Set up cancellation of 'watch' via SIGINT.  We redo this each time
@@ -3084,6 +3106,8 @@ do_watch(PQExpBuffer query_buf, long sleep)
 		sigint_interrupt_enabled = false;
 	}
 
+	pg_free(title);
+	pg_free(head_title);
 	return true;
 }
 
-- 
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux