Skip to content

Commit 11e8ad4

Browse files
Bypass PS1s conversion of \[
In PS1 you need to escape non-printing characters, like the color codes. The standard way is wrapping it in `\[` and `\]`. But for a dynamic prompt, i.e. one that renders the results of a function every time the prompt renders, that `\[` will be output as literals. To fix this we bypass the conversion and wrap our non-printing characters in the desired characters directly: `\[` -> `\x01` `\]` -> `\x02`
1 parent 1faeef8 commit 11e8ad4

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

prompt.bash

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ dot="$(cd "$(dirname "$0")"; pwd)"
44
source "$dot/radar-base.sh"
55

66
if is_repo; then
7-
printf " \[\033[1;30m\]git:(\[\033[0m\]"
7+
printf " \x01\033[1;30m\x02git:(\x01\033[0m\x02"
88
bash_color_remote_commits
9-
printf "\[\033[0;37m\]"
9+
printf "\x01\033[0;37m\x02"
1010
readable_branch_name
11-
printf "\[\033[0m\]"
11+
printf "\x01\033[0m\x02"
1212
bash_color_local_commits
13-
printf "\[\033[1;30m\])\[\033[0m\]"
13+
printf "\x01\033[1;30m\x02)\x01\033[0m\x02"
1414
bash_color_changes_status
1515
fi

radar-base.sh

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -305,11 +305,11 @@ bash_color_changes_status() {
305305
local changes=""
306306

307307
if [[ -n "$porcelain" ]]; then
308-
local green_staged_prefix="\[\033[1;32m\]"
309-
local red_unstaged_prefix="\[\033[1;31m\]"
310-
local yellow_conflicted_prefix="\[\033[1;33m\]"
311-
local grey_untracked_prefix="\[\033[1;37m\]"
312-
local reset_suffix="\[\033[0m\]"
308+
local green_staged_prefix="\x01\033[1;32m\x02"
309+
local red_unstaged_prefix="\x01\033[1;31m\x02"
310+
local yellow_conflicted_prefix="\x01\033[1;33m\x02"
311+
local grey_untracked_prefix="\x01\033[1;37m\x02"
312+
local reset_suffix="\x01\033[0m\x02"
313313

314314
local staged_changes="$(staged_status "$porcelain" "$green_staged_prefix" "$reset_suffix")"
315315
local unstaged_changes="$(unstaged_status "$porcelain" "$red_unstaged_prefix" "$reset_suffix")"
@@ -377,9 +377,9 @@ zsh_color_changes_status() {
377377
bash_color_local_commits() {
378378
local separator="${1:- }"
379379

380-
local green_ahead_arrow="\[\033[1;32m\]↑\[\033[0m\]"
381-
local red_behind_arrow="\[\033[1;31m\]↓\[\033[0m\]"
382-
local yellow_diverged_arrow="\[\033[1;33m\]⇵\[\033[0m\]"
380+
local green_ahead_arrow="\x01\033[1;32m\x02↑\x01\033[0m\x02"
381+
local red_behind_arrow="\x01\033[1;31m\x02↓\x01\033[0m\x02"
382+
local yellow_diverged_arrow="\x01\033[1;33m\x02⇵\x01\033[0m\x02"
383383

384384
local local_commits=""
385385
if remote_branch="$(remote_branch_name)"; then
@@ -422,10 +422,10 @@ zsh_color_local_commits() {
422422

423423
bash_color_remote_commits() {
424424
local remote_master="\xF0\x9D\x98\xAE" # an italic m to represent master
425-
local green_ahead_arrow="\[\033[1;32m\]←\[\033[0m\]"
426-
local red_behind_arrow="\[\033[1;31m\]→\[\033[0m\]"
427-
local yellow_diverged_arrow="\[\033[1;33m\]⇄\[\033[0m\]"
428-
local not_upstream="\[\033[1;31m\]⚡\[\033[0m\]"
425+
local green_ahead_arrow="\x01\033[1;32m\x02←\x01\033[0m\x02"
426+
local red_behind_arrow="\x01\033[1;31m\x02→\x01\033[0m\x02"
427+
local yellow_diverged_arrow="\x01\033[1;33m\x02⇄\x01\033[0m\x02"
428+
local not_upstream="\x01\033[1;31m\x02⚡\x01\033[0m\x02"
429429

430430
if remote_branch="$(remote_branch_name)"; then
431431
remote_ahead="$(remote_ahead_of_master "$remote_branch")"

test-commits.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,9 +374,9 @@ test_zsh_and_bash_local_commits() {
374374
local zsh_both="%{[yellow]%}⇵%{%}"
375375
local zsh_down="%{[red]%}↓%{%}"
376376

377-
printf -v bash_up "\033[1;32m↑\033[0m"
378-
printf -v bash_both "\033[1;33m⇵\033[0m"
379-
printf -v bash_down "\033[1;31m↓\033[0m"
377+
printf -v bash_up "\x01\033[1;32m\x02↑\x01\033[0m\x02"
378+
printf -v bash_both "\x01\033[1;33m\x02⇵\x01\033[0m\x02"
379+
printf -v bash_down "\x01\033[1;31m\x02↓\x01\033[0m\x02"
380380

381381
cd_to_tmp "remote"
382382

0 commit comments

Comments
 (0)