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:
1cca9e4b72
This commit is contained in:
Vidar Holen 2019-11-26 04:36:31 -08:00 committed by Mischievous Meerkat
parent e81efdb5e6
commit 1bf49c80e7
7 changed files with 30 additions and 35 deletions

View file

@ -37,7 +37,7 @@ _nnn ()
COMPREPLY=( $(compgen -f -d -- "$cur") ) COMPREPLY=( $(compgen -f -d -- "$cur") )
elif [[ $prev == -e ]]; then elif [[ $prev == -e ]]; then
local sessions_dir=${XDG_CONFIG_HOME:-$HOME/.config}/nnn/sessions 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 elif [[ $cur == -* ]]; then
COMPREPLY=( $(compgen -W "${opts[*]}" -- "$cur") ) COMPREPLY=( $(compgen -W "${opts[*]}" -- "$cur") )
else else

View file

@ -1,14 +1,14 @@
n () n ()
{ {
# Block nesting of nnn in subshells # Block nesting of nnn in subshells
if [ "$((NNNLVL + 0))" -ge 1 ]; then if [ "${NNNLVL:-0}" -ge 1 ]; then
echo "nnn is already running" echo "nnn is already running"
return return
fi fi
# The default behaviour is to cd on quit (nnn checks if NNN_TMPFILE is set) # 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 # 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 # Unmask ^Q (, ^V etc.) (if required, see `stty -a`) to Quit nnn
# stty start undef # stty start undef

View file

@ -1,14 +1,14 @@
n () n ()
{ {
# Block nesting of nnn in subshells # Block nesting of nnn in subshells
if [ "$((NNNLVL + 0))" -ge 1 ]; then if [ "${NNNLVL:-0}" -ge 1 ]; then
echo "nnn is already running" echo "nnn is already running"
return return
fi fi
# The default behaviour is to cd on quit (nnn checks if NNN_TMPFILE is set) # 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 # 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 # Unmask ^Q (, ^V etc.) (if required, see `stty -a`) to Quit nnn
# stty start undef # stty start undef

View file

@ -24,8 +24,7 @@ fi
add_file () add_file ()
{ {
printf "%s" "$@" >> "$selection" printf '%s\0' "$@" >> "$selection"
printf "\0" >> "$selection"
} }
use_all () use_all ()
@ -57,7 +56,7 @@ elif [ "$resp" = "d" ]; then
use_all use_all
"$dnd" "$all" "$PWD/"* & "$dnd" "$all" "$PWD/"* &
elif [ "$resp" = "r" ]; then elif [ "$resp" = "r" ]; then
printf > "$selection" true > "$selection"
"$dnd" --print-path --target | while read -r f "$dnd" --print-path --target | while read -r f
do do
if printf "%s" "$f" | grep '^\(https\?\|ftps\?\|s\?ftp\):\/\/' ; then if printf "%s" "$f" | grep '^\(https\?\|ftps\?\|s\?ftp\):\/\/' ; then

View file

@ -18,18 +18,17 @@ if [ "$(cmd_exists fzy)" -eq "0" ]; then
sel=$($fd | fzy) sel=$($fd | fzy)
elif [ "$(cmd_exists fzf)" -eq "0" ]; then elif [ "$(cmd_exists fzf)" -eq "0" ]; then
sel=$(fzf --print0) sel=$(fzf)
else else
exit 1 exit 1
fi fi
if ! [ -z "$sel" ]; then if [ -n "$sel" ]; then
case "$(file -bi "$sel")" in if ! [ -d "$sel" ]; then
*directory*) ;; sel=$(dirname "$sel")
*) sel=$(dirname "$sel") ;; fi
esac
# Remove "./" prefix # Remove "./" prefix if it exists
sel="$(echo "$sel" | cut -c 3-)" sel="${sel#./}"
nnn_cd "$PWD/$sel" nnn_cd "$PWD/$sel"
fi fi

View file

@ -19,29 +19,29 @@
# Author: Arun Prakash Jana # Author: Arun Prakash Jana
EBOOK_ID= 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" BROWSE_LINK="http://www.gutenberg.org/ebooks/search/?sort_order=downloads"
BROWSER=w3m BROWSER=w3m
READER= READER=
if [ ! -z "$EBOOK_ID" ]; then if [ -n "$EBOOK_ID" ]; then
if [ ! -e "$DIR" ]; then if [ ! -e "$DIR" ]; then
mkdir -p "$DIR" mkdir -p "$DIR"
cd "$DIR" || exit 1 cd "$DIR" || exit 1
if [ -z "$READER" ]; then 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 unzip "$EBOOK_ID"-h.zip
else 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
fi fi
if [ -d "$DIR" ]; then if [ -d "$DIR" ]; then
if [ -z "$READER" ]; then if [ -z "$READER" ]; then
"$BROWSER" "$DIR"/"$EBOOK_ID"-h/"$EBOOK_ID"-h.htm "$BROWSER" "$DIR/$EBOOK_ID-h/$EBOOK_ID-h.htm"
else else
"$READER" "$DIR"/"$EBOOK_ID".epub "$READER" "$DIR/$EBOOK_ID.epub"
fi fi
fi fi
else else

View file

@ -77,23 +77,16 @@ upload_connect_timeout="5"
upload_timeout="120" upload_timeout="120"
upload_retries="1" upload_retries="1"
# shellcheck disable=SC2034
if is_mac; then if is_mac; then
# shellcheck disable=SC2034
screenshot_select_command="screencapture -i %img" screenshot_select_command="screencapture -i %img"
# shellcheck disable=SC2034
screenshot_window_command="screencapture -iWa %img" screenshot_window_command="screencapture -iWa %img"
# shellcheck disable=SC2034
screenshot_full_command="screencapture %img" screenshot_full_command="screencapture %img"
# shellcheck disable=SC2034
open_command="open %url" open_command="open %url"
else else
# shellcheck disable=SC2034
screenshot_select_command="scrot -s %img" screenshot_select_command="scrot -s %img"
# shellcheck disable=SC2034
screenshot_window_command="scrot %img" screenshot_window_command="scrot %img"
# shellcheck disable=SC2034
screenshot_full_command="scrot %img" screenshot_full_command="scrot %img"
# shellcheck disable=SC2034
open_command="xdg-open %url" open_command="xdg-open %url"
fi fi
open="true" open="true"
@ -169,8 +162,7 @@ function take_screenshot() {
cmd="screenshot_${mode}_command" cmd="screenshot_${mode}_command"
cmd=${!cmd//\%img/${1}} cmd=${!cmd//\%img/${1}}
shot_err="$(${cmd} &>/dev/null)" #takes a screenshot with selection if ! shot_err="$(${cmd} &>/dev/null)"; then #takes a screenshot with selection
if ! [ -z "$shot_err" ]; then
echo "Failed to take screenshot '${1}': '${shot_err}'. For more information visit https://github.com/jomo/imgur-screenshot/wiki/Troubleshooting" | tee -a "${log_file}" 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" notify error "Something went wrong :(" "Information has been logged"
exit 1 exit 1
@ -256,8 +248,13 @@ function refresh_access_token() {
check_oauth2_client_secrets check_oauth2_client_secrets
token_url="https://api.imgur.com/oauth2/token" token_url="https://api.imgur.com/oauth2/token"
# exchange the refresh token for access_token and refresh_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 ! response="$(curl --compressed -fsSL --stderr - \
if ! [ -z "$response" ]; then -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 # curl failed
handle_upload_error "${response}" "${token_url}" handle_upload_error "${response}" "${token_url}"
exit 1 exit 1