-
Notifications
You must be signed in to change notification settings - Fork 0
/
open.sh
executable file
·49 lines (46 loc) · 1.18 KB
/
open.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
#!/usr/bin/env bash
edit() {
if [ -e "$1" ] && [ ! -w "$1" ]; then
exec sudo -E "$EDITOR" "$@"
elif [ ! -e "$1" ] && [ -z "$(find . -maxdepth 0 -user "$USER")" ]; then
exec sudo -E "$EDITOR" "$@"
else
exec "$EDITOR" "$@"
fi
}
open() {
[ $# = 0 ] && return
[ "$1" = -n ] && TEST=$1 && shift
CMD="edit"
EXTENSION=$(echo "$1" | tr "[:upper:]" "[:lower:]")
case $EXTENSION in
*.jpg | *.jpeg | *.png)
if path=$(command -v gthumb); then
CMD=$path
else CMD=$BROWSER; fi ;;
*.odt | *.ods | *.docx | *.csv | *.xlsx)
CMD=libreoffice ;;
*.pdf)
if path=$(command -v evince); then
CMD=$path
else CMD=$BROWSER; fi ;;
*.webm | *.mp4 | *.mkv)
if path=$(command -v mpv); then
CMD=$path
else CMD=$BROWSER; fi ;;
esac
if [ "$CMD" = edit ]; then
[ "$TEST" = -n ] && exit 1
"$CMD" "$@"
else
[ "$TEST" = -n ] && exit 0
spawn "$CMD" "$@"
fi
}
files=( "${@}" )
if [ $# = 0 ]; then
mapfile -t files < <(fzf)
open "${files[@]}"
else
open "$@"
fi