Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple maintenance improvements to shell scripts #2598

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions bin/fzf-tmux
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ done

if [[ -z "$TMUX" ]]; then
"$fzf" "${args[@]}"
exit $?
exit "$?"
fi

# --height option is not allowed. CTRL-Z is also disabled.
Expand All @@ -140,7 +140,7 @@ if [[ ! "$opt" =~ "-E" ]] && tmux list-panes -F '#F' | grep -q Z; then
zoomed_without_popup=1
original_window=$(tmux display-message -p "#{window_id}")
tmp_window=$(tmux new-window -d -P -F "#{window_id}" "bash -c 'while :; do for c in \\| / - '\\;' do sleep 0.2; printf \"\\r\$c fzf-tmux is running\\r\"; done; done'")
tmux swap-pane -t $tmp_window \; select-window -t $tmp_window
tmux swap-pane -t "$tmp_window" \; select-window -t "$tmp_window"
fi

set -e
Expand All @@ -153,7 +153,7 @@ fifo2="${TMPDIR:-/tmp}/fzf-fifo2-$id"
fifo3="${TMPDIR:-/tmp}/fzf-fifo3-$id"
tmux_win_opts=( $(tmux show-window-options remain-on-exit \; show-window-options synchronize-panes | sed '/ off/d; s/^/set-window-option /; s/$/ \\;/') )
cleanup() {
\rm -f $argsf $fifo1 $fifo2 $fifo3
\rm -f "$argsf" "$fifo1" "$fifo2" "$fifo3"

# Restore tmux window options
if [[ "${#tmux_win_opts[@]}" -gt 0 ]]; then
Expand All @@ -163,9 +163,9 @@ cleanup() {
# Remove temp window if we were zoomed without popup options
if [[ -n "$zoomed_without_popup" ]]; then
tmux display-message -p "#{window_id}" > /dev/null
tmux swap-pane -t $original_window \; \
select-window -t $original_window \; \
kill-window -t $tmp_window \; \
tmux swap-pane -t "$original_window" \; \
select-window -t "$original_window" \; \
kill-window -t "$tmp_window" \; \
resize-pane -Z
fi

Expand Down Expand Up @@ -199,36 +199,36 @@ echo "$envs;" > "$argsf"
opts=$(printf "%q " "${args[@]}")

pppid=$$
echo -n "trap 'kill -SIGUSR1 -$pppid' EXIT SIGINT SIGTERM;" >> $argsf
echo -n "trap 'kill -SIGUSR1 -$pppid' EXIT SIGINT SIGTERM;" >> "$argsf"
close="; trap - EXIT SIGINT SIGTERM $close"

export TMUX=$(cut -d , -f 1,2 <<< "$TMUX")
mkfifo -m o+w $fifo2
mkfifo -m o+w "$fifo2"
if [[ "$opt" =~ "-E" ]]; then
cat $fifo2 &
cat "$fifo2" &
if [[ -n "$term" ]] || [[ -t 0 ]]; then
cat <<< "\"$fzf\" $opts > $fifo2; out=\$? $close; exit \$out" >> $argsf
cat <<< "\"$fzf\" $opts > $fifo2; out=\$? $close; exit \$out" >> "$argsf"
else
mkfifo $fifo1
cat <<< "\"$fzf\" $opts < $fifo1 > $fifo2; out=\$? $close; exit \$out" >> $argsf
cat <&0 > $fifo1 &
mkfifo "$fifo1"
cat <<< "\"$fzf\" $opts < $fifo1 > $fifo2; out=\$? $close; exit \$out" >> "$argsf"
cat <&0 > "$fifo1" &
fi

tmux popup -d "$PWD" $opt "bash $argsf" > /dev/null 2>&1
exit $?
exit "$?"
fi

mkfifo -m o+w $fifo3
mkfifo -m o+w "$fifo3"
if [[ -n "$term" ]] || [[ -t 0 ]]; then
cat <<< "\"$fzf\" $opts > $fifo2; echo \$? > $fifo3 $close" >> $argsf
cat <<< "\"$fzf\" $opts > $fifo2; echo \$? > $fifo3 $close" >> "$argsf"
else
mkfifo $fifo1
cat <<< "\"$fzf\" $opts < $fifo1 > $fifo2; echo \$? > $fifo3 $close" >> $argsf
cat <&0 > $fifo1 &
mkfifo "$fifo1"
cat <<< "\"$fzf\" $opts < $fifo1 > $fifo2; echo \$? > $fifo3 $close" >> "$argsf"
cat <&0 > "$fifo1" &
fi
tmux set-window-option synchronize-panes off \;\
set-window-option remain-on-exit off \;\
split-window -c "$PWD" $opt "bash -c 'exec -a fzf bash $argsf'" $swap \
> /dev/null 2>&1 || { "$fzf" "${args[@]}"; exit $?; }
cat $fifo2
exit "$(cat $fifo3)"
> /dev/null 2>&1 || { "$fzf" "${args[@]}"; exit "$?"; }
cat "$fifo2"
exit "$(cat "$fifo3")"
78 changes: 39 additions & 39 deletions install
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ check_binary() {
echo -n " - Checking fzf executable ... "
local output
output=$("$fzf_base"/bin/fzf --version 2>&1)
if [ $? -ne 0 ]; then
if [ "$?" -ne 0 ]; then
echo "Error: $output"
binary_error="Invalid binary"
else
Expand Down Expand Up @@ -115,20 +115,20 @@ link_fzf_in_path() {
try_curl() {
command -v curl > /dev/null &&
if [[ $1 =~ tar.gz$ ]]; then
curl -fL $1 | tar -xzf -
curl -fL "$1" | tar -xzf -
else
local temp=${TMPDIR:-/tmp}/fzf.zip
curl -fLo "$temp" $1 && unzip -o "$temp" && rm -f "$temp"
curl -fLo "$temp" "$1" && unzip -o "$temp" && rm -f "$temp"
fi
}

try_wget() {
command -v wget > /dev/null &&
if [[ $1 =~ tar.gz$ ]]; then
wget -O - $1 | tar -xzf -
wget -O - "$1" | tar -xzf -
else
local temp=${TMPDIR:-/tmp}/fzf.zip
wget -O "$temp" $1 && unzip -o "$temp" && rm -f "$temp"
wget -O "$temp" "$1" && unzip -o "$temp" && rm -f "$temp"
fi
}

Expand All @@ -140,15 +140,15 @@ download() {
fi
link_fzf_in_path && return
mkdir -p "$fzf_base"/bin && cd "$fzf_base"/bin
if [ $? -ne 0 ]; then
if [ "$?" -ne 0 ]; then
binary_error="Failed to create bin directory"
return
fi

local url
url=https://github.com/junegunn/fzf/releases/download/$version/${1}
set -o pipefail
if ! (try_curl $url || try_wget $url); then
if ! (try_curl "$url" || try_wget "$url"); then
set +o pipefail
binary_error="Failed to download with curl and wget"
return
Expand All @@ -168,29 +168,29 @@ archi=$(uname -sm)
binary_available=1
binary_error=""
case "$archi" in
Darwin\ arm64) download fzf-$version-darwin_arm64.zip ;;
Darwin\ x86_64) download fzf-$version-darwin_amd64.zip ;;
Linux\ armv5*) download fzf-$version-linux_armv5.tar.gz ;;
Linux\ armv6*) download fzf-$version-linux_armv6.tar.gz ;;
Linux\ armv7*) download fzf-$version-linux_armv7.tar.gz ;;
Linux\ armv8*) download fzf-$version-linux_arm64.tar.gz ;;
Linux\ aarch64*) download fzf-$version-linux_arm64.tar.gz ;;
Linux\ loongarch64) download fzf-$version-linux_loong64.tar.gz ;;
Linux\ ppc64le) download fzf-$version-linux_ppc64le.tar.gz ;;
Linux\ *64) download fzf-$version-linux_amd64.tar.gz ;;
Linux\ s390x) download fzf-$version-linux_s390x.tar.gz ;;
FreeBSD\ *64) download fzf-$version-freebsd_amd64.tar.gz ;;
OpenBSD\ *64) download fzf-$version-openbsd_amd64.tar.gz ;;
CYGWIN*\ *64) download fzf-$version-windows_amd64.zip ;;
MINGW*\ *64) download fzf-$version-windows_amd64.zip ;;
MSYS*\ *64) download fzf-$version-windows_amd64.zip ;;
Windows*\ *64) download fzf-$version-windows_amd64.zip ;;
*) binary_available=0 binary_error=1 ;;
Darwin\ arm64) download "fzf-$version-darwin_arm64.zip" ;;
Darwin\ x86_64) download "fzf-$version-darwin_amd64.zip" ;;
Linux\ armv5*) download "fzf-$version-linux_armv5.tar.gz" ;;
Linux\ armv6*) download "fzf-$version-linux_armv6.tar.gz" ;;
Linux\ armv7*) download "fzf-$version-linux_armv7.tar.gz" ;;
Linux\ armv8*) download "fzf-$version-linux_arm64.tar.gz" ;;
Linux\ aarch64*) download "fzf-$version-linux_arm64.tar.gz" ;;
Linux\ loongarch64) download "fzf-$version-linux_loong64.tar.gz" ;;
Linux\ ppc64le) download "fzf-$version-linux_ppc64le.tar.gz" ;;
Linux\ *64) download "fzf-$version-linux_amd64.tar.gz" ;;
Linux\ s390x) download "fzf-$version-linux_s390x.tar.gz" ;;
FreeBSD\ *64) download "fzf-$version-freebsd_amd64.tar.gz" ;;
OpenBSD\ *64) download "fzf-$version-openbsd_amd64.tar.gz" ;;
CYGWIN*\ *64) download "fzf-$version-windows_amd64.zip" ;;
MINGW*\ *64) download "fzf-$version-windows_amd64.zip" ;;
MSYS*\ *64) download "fzf-$version-windows_amd64.zip" ;;
Windows*\ *64) download "fzf-$version-windows_amd64.zip" ;;
*) binary_available=0 binary_error=1 ;;
esac

cd "$fzf_base"
if [ -n "$binary_error" ]; then
if [ $binary_available -eq 0 ]; then
if [ "$binary_available" -eq 0 ]; then
echo "No prebuilt binary for $archi ..."
else
echo " - $binary_error !!!"
Expand Down Expand Up @@ -246,12 +246,12 @@ for shell in $shells; do
echo -n "Generate $src ... "

fzf_completion="[[ \$- == *i* ]] && source \"$fzf_base/shell/completion.${shell}\" 2> /dev/null"
if [ $auto_completion -eq 0 ]; then
if [ "$auto_completion" -eq 0 ]; then
fzf_completion="# $fzf_completion"
fi

fzf_key_bindings="source \"$fzf_base/shell/key-bindings.${shell}\""
if [ $key_bindings -eq 0 ]; then
if [ "$key_bindings" -eq 0 ]; then
fzf_key_bindings="# $fzf_key_bindings"
fi

Expand Down Expand Up @@ -280,11 +280,11 @@ if [[ "$shells" =~ fish ]]; then
echo \$fish_user_paths | \grep "$fzf_base"/bin > /dev/null
or set --universal fish_user_paths \$fish_user_paths "$fzf_base"/bin
EOF
[ $? -eq 0 ] && echo "OK" || echo "Failed"
[ "$?" -eq 0 ] && echo "OK" || echo "Failed"

mkdir -p "${fish_dir}/functions"
fish_binding="${fish_dir}/functions/fzf_key_bindings.fish"
if [ $key_bindings -ne 0 ]; then
if [ "$key_bindings" -ne 0 ]; then
echo -n "Symlink $fish_binding ... "
ln -sf "$fzf_base/shell/key-bindings.fish" \
"$fish_binding" && echo "OK" || echo "Failed"
Expand All @@ -308,7 +308,7 @@ append_line() {
echo "Update $file:"
echo " - $line"
if [ -f "$file" ]; then
if [ $# -lt 4 ]; then
if [ "$#" -lt 4 ]; then
lno=$(\grep -nF "$line" "$file" | sed 's/:.*//' | tr '\n' ' ')
else
lno=$(\grep -nF "$pat" "$file" | sed 's/:.*//' | tr '\n' ' ')
Expand All @@ -317,7 +317,7 @@ append_line() {
if [ -n "$lno" ]; then
echo " - Already exists: line #$lno"
else
if [ $update -eq 1 ]; then
if [ "$update" -eq 1 ]; then
[ -f "$file" ] && echo >> "$file"
echo "$line" >> "$file"
echo " + Added"
Expand All @@ -340,39 +340,39 @@ create_file() {
echo
}

if [ $update_config -eq 2 ]; then
if [ "$update_config" -eq 2 ]; then
echo
ask "Do you want to update your shell configuration files?"
update_config=$?
fi
echo
for shell in $shells; do
[[ "$shell" = fish ]] && continue
[ $shell = zsh ] && dest=${ZDOTDIR:-~}/.zshrc || dest=~/.bashrc
append_line $update_config "[ -f ${prefix}.${shell} ] && source ${prefix}.${shell}" "$dest" "${prefix}.${shell}"
[ "$shell" = zsh ] && dest=${ZDOTDIR:-~}/.zshrc || dest=~/.bashrc
append_line "$update_config" "[ -f ${prefix}.${shell} ] && source ${prefix}.${shell}" "$dest" "${prefix}.${shell}"
done

if [ $key_bindings -eq 1 ] && [[ "$shells" =~ fish ]]; then
if [ "$key_bindings" -eq 1 ] && [[ "$shells" =~ fish ]]; then
bind_file="${fish_dir}/functions/fish_user_key_bindings.fish"
if [ ! -e "$bind_file" ]; then
create_file "$bind_file" \
'function fish_user_key_bindings' \
' fzf_key_bindings' \
'end'
else
append_line $update_config "fzf_key_bindings" "$bind_file"
append_line "$update_config" "fzf_key_bindings" "$bind_file"
fi
fi

if [ $update_config -eq 1 ]; then
if [ "$update_config" -eq 1 ]; then
echo 'Finished. Restart your shell or reload config file.'
if [[ "$shells" =~ bash ]]; then
echo -n ' source ~/.bashrc # bash'
[[ "$archi" =~ Darwin ]] && echo -n ' (.bashrc should be loaded from .bash_profile)'
echo
fi
[[ "$shells" =~ zsh ]] && echo " source ${ZDOTDIR:-~}/.zshrc # zsh"
[[ "$shells" =~ fish ]] && [ $key_bindings -eq 1 ] && echo ' fzf_key_bindings # fish'
[[ "$shells" =~ fish ]] && [ "$key_bindings" -eq 1 ] && echo ' fzf_key_bindings # fish'
echo
echo 'Use uninstall script to remove fzf.'
echo
Expand Down
2 changes: 1 addition & 1 deletion shell/completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ _fzf_handle_dynamic_completion() {
eval "$orig_complete"
fi
fi
return $ret
return "$ret"
fi
}

Expand Down
12 changes: 5 additions & 7 deletions uninstall
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ remove_line() {
match=0
while [ -n "$1" ]; do
line=$(sed -n "$line_no,\$p" "$src" | \grep -m1 -nF "$1")
if [ $? -ne 0 ]; then
if [ "$?" -ne 0 ]; then
shift
line_no=1
continue
Expand All @@ -69,17 +69,17 @@ remove_line() {
match=1
echo " - Line #$line_no: $content"
[ "$content" = "$1" ] || ask " - Remove?"
if [ $? -eq 0 ]; then
if [ "$?" -eq 0 ]; then
temp=$(mktemp)
awk -v n=$line_no 'NR == n {next} {print}' "$src" > "$temp" &&
awk -v n="$line_no" 'NR == n {next} {print}' "$src" > "$temp" &&
cat "$temp" > "$src" && rm -f "$temp" || break
echo " - Removed"
else
echo " - Skipped"
line_no=$(( line_no + 1 ))
fi
done
[ $match -eq 0 ] && echo " - Nothing found"
[ "$match" -eq 0 ] && echo " - Nothing found"
echo
}

Expand All @@ -100,9 +100,7 @@ if [ -d "${fish_dir}/functions" ]; then
remove "${fish_dir}/functions/fzf.fish"
remove "${fish_dir}/functions/fzf_key_bindings.fish"

if [ -z "$(ls -A "${fish_dir}/functions")" ]; then
rmdir "${fish_dir}/functions"
else
if ! rmdir "${fish_dir}/functions" 2> /dev/null; then
echo "Can't delete non-empty directory: \"${fish_dir}/functions\""
fi
fi
Expand Down