summary refs log tree commit diff
path: root/bin/open
blob: 9a1eb3f56f54917a9f2c4175181a72c929ff88fa (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
54
55
56
57
58
59
#!/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 $@ 1>&2 2>/dev/null &
}

term() {
    fork alacritty --title="$1" --command "$@"
}

case $mime in
    inode/directory)    [ $termmode -eq 0 ] && vifm "$in" || fork nautilus -w "$in" ;;
    # inode/file)         fork nautilus -w $in ;;

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

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

    # now text...
    text/*)
        # echo $mime $ext
        case $ext in
            md|markdown) fork shiba "$(readlink -f "$in")" ;;
            desktop) fork gtk-launch "$(basename "$in" | cut -d'.' -f1)" ;;
            *)           fork term nvim "$in" ;;
        esac
        ;;

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