From 1bf49c80e734d7fdefda0686d81f95a1ce619b10 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Tue, 26 Nov 2019 04:36:31 -0800 Subject: [PATCH] Some of the shell script fixes as per discussion (#389) * Improved completion support for files with spaces Filenames with spaces, both generally and for session names, will now complete as expected. * Misc shell script improvements See discussion on: https://github.com/jarun/nnn/commit/1cca9e4b72b106374f203890b266f18609deefe3 --- misc/auto-completion/bash/nnn-completion.bash | 4 ++-- misc/quitcd/quitcd.bash | 4 ++-- misc/quitcd/quitcd.zsh | 4 ++-- plugins/dragdrop | 5 ++--- plugins/fzcd | 15 +++++++------ plugins/gutenread | 12 +++++------ plugins/imgur | 21 ++++++++----------- 7 files changed, 30 insertions(+), 35 deletions(-) diff --git a/misc/auto-completion/bash/nnn-completion.bash b/misc/auto-completion/bash/nnn-completion.bash index 638df496..6b73d059 100644 --- a/misc/auto-completion/bash/nnn-completion.bash +++ b/misc/auto-completion/bash/nnn-completion.bash @@ -8,7 +8,7 @@ _nnn () { COMPREPLY=() - local IFS=$' \n' + local IFS=$'\n' local cur=$2 prev=$3 local -a opts opts=( @@ -37,7 +37,7 @@ _nnn () COMPREPLY=( $(compgen -f -d -- "$cur") ) elif [[ $prev == -e ]]; then local sessions_dir=${XDG_CONFIG_HOME:-$HOME/.config}/nnn/sessions - COMPREPLY=( $(compgen -W "$(ls "$sessions_dir")" -- "$cur") ) + COMPREPLY=( $(cd "$sessions_dir" && compgen -f -d -- "$cur") ) elif [[ $cur == -* ]]; then COMPREPLY=( $(compgen -W "${opts[*]}" -- "$cur") ) else diff --git a/misc/quitcd/quitcd.bash b/misc/quitcd/quitcd.bash index 5c94b11f..5ea82c45 100644 --- a/misc/quitcd/quitcd.bash +++ b/misc/quitcd/quitcd.bash @@ -1,14 +1,14 @@ n () { # Block nesting of nnn in subshells - if [ "$((NNNLVL + 0))" -ge 1 ]; then + if [ "${NNNLVL:-0}" -ge 1 ]; then echo "nnn is already running" return fi # The default behaviour is to cd on quit (nnn checks if NNN_TMPFILE is set) # To cd on quit only on ^G, export NNN_TMPFILE after the call to nnn - export NNN_TMPFILE=${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.lastd + export NNN_TMPFILE="${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.lastd" # Unmask ^Q (, ^V etc.) (if required, see `stty -a`) to Quit nnn # stty start undef diff --git a/misc/quitcd/quitcd.zsh b/misc/quitcd/quitcd.zsh index 5c94b11f..5ea82c45 100644 --- a/misc/quitcd/quitcd.zsh +++ b/misc/quitcd/quitcd.zsh @@ -1,14 +1,14 @@ n () { # Block nesting of nnn in subshells - if [ "$((NNNLVL + 0))" -ge 1 ]; then + if [ "${NNNLVL:-0}" -ge 1 ]; then echo "nnn is already running" return fi # The default behaviour is to cd on quit (nnn checks if NNN_TMPFILE is set) # To cd on quit only on ^G, export NNN_TMPFILE after the call to nnn - export NNN_TMPFILE=${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.lastd + export NNN_TMPFILE="${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.lastd" # Unmask ^Q (, ^V etc.) (if required, see `stty -a`) to Quit nnn # stty start undef diff --git a/plugins/dragdrop b/plugins/dragdrop index ee4fc201..bbbd19e7 100755 --- a/plugins/dragdrop +++ b/plugins/dragdrop @@ -24,8 +24,7 @@ fi add_file () { - printf "%s" "$@" >> "$selection" - printf "\0" >> "$selection" + printf '%s\0' "$@" >> "$selection" } use_all () @@ -57,7 +56,7 @@ elif [ "$resp" = "d" ]; then use_all "$dnd" "$all" "$PWD/"* & elif [ "$resp" = "r" ]; then - printf > "$selection" + true > "$selection" "$dnd" --print-path --target | while read -r f do if printf "%s" "$f" | grep '^\(https\?\|ftps\?\|s\?ftp\):\/\/' ; then diff --git a/plugins/fzcd b/plugins/fzcd index 3633e645..6ee67d60 100755 --- a/plugins/fzcd +++ b/plugins/fzcd @@ -18,18 +18,17 @@ if [ "$(cmd_exists fzy)" -eq "0" ]; then sel=$($fd | fzy) elif [ "$(cmd_exists fzf)" -eq "0" ]; then - sel=$(fzf --print0) + sel=$(fzf) else exit 1 fi -if ! [ -z "$sel" ]; then - case "$(file -bi "$sel")" in - *directory*) ;; - *) sel=$(dirname "$sel") ;; - esac +if [ -n "$sel" ]; then + if ! [ -d "$sel" ]; then + sel=$(dirname "$sel") + fi - # Remove "./" prefix - sel="$(echo "$sel" | cut -c 3-)" + # Remove "./" prefix if it exists + sel="${sel#./}" nnn_cd "$PWD/$sel" fi diff --git a/plugins/gutenread b/plugins/gutenread index 95634ab9..029cccf3 100755 --- a/plugins/gutenread +++ b/plugins/gutenread @@ -19,29 +19,29 @@ # Author: Arun Prakash Jana EBOOK_ID= -DIR=${XDG_CACHE_HOME:-$HOME/.cache}/nnn/gutenbooks/"$EBOOK_ID" +DIR="${XDG_CACHE_HOME:-$HOME/.cache}/nnn/gutenbooks/$EBOOK_ID" BROWSE_LINK="http://www.gutenberg.org/ebooks/search/?sort_order=downloads" BROWSER=w3m READER= -if [ ! -z "$EBOOK_ID" ]; then +if [ -n "$EBOOK_ID" ]; then if [ ! -e "$DIR" ]; then mkdir -p "$DIR" cd "$DIR" || exit 1 if [ -z "$READER" ]; then - curl -L -O https://www.gutenberg.org/files/"$EBOOK_ID"/"$EBOOK_ID"-h.zip + curl -L -O "https://www.gutenberg.org/files/$EBOOK_ID/$EBOOK_ID-h.zip" unzip "$EBOOK_ID"-h.zip else - curl -L -o "$EBOOK_ID".epub http://www.gutenberg.org/ebooks/"$EBOOK_ID".epub.noimages + curl -L -o "$EBOOK_ID".epub "http://www.gutenberg.org/ebooks/$EBOOK_ID.epub.noimages" fi fi if [ -d "$DIR" ]; then if [ -z "$READER" ]; then - "$BROWSER" "$DIR"/"$EBOOK_ID"-h/"$EBOOK_ID"-h.htm + "$BROWSER" "$DIR/$EBOOK_ID-h/$EBOOK_ID-h.htm" else - "$READER" "$DIR"/"$EBOOK_ID".epub + "$READER" "$DIR/$EBOOK_ID.epub" fi fi else diff --git a/plugins/imgur b/plugins/imgur index a5715157..e95360c7 100755 --- a/plugins/imgur +++ b/plugins/imgur @@ -77,23 +77,16 @@ upload_connect_timeout="5" upload_timeout="120" upload_retries="1" +# shellcheck disable=SC2034 if is_mac; then - # shellcheck disable=SC2034 screenshot_select_command="screencapture -i %img" - # shellcheck disable=SC2034 screenshot_window_command="screencapture -iWa %img" - # shellcheck disable=SC2034 screenshot_full_command="screencapture %img" - # shellcheck disable=SC2034 open_command="open %url" else - # shellcheck disable=SC2034 screenshot_select_command="scrot -s %img" - # shellcheck disable=SC2034 screenshot_window_command="scrot %img" - # shellcheck disable=SC2034 screenshot_full_command="scrot %img" - # shellcheck disable=SC2034 open_command="xdg-open %url" fi open="true" @@ -169,8 +162,7 @@ function take_screenshot() { cmd="screenshot_${mode}_command" cmd=${!cmd//\%img/${1}} - shot_err="$(${cmd} &>/dev/null)" #takes a screenshot with selection - if ! [ -z "$shot_err" ]; then + if ! shot_err="$(${cmd} &>/dev/null)"; then #takes a screenshot with selection echo "Failed to take screenshot '${1}': '${shot_err}'. For more information visit https://github.com/jomo/imgur-screenshot/wiki/Troubleshooting" | tee -a "${log_file}" notify error "Something went wrong :(" "Information has been logged" exit 1 @@ -256,8 +248,13 @@ function refresh_access_token() { check_oauth2_client_secrets token_url="https://api.imgur.com/oauth2/token" # exchange the refresh token for access_token and refresh_token - response="$(curl --compressed -fsSL --stderr - -F "client_id=${imgur_acct_key}" -F "client_secret=${imgur_secret}" -F "grant_type=refresh_token" -F "refresh_token=${refresh_token}" "${token_url}")" - if ! [ -z "$response" ]; then + if ! response="$(curl --compressed -fsSL --stderr - \ + -F "client_id=${imgur_acct_key}" \ + -F "client_secret=${imgur_secret}" \ + -F "grant_type=refresh_token" \ + -F "refresh_token=${refresh_token}" \ + "${token_url}" + )"; then # curl failed handle_upload_error "${response}" "${token_url}" exit 1