forked from Icycoide/linfm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.sh
444 lines (438 loc) · 12.3 KB
/
main.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#!/bin/env bash
declare -a -x tifm_extensions
declare -A -x tifm_extensions_commands
declare -a -x tifm_extensions_commands_list
declare -A -x tifm_extensions_commands_help
declare -a -x tifm_extensions_longcommands
declare -A -x tifm_extensions_subcommands
tifmx.bind() {
tifm_extensions_commands["$1"]="$2"
}
tifmx.add_long() {
tifm_extensions_longcommands+=("$1")
}
tifmx.bind_sub() {
tifm_extensions_subcommands["$1$2"]="$3"
}
tifmx.add_help() {
tifm_extensions_commands_list+=("$1")
tifm_extensions_commands_help["$1"]="$2"
}
# consts
__TIFM_VERSION="0.2.1"
BLACK=$(tput setaf 0)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
LIME_YELLOW=$(tput setaf 190)
POWDER_BLUE=$(tput setaf 153)
BLUE=$(tput setaf 4)
MAGENTA=$(tput setaf 5)
CYAN=$(tput setaf 6)
WHITE=$(tput setaf 7)
BRIGHT=$(tput bold)
NORMAL=$(tput sgr0)
BLINK=$(tput blink)
REVERSE=$(tput smso)
UNDERLINE=$(tput smul)
DIM=$(tput dim)
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
init() {
# vars
STATUS=0
# config
source "$SCRIPT_DIR/config.sh"
# load extensions
tput sc
printf "Loading extensions... "
load_extensions
printf "Done.\n"
}
load_extensions() {
local count=0
# check how many files are in the extensions directory
local files=$(ls -1 "$SCRIPT_DIR/extensions" | wc -l)
for extension in "$SCRIPT_DIR/extensions/"*".sh"; do
# if there is no save folder, create it
if [ ! -d "$SCRIPT_DIR/save" ]; then
mkdir "$SCRIPT_DIR/save"
fi
local ext_name=$(basename "$extension" | cut -d. -f1)
# if the name is in the extension_ignore file, skip it
if grep -q "$ext_name" "$SCRIPT_DIR/extension_ignore"; then
continue
fi
source "$extension"
# add only the name of the extension to the array (ex: extensions/git.sh -> git)
tifm_extensions+=("$ext_name")
count=$((count + 1))
# percentage of completion
local percent=$((count / files * 100))
tput rc
printf "Loading extensions... $count/$files ($percent%%)\n"
# if function $ext_name.init exists, call it
if type "$ext_name".init &> /dev/null; then
"$ext_name".init
fi
done
}
main() {
echo "$__TIFM_DECO_COLOUR$__ANGLE_UP_RIGHT $(__TIFM_DISPLAY)$NORMAL"
while read file; do
__color="$__TIFM_LS_COLOUR_FILE"
__post=""
__icon="🗎"
if [ -d "$file" ]; then
__color="$__TIFM_LS_COLOUR_DIRECTORY"
__post="/"
__icon="␣"
fi
echo "$__TIFM_DECO_COLOUR$__VBAR$__color $__icon $file$__post$NORMAL"
done < <(ls -a)
read -n 1 -p "$__TIFM_DECO_COLOUR$__ANGLE_DOWN_RIGHT $(__TIFM_PROMPT) $YELLOW" ans
if [[ "$ans" != "n" ]] && [[ "$ans" != "r" ]] && [[ "$ans" != ";" ]] && [[ ! "${tifm_extensions_longcommands[*]}" =~ "$ans" ]]; then
echo ""
fi
printf "$NORMAL"
# command execution
case "$ans" in
N)
echo "Select a directory to go to (leave blank to cancel)."
read -p "nav:: " tifm_dir
if [[ "$tifm_dir" == "" ]]; then
echo "Cancelled."
return
else
cd $tifm_dir
# for every extension, if it has a function called name.nav, call it
for ext_name in "${tifm_extensions[@]}"; do
if type "$ext_name".nav &> /dev/null; then
"$ext_name".nav "$tifm_dir"
fi
done
fi
;;
o)
echo "Choose a file to open (leave blank to cancel)."
read -p "file:: " tifm_file
if [[ "$tifm_file" == "" ]]; then
echo "Cancelled."
return
else
xdg-open $tifm_file
fi
;;
p)
echo "Choose a file to view (leave blank to cancel)."
read -p "file:: " tifm_file
if [[ "$tifm_file" == "" ]]; then
echo "Cancelled."
return
else
$__TIFM_PAGER $tifm_file
fi
;;
e)
echo "Choose a file to edit (leave blank to cancel)."
read -p "file:: " tifm_file
if [[ "$tifm_file" == "" ]]; then
echo "Cancelled."
return
else
$__TIFM_EDITOR $tifm_file
fi
# for every extension, if it has a function called name.edit, call it
for ext_name in "${tifm_extensions[@]}"; do
if type "$ext_name".edit &> /dev/null; then
"$ext_name".edit "$tifm_file"
fi
done
;;
c)
echo "Choose the file and the location you would like to copy it to (leave blank to cancel)."
read -p "from:: " tifm_file_from
if [[ "$tifm_file_from" == "" ]]; then
echo "Cancelled."
return
fi
read -p "to:: " tifm_file_to
if [[ "$tifm_file_to" == "" ]]; then
echo "Cancelled."
return
fi
cp "$tifm_file_from" "$tifm_file_to"
# for every extension, if it has a function called name.copy, call it
for ext_name in "${tifm_extensions[@]}"; do
if type "$ext_name".copy &> /dev/null; then
"$ext_name".copy "$tifm_file_from" "$tifm_file_to"
fi
done
;;
m)
echo "Choose the file and the new location you would like to move it to."
read -p "from:: " tifm_file_from
if [[ "$tifm_file_from" == "" ]]; then
echo "Cancelled."
return
fi
read -p "to:: " tifm_file_to
if [[ "$tifm_file_to" == "" ]]; then
echo "Cancelled."
return
fi
mv "$tifm_file_from" "$tifm_file_to"
# for every extension, if it has a function called name.move, call it
for ext_name in "${tifm_extensions[@]}"; do
if type "$ext_name".move &> /dev/null; then
"$ext_name".move "$tifm_file_from" "$tifm_file_to"
fi
done
;;
i)
echo "Choose the directory to inspect (leave blank to cancel)."
read -p "dir:: " tifm_dir
if [[ "$tifm_dir" == "" ]]; then
echo "Cancelled."
return
else
ls -l $tifm_dir | $__TIFM_PAGER
fi
;;
n)
read -n 1 tifm_type
echo ""
case "$tifm_type" in
d)
echo "Choose the directory you would like to create (leave blank to cancel)."
read -p "name:: " tifm_dir_name
if [[ "$tifm_dir_name" == "" ]]; then
echo "Cancelled."
return
fi
mkdir "$tifm_dir_name"
# for every extension, if it has a function called name.mkdir, call it
for ext_name in "${tifm_extensions[@]}"; do
if type "$ext_name".mkdir &> /dev/null; then
"$ext_name".mkdir "$tifm_dir_name"
fi
done
;;
f)
echo "Choose the file you would like to create (leave blank to cancel)."
read -p "name:: " tifm_file_name
if [[ "$tifm_file_name" == "" ]]; then
echo "Cancelled."
return
fi
touch "$tifm_file_name"
# for every extension, if it has a function called name.mkfile, call it
for ext_name in "${tifm_extensions[@]}"; do
if type "$ext_name".mkfile &> /dev/null; then
"$ext_name".mkfile "$tifm_file_name"
fi
done
;;
*)
echo "Invalid type ([d]irectory/[f]ile)."
;;
esac
;;
r)
read -n 1 tifm_type
echo ""
case "$tifm_type" in
d)
echo "Choose the directory you would like to remove (leave blank to cancel)."
read -p "name:: " tifm_dir_name
if [[ "$tifm_dir_name" == "" ]]; then
echo "Cancelled."
return
fi
rm -rf "$tifm_dir_name"
# for every extension, if it has a function called name.rmdir, call it
for ext_name in "${tifm_extensions[@]}"; do
if type "$ext_name".rmdir &> /dev/null; then
"$ext_name".rmdir "$tifm_dir_name"
fi
done
;;
f)
echo "Choose the file you would like to remove (leave blank to cancel)."
read -p "name:: " tifm_file_name
if [[ "$tifm_file_name" == "" ]]; then
echo "Cancelled."
return
fi
rm "$tifm_file_name"
# for every extension, if it has a function called name.rmfile, call it
for ext_name in "${tifm_extensions[@]}"; do
if type "$ext_name".rmfile &> /dev/null; then
"$ext_name".rmfile "$tifm_file_name"
fi
done
;;
*)
echo "Invalid type ([d]irectory/[f]ile)."
;;
esac
;;
P)
echo "Choose a file or folder to change the permission (leave blank to cancel)."
read -p "item:: " tifm_select
if [[ "$tifm_select" == "" ]]; then
echo "Cancelled."
return
fi
echo "Choose the arguments from 'chmod' to set as permission for the file or folder (leave blank to cancel). (Read the manual for chmod for more info)"
read -p "perm:: " tifm_perm
if [[ "$tifm_perm" == "" ]]; then
echo "Cancelled."
return
fi
chmod $tifm_perm $tifm_select
# for every extension, if it has a function called name.fperms, call it
for ext_name in "${tifm_extensions[@]}"; do
if type "$ext_name".fperms &> /dev/null; then
"$ext_name".fperms "$tifm_select" "$tifm_perm"
fi
done
;;
t)
/bin/bash
;;
";")
read -n 1 tifm_sub
echo ""
case "$tifm_sub" in
c)
# open config
$__TIFM_EDITOR "$SCRIPT_DIR/config.sh"
# reload config
source "$SCRIPT_DIR/config.sh"
;;
e)
# open extension menu
local all_extensions=()
for extension in "$SCRIPT_DIR/extensions/"*".sh"; do
local ext_name=$(basename "$extension" .sh)
# if the extension name is in the extension_ignore file, ignore it
all_extensions+=("$ext_name")
done
display_extensions() {
clear
# display all extensions
echo "Extensions:"
for extension in "${all_extensions[@]}"; do
# if not in the ignored extensions list, display with a green checkmark
local grepped=$(grep "$extension" "$SCRIPT_DIR/extension_ignore")
if [[ -z "$grepped" ]]; then
echo -e " $GREEN✔$NORMAL $extension"
else
echo -e " $RED✘$NORMAL $extension"
fi
done
}
local executing=true
while $executing; do
display_extensions
echo ""
echo "type the name of an extension to toggle it, or type 'q' to quit."
read -p "extension:: " tifm_extension
if [[ "$tifm_extension" == "q" ]]; then
executing=false
continue
fi
# if the extension is ignored, unignore it
if [[ -n $(grep "$tifm_extension" "$SCRIPT_DIR/extension_ignore") ]]; then
sed -i "/$tifm_extension/d" "$SCRIPT_DIR/extension_ignore"
# remove from the ignored extensions list
local index=0
for ext in "${ignored_extensions[@]}"; do
if [[ "$ext" == "$tifm_extension" ]]; then
unset ignored_extensions[$index]
fi
((index++))
done
else
# if the extension is not ignored, ignore it
echo "$tifm_extension" >> "$SCRIPT_DIR/extension_ignore"
fi
done
;;
*)
echo "Invalid subcommand."
;;
esac
;;
"?")
echo "${LIME_YELLOW}List of native commands:$NORMAL
N - Goes to a folder
o - Opens a file
p - View file (uses 'less' by default - change in config.sh)
e - edit a file (uses 'nano' by default - change in config.sh)
m - Moves/Renames a file
c - Copies a file to a location
i - Inspects a directory (ls -l) and pipes it to the selected pager ('less' by default - change in config.sh)
n(f/d) - Creates a file or directory
r(f/d) - Removes a file or directory
P - Sets permissions for a specific file or folder
t - Switches to command line mode, run 'exit' to exit.
;(c/e) - Open [c]onfig file, [e]xtension menu
Q - Quits the program"
if [[ ! -z "${tifm_extensions[@]}" ]]; then
echo "${LIME_YELLOW}List of commands defined by extensions [${tifm_extensions[@]}]:$NORMAL"
for cmd in "${tifm_extensions_commands_list[@]}"; do
echo "$cmd - ${tifm_extensions_commands_help[$cmd]}"
done
fi
;;
Q)
clear
exit
;;
"")
# do nothing
;;
*)
if [[ ! -z "${tifm_extensions_commands[$ans]}" ]]; then
eval "${tifm_extensions_commands[$ans]}"
elif [[ "${tifm_extensions_longcommands[*]}" =~ "$ans" ]]; then
read -n 1 tifm_subcommand
echo ""
if [[ ! -z "${tifm_extensions_subcommands[$ans$tifm_subcommand]}" ]]; then
eval "${tifm_extensions_subcommands[$ans$tifm_subcommand]}"
else
echo "Invalid subcommand."
fi
else
echo "Unrecognized command. Type '?' for a list of commands."
fi
;;
esac
STATUS=$?
}
(
clear
init
echo "$__TIFM_DECO_COLOUR$__ANGLE_UP_RIGHT$GREEN tifm $__TIFM_VERSION$NORMAL
$__TIFM_DECO_COLOUR$__VBAR$NORMAL $BLUE${UNDERLINE}https://github.com/Rexxt/tifm$NORMAL
$__TIFM_DECO_COLOUR$__VBAR$NORMAL $RED$REVERSE/!\\ you are running a bleeding edge version of tifm. $NORMAL
$__TIFM_DECO_COLOUR$__VBAR$NORMAL $RED$REVERSE if you encounter any bugs, report them on github.$NORMAL
$__TIFM_DECO_COLOUR$__ANGLE_DOWN_RIGHT$NORMAL strike '?' for help"
echo ""
while true; do
main
if "$__TIFM_CONFIRM_RETURN"; then
# wait for user to agree to return to the file view
echo
echo "${BRIGHT}${RED}press q to return to file view${NORMAL}"
key=""
while [ ! "$key" == "q" ]; do
read -n 1 key
done
clear
else
echo
fi
done
)