Thread: Quick little \h enhancement for psql

Quick little \h enhancement for psql

From
Greg Sabino Mullane
Date:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

                      

                      
Attached is a patch that enhances the "\h" capability in psql. I often
find myself typing a command and then wanting to get the syntax for
it. So I do a ctrl-a and add a \h: but psql does not recognize the
command, because I have stuff attached to it (e.g. "alter table
foobar"),
so I have to scroll over and delete everything except the name of the
command itself. This patch gives \h three chances to match: if nothing
matches the complete string (current behavior), it tries to match the
first two words (e.g. "ALTER TABLE"). If that fails, it tries to match
the first word (e.g. "DELETE").

                      
- --
Greg Sabino Mullane greg@turnstep.com
PGP Key: 0x14964AC8 200505161840
http://biglumber.com/x/web?pk=2529DF6AB8F79407E94445B4BC9B906714964AC8
-----BEGIN PGP SIGNATURE-----

                      
iD8DBQFCiSF3vJuQZxSWSsgRAumaAKCKd0xns4V+ITRSfxGJCrJUtZOrjgCfW8FC
qbFC2jdXGOWWP8cN3Um/1hY=
=kZeh
-----END PGP SIGNATURE-----


Attachment

Re: Quick little \h enhancement for psql

From
"Greg Sabino Mullane"
Date:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


      
> Attached is a patch that enhances the "\h" capability in psql.

Any feedback on this? I'd like to see it added, but willing to
discuss/rewrite if not acceptable.

Thanks,
- --
Greg Sabino Mullane greg@turnstep.com
PGP Key: 0x14964AC8 200506171234
http://biglumber.com/x/web?pk=2529DF6AB8F79407E94445B4BC9B906714964AC8
-----BEGIN PGP SIGNATURE-----

iD8DBQFCsvxevJuQZxSWSsgRAhwTAJ0bv/OrJ+7uXJvjI8M8DStXL7tOZQCgqKUL
lqDbO3AWAs9kJJWJ8npKJeI=
=SqgW
-----END PGP SIGNATURE-----



Re: Quick little \h enhancement for psql

From
Bruce Momjian
Date:
Greg Sabino Mullane wrote:
[ There is text before PGP section. ]
>
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
>
        
> > Attached is a patch that enhances the "\h" capability in psql.
>
> Any feedback on this? I'd like to see it added, but willing to
> discuss/rewrite if not acceptable.

Uh, where is that patch?  I don't have it and don't remember seeing it
either.

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073

Re: Quick little \h enhancement for psql

From
Bruce Momjian
Date:
Greg Sabino Mullane wrote:
>
> http://archives.postgresql.org/pgsql-patches/2005-05/msg00197.php

Applied.

---------------------------------------------------------------------------

Attached is a patch that enhances the "\h" capability in psql. I often
find myself typing a command and then wanting to get the syntax for
it. So I do a ctrl-a and add a \h: but psql does not recognize the
command, because I have stuff attached to it (e.g. "alter table
foobar"),
so I have to scroll over and delete everything except the name of the
command itself. This patch gives \h three chances to match: if nothing
matches the complete string (current behavior), it tries to match the
first two words (e.g. "ALTER TABLE"). If that fails, it tries to match
the first word (e.g. "DELETE").

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
Index: src/bin/psql/help.c
===================================================================
RCS file: /cvsroot/pgsql/src/bin/psql/help.c,v
retrieving revision 1.102
diff -c -c -r1.102 help.c
*** src/bin/psql/help.c    14 Jun 2005 02:57:41 -0000    1.102
--- src/bin/psql/help.c    6 Jul 2005 02:18:59 -0000
***************
*** 305,356 ****
      }
      else
      {
!         int            i;
          bool        help_found = false;
          FILE       *output;
!         size_t        len;
          int            nl_count = 0;
          char       *ch;

!         /* don't care about trailing spaces or semicolons */
          len = strlen(topic);
          while (topic[len - 1] == ' ' || topic[len - 1] == ';')
!             len--;

!         /* Count newlines for pager */
!         for (i = 0; QL_HELP[i].cmd; i++)
          {
!             if (pg_strncasecmp(topic, QL_HELP[i].cmd, len) == 0 ||
!                 strcmp(topic, "*") == 0)
!             {
!                 nl_count += 5;
!                 for (ch = QL_HELP[i].syntax; *ch != '\0'; ch++)
!                     if (*ch == '\n')
!                         nl_count++;
!                 /* If we have an exact match, exit.  Fixes \h SELECT */
!                 if (pg_strcasecmp(topic, QL_HELP[i].cmd) == 0)
!                     break;
!             }
!         }
!
!         output = PageOutput(nl_count, pager);
!
!         for (i = 0; QL_HELP[i].cmd; i++)
!         {
!             if (pg_strncasecmp(topic, QL_HELP[i].cmd, len) == 0 ||
!                 strcmp(topic, "*") == 0)
!             {
!                 help_found = true;
!                 fprintf(output, _("Command:     %s\n"
!                                   "Description: %s\n"
!                                   "Syntax:\n%s\n\n"),
!                         QL_HELP[i].cmd,
!                         _(QL_HELP[i].help),
!                         _(QL_HELP[i].syntax));
!                 /* If we have an exact match, exit.  Fixes \h SELECT */
!                 if (pg_strcasecmp(topic, QL_HELP[i].cmd) == 0)
!                     break;
!             }
          }

          if (!help_found)
--- 305,382 ----
      }
      else
      {
!             int            i,j,x=0;
          bool        help_found = false;
          FILE       *output;
!         size_t        len, wordlen;
          int            nl_count = 0;
          char       *ch;

!         /* User gets two chances: exact match, then the first word */
!
!         /* First pass : strip trailing spaces and semicolons */
          len = strlen(topic);
          while (topic[len - 1] == ' ' || topic[len - 1] == ';')
!                 len--;

!         for (x=1; x<=3; x++) /* Three chances to guess that word... */
          {
!                 if (x>1) /* Nothing on first pass - try the opening words */
!                 {
!                         wordlen=j=1;
!                         while (topic[j] != ' ' && j++<len)
!                                 wordlen++;
!                         if (x==2)
!                         {
!                                 j++;
!                                 while (topic[j] != ' ' && j++<=len)
!                                         wordlen++;
!                         }
!                         if (wordlen >= len) /* Don't try again if the same word */
!                         {
!                                 output = PageOutput(nl_count, pager);
!                                 break;
!                         }
!                         len = wordlen;
!                 }
!
!                 /* Count newlines for pager */
!                 for (i = 0; QL_HELP[i].cmd; i++)
!                 {
!                         if (pg_strncasecmp(topic, QL_HELP[i].cmd, len) == 0 ||
!                                 strcmp(topic, "*") == 0)
!                         {
!                                 nl_count += 5;
!                                 for (ch = QL_HELP[i].syntax; *ch != '\0'; ch++)
!                                         if (*ch == '\n')
!                                                 nl_count++;
!                                 /* If we have an exact match, exit.  Fixes \h SELECT */
!                                 if (pg_strcasecmp(topic, QL_HELP[i].cmd) == 0)
!                                         break;
!                         }
!                 }
!
!                 output = PageOutput(nl_count, pager);
!
!                 for (i = 0; QL_HELP[i].cmd; i++)
!                 {
!                         if (pg_strncasecmp(topic, QL_HELP[i].cmd, len) == 0 ||
!                                 strcmp(topic, "*") == 0)
!                         {
!                                 help_found = true;
!                                 fprintf(output, _("Command:     %s\n"
!                                                                     "Description: %s\n"
!                                                                     "Syntax:\n%s\n\n"),
!                                                 QL_HELP[i].cmd,
!                                                 _(QL_HELP[i].help),
!                                                 _(QL_HELP[i].syntax));
!                                 /* If we have an exact match, exit.  Fixes \h SELECT */
!                                 if (pg_strcasecmp(topic, QL_HELP[i].cmd) == 0)
!                                         break;
!                         }
!                 }
!                 if (help_found) /* Don't keep trying if we got a match */
!                         break;
          }

          if (!help_found)