Skip to content

Instantly share code, notes, and snippets.

@khardix
Last active December 1, 2015 10:57
Show Gist options
  • Save khardix/623847b3b9ea7cf5cc02 to your computer and use it in GitHub Desktop.
Save khardix/623847b3b9ea7cf5cc02 to your computer and use it in GitHub Desktop.
Existing game launcher script overview script
#!/bin/zsh
# Scan this directory for game launchers and present window to pick from them.
# Dependencies: awk zenity
# Game launcher format:
# Game launcher is executable text file (i.e. shell script), which contains
# one or more lines describing the game being launched. The basic format is:
#
# #GAME: Game title
#
# which indicates that this script launches game titled "Game title".
#
# In case the script can launch multiple games, the expected way is to have
# one main launcher and symlinks with game names pointing to it. Then the
# extended description format should be used (possible in addition to the
# basic one):
#
# #GAME:name: Game title
#
# This indicates that if the file (or symlink) name is "name", the game
# launched is titled "Game title".
#
# Note that whichever valid format is found first, is taken as the correct
# one. Taht means when combining basic and extended descriptions, the basic
# one must come LAST, because it always matches.
readonly SCRIPT_DIR="${0:A:h}"
# awk script for parsing the game launcher scripts
read -r -d '' AWK_SOURCE <<'EOF'
function print_game_entry(name, title_start) {
for (f=title_start; f < NF; ++f) printf $f " "; print $NF;
print name;
}
/^#GAME:\w+:/ {
split($1, p, /:/);
if (p[2] ~ FILENAME) { print_game_entry(p[2], 2); nextfile; };
}
/^#GAME:[[:space:]]/ { print_game_entry(FILENAME, 2); nextfile; }
EOF
readonly AWK_SOURCE
# Do the scan, capture output
cd "$SCRIPT_DIR"
readonly gamelist=$(awk "${AWK_SOURCE}" *(-*))
readonly choice=$(echo "$gamelist"|\
zenity --list --title="Choose a game to play…"\
--column "Title" --column "Launcher" --print-column 2\
2>/dev/null)
#echo "$choice" 1>&2
# Launch the chosen game, if any
[ -n "$choice" ] && exec "./$choice"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment