Skip to content

Instantly share code, notes, and snippets.

@dvessel
Last active May 17, 2024 15:51
Show Gist options
  • Save dvessel/c71a3d2606856adc35fb5dc8e1a7f394 to your computer and use it in GitHub Desktop.
Save dvessel/c71a3d2606856adc35fb5dc8e1a7f394 to your computer and use it in GitHub Desktop.
Launch MAME through fuzzy search (fzf).
#!/usr/bin/env zsh
# helper to work with rom paths easier.
# - scans for roms in current working directory.
# - first argument can be a path.
# - if it's not, pass all arguments to lmame.
local rompath=`pwd`
# first parameter can be ambigous. assume directory if it exists.
if [[ -d ${1:a} ]]; then
rompath=${1:a}
shift
fi
lmame $@ -rompath $rompath
#!/usr/bin/env zsh
local rompath help
zparseopts -D -E - rompath:=rompath help=help || {
print "see -help" >&2
return 1
}
if [[ -n $help ]]; then
cat <<- --------
Presents a friendly list of full names for fuzzy search. fzf is required.
A query string can be passed anywhere mame expects a machine name. If
passed as the first argument, it will pre-filter the list and run after
selection. Without a pre-filter, the full list will be presented.
Some options accept machine names and the same pre-filter query can be
applied. Most options prefixed with -list* or -verify* are accepted and
some accept multiple selections. Use tab or ctrl-i for selection.
Examples:
- ${0:t} pacman Simple query string.
- ${0:t} "pacman midway" Quote spaces and special characters.
- ${0:t} "^pac-man (midway)$" ^ Starts with. $ Ends with.
- ${0:t} -verifyroms pacman Same applies to utility commands.
- ${0:t} -listroms Full list presented.
If the query string in the argument filters your collection down to one
result, it will be automatically selected.
-rompath <dir> ROM path is respected. Point to a different
directory and the list will reflect its contents.
See 'mame -showusage' for more information.
--------
return
fi
scandir=${${${rompath[-1]:a}:-`eval print $( mame -showconfig | awk '$1=="rompath" { $1=""; print $0 }' )`}}
if [[ ! -d $scandir ]]; then
printf "rom path not found:%s\n" "$scandir" >&2
return 1
fi
if [[ -f $scandir/.romlist && $scandir -nt $scandir/.romlist ]]; then
rm $scandir/.romlist
fi
if [[ ! -f $scandir/.romlist ]]; then
print "Generating list..."
typeset -A listselect
typeset files=( $scandir/*.(chd|7z|zip)(N) )
if [[ -n $files ]]; then
while read l; do if [[ $l =~ ^([0-9a-z_]*)[^\"]*\"(.*)\"$ ]]; then
listselect[$match[1]]=$match[2]
fi; done < <( mame -listfull $files:t:r | sed 1,1d )
else
print "No qualifying files found to scan. Supported extensions: zip,7z,chd." >&2
return 1
fi
if [[ -n $listselect ]]; then
printf "%s\t%s\n" ${(kv)listselect} | sort -k 2 > $scandir/.romlist
else
print "No qualifying files detected." >&2
return 1
fi
fi
local headers prefix fzargs=() accepts_multi
headers=( "`mame -version`" )
# -listmidi and -listnetwork completely unrelated. Do not prefix.
if [[ -n $1 && $1 =~ ^(-(list[^(midi|network)]|verify)[a-z]+) ]]; then
prefix=$match[1]
shift
fzargs+=--prompt=" $prefix "
accepts_multi=( -listxml -listfull -listsource -listcrc -listroms -verifyroms )
if [[ -n ${prefix:*accepts_multi} ]]; then
fzargs+=--multi
headers+=multi-select
fi
fi
if [[ -n $1 && ${1[1]} != - ]]; then
fzargs+=( --select-1 --query="$1" )
shift
fi
if [[ -n $@ ]]; then
headers=( $headers "${(j[ ])@}" )
fi
fzargs+=--header="${(j[ | ])headers}"
local selected names
selected=`cat $scandir/.romlist | fzf --delimiter='\t' --with-nth=2 $fzargs`
if [[ -n $selected ]]; then
if [[ -z $prefix ]]; then
printf "\e[1m%s\e[0m\n\n" "`print $selected | cut -f2`"
fi
names=( `print $selected | cut -f1` )
mame $prefix ${names[@]} $@ -rompath $scandir
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment