提交 5adf98ae 编写于 作者: B 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
上级 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
-->
......@@ -1514,7 +1514,8 @@ lo_import 152801
<listitem>
<para>
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>.
Unique abbreviations are allowed. (That would mean one letter
is enough.)
......@@ -1526,8 +1527,21 @@ lo_import 152801
is intended to create output that might be intended to be read
in by other programs (tab-separated, comma-separated).
<quote>Aligned</quote> mode is the standard, human-readable,
nicely formatted text output that is default. The
<quote><acronym>HTML</acronym></quote> and
nicely formatted text output that is default.
</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
be included in documents using the respective mark-up
language. They are not complete documents! (This might not be
......@@ -1537,6 +1551,17 @@ lo_import 152801
</listitem>
</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>
<term><literal>border</literal></term>
<listitem>
......@@ -2706,6 +2731,18 @@ $endif
<title>Environment</title>
<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>
<term><envar>PAGER</envar></term>
......
......@@ -3,7 +3,7 @@
*
* 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 "command.h"
......@@ -1502,6 +1502,9 @@ _align2string(enum printFormat in)
case PRINT_ALIGNED:
return "aligned";
break;
case PRINT_WRAPPED:
return "wrapped";
break;
case PRINT_HTML:
return "html";
break;
......@@ -1535,6 +1538,8 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
popt->topt.format = PRINT_UNALIGNED;
else if (pg_strncasecmp("aligned", value, vallen) == 0)
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)
popt->topt.format = PRINT_HTML;
else if (pg_strncasecmp("latex", value, vallen) == 0)
......@@ -1543,7 +1548,7 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
popt->topt.format = PRINT_TROFF_MS;
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;
}
......@@ -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
{
psql_error("\\pset: unknown option: %s\n", param);
......
......@@ -3,7 +3,7 @@
*
* 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?
* 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)
/*
* pg_wcssize takes the given string in the given encoding and returns three
* values:
* result_width: Width in display character of longest line in string
* result_height: Number of lines in display output
* result_width: Width in display characters of the longest line in string
* result_height: Number of newlines in display output
* result_format_size: Number of bytes required to store formatted representation of string
*/
int
......@@ -279,9 +279,14 @@ pg_wcssize(unsigned char *pwcs, size_t len, int encoding, int *result_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
pg_wcsformat(unsigned char *pwcs, size_t len, int encoding,
struct lineptr * lines, int count)
struct lineptr *lines, int count)
{
int w,
chlen = 0;
......@@ -307,6 +312,7 @@ pg_wcsformat(unsigned char *pwcs, size_t len, int encoding,
if (count == 0)
exit(1); /* Screwup */
/* make next line point to remaining memory */
lines->ptr = ptr;
}
else if (*pwcs == '\r') /* Linefeed */
......@@ -353,12 +359,13 @@ pg_wcsformat(unsigned char *pwcs, size_t len, int encoding,
}
len -= chlen;
}
*ptr++ = '\0';
lines->width = linewidth;
lines++;
count--;
if (count > 0)
lines->ptr = NULL;
*ptr++ = '\0'; /* Terminate formatted string */
if (count == 0)
exit(1); /* Screwup */
(lines+1)->ptr = NULL; /* terminate line array */
}
unsigned char *
......
此差异已折叠。
......@@ -3,7 +3,7 @@
*
* 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
#define PRINT_H
......@@ -21,6 +21,7 @@ enum printFormat
PRINT_NOTHING = 0, /* to make sure someone initializes this */
PRINT_UNALIGNED,
PRINT_ALIGNED,
PRINT_WRAPPED,
PRINT_HTML,
PRINT_LATEX,
PRINT_TROFF_MS
......@@ -47,6 +48,8 @@ typedef struct _printTableOpt
* decimal marker */
char *tableAttr; /* attributes for HTML <table ...> */
int encoding; /* character encoding */
int env_columns; /* $COLUMNS on psql start, 0 is unset */
int columns; /* target width for wrapped format */
} printTableOpt;
......
......@@ -3,7 +3,7 @@
*
* 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"
......@@ -147,6 +147,8 @@ main(int argc, char *argv[])
pset.popt.topt.start_table = true;
pset.popt.topt.stop_table = 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)));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册