-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathview
executable file
·54 lines (47 loc) · 1.68 KB
/
view
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
#!/bin/bash
TGT="$1"
TGT=${TGT:-./}
EDITOR="vim -c:{2} {1}"
TOPDIR="$PWD"
TMPFILE=$(mktemp "/tmp/view-$USER-XXXXXX")
# Break execution and keep term clean
quit() { rm -rf $TMPFILE ; echo -e "\e[0m" ; exit; }
# Keep a sound terminal
trap quit SIGINT
while : ; do
COL=$(tput cols)
read pathindicator <<< $(sed 's/.*\(\(\/[^/]\+\)\{2\}\)$/\1/g' <<< $PWD)
[[ "$pathindicator" != "$PWD" ]] && pathindicator="…${pathindicator}"
# Smart orientation...
# useful with tiling WM's and Termux usage
#if [[ $COL -lt 200 ]]; then
prevwin='top,80%,border-bottom'
#else
# prevwin='right,80%,border-left'
#fi
grep -Rn '^--\$\|^--|\ #' | sed 's/:--\(\$|\|\s*#\)\s*/:/g' |\
fzf \
--color 16 \
--delimiter=: --margin=0 \
--keep-right --preview-window="${prevwin}" \
--info=inline --prompt=" $pathindicator > "\
--layout=reverse-list \
--bind "enter:execute($EDITOR)" \
--bind "ctrl-e:execute($EDITOR)" \
--bind "alt-up:preview-page-up" \
--bind "alt-down:preview-page-down" \
--bind "tab:execute(echo \"cd;{1}\" > \"${TMPFILE}\")+accept" \
--bind "backward-eof:execute(echo \"cd;..\" > \"${TMPFILE}\")+accept" \
--preview='bat --paging=always --line-range {2}: --highlight-line {2} --color=always --wrap=character --number {1} | less -R +e{2}'
[[ $? -eq 130 ]] && quit
read cmd <<< $(cut -d\; -f1 $TMPFILE)
read opt <<< $(cut -d\; -f2- $TMPFILE)
if [[ "$cmd" == "cd" ]]; then
if [[ "$opt" == ".." ]]; then
[[ "$TOPDIR" != "$PWD" ]] && cd ..
else
cd "$(cut -d\' -f2 <<< "$(dirname "$opt")")"
fi
fi
done
quit