Skip to content

Commit

Permalink
adds escaping for zsh
Browse files Browse the repository at this point in the history
  • Loading branch information
betafcc committed May 18, 2021
1 parent abeedc8 commit b4c7368
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ The motivating use case was easily stacking styles, which is not possible to do

![](./examples/stacking.gif)

Useful for templating colorful prompts (use `-e|--escape` for this):
Useful for templating colorful prompts (use `-e|--escape [zsh|bash]` for this)

for bash: `PS1=$(clc -e bash '<bold:<red:[<yellow:\\u><green:@><blue:\\h><magenta:\\W>]>$ >')`

for zsh: `PS1=$(clc -e zsh '<bold:<red:[<yellow:%n><green:@><blue:%m><magenta:%1d>]>$ >')`

`PS1=$(clc -e '<bold:<red:[<yellow:\\u><green:@><blue:\\h><magenta:\\W>]>$ >')`

![](./examples/prompt.png)

Expand Down
21 changes: 16 additions & 5 deletions clc
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
#!/usr/bin/env bash

clc() {
local open="${open:-'\e[%bm'}"
local close="${close:-'\e[0m'}"

if [ $# -eq 0 ]; then
clc_parse
else
case "${1}" in
-v|--version) echo 'v1.2.0';;
-e|--escape) shift; clc "$@" | sed -E 's,(\x1B\[[0-9;]*[a-zA-Z]),\\\[\1\\\],g';;
-v|--version) echo 'v2.0.0';;
-e|--escape)
shift
case "${1}" in
zsh) shift; open='%%{\e[%bm%%}'; close='%%{\e[0m%%}';;
bash) shift; open='\[\e[%bm\]'; close='\[\e[0m\]';;
*) shift; open='\001\e[%bm\002'; close='\001\e[0m\002';;
esac
clc "$@"
;;
*) printf '%s' "$@" | clc
esac
fi
Expand All @@ -15,11 +26,11 @@ clc() {
clc_parse() (
while IFS= read -n 1 -d '' char; do
case "${char}" in
'>') printf '\e[0m'; return 0;;
'>') printf "${close}"; return 0;;
'<') raw_code=$(clc_parse_directive)
printf '\e[%bm' "${raw_code}"
printf "${open}" "${raw_code}"
clc_parse "$@" "${raw_code}"
printf '\e[%bm' "$@";;
printf "${open}" "$@";;
*) printf '%b' "${char}";;
esac
done
Expand Down

0 comments on commit b4c7368

Please sign in to comment.