Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 18f7a8e

Browse files
committed
This is a patch to support readline prompts which contain non-printing
characters, as for fancy colorized prompts. This was nearly a direct lift from bash-2.05b's lib/readline/display.c, per guidance from Chet Ramey. Reece Hart
1 parent bd046b9 commit 18f7a8e

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

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

Lines changed: 23 additions & 1 deletion
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.102 2003/12/23 23:13:14 momjian Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.103 2004/01/20 19:49:34 tgl Exp $
33
PostgreSQL documentation
44
-->
55

@@ -2315,6 +2315,28 @@ testdb=> <userinput>\set content '\'' `sed -e "s/'/\\\\\\'/g" < my_file.txt` '\'
23152315
</listitem>
23162316
</varlistentry>
23172317

2318+
<varlistentry>
2319+
<term><literal>%[</literal> ... <literal>%]</literal></term>
2320+
<listitem>
2321+
<para>
2322+
Prompts may contain terminal control characters which, for
2323+
example, change the color, background, or style of the prompt
2324+
text, or change the title of the terminal window. In order for
2325+
the line editing features of readline to work properly, these
2326+
non-printing control characters must be designated as invisible
2327+
by surrounding them with <literal>%[</literal> and
2328+
<literal>%]</literal>. Multiple pairs of these may occur within
2329+
the prompt. For example,
2330+
<programlisting>
2331+
testdb=> \set PROMPT1 '%[%033[1;33;40m%]%n@%/%R%[%033[0m%#%] '
2332+
</programlisting>
2333+
results in a boldfaced (<literal>1;</literal>) yellow-on-black
2334+
(<literal>33;40</literal>) prompt on VT100-compatible, color-capable
2335+
terminals.
2336+
</para>
2337+
</listitem>
2338+
</varlistentry>
2339+
23182340
</variablelist>
23192341

23202342
To insert a percent sign into your prompt, write

src/bin/psql/prompt.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2003, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/prompt.c,v 1.31 2003/11/29 19:52:07 pgsql Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/prompt.c,v 1.32 2004/01/20 19:49:34 tgl Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "prompt.h"
@@ -12,6 +12,7 @@
1212

1313
#include "settings.h"
1414
#include "common.h"
15+
#include "input.h"
1516
#include "variables.h"
1617

1718
#ifdef WIN32
@@ -57,7 +58,9 @@
5758
* %:name: - The value of the psql variable 'name'
5859
* (those will not be rescanned for more escape sequences!)
5960
*
60-
* If the application-wide prompts became NULL somehow, the returned string
61+
* %[ ... %] - tell readline that the contained text is invisible
62+
*
63+
* If the application-wide prompts become NULL somehow, the returned string
6164
* will be empty (not NULL!).
6265
*--------------------------
6366
*/
@@ -282,10 +285,23 @@ get_prompt(promptStatus_t status)
282285
break;
283286
}
284287

288+
case '[':
289+
case ']':
290+
#if defined(USE_READLINE) && defined(RL_PROMPT_START_IGNORE)
291+
/*
292+
* readline >=4.0 undocumented feature: non-printing
293+
* characters in prompt strings must be marked as such,
294+
* in order to properly display the line during editing.
295+
*/
296+
buf[0] = '\001';
297+
buf[1] = (*p == '[') ? RL_PROMPT_START_IGNORE : RL_PROMPT_END_IGNORE;
298+
#endif /* USE_READLINE */
299+
break;
285300

286301
default:
287302
buf[0] = *p;
288303
buf[1] = '\0';
304+
break;
289305

290306
}
291307
esc = false;

0 commit comments

Comments
 (0)