Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf7108ce

Browse files
committed
Fix psql \h output for case of no parameters (ie, list all the known commands)
to format properly for the actually needed column width, instead of havinga hard-wired assumption about the longest command name length. Also make itrespond to the current screen width. In passing, const-ify the constanttable.
1 parent000666b commitf7108ce

File tree

2 files changed

+54
-29
lines changed

2 files changed

+54
-29
lines changed

‎src/bin/psql/create_help.pl

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#
66
# Copyright (c) 2000-2008, PostgreSQL Global Development Group
77
#
8-
# $PostgreSQL: pgsql/src/bin/psql/create_help.pl,v 1.16 2008/01/01 19:45:55momjian Exp $
8+
# $PostgreSQL: pgsql/src/bin/psql/create_help.pl,v 1.17 2008/01/20 21:13:55tgl Exp $
99
#################################################################
1010

1111
#
@@ -51,20 +51,21 @@
5151
#ifndef$define
5252
#define$define
5353
54-
#define N_(x) (x)/* gettext noop */
54+
#define N_(x) (x)/* gettext noop */
5555
5656
struct _helpStruct
5757
{
58-
char *cmd;/* the command name */
59-
char *help;/* the help associated with it */
60-
char *syntax;/* the syntax associated with it */
58+
constchar *cmd;/* the command name */
59+
constchar *help;/* the help associated with it */
60+
constchar *syntax;/* the syntax associated with it */
6161
};
6262
6363
64-
static struct _helpStruct QL_HELP[] = {
64+
staticconststruct _helpStruct QL_HELP[] = {
6565
";
6666

6767
$count = 0;
68+
$maxlen = 0;
6869

6970
foreach$file (sortreaddir DIR) {
7071
local ($cmdname,$cmddesc,$cmdsynopsis);
@@ -113,7 +114,9 @@
113114
$cmdsynopsis =~s/\"/\\"/g;
114115

115116
print OUT" {\"$cmdname\",\n N_(\"$cmddesc\"),\n N_(\"$cmdsynopsis\") },\n\n";
116-
$count++;
117+
118+
$count++;
119+
$maxlen = ($maxlen >=length$cmdname) ?$maxlen :length$cmdname;
117120
}
118121
else {
119122
printSTDERR"$0: parsing file '$file' failed (N='$cmdname' D='$cmddesc')\n";
@@ -125,7 +128,8 @@
125128
};
126129
127130
128-
#define QL_HELP_COUNT$count
131+
#define QL_HELP_COUNT$count/* number of help items */
132+
#define QL_MAX_CMD_LEN$maxlen/* largest strlen(cmd) */
129133
130134
131135
#endif /*$define */

‎src/bin/psql/help.c

Lines changed: 42 additions & 21 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/help.c,v 1.121 2008/01/01 19:45:56 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/help.c,v 1.122 2008/01/20 21:13:55 tgl Exp $
77
*/
88
#include"postgres_fe.h"
99

@@ -19,6 +19,14 @@
1919
#include<win32.h>
2020
#endif
2121

22+
#ifndefWIN32
23+
#include<sys/ioctl.h>/* for ioctl() */
24+
#endif
25+
26+
#ifdefHAVE_TERMIOS_H
27+
#include<termios.h>
28+
#endif
29+
2230
#include"pqsignal.h"
2331

2432
#include"common.h"
@@ -147,15 +155,6 @@ usage(void)
147155
*
148156
* print out help for the backslash commands
149157
*/
150-
151-
#ifndefTIOCGWINSZ
152-
structwinsize
153-
{
154-
intws_row;
155-
intws_col;
156-
};
157-
#endif
158-
159158
void
160159
slashUsage(unsigned shortintpager)
161160
{
@@ -280,24 +279,46 @@ helpSQL(const char *topic, unsigned short int pager)
280279

281280
if (!topic||strlen(topic)==0)
282281
{
283-
inti;
284-
intitems_per_column= (QL_HELP_COUNT+2) /3;
282+
/* Print all the available command names */
283+
intscreen_width;
284+
intncolumns;
285+
intnrows;
285286
FILE*output;
287+
inti;
288+
intj;
286289

287-
output=PageOutput(items_per_column+1,pager);
290+
#ifdefTIOCGWINSZ
291+
structwinsizescreen_size;
292+
293+
if (ioctl(fileno(stdout),TIOCGWINSZ,&screen_size)==-1)
294+
screen_width=80;/* ioctl failed, assume 80 */
295+
else
296+
screen_width=screen_size.ws_col;
297+
#else
298+
screen_width=80;/* default assumption */
299+
#endif
300+
301+
ncolumns= (screen_width-3) / (QL_MAX_CMD_LEN+1);
302+
ncolumns=Max(ncolumns,1);
303+
nrows= (QL_HELP_COUNT+ (ncolumns-1)) /ncolumns;
304+
305+
output=PageOutput(nrows+1,pager);
288306

289307
fputs(_("Available help:\n"),output);
290308

291-
for (i=0;i<items_per_column;i++)
309+
for (i=0;i<nrows;i++)
292310
{
293-
fprintf(output," %-26s%-26s",
294-
VALUE_OR_NULL(QL_HELP[i].cmd),
295-
VALUE_OR_NULL(QL_HELP[i+items_per_column].cmd));
296-
if (i+2*items_per_column<QL_HELP_COUNT)
297-
fprintf(output,"%-26s",
298-
VALUE_OR_NULL(QL_HELP[i+2*items_per_column].cmd));
311+
fprintf(output," ");
312+
for (j=0;j<ncolumns-1;j++)
313+
fprintf(output,"%-*s",
314+
QL_MAX_CMD_LEN+1,
315+
VALUE_OR_NULL(QL_HELP[i+j*nrows].cmd));
316+
if (i+j*nrows<QL_HELP_COUNT)
317+
fprintf(output,"%s",
318+
VALUE_OR_NULL(QL_HELP[i+j*nrows].cmd));
299319
fputc('\n',output);
300320
}
321+
301322
/* Only close if we used the pager */
302323
if (output!=stdout)
303324
{
@@ -317,7 +338,7 @@ helpSQL(const char *topic, unsigned short int pager)
317338
size_tlen,
318339
wordlen;
319340
intnl_count=0;
320-
char*ch;
341+
constchar*ch;
321342

322343
/* User gets two chances: exact match, then the first word */
323344

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp