-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcdi.sh
executable file
·178 lines (151 loc) · 5.06 KB
/
cdi.sh
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/bin/bash
#
# CDI - Change Dir Interactively
# Don't waste more time in the terminal browsing folders with CD
# helper function to colorize terminal text
color_print() {
# function call syntax
# print_color "text to print" <foreground-color> <formatting> <background-color>
# Foreground colors
get_foreground() {
case $1 in
'black') printf "\033[30m" ;;
'red') printf "\033[31m" ;;
'green') printf "\033[32m" ;;
'orange') printf "\033[33m" ;;
'blue') printf "\033[34m" ;;
'magenta') printf "\033[35m" ;;
'cyan') printf "\033[36m" ;;
'gray') printf "\033[37m" ;;
*) printf "\033[39m" ;; # default or anything else
esac
}
# Formatting
get_formatting() {
case $1 in
'bold') printf '\033[1m' ;;
'underline') printf '\033[4m' ;;
'bold-underline') printf '\033[4m\033[1m' ;;
*) printf '\033[22m\033[24m' ;; # code 22 to disable bold, 24 for underline
esac
}
# Background colors
get_background() {
case $1 in
'black') printf '\033[40m' ;;
'red') printf '\033[41m' ;;
'green') printf '\033[42m' ;;
'orange') printf '\033[43m' ;;
'blue') printf '\033[44m' ;;
'magenta') printf '\033[45m' ;;
'cyan') printf '\033[46m' ;;
'light-gray') printf '\033[47m' ;;
'gray') printf '\033[100m' ;;
'light-red') printf '\033[101m' ;;
'light-green') printf '\033[102m' ;;
'yellow') printf '\033[103m' ;;
'light-blue') printf '\033[104m' ;;
'light-purple') printf '\033[105m' ;;
'teal') printf '\033[106m' ;;
'white') printf '\033[107m' ;;
*) printf '\033[49m' ;; # default or anything else
esac
}
FORE=`get_foreground $2`
BACK=`get_background $4`
FMT=`get_formatting $3`
printf "$FORE$BACK$FMT$1\n"
# reset all formatting, can be combined to a single line for reduced code
printf '\033[39m' # default foreground
printf '\033[49m' # default background
printf '\033[22m\033[24m' # default formatting
}
print_folders() {
# $1 = current_dir
# $2 = current_selection
# List files and filter only those ending with "/"
array=($(ls -p $1 | grep /))
folders_list_size=$((${#array[@]}-1))
if [ "${#array[@]}" -ne 0 ]; then
for i in "${!array[@]}"; do
if test "$i" -eq $2; then
# echo -e "\033[1m→ ${array[i]}\033[0m"
printf "→ " # print selection arrow on same line
color_print "${array[i]}" green bold-underline
else
# echo " ${array[i]}"
color_print " ${array[i]}"
fi
done
else
#echo -e 'No folders here, press \033[1m←\033[0m to back'
color_print 'No folders here, press ← to back' red
fi
}
print_status() {
#echo -e "[ \033[1m$1\033[0m ]\n"
color_print " [ $1 ]\n" orange bold
#echo -e "Selection $2 \n"
}
get_selected_folder() {
# $1 = current_dir
# $2 = current_selection
array=($(ls -p $1 | grep /))
for i in "${!array[@]}"; do
if test "$i" -eq $2
then
selected_folder=${array[i]::-1}
fi
done
}
init() {
# Initial values
current_dir=$(pwd)
current_selection=0
clear
escape_char=$(printf "\u1b")
while
print_status $current_dir $current_selection
print_folders $current_dir $current_selection
read -rsn1 mode
clear
do
if [[ $mode == "$escape_char" ]]; then
read -rsn2 mode
fi
# echo $mode
case $mode in
'[A') # UP
if [ "$current_selection" -eq 0 ]; then
current_selection=$folders_list_size
else
current_selection=$((current_selection-1))
fi
;;
'[B') # DOWN
if [ "$current_selection" -eq "$folders_list_size" ]; then
current_selection=0
else
current_selection=$((current_selection+1))
fi
;;
'[D') # LEFT
# Removes the last path level
current_dir=${current_dir%/*}
current_selection=0
;;
'[C') # RIGHT
get_selected_folder $current_dir $current_selection
current_dir="$current_dir/$selected_folder"
current_selection=0
;;
*)
# Change to directory
# Since script was invoked through the source command (. ./Script) we are still in the same Shell instance,
# so it is possible to execute the CD command and thus change the directory.
cd "$current_dir" || exit
return
esac
done
}
init