Skip to content

Instantly share code, notes, and snippets.

@dvessel
Last active July 18, 2023 07:22
Show Gist options
  • Save dvessel/801777814e0a925cabf617963df802d8 to your computer and use it in GitHub Desktop.
Save dvessel/801777814e0a925cabf617963df802d8 to your computer and use it in GitHub Desktop.
Helper functions to bulk convert bin/cue or gdi files to chd's and back.
#!/bin/zsh
# - Requires homebrew. https://brew.sh
# - Converted files will be moved to the current working directory in all cases.
# - You can pass the starting directory for fzf search functions.
# - Pass in glob patterns for the standalone function to convert in bulk.
# For example, to convert gdi's in a sub-directory: chd.create */*.gdi
alias chd.fzf.create='_fzf-paths-to chd.create f ".(gdi|cue)$"'
alias chd.fzf.extract-to-cue='_fzf-paths-to chd.extract-to-cue f .chd$'
alias chd.fzf.extract-to-gdi='_fzf-paths-to chd.extract-to-gdi f .chd$'
function chd.create {
_brew_check chdman:rom-tools && return 1
trap '[[ -f "${c:r}.$tmp" ]] && rm "${c:r}.$tmp"' INT TERM QUIT
local ext=chd
local tmp=$ext.compressing
for c in $@; do
chdman createcd -i "$c" -o "${c:r}.$tmp" &&
mv "${c:r}.$tmp" ./"${c:t:r}.$ext" ||
break
done
}
function chd.extract-to-cue { chd.extract-to cue $@ }
function chd.extract-to-gdi { chd.extract-to gdi $@ }
function chd.extract-to {
_brew_check chdman:rom-tools && return 1
trap '[[ -d "${c:r} $tmp" ]] && rm -r "${c:r} $tmp"' INT TERM QUIT
local ext=$1
local tmp='- extracting'
for c in $2; do
mkdir -p "${c:r} $tmp" &&
chdman extractcd -i "$c" -o "${c:r} $tmp/${c:t:r}.$ext" &&
mv "${c:r} $tmp" ./"${c:t:r}" ||
break
done
}
# Helper function. Not to be called directly. Use aliases instead.
#
# example: alias foo='_fzf-paths-to bar f'
#
# arguments in this order: @see man fd
# - command
# - type [f]ile or [d]irectory (optional, defaults to file.)
# - glob pattern (optional)
# - starting directory (optional)
#
function _fzf-paths-to {
_brew_check fd fzf && return 1
local command=${1:?'command argument required.'}
if type "$command" &>/dev/null; then
local type=${2:-f}
local pattern=${3:-.}
local dir=${4:-.}
local files=( "${(f)$(fd --follow --type $type $pattern $dir | fzf -m)}" )
[[ ! -z $files ]] && $command $files
else
echo "$command does not exist."
fi
}
# Helper function used to check for a brew installed executibles.
#
# usage:
# _brew_check executible[:fomulae - optional] [executible]... && return 1
#
# Fomulae is optional. Use when the executible and formulae does not match.
# Add as many executibles:formulae as needed. return 1 to exit on fails.
#
function _brew_check {
for i in $@; ! type ${i/:*/} &>/dev/null &&
echo "${i/:*/} required. Install with 'brew install ${i/*:/}'."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment