</listitem>
</varlistentry>
+ <varlistentry>
+ <term><literal>%w</literal></term>
+ <listitem>
+ <para>
+ Whitespace of the same width as the most recent output of
+ <varname>PROMPT1</varname>. This can be used as a
+ <varname>PROMPT2</varname> setting, so that multi-line statements are
+ aligned with the first line, but there is no visible secondary prompt.
+ </para>
+ </listitem>
+ </varlistentry>
+
</variablelist>
To insert a percent sign into your prompt, write
* %n - database user name
* %/ - current database
* %~ - like %/ but "~" when database name equals user name
+ * %w - whitespace of the same width as the most recent output of PROMPT1
* %# - "#" if superuser, ">" otherwise
* %R - in prompt1 normally =, or ^ if single line mode,
* or a ! if session is not connected to a database;
bool esc = false;
const char *p;
const char *prompt_string = "? ";
+ static size_t last_prompt1_width = 0;
switch (status)
{
}
break;
+ /* Whitespace of the same width as the last PROMPT1 */
+ case 'w':
+ if (pset.db)
+ memset(buf, ' ',
+ Min(last_prompt1_width, sizeof(buf) - 1));
+ break;
+
/* DB server hostname (long/short) */
case 'M':
case 'm':
strlcat(destination, buf, sizeof(destination));
}
+ /* Compute the visible width of PROMPT1, for PROMPT2's %w */
+ if (prompt_string == pset.prompt1)
+ {
+ char *p = destination;
+ char *end = p + strlen(p);
+ bool visible = true;
+
+ last_prompt1_width = 0;
+ while (*p)
+ {
+#if defined(USE_READLINE) && defined(RL_PROMPT_START_IGNORE)
+ if (*p == RL_PROMPT_START_IGNORE)
+ {
+ visible = false;
+ ++p;
+ }
+ else if (*p == RL_PROMPT_END_IGNORE)
+ {
+ visible = true;
+ ++p;
+ }
+ else
+#endif
+ {
+ int chlen,
+ chwidth;
+
+ chlen = PQmblen(p, pset.encoding);
+ if (p + chlen > end)
+ break; /* Invalid string */
+
+ if (visible)
+ {
+ chwidth = PQdsplen(p, pset.encoding);
+ if (chwidth > 0)
+ last_prompt1_width += chwidth;
+ }
+
+ p += chlen;
+ }
+ }
+ }
+
return destination;
}