Skip to content

Commit

Permalink
lib/omb-prompt-base: Escape special characters in prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Jul 1, 2024
1 parent 27cf2a4 commit ece0ed3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/omb-prompt-base.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#! bash oh-my-bash.module

_omb_module_require lib:omb-util
_omb_module_require lib:omb-deprecate
_omb_module_require lib:omb-prompt-colors

Expand Down Expand Up @@ -101,7 +102,9 @@ function _omb_prompt_format {
local __suffix=${3#*:}_SUFFIX; __suffix=${!__suffix-} # deprecate name
__format=${__prefix//'%'/'%%'}%s${__suffix//'%'/'%%'}
fi
printf -v "$1" "$__format" "$2"
local REPLY
_omb_string_escape_prompt "$2"
printf -v "$1" "$__format" "$REPLY"
}

function _omb_prompt_git {
Expand Down
14 changes: 14 additions & 0 deletions lib/omb-util.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#! bash oh-my-bash.module

function _omb_string_escape_prompt {
REPLY=$1
local specialchars='\`$'
if [[ $REPLY == ["$specialchars"] ]]; then
local i n=${#specialchars} a b
for ((i=0;i<n;i++)); do
a=${specialchars:i:1}
b='\'$a
REPLY=${REPLY//"$a"/"$b"}
done
fi
}

0 comments on commit ece0ed3

Please sign in to comment.