Skip to content

Commit 5adf98a

Browse files
committed
Add psql '\pset format wrapped' mode to wrap output to screen width, or
file/pipe output too if \pset columns' is set. Bryce Nesbitt
1 parent eb915ca commit 5adf98a

File tree

6 files changed

+445
-198
lines changed

6 files changed

+445
-198
lines changed

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

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.202 2008/05/08 00:27:57 momjian Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.203 2008/05/08 17:04:26 momjian Exp $
33
PostgreSQL documentation
44
-->
55

@@ -1514,7 +1514,8 @@ lo_import 152801
15141514
<listitem>
15151515
<para>
15161516
Sets the output format to one of <literal>unaligned</literal>,
1517-
<literal>aligned</literal>, <literal>html</literal>,
1517+
<literal>aligned</literal>, <literal>wrapped</literal>,
1518+
<literal>html</literal>,
15181519
<literal>latex</literal>, or <literal>troff-ms</literal>.
15191520
Unique abbreviations are allowed. (That would mean one letter
15201521
is enough.)
@@ -1526,8 +1527,21 @@ lo_import 152801
15261527
is intended to create output that might be intended to be read
15271528
in by other programs (tab-separated, comma-separated).
15281529
<quote>Aligned</quote> mode is the standard, human-readable,
1529-
nicely formatted text output that is default. The
1530-
<quote><acronym>HTML</acronym></quote> and
1530+
nicely formatted text output that is default.
1531+
</para>
1532+
1533+
<para>
1534+
<quote>Wrapped</quote> is like <literal>aligned</> but wraps
1535+
output to the specified width. If <literal>\pset columns</> is
1536+
zero (the default), <literal>wrapped</> mode only affects screen
1537+
output and wrapped width is controlled by the environment
1538+
variable <envar>COLUMNS</> or the detected screen width. If
1539+
<literal>\pset columns</> is set to a non-zero value, all output
1540+
is wrapped, including file and pipe output.
1541+
</para>
1542+
1543+
<para>
1544+
The <quote><acronym>HTML</acronym></quote> and
15311545
<quote>LaTeX</quote> modes put out tables that are intended to
15321546
be included in documents using the respective mark-up
15331547
language. They are not complete documents! (This might not be
@@ -1537,6 +1551,17 @@ lo_import 152801
15371551
</listitem>
15381552
</varlistentry>
15391553

1554+
<varlistentry>
1555+
<term><literal>columns</literal></term>
1556+
<listitem>
1557+
<para>
1558+
Controls the target width for the <literal>wrapped</> format.
1559+
Zero (the default) causes the <literal>wrapped</> format to
1560+
affect only screen output.
1561+
</para>
1562+
</listitem>
1563+
</varlistentry>
1564+
15401565
<varlistentry>
15411566
<term><literal>border</literal></term>
15421567
<listitem>
@@ -2706,6 +2731,18 @@ $endif
27062731
<title>Environment</title>
27072732

27082733
<variablelist>
2734+
2735+
<varlistentry>
2736+
<term><envar>COLUMNS</envar></term>
2737+
2738+
<listitem>
2739+
<para>
2740+
Used for the <literal>wrapped</> output format if
2741+
<literal>\pset columns</> is zero.
2742+
</para>
2743+
</listitem>
2744+
</varlistentry>
2745+
27092746
<varlistentry>
27102747
<term><envar>PAGER</envar></term>
27112748

src/bin/psql/command.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2008, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.187 2008/05/02 09:27:50 petere Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.188 2008/05/08 17:04:26 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "command.h"
@@ -1502,6 +1502,9 @@ _align2string(enum printFormat in)
15021502
case PRINT_ALIGNED:
15031503
return "aligned";
15041504
break;
1505+
case PRINT_WRAPPED:
1506+
return "wrapped";
1507+
break;
15051508
case PRINT_HTML:
15061509
return "html";
15071510
break;
@@ -1535,6 +1538,8 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
15351538
popt->topt.format = PRINT_UNALIGNED;
15361539
else if (pg_strncasecmp("aligned", value, vallen) == 0)
15371540
popt->topt.format = PRINT_ALIGNED;
1541+
else if (pg_strncasecmp("wrapped", value, vallen) == 0)
1542+
popt->topt.format = PRINT_WRAPPED;
15381543
else if (pg_strncasecmp("html", value, vallen) == 0)
15391544
popt->topt.format = PRINT_HTML;
15401545
else if (pg_strncasecmp("latex", value, vallen) == 0)
@@ -1543,7 +1548,7 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
15431548
popt->topt.format = PRINT_TROFF_MS;
15441549
else
15451550
{
1546-
psql_error("\\pset: allowed formats are unaligned, aligned, html, latex, troff-ms\n");
1551+
psql_error("\\pset: allowed formats are unaligned, aligned, wrapped, html, latex, troff-ms\n");
15471552
return false;
15481553
}
15491554

@@ -1724,6 +1729,16 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
17241729
}
17251730
}
17261731

1732+
/* set border style/width */
1733+
else if (strcmp(param, "columns") == 0)
1734+
{
1735+
if (value)
1736+
popt->topt.columns = atoi(value);
1737+
1738+
if (!quiet)
1739+
printf(_("Target width for \"wrapped\" format is %d.\n"), popt->topt.columns);
1740+
}
1741+
17271742
else
17281743
{
17291744
psql_error("\\pset: unknown option: %s\n", param);

src/bin/psql/mbprint.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2008, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/mbprint.c,v 1.30 2008/04/16 18:18:00 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/mbprint.c,v 1.31 2008/05/08 17:04:26 momjian Exp $
77
*
88
* XXX this file does not really belong in psql/. Perhaps move to libpq?
99
* It also seems that the mbvalidate function is redundant with existing
@@ -204,8 +204,8 @@ pg_wcswidth(const unsigned char *pwcs, size_t len, int encoding)
204204
/*
205205
* pg_wcssize takes the given string in the given encoding and returns three
206206
* values:
207-
* result_width: Width in display character of longest line in string
208-
* result_height: Number of lines in display output
207+
* result_width: Width in display characters of the longest line in string
208+
* result_height: Number of newlines in display output
209209
* result_format_size: Number of bytes required to store formatted representation of string
210210
*/
211211
int
@@ -279,9 +279,14 @@ pg_wcssize(unsigned char *pwcs, size_t len, int encoding, int *result_width,
279279
return width;
280280
}
281281

282+
/*
283+
* Filter out unprintable characters, companion to wcs_size.
284+
* Break input into lines based on \n. lineptr[i].ptr == NULL
285+
* indicates the end of the array.
286+
*/
282287
void
283288
pg_wcsformat(unsigned char *pwcs, size_t len, int encoding,
284-
struct lineptr * lines, int count)
289+
struct lineptr *lines, int count)
285290
{
286291
int w,
287292
chlen = 0;
@@ -307,6 +312,7 @@ pg_wcsformat(unsigned char *pwcs, size_t len, int encoding,
307312
if (count == 0)
308313
exit(1); /* Screwup */
309314

315+
/* make next line point to remaining memory */
310316
lines->ptr = ptr;
311317
}
312318
else if (*pwcs == '\r') /* Linefeed */
@@ -353,12 +359,13 @@ pg_wcsformat(unsigned char *pwcs, size_t len, int encoding,
353359
}
354360
len -= chlen;
355361
}
356-
*ptr++ = '\0';
357362
lines->width = linewidth;
358-
lines++;
359-
count--;
360-
if (count > 0)
361-
lines->ptr = NULL;
363+
*ptr++ = '\0'; /* Terminate formatted string */
364+
365+
if (count == 0)
366+
exit(1); /* Screwup */
367+
368+
(lines+1)->ptr = NULL; /* terminate line array */
362369
}
363370

364371
unsigned char *

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy