On Wed, Sep 30, 2009 at 01:30:17PM -0400, Andrew Dunstan wrote:
>
>
> Peter Eisentraut wrote:
>> On Wed, 2009-09-30 at 11:03 -0400, Andrew Dunstan wrote:
>>   
>>> Thinking about this some more, ISTM a much better way of approaching 
>>> it would be to provide a flag for psql to turn off the fancy 
>>> formatting, and have pg_regress use that flag.
>>>     
>>
>> Well, it might not be a bad idea, but adding a feature just to satisfy
>> the test suite instead of fixing the test suite doesn't feel satisfying.
>> Is there another use case?
>
> Sure, as Tom noted pg_regress probably won't be the only user. There are  
> lots of legacy scripts out there that parse psql output, and it should  
> be of use to them.

The attached patch implements this feature.  It adds a
--no-pretty-formatting/-G option to psql (naming isn't my forté,
so feel free to change it!).  This is also documented in the
SGML docs, and help text.  Lastly, this option is used when invoking
psql by pg_regress, which results in a working testsuite in a UTF-8
locale.

Hopefully this is OK!  I'll be happy to make any amendments required.


Regards,
Roger

-- 
  .''`.  Roger Leigh
 : :' :  Debian GNU/Linux             http://people.debian.org/~rleigh/
 `. `'   Printing on GNU/Linux?       http://gutenprint.sourceforge.net/
   `-    GPG Public Key: 0x25BFB848   Please GPG sign your mail.
diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index 85e9375..95f1365 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -192,6 +192,19 @@ PostgreSQL documentation
     </varlistentry>
 
     <varlistentry>
+      <term><option>-G</option></term>
+      <term><option>--no-pretty-formatting</option></term>
+      <para>
+      Only use ASCII characters to draw tables. Without this option,
+      box drawing characters may be used if avilable which will result
+      in nicer output.  However, if compatibility with the output of
+      older psql versions, or psql used in other locales is required, it
+      may be desirable to disable pretty table formatting in these
+      situations.
+      </para>
+    </varlistentry>
+
+    <varlistentry>
       <term><option>-h <replaceable class="parameter">hostname</replaceable></></term>
       <term><option>--host <replaceable class="parameter">hostname</replaceable></></term>
       <listitem>
diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c
index d498d58..d596ece 100644
--- a/src/bin/psql/help.c
+++ b/src/bin/psql/help.c
@@ -121,6 +121,8 @@ usage(void)
 	   "                           set field separator (default: \"%s\")\n"),
 		   DEFAULT_FIELD_SEP);
 	printf(_("  -H, --html               HTML table output mode\n"));
+	printf(_("  -G, --no-pretty-formatting\n"
+	"                           only use ASCII characters to format tables\n"));
 	printf(_("  -P, --pset=VAR[=ARG]     set printing option VAR to ARG (see \\pset command)\n"));
 	printf(_("  -R, --record-separator=STRING\n"
 	"                           set record separator (default: newline)\n"));
diff --git a/src/bin/psql/print.c b/src/bin/psql/print.c
index 41d508e..e01e512 100644
--- a/src/bin/psql/print.c
+++ b/src/bin/psql/print.c
@@ -2290,9 +2290,10 @@ printTable(const printTableContent *cont, FILE *fout, FILE *flog)
 	const printTextFormat	*text_format = &asciiformat;
 
 #if (defined(HAVE_LANGINFO_H) && defined(CODESET))
-	if (pg_strcasecmp(nl_langinfo(CODESET), "UTF-8") == 0 ||
-	    pg_strcasecmp(nl_langinfo(CODESET), "utf8") == 0 ||
-	    pg_strcasecmp(nl_langinfo(CODESET), "CP65001") == 0)
+	if ((pg_strcasecmp(nl_langinfo(CODESET), "UTF-8") == 0 ||
+	     pg_strcasecmp(nl_langinfo(CODESET), "utf8") == 0 ||
+	     pg_strcasecmp(nl_langinfo(CODESET), "CP65001") == 0) &&
+	    cont->opt->use_text_format)
 		text_format = &utf8format;
 #endif
 
diff --git a/src/bin/psql/print.h b/src/bin/psql/print.h
index ea1dc97..97772a6 100644
--- a/src/bin/psql/print.h
+++ b/src/bin/psql/print.h
@@ -27,6 +27,7 @@ enum printFormat
 typedef struct printTableOpt
 {
 	enum printFormat format;	/* one of the above */
+	bool		use_text_format; /* use text formats for pretty printing */
 	bool		expanded;		/* expanded/vertical output (if supported by
 								 * output format) */
 	unsigned short int border;	/* Print a border around the table. 0=none,
diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
index 429e5d9..3fdf904 100644
--- a/src/bin/psql/startup.c
+++ b/src/bin/psql/startup.c
@@ -125,6 +125,7 @@ main(int argc, char *argv[])
 
 	/* We rely on unmentioned fields of pset.popt to start out 0/false/NULL */
 	pset.popt.topt.format = PRINT_ALIGNED;
+	pset.popt.topt.use_text_format = 1;
 	pset.popt.topt.border = 1;
 	pset.popt.topt.pager = 1;
 	pset.popt.topt.start_table = true;
@@ -317,6 +318,7 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts * options)
 		{"list", no_argument, NULL, 'l'},
 		{"log-file", required_argument, NULL, 'L'},
 		{"no-readline", no_argument, NULL, 'n'},
+		{"no-pretty-formatting", no_argument, NULL, 'G'},
 		{"single-transaction", no_argument, NULL, '1'},
 		{"output", required_argument, NULL, 'o'},
 		{"port", required_argument, NULL, 'p'},
@@ -346,7 +348,7 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts * options)
 
 	memset(options, 0, sizeof *options);
 
-	while ((c = getopt_long(argc, argv, "aAc:d:eEf:F:h:HlL:no:p:P:qR:sStT:U:v:VwWxX?1",
+	while ((c = getopt_long(argc, argv, "aAc:d:eEf:GF:h:HlL:no:p:P:qR:sStT:U:v:VwWxX?1",
 							long_options, &optindex)) != -1)
 	{
 		switch (c)
@@ -383,6 +385,9 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts * options)
 			case 'F':
 				pset.popt.topt.fieldSep = pg_strdup(optarg);
 				break;
+			case 'G':
+				pset.popt.topt.use_text_format = false;
+				break;
 			case 'h':
 				options->host = optarg;
 				break;
diff --git a/src/test/regress/pg_regress_main.c b/src/test/regress/pg_regress_main.c
index cb477a6..2c2762b 100644
--- a/src/test/regress/pg_regress_main.c
+++ b/src/test/regress/pg_regress_main.c
@@ -59,7 +59,7 @@ psql_start_test(const char *testname,
 	add_stringlist_item(expectfiles, expectfile);
 
 	snprintf(psql_cmd, sizeof(psql_cmd),
-			 SYSTEMQUOTE "\"%s%spsql\" -X -a -q -d \"%s\" < \"%s\" > \"%s\" 2>&1" SYSTEMQUOTE,
+			 SYSTEMQUOTE "\"%s%spsql\" -X -a -q -G -d \"%s\" < \"%s\" > \"%s\" 2>&1" SYSTEMQUOTE,
 			 psqldir ? psqldir : "",
 			 psqldir ? "/" : "",
 			 dblist->str,
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to