2019-10-23 10:04:12 +00:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
|
|
|
# Description: Helper script for plugins
|
|
|
|
#
|
|
|
|
# Shell: POSIX compliant
|
|
|
|
# Author: Anna Arad
|
|
|
|
|
2020-04-24 04:42:24 +00:00
|
|
|
selection=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection}
|
2019-11-21 20:44:25 +00:00
|
|
|
export selection
|
2019-10-23 10:04:12 +00:00
|
|
|
|
2020-02-20 02:24:32 +00:00
|
|
|
## Set CUR_CTX to 1 to open directory in current context
|
|
|
|
CUR_CTX=0
|
|
|
|
export CUR_CTX
|
|
|
|
|
2023-02-17 13:51:57 +00:00
|
|
|
NNN_PREFER_SELECTION="${NNN_PREFER_SELECTION:-0}"
|
|
|
|
export NNN_PREFER_SELECTION
|
|
|
|
|
2019-10-23 10:04:12 +00:00
|
|
|
## Ask nnn to switch to directory $1 in context $2.
|
|
|
|
## If $2 is not provided, the function asks explicitly.
|
|
|
|
nnn_cd () {
|
2020-05-06 13:11:01 +00:00
|
|
|
dir="$1"
|
2019-10-23 10:04:12 +00:00
|
|
|
|
|
|
|
if [ -z "$NNN_PIPE" ]; then
|
|
|
|
echo "No pipe file found" 1>&2
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "$2" ]; then
|
|
|
|
context=$2
|
2020-02-20 02:24:32 +00:00
|
|
|
elif [ $CUR_CTX -ne 1 ]; then
|
2019-11-21 20:44:25 +00:00
|
|
|
printf "Choose context 1-4 (blank for current): "
|
|
|
|
read -r context
|
2019-10-23 10:04:12 +00:00
|
|
|
fi
|
|
|
|
|
2020-05-03 09:25:33 +00:00
|
|
|
printf "%s" "${context:-0}c$dir" > "$NNN_PIPE"
|
2019-10-23 10:04:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd_exists () {
|
2021-05-15 01:27:20 +00:00
|
|
|
type "$1" > /dev/null 2>&1
|
|
|
|
echo $?
|
2019-10-23 10:04:12 +00:00
|
|
|
}
|
2023-02-17 13:51:57 +00:00
|
|
|
|
|
|
|
nnn_use_selection() {
|
|
|
|
if ! [ -s "$selection" ]; then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$NNN_PREFER_SELECTION" -eq 1 ]; then
|
|
|
|
return 0
|
|
|
|
else
|
2023-02-17 14:12:07 +00:00
|
|
|
[ -n "$1" ] && printf "%s " "$1"
|
2023-02-17 13:51:57 +00:00
|
|
|
printf "(s)election/(c)urrent? [default=c] "
|
|
|
|
read -r resp__
|
|
|
|
|
|
|
|
if [ "$resp__" = "s" ]; then
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|