dotfiles/dot_config/fish/functions/extract.fish

71 lines
1.7 KiB
Fish
Raw Normal View History

2024-04-14 18:41:15 -07:00
#!/usr/bin/env fish
# Extracts archived files / mounts disk images.
# Usage: extract <file> <extra args>
function run_cmd --argument-names cmd
set -f cmd_name (string split -f1 ' ' "$cmd")
if command -v "$cmd" &>/dev/null
eval "$cmd"
else
printf "Please install %s to extract this file.\n" "$cmd_name"
end
end
2024-04-14 18:41:15 -07:00
function extract -a file
set --erase argv[1]
2024-04-15 13:14:53 -07:00
if test -f "$file"
switch "$file"
2024-04-14 18:41:15 -07:00
case "*.tar.bz2"
run_cmd "tar -jxvf $argv $file"
2024-04-14 18:41:15 -07:00
case "*.tar.gz"
run_cmd "tar -zxvf $argv $file"
2024-04-14 18:41:15 -07:00
case "*.bz2"
run_cmd "bunzip2 $argv $file"
2024-04-14 18:41:15 -07:00
case "*.dmg"
2024-04-15 13:14:53 -07:00
if test "$(uname)" = Darwin
hdiutil mount "$file"
2024-04-14 18:41:15 -07:00
end
2024-04-14 18:41:15 -07:00
case "*.gz"
run_cmd "gunzip $argv $file"
2024-04-14 18:41:15 -07:00
case "*.tar"
run_cmd "tar -xvf $argv $file"
2024-04-14 18:41:15 -07:00
case "*.tbz2"
run_cmd "tar -jxvf $argv $file"
2024-04-14 18:41:15 -07:00
case "*.tgz"
run_cmd "tar -zxvf $argv $file"
case "*.zip" "*.ZIP"
set -f dir (string replace --ignore-case '.zip' '')
mkdir "$dir"
run_cmd "unzip $argv $file -d $dir"
2024-04-14 18:41:15 -07:00
case "*pax"
run_cmd "cat $file | pax -r $argv"
2024-04-14 18:41:15 -07:00
case "*.pax.Z"
run_cmd "uncompress $file --stdout | pax -r $argv"
2024-04-14 18:41:15 -07:00
case "*.rar"
run_cmd "unrar x $file"
2024-04-14 18:41:15 -07:00
case "*.Z"
run_cmd "uncompress $argv $file"
2024-04-14 18:41:15 -07:00
case "*"
echo "'$file' cannot be extracted/mounted via extract()."
end
else
echo "'$file' is not a valid file."
end
end