Commit 5adf98ae authored by Bruce Momjian's avatar Bruce Momjian

Add psql '\pset format wrapped' mode to wrap output to screen width, or

file/pipe output too if \pset columns' is set.

Bryce Nesbitt
parent eb915caf
<!-- <!--
$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.202 2008/05/08 00:27:57 momjian Exp $ $PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.203 2008/05/08 17:04:26 momjian Exp $
PostgreSQL documentation PostgreSQL documentation
--> -->
...@@ -1514,7 +1514,8 @@ lo_import 152801 ...@@ -1514,7 +1514,8 @@ lo_import 152801
<listitem> <listitem>
<para> <para>
Sets the output format to one of <literal>unaligned</literal>, Sets the output format to one of <literal>unaligned</literal>,
<literal>aligned</literal>, <literal>html</literal>, <literal>aligned</literal>, <literal>wrapped</literal>,
<literal>html</literal>,
<literal>latex</literal>, or <literal>troff-ms</literal>. <literal>latex</literal>, or <literal>troff-ms</literal>.
Unique abbreviations are allowed. (That would mean one letter Unique abbreviations are allowed. (That would mean one letter
is enough.) is enough.)
...@@ -1526,8 +1527,21 @@ lo_import 152801 ...@@ -1526,8 +1527,21 @@ lo_import 152801
is intended to create output that might be intended to be read is intended to create output that might be intended to be read
in by other programs (tab-separated, comma-separated). in by other programs (tab-separated, comma-separated).
<quote>Aligned</quote> mode is the standard, human-readable, <quote>Aligned</quote> mode is the standard, human-readable,
nicely formatted text output that is default. The nicely formatted text output that is default.
<quote><acronym>HTML</acronym></quote> and </para>
<para>
<quote>Wrapped</quote> is like <literal>aligned</> but wraps
output to the specified width. If <literal>\pset columns</> is
zero (the default), <literal>wrapped</> mode only affects screen
output and wrapped width is controlled by the environment
variable <envar>COLUMNS</> or the detected screen width. If
<literal>\pset columns</> is set to a non-zero value, all output
is wrapped, including file and pipe output.
</para>
<para>
The <quote><acronym>HTML</acronym></quote> and
<quote>LaTeX</quote> modes put out tables that are intended to <quote>LaTeX</quote> modes put out tables that are intended to
be included in documents using the respective mark-up be included in documents using the respective mark-up
language. They are not complete documents! (This might not be language. They are not complete documents! (This might not be
...@@ -1537,6 +1551,17 @@ lo_import 152801 ...@@ -1537,6 +1551,17 @@ lo_import 152801
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry>
<term><literal>columns</literal></term>
<listitem>
<para>
Controls the target width for the <literal>wrapped</> format.
Zero (the default) causes the <literal>wrapped</> format to
affect only screen output.
</para>
</listitem>
</varlistentry>
<varlistentry> <varlistentry>
<term><literal>border</literal></term> <term><literal>border</literal></term>
<listitem> <listitem>
...@@ -2706,6 +2731,18 @@ $endif ...@@ -2706,6 +2731,18 @@ $endif
<title>Environment</title> <title>Environment</title>
<variablelist> <variablelist>
<varlistentry>
<term><envar>COLUMNS</envar></term>
<listitem>
<para>
Used for the <literal>wrapped</> output format if
<literal>\pset columns</> is zero.
</para>
</listitem>
</varlistentry>
<varlistentry> <varlistentry>
<term><envar>PAGER</envar></term> <term><envar>PAGER</envar></term>
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright (c) 2000-2008, PostgreSQL Global Development Group * Copyright (c) 2000-2008, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.187 2008/05/02 09:27:50 petere Exp $ * $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.188 2008/05/08 17:04:26 momjian Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
#include "command.h" #include "command.h"
...@@ -1502,6 +1502,9 @@ _align2string(enum printFormat in) ...@@ -1502,6 +1502,9 @@ _align2string(enum printFormat in)
case PRINT_ALIGNED: case PRINT_ALIGNED:
return "aligned"; return "aligned";
break; break;
case PRINT_WRAPPED:
return "wrapped";
break;
case PRINT_HTML: case PRINT_HTML:
return "html"; return "html";
break; break;
...@@ -1535,6 +1538,8 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet) ...@@ -1535,6 +1538,8 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
popt->topt.format = PRINT_UNALIGNED; popt->topt.format = PRINT_UNALIGNED;
else if (pg_strncasecmp("aligned", value, vallen) == 0) else if (pg_strncasecmp("aligned", value, vallen) == 0)
popt->topt.format = PRINT_ALIGNED; popt->topt.format = PRINT_ALIGNED;
else if (pg_strncasecmp("wrapped", value, vallen) == 0)
popt->topt.format = PRINT_WRAPPED;
else if (pg_strncasecmp("html", value, vallen) == 0) else if (pg_strncasecmp("html", value, vallen) == 0)
popt->topt.format = PRINT_HTML; popt->topt.format = PRINT_HTML;
else if (pg_strncasecmp("latex", value, vallen) == 0) else if (pg_strncasecmp("latex", value, vallen) == 0)
...@@ -1543,7 +1548,7 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet) ...@@ -1543,7 +1548,7 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
popt->topt.format = PRINT_TROFF_MS; popt->topt.format = PRINT_TROFF_MS;
else else
{ {
psql_error("\\pset: allowed formats are unaligned, aligned, html, latex, troff-ms\n"); psql_error("\\pset: allowed formats are unaligned, aligned, wrapped, html, latex, troff-ms\n");
return false; return false;
} }
...@@ -1724,6 +1729,16 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet) ...@@ -1724,6 +1729,16 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
} }
} }
/* set border style/width */
else if (strcmp(param, "columns") == 0)
{
if (value)
popt->topt.columns = atoi(value);
if (!quiet)
printf(_("Target width for \"wrapped\" format is %d.\n"), popt->topt.columns);
}
else else
{ {
psql_error("\\pset: unknown option: %s\n", param); psql_error("\\pset: unknown option: %s\n", param);
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright (c) 2000-2008, PostgreSQL Global Development Group * Copyright (c) 2000-2008, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/bin/psql/mbprint.c,v 1.30 2008/04/16 18:18:00 momjian Exp $ * $PostgreSQL: pgsql/src/bin/psql/mbprint.c,v 1.31 2008/05/08 17:04:26 momjian Exp $
* *
* XXX this file does not really belong in psql/. Perhaps move to libpq? * XXX this file does not really belong in psql/. Perhaps move to libpq?
* It also seems that the mbvalidate function is redundant with existing * 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) ...@@ -204,8 +204,8 @@ pg_wcswidth(const unsigned char *pwcs, size_t len, int encoding)
/* /*
* pg_wcssize takes the given string in the given encoding and returns three * pg_wcssize takes the given string in the given encoding and returns three
* values: * values:
* result_width: Width in display character of longest line in string * result_width: Width in display characters of the longest line in string
* result_height: Number of lines in display output * result_height: Number of newlines in display output
* result_format_size: Number of bytes required to store formatted representation of string * result_format_size: Number of bytes required to store formatted representation of string
*/ */
int int
...@@ -279,9 +279,14 @@ pg_wcssize(unsigned char *pwcs, size_t len, int encoding, int *result_width, ...@@ -279,9 +279,14 @@ pg_wcssize(unsigned char *pwcs, size_t len, int encoding, int *result_width,
return width; return width;
} }
/*
* Filter out unprintable characters, companion to wcs_size.
* Break input into lines based on \n. lineptr[i].ptr == NULL
* indicates the end of the array.
*/
void void
pg_wcsformat(unsigned char *pwcs, size_t len, int encoding, pg_wcsformat(unsigned char *pwcs, size_t len, int encoding,
struct lineptr * lines, int count) struct lineptr *lines, int count)
{ {
int w, int w,
chlen = 0; chlen = 0;
...@@ -307,6 +312,7 @@ pg_wcsformat(unsigned char *pwcs, size_t len, int encoding, ...@@ -307,6 +312,7 @@ pg_wcsformat(unsigned char *pwcs, size_t len, int encoding,
if (count == 0) if (count == 0)
exit(1); /* Screwup */ exit(1); /* Screwup */
/* make next line point to remaining memory */
lines->ptr = ptr; lines->ptr = ptr;
} }
else if (*pwcs == '\r') /* Linefeed */ else if (*pwcs == '\r') /* Linefeed */
...@@ -353,12 +359,13 @@ pg_wcsformat(unsigned char *pwcs, size_t len, int encoding, ...@@ -353,12 +359,13 @@ pg_wcsformat(unsigned char *pwcs, size_t len, int encoding,
} }
len -= chlen; len -= chlen;
} }
*ptr++ = '\0';
lines->width = linewidth; lines->width = linewidth;
lines++; *ptr++ = '\0'; /* Terminate formatted string */
count--;
if (count > 0) if (count == 0)
lines->ptr = NULL; exit(1); /* Screwup */
(lines+1)->ptr = NULL; /* terminate line array */
} }
unsigned char * unsigned char *
......
This diff is collapsed.
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright (c) 2000-2008, PostgreSQL Global Development Group * Copyright (c) 2000-2008, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/bin/psql/print.h,v 1.35 2008/01/01 19:45:56 momjian Exp $ * $PostgreSQL: pgsql/src/bin/psql/print.h,v 1.36 2008/05/08 17:04:26 momjian Exp $
*/ */
#ifndef PRINT_H #ifndef PRINT_H
#define PRINT_H #define PRINT_H
...@@ -21,6 +21,7 @@ enum printFormat ...@@ -21,6 +21,7 @@ enum printFormat
PRINT_NOTHING = 0, /* to make sure someone initializes this */ PRINT_NOTHING = 0, /* to make sure someone initializes this */
PRINT_UNALIGNED, PRINT_UNALIGNED,
PRINT_ALIGNED, PRINT_ALIGNED,
PRINT_WRAPPED,
PRINT_HTML, PRINT_HTML,
PRINT_LATEX, PRINT_LATEX,
PRINT_TROFF_MS PRINT_TROFF_MS
...@@ -47,6 +48,8 @@ typedef struct _printTableOpt ...@@ -47,6 +48,8 @@ typedef struct _printTableOpt
* decimal marker */ * decimal marker */
char *tableAttr; /* attributes for HTML <table ...> */ char *tableAttr; /* attributes for HTML <table ...> */
int encoding; /* character encoding */ int encoding; /* character encoding */
int env_columns; /* $COLUMNS on psql start, 0 is unset */
int columns; /* target width for wrapped format */
} printTableOpt; } printTableOpt;
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright (c) 2000-2008, PostgreSQL Global Development Group * Copyright (c) 2000-2008, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/bin/psql/startup.c,v 1.146 2008/01/01 19:45:56 momjian Exp $ * $PostgreSQL: pgsql/src/bin/psql/startup.c,v 1.147 2008/05/08 17:04:26 momjian Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
...@@ -147,6 +147,8 @@ main(int argc, char *argv[]) ...@@ -147,6 +147,8 @@ main(int argc, char *argv[])
pset.popt.topt.start_table = true; pset.popt.topt.start_table = true;
pset.popt.topt.stop_table = true; pset.popt.topt.stop_table = true;
pset.popt.default_footer = true; pset.popt.default_footer = true;
/* We must get COLUMNS here before readline() sets it */
pset.popt.topt.env_columns = getenv("COLUMNS") ? atoi(getenv("COLUMNS")) : 0;
pset.notty = (!isatty(fileno(stdin)) || !isatty(fileno(stdout))); pset.notty = (!isatty(fileno(stdin)) || !isatty(fileno(stdout)));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment