[zsh] carefully rewrite all prints to not produce escape sequences

This commit is contained in:
Dmytro Meleshko 2021-03-30 15:15:33 +03:00
parent 3d34200ac6
commit 5ebbadb45d
9 changed files with 21 additions and 21 deletions

View File

@ -11,8 +11,8 @@ install_dotfile() {
fi fi
mkdir -pv "${dest:h}" mkdir -pv "${dest:h}"
echo "installing dotfile '$dest'" print -r -- "installing dotfile '$dest'"
echo "$contents" > "$dest" print -r -- "$contents" > "$dest"
} }
# ZSH # ZSH

View File

@ -77,7 +77,7 @@ alias free='free -h'
if command_exists apt && command_exists apt-get; then if command_exists apt && command_exists apt-get; then
apt_get_message="use 'apt' instead of 'apt-get' apt_get_message="use 'apt' instead of 'apt-get'
if you really want to use 'apt-get', type '\\apt-get'" if you really want to use 'apt-get', type '\\apt-get'"
alias apt-get="echo -E ${(qqq)apt_get_message} #" alias apt-get="print -r -- ${(qqq)apt_get_message} #"
unset apt_get_message unset apt_get_message
fi fi

View File

@ -39,11 +39,11 @@ zstyle ':completion:*:processes-names' command "ps xho comm="
zstyle ':completion:*:processes' force-list always zstyle ':completion:*:processes' force-list always
_completion_get_hosts() { _completion_get_hosts() {
print localhost print -r -- localhost
local line local line
< ~/.ssh/config while IFS= read -r line; do < ~/.ssh/config while IFS= read -r line; do
if [[ "$line" =~ '^Host[[:blank:]]+(.*)[[:blank:]]*' ]]; then if [[ "$line" =~ '^Host[[:blank:]]+(.*)[[:blank:]]*' ]]; then
print -- "${match[1]}" print -r -- "${match[1]}"
fi fi
done done
} }

View File

@ -1,6 +1,6 @@
#!/usr/bin/env zsh #!/usr/bin/env zsh
count() { echo "$#"; } count() { print -r -- "$#"; }
bytecount() { wc -c "$@" | numfmt --to=iec-i; } bytecount() { wc -c "$@" | numfmt --to=iec-i; }
@ -39,7 +39,7 @@ if (( ! _is_macos )); then
elif command_exists xdg-open; then elif command_exists xdg-open; then
open_cmd='nohup xdg-open &> /dev/null' open_cmd='nohup xdg-open &> /dev/null'
else else
open_cmd='print >&2 "open: Platform $OSTYPE is not supported"; return 1' open_cmd='print >&2 -r -- "open: Platform $OSTYPE is not supported"; return 1'
fi fi
eval "open(){local f; for f in \"\$@\"; do $open_cmd \"\$f\"; done;}" eval "open(){local f; for f in \"\$@\"; do $open_cmd \"\$f\"; done;}"
unset open_cmd unset open_cmd
@ -55,8 +55,8 @@ elif command_exists termux-clipboard-set && command_exists termux-clipboard-get;
copy_cmd='termux-clipboard-set' paste_cmd='termux-clipboard-get' copy_cmd='termux-clipboard-set' paste_cmd='termux-clipboard-get'
else else
error_msg='Platform $OSTYPE is not supported' error_msg='Platform $OSTYPE is not supported'
copy_cmd='print >&2 "clipcopy: '"$error_msg"'"; return 1' copy_cmd='print >&2 -r -- "clipcopy: '"$error_msg"'"; return 1'
paste_cmd='print >&2 "clippaste: '"$error_msg"'"; return 1' paste_cmd='print >&2 -r -- "clippaste: '"$error_msg"'"; return 1'
unset error_msg unset error_msg
fi fi
eval "clipcopy() { $copy_cmd; }; clippaste() { $paste_cmd; }" eval "clipcopy() { $copy_cmd; }; clippaste() { $paste_cmd; }"
@ -74,7 +74,7 @@ git_current_branch() {
command git symbolic-ref --quiet HEAD 2> /dev/null || command git symbolic-ref --quiet HEAD 2> /dev/null ||
command git rev-parse --short HEAD 2> /dev/null command git rev-parse --short HEAD 2> /dev/null
)" || return )" || return
echo "${ref#refs/heads/}" print -r -- "${ref#refs/heads/}"
} }
declare -A date_formats=( declare -A date_formats=(

View File

@ -9,7 +9,7 @@ export -T LD_LIBRARY_PATH ld_library_path ':'
path_prepend() { path_prepend() {
if (( $# < 1 )); then if (( $# < 1 )); then
print >&2 "usage: $0 <var_name> <value...>" print >&2 -r -- "usage: $0 <var_name> <value...>"
return 1 return 1
fi fi
local var_name="$1"; shift local var_name="$1"; shift

View File

@ -34,7 +34,7 @@ _plugin completions 'zsh-users/zsh-completions' "$_checkout_latest_version"
done; unset match done; unset match
if (( $run_compdump )); then if (( $run_compdump )); then
echo "$0: rebuilding zsh completion dump" print -r -- "$0: rebuilding zsh completion dump"
# -D flag turns off compdump loading # -D flag turns off compdump loading
compinit -D compinit -D
compdump compdump
@ -98,7 +98,7 @@ _plugin completions 'zsh-users/zsh-completions' "$_checkout_latest_version"
if [[ -d "$_fasd_ret" ]]; then if [[ -d "$_fasd_ret" ]]; then
cd -- "$_fasd_ret" cd -- "$_fasd_ret"
elif [[ -n "$_fasd_ret" ]]; then elif [[ -n "$_fasd_ret" ]]; then
print -- "$_fasd_ret" print -r -- "$_fasd_ret"
fi fi
} }
fi fi

View File

@ -3,7 +3,7 @@
# Escapes `%` in all arguments by replacing it with `%%`. Escaping is needed so # Escapes `%` in all arguments by replacing it with `%%`. Escaping is needed so
# that untrusted input (e.g. git branch names) doesn't affect prompt rendering. # that untrusted input (e.g. git branch names) doesn't affect prompt rendering.
prompt_escape() { prompt_escape() {
print -n "${@//\%/%%}" print -rn -- "${@//\%/%%}"
} }
prompt_preexec_hook() { prompt_preexec_hook() {
@ -51,7 +51,7 @@ prompt_vcs_info() {
fi fi
done done
print -n ' %F{blue}git:%F{magenta}'"$(prompt_escape "$branch")"'%f' print -rn -- ' %F{blue}git:%F{magenta}'"$(prompt_escape "$branch")"'%f'
} }
# configure prompt expansion # configure prompt expansion

View File

@ -60,9 +60,9 @@
_palette_widget() { _palette_widget() {
# download "TLDR pages" if we don't have them # download "TLDR pages" if we don't have them
if [[ ! -d "$PALETTE_TLDR_PAGES_DIR" ]]; then if [[ ! -d "$PALETTE_TLDR_PAGES_DIR" ]]; then
echo print -r
_palette_download_tldr_pages _palette_download_tldr_pages
echo print -r
fi fi
# try to fill in a placeholder if there're any, otherwise pick a snippet # try to fill in a placeholder if there're any, otherwise pick a snippet
@ -118,12 +118,12 @@
# This function downloads the "TLDR pages" # This function downloads the "TLDR pages"
_palette_download_tldr_pages() { _palette_download_tldr_pages() {
mkdir -pv "$PALETTE_TLDR_PAGES_DIR" mkdir -pv "$PALETTE_TLDR_PAGES_DIR"
echo "Downloading tldr pages..." print -r -- "Downloading tldr pages..."
if curl -Lf https://github.com/tldr-pages/tldr/archive/master.tar.gz | if curl -Lf https://github.com/tldr-pages/tldr/archive/master.tar.gz |
tar -C "$PALETTE_TLDR_PAGES_DIR" --gzip --strip-components 2 --extract tldr-master/pages tar -C "$PALETTE_TLDR_PAGES_DIR" --gzip --strip-components 2 --extract tldr-master/pages
then then
echo "Done!" print -r -- "Done!"
fi fi
} }

View File

@ -51,7 +51,7 @@ _ZPLG_PLUGINS_DIR="$ZPLG_HOME/plugins"
# basic logging {{{ # basic logging {{{
_zplg_log() { _zplg_log() {
print >&2 "${fg_bold[blue]}[zplg]${reset_color} $@" print >&2 -r -- "${fg_bold[blue]}[zplg]${reset_color} $@"
} }
_zplg_debug() { _zplg_debug() {
@ -455,7 +455,7 @@ _zplg_is_plugin_loaded() {
# Prints IDs of all loaded plugins. # Prints IDs of all loaded plugins.
zplg-list() { zplg-list() {
# (F) modifier joins an array with newlines # (F) modifier joins an array with newlines
print "${(F)ZPLG_LOADED_PLUGINS}" print -r -- "${(F)ZPLG_LOADED_PLUGINS}"
} }
# Upgrades all plugins if no arguments are given, otherwise upgrades plugins by # Upgrades all plugins if no arguments are given, otherwise upgrades plugins by