-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnex
More file actions
executable file
·114 lines (99 loc) · 2.17 KB
/
nex
File metadata and controls
executable file
·114 lines (99 loc) · 2.17 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/bin/sh
set -e
FZF=$(which fzf || true)
if [ -z "$FZF" ]; then
echo "FZF not found :("
fi
NC=$(which nc)
if [ -z "$NC" ]; then
echo "nc (netcat) not found :("
fi
HOST=""
if [ -z "$1" ]; then
printf "Usage: nex HOST"
exit 1
else
HOST="$1"
fi
LOC="${2:-/}"
# collapses multiple slashes and handle . and ..
normalize_path() {
# split $1 by /
path="$1"
oldIFS=$IFS
IFS=/
set -- $path # no quotes!
IFS=$oldIFS
out='/'
for comp; do
case "$comp" in
''|.) continue ;;
..)
out="${out%/*}"
;;
*)
case "$out" in
/) out="/$comp" ;;
*) out="$out/$comp" ;;
esac
;;
esac
done
case "$path" in
*/)
case "$out" in
*/) ;;
*) out="$out/"
esac
;;
esac
printf '%s\n' "$out"
}
LOC=$(normalize_path "$LOC")
VIEW="$FZF +s +m --no-mouse --info=hidden --layout=reverse-list --height=~100%"
while :; do
case "$LOC" in
*/) IS_DIR=1 ;;
*) IS_DIR='' ;;
esac
NEXT=$(
{
[ "$LOC" != '/' ] && echo '=> /'
[ -z "$IS_DIR" ] && echo '=> ./'
[ "$LOC" != '/' ] && echo '=> ../'
printf '%s\n' "$LOC" | $NC "$HOST" 1900
} | $VIEW --footer="$HOST :: $LOC" || echo ':q'
)
if [ "$NEXT" = ':q' ]; then
echo "bye!"
break
fi
case "$NEXT" in
"=> "*)
set -- $NEXT # split by spaces
LINK=$2
;;
*)
# non-link selected, just do page refresh
continue
;;
esac
case "$LINK" in
nex://*)
echo TODO
;;
/*)
# local absolute path, just go to it
LOC=$(normalize_path "$LINK")
;;
*)
# select current directory if LOC points to the file
case "$LOC" in
/) ;;
*/) ;;
*) LOC="$(dirname "$LOC")/";;
esac
LOC=$(normalize_path "$LOC$LINK")
;;
esac
done