diff options
| author | Akshay Nair <phenax5@gmail.com> | 2020-12-24 00:06:27 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2020-12-24 00:06:27 +0530 |
| commit | 865728068c965e066d0f8971c5b5ba26e12e7dce (patch) | |
| tree | c16195f0a04cf7d004fe5b396255734f4d60bb00 /scripts/bin/open | |
| parent | 649ef5f225039e498358c3d056dfd7fcbc56f014 (diff) | |
| download | nixos-config-865728068c965e066d0f8971c5b5ba26e12e7dce.tar.gz nixos-config-865728068c965e066d0f8971c5b5ba26e12e7dce.zip | |
Adds scripts and links scripts and wallpapers
Diffstat (limited to 'scripts/bin/open')
| -rwxr-xr-x | scripts/bin/open | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/scripts/bin/open b/scripts/bin/open new file mode 100755 index 0000000..9daa34a --- /dev/null +++ b/scripts/bin/open @@ -0,0 +1,70 @@ +#!/usr/bin/env bash + +srun() { setsid -f "$@"; } +run() { srun "$@" > /dev/null 2> /dev/null; } +run_async() { run "$@" & disown; } + +open_by_mimetype() { + local file="$1"; + local mime=`file "$file" --mime-type -bL`; + + local is_opened=1; + + echo "Opening $file ($mime)..."; + + case "$mime" in + inode/directory) lf "$file" ;; + + application/x-bittorrent) ~/scripts/torrent.sh torrent "$file" ;; + + text/*) sensible-editor "$file" ;; + application/javascript) sensible-editor "$file" ;; + application/json) sensible-editor "$file" ;; + application/xml) sensible-editor "$file" ;; + application/x-*) sensible-editor "$file" ;; + *pdf) run_async zathura "$file" ;; + + video/*|image/gif) run_async mpv "$file" ;; + image/*) run_async sxiv "$file" ;; + audio/*) + mpc insert "file://$(readlink -e "$file")" && mpc play && \ + echo "Adding song to queue"; + ;; + + *) is_opened=0 ;; + esac; + + [[ "$is_opened" == 1 ]] && exit 0; +} + +open_by_path() { + local file="$1"; + + # Magnet torrent url + if [[ "$file" =~ ^magnet: ]]; then + echo "opening magnet link..."; + ~/scripts/torrent.sh magnet "$file"; + exit 0; + fi; + + # Https? and file urls + if [[ "$file" =~ ^https?:// ]] || [[ "$file" =~ ^file?:// ]]; then + echo "opening url $file..."; + run_async sensible-browser "$file"; + exit 0; + fi; +} + + +filepath="$1"; +[[ -z "$filepath" ]] && exit 1; + +open() { + open_by_path "$filepath"; + open_by_mimetype "$filepath"; + + # If it doesn't exit out, it couldn't open the file + echo "Couldn't open file. Add rule to opener."; exit 1; +} +open; + |
