summary refs log tree commit diff
path: root/bin/open
blob: ba809bfabd821b95330a492231de491a74f52bf0 (plain) (blame)
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
#!/bin/bash

set -m
termmode=0

while getopts ":hg" opt; do
    case $opt in
        g)
            termmode=1
            ;;
        h|*)
            echo "usage: open [-t] FILE"
            echo ""
            echo "  -t  Use only Terminal-based applications"
            exit 2
            ;;
    esac
done
shift $((OPTIND-1))

in="$1"

mime=$(file --mime-type "$in" | awk -F': ' '{print $2}' )
ext=$(basename "$in" | rev | cut -d'.' -f1 | rev)
opener=xdg-open

fork() {
    exec $@ >/dev/null 2>&1 &
}
term() {
    fork alacritty --title="$1" --command "$@"
}

case $mime in
    inode/directory)    [ $termmode -eq 0 ] && vifm "$in" ;;
    inode/file)         [ $termmode -eq 0 ] && vifm "$in" ;;

    image/*)            fork mvi "$in" ;;
    video/*)            fork mpv --force-window=yes "$in" ;;
    audio/*)            fork mpv --force-window=yes "$in" ;;

    application/pdf)    fork zathura "$in" ;;
    # application/vnd.ms-opentype|application/font-sfnt)
                        # fork gnome-font-viewer "$in" ;;

    # now text...
    text/md|text/markdown)
                        fork shiba "$(readlink -f "$in")" ;;
    text/*)             fork term ${EDITOR} "$in" ;;

    # everything else...
    *) exec $opener "$in" ;;
esac