-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinc.bash
executable file
·95 lines (84 loc) · 2.16 KB
/
inc.bash
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
#!/bin/bash
HEADER_HEIGHT=3
function showPaths {
path_list="$1"
field_height="$2"
curpos="$3"
distpath="$4"
echo "$path_list" | head -n $field_height
tput cup $curpos 0
tput rev
echo "$distpath"
tput sgr0
}
distpath=
keyword=
path_list=
cur=
function updateKeyword {
keyword=$1
cur=1
path_list=$(find -L -path "*${keyword// /*}*" -iname '*.gpg' | sed -e "s/^\.\///" -e "s/\.gpg$//")
}
tput init
tput clear
cd $PREFIX
updateKeyword ""
while [[ true ]]; do
# Calculate number of lines and cursor position to show
field_height=$(($(tput lines) - $HEADER_HEIGHT - 1))
path_list_height=$(echo "$path_list" | wc -l)
(($path_list_height > $field_height)) && path_list_height=$field_height
(($path_list_height < $cur)) && cur=$path_list_height
distpath=$(echo "$path_list" | sed -n "${cur}p")
tput civis
tput clear
tput cup $HEADER_HEIGHT 0
showPaths "$path_list" "$field_height" $(($HEADER_HEIGHT + $cur - 1)) "$distpath"
tput cup 0 0
tput cnorm
IFS= read -p "Keyword: $keyword" -r -s -N 1 c
if [[ "$c" = $'\e' ]]; then
# Escape Character
read -sN1 -t 0.0001 k1
read -sN1 -t 0.0001 k2
read -sN1 -t 0.0001 k3
c+="$k1$k2$k3"
fi
case $c in
$'\b'|$'\x7f') # BackSpace
if [[ "$keyword" = "" ]]; then
continue
fi
updateKeyword ${keyword::-1}
;;
$'\cu')
# Clear line
updateKeyword ""
;;
$'\cl') # Ignore
;;
$'\cp'|$'\e[A'|$'\e0A'|$'\e[D'|$'\e0D') # Up
((cur > 1)) && ((cur--))
;;
$'\cn'|$'\e[B'|$'\e0B'|$'\e[C'|$'\e0C') # Down
((cur++))
;;
$'\x0a') # Enter
tput clear
echo "$PROGRAM" "$distpath" "$@"
"$PROGRAM" "$distpath" "$@"
break
;;
$'\e'|$'\cd') # ESC or Ctrl-D
tput clear
echo "Canceled"
break
;;
$'\e'*) # Other escape sequence
;;
*)
updateKeyword "$keyword$c"
;;
esac
done