|
39 | 39 | * %n - database user name
|
40 | 40 | * %/ - current database
|
41 | 41 | * %~ - like %/ but "~" when database name equals user name
|
| 42 | + * %w - whitespace of the same width as the most recent output of PROMPT1 |
42 | 43 | * %# - "#" if superuser, ">" otherwise
|
43 | 44 | * %R - in prompt1 normally =, or ^ if single line mode,
|
44 | 45 | * or a ! if session is not connected to a database;
|
@@ -74,6 +75,7 @@ get_prompt(promptStatus_t status, ConditionalStack cstack)
|
74 | 75 | bool esc = false;
|
75 | 76 | const char *p;
|
76 | 77 | const char *prompt_string = "? ";
|
| 78 | + static size_t last_prompt1_width = 0; |
77 | 79 |
|
78 | 80 | switch (status)
|
79 | 81 | {
|
@@ -124,6 +126,13 @@ get_prompt(promptStatus_t status, ConditionalStack cstack)
|
124 | 126 | }
|
125 | 127 | break;
|
126 | 128 |
|
| 129 | + /* Whitespace of the same width as the last PROMPT1 */ |
| 130 | + case 'w': |
| 131 | + if (pset.db) |
| 132 | + memset(buf, ' ', |
| 133 | + Min(last_prompt1_width, sizeof(buf) - 1)); |
| 134 | + break; |
| 135 | + |
127 | 136 | /* DB server hostname (long/short) */
|
128 | 137 | case 'M':
|
129 | 138 | case 'm':
|
@@ -336,5 +345,48 @@ get_prompt(promptStatus_t status, ConditionalStack cstack)
|
336 | 345 | strlcat(destination, buf, sizeof(destination));
|
337 | 346 | }
|
338 | 347 |
|
| 348 | + /* Compute the visible width of PROMPT1, for PROMPT2's %w */ |
| 349 | + if (prompt_string == pset.prompt1) |
| 350 | + { |
| 351 | + char *p = destination; |
| 352 | + char *end = p + strlen(p); |
| 353 | + bool visible = true; |
| 354 | + |
| 355 | + last_prompt1_width = 0; |
| 356 | + while (*p) |
| 357 | + { |
| 358 | +#if defined(USE_READLINE) && defined(RL_PROMPT_START_IGNORE) |
| 359 | + if (*p == RL_PROMPT_START_IGNORE) |
| 360 | + { |
| 361 | + visible = false; |
| 362 | + ++p; |
| 363 | + } |
| 364 | + else if (*p == RL_PROMPT_END_IGNORE) |
| 365 | + { |
| 366 | + visible = true; |
| 367 | + ++p; |
| 368 | + } |
| 369 | + else |
| 370 | +#endif |
| 371 | + { |
| 372 | + int chlen, |
| 373 | + chwidth; |
| 374 | + |
| 375 | + chlen = PQmblen(p, pset.encoding); |
| 376 | + if (p + chlen > end) |
| 377 | + break; /* Invalid string */ |
| 378 | + |
| 379 | + if (visible) |
| 380 | + { |
| 381 | + chwidth = PQdsplen(p, pset.encoding); |
| 382 | + if (chwidth > 0) |
| 383 | + last_prompt1_width += chwidth; |
| 384 | + } |
| 385 | + |
| 386 | + p += chlen; |
| 387 | + } |
| 388 | + } |
| 389 | + } |
| 390 | + |
339 | 391 | return destination;
|
340 | 392 | }
|
0 commit comments