aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/.nnn-plugin-helper
blob: bcb9649fc42541b6738bb0d49398f5cf3e5a1563 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env sh

# Description: Helper script for plugins
#
# Shell: POSIX compliant
# Author: Anna Arad

selection=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection}
export selection

## Set CUR_CTX to 1 to open directory in current context
CUR_CTX=0
export CUR_CTX

## Ask nnn to switch to directory $1 in context $2.
## If $2 is not provided, the function asks explicitly.
nnn_cd () {
    dir="$1"

    if [ -z "$NNN_PIPE" ]; then
        echo "No pipe file found" 1>&2
        return
    fi

    if [ -n "$2" ]; then
        context=$2
    elif [ $CUR_CTX -ne 1 ]; then
        printf "Choose context 1-4 (blank for current): "
        read -r context
    fi

    printf "%s" "${context:-0}c$dir" > "$NNN_PIPE"
}

cmd_exists () {
	which "$1" > /dev/null 2>&1
	echo $?
}