summaryrefslogtreecommitdiff
path: root/.local/bin/open
blob: 9b832653ba9dbd6bc9d4b87d34443b2559ff7ba5 (plain)
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
#!/bin/sh

[ -z "$1" ] && echo "USAGE: open <FILE>" >&2 && exit 1

for file in "$@"; do
	case "$file" in

		*.pdf|*.djvu|*.epub) setsid -f zathura "$file" ;;
		*.png|*.jpg|*.jpeg|*.webp) setsid -f nsxiv "$file" ;;
		*.gif|*.mkv|*.mp3|*.mp4|*.webm|*.ogg) ${PLAYER:-mpv --sub-auto=fuzzy} "$file" ;;
		*.html) ${BROWSER:-w3m} "$file" ;;
		*.pl.txt) fpl "$file" ;;
		*.txt|*.description) ${PAGER:-less} "$file" ;;
		*.md) glow --pager --width $(("$(tput cols)" - 5)) "$file" ;;
		*.css|*.json|*.ipynb|*.vim|*.lua|*.iso|*.zip) LESSOPEN="|preview %s" less -r "$file" ;;
		*.srt) fzf-subtitles -o "$file" ;;
		*.dig) setsid -f digital "$file" ;;
		*.doc|*.docx|*.ppt|*.pptx) setsid -f libreoffice "$file" ;;
		*.sc|*.xls|*.xlsx) sc-im "$file" ;;
		*.sim1) simulide "$file" ;;
		*.xz) ${EDITOR:-nvim} "$file" ;;

		*)
			filetype="$(file --dereference --brief --mime-type "$file")"
			case "$filetype" in
				inode/directory) ${FILES:-lf} "$file" ; exit ;;
				text/html*) ${BROWSER:-w3m} "$file" ;;
				*pdf|*djvu|*epub+zip|*oxps|*fictionbook) setsid -f zathura "$file" ;;
				text/*|*json|*zip|*zstd) "${EDITOR:-nvim}" "$file" ;;
				application/vnd.openxmlformats-officedocument.*) setsid -f libreoffice "$file" ;;
				application/sc) sc-im "$file" ;;
				application/x-executable) "$file" ;;
				image/*) setsid -f nsxiv "$file" ;;
				video/*|audio/*) ${PLAYER:- mpv --sub-auto=fuzzy} "$file" ;;
				*)
					xdg-open "$file" || exit
					[ -n "$DISPLAY" ] && notify-send "⚠️ open: unknown filetype" "$filetype"
					echo "open: unknown filetype: $filetype" >&2
					;;
			esac
			;;

	esac

	file="$(realpath "$file")"
	sed -i "\|^$file$|d" "${XDG_DATA_HOME:-$HOME/.local/share}/openhist"
	echo "$file" >> "${XDG_DATA_HOME:-$HOME/.local/share}/openhist"
done