Skip to content

Instantly share code, notes, and snippets.

@markusfisch markusfisch/README.md
Last active Dec 19, 2018

Embed
What would you like to do?
Explore a random snippet from five different popular open source projects like a dungeon in a roguelike

Guess the file

Explore a random snippet from five different popular open source projects like a dungeon in a Roguelike.

Can you guess what programming language or project a file is from?

Run it like this:

$ bash what_file.sh

Press Enter and move the cursor with WASD or HJKL. As soon as you are sure what file it is, press Enter and you will see a couple of options to pick from.

The character ˙ is shown beyond the end of a line. All your steps are counted.

#!/usr/bin/env bash
# Fallback for systems without `tput` (man terminfo)
type tput &>/dev/null || tput() {
case "$1" in
cols)
echo 80
;;
lines)
echo 24
;;
clear) # clear screen and move cursor top/left
printf '\033[2J\033[;H'
;;
civis) # hide cursor
printf '\033[?25l'
;;
cnorm) # restore cursor to normal
printf '\033[?25h'
;;
cup) # move to y/x
printf '\033[%d;%dH' $(($2 + 1)) $(($3 + 1))
;;
esac
}
# Restore terminal defaults and end quiz
restore() {
# clear screen and show cursor
tput clear
tput cnorm
# reset to default values
stty sane
}
# Reveal current position
reveal() {
tput cup "$PY" "$PX"
echo -n "${MAP:$((PY * WIDTH + PX)):1}"
}
# Process input from STDIN
input() {
read -r -n 1
case "$REPLY" in
''|q)
return 1
;;
[kw])
((PY > 0)) && {
reveal
((--PY))
((++MOVES))
}
;;
[ld])
((PX < WIDTH)) && {
reveal
((++PX))
((++MOVES))
}
;;
[ha])
((PX > 0)) && {
reveal
((--PX))
((++MOVES))
}
;;
[js])
((PY < HEIGHT - 2)) && {
reveal
((++PY))
((++MOVES))
}
;;
esac
return 0
}
# Draw player
draw_player() {
tput cup "$PY" "$PX"
echo -n '@'
}
# Inspect map
inspect() {
local PX=0
local PY=0
# stop if STDIN isn't a terminal
[ -t 0 ] || return 1
# stop echoing typed characters
stty -echo
# clear screen and hide cursor
tput clear
tput civis
while true
do
draw_player
input || break
done
restore
}
# Ask the user what file it was
query() {
echo 'Was it ...'
echo
local ANSWERS=3
local CORRECT=$((RANDOM % ANSWERS))
local IDX=$((RANDOM % URLS_SIZE))
local I=0
for ((I=0; I < ANSWERS; ++I, ++IDX))
do
local OPTION
if ((I == CORRECT))
then
OPTION=${URL%#*}
else
while true
do
OPTION=${URLS[$((IDX % URLS_SIZE))]}
[[ $OPTION != "$URL" ]] && break
((++IDX))
done
OPTION=${OPTION%%#*}
fi
echo "$I) ${OPTION##*/}"
done
# read answer
echo
while true
do
read -r -n 1 -p 'Your choice? (q to give up and quit) '
echo
case "$REPLY" in
[0-9])
if ((REPLY == CORRECT))
then
echo 'Right!'
((++HITS))
else
echo "That was WRONG. It was $CORRECT."
fi
break
;;
q)
exit
;;
*)
echo 'Beg your pardon?'
;;
esac
done
sleep 1
}
# Read from STDIN into map
map_file() {
# create blank backstore
local BLANKS='˙'
while ((${#BLANKS} < WIDTH))
do
BLANKS="$BLANKS$BLANKS"
done
# read snippet into map
MAP=
local I=0
while read -r
do
((--OFFSET > 0)) && continue
# expand tabs
REPLY=${REPLY//$'\t'/${BLANKS:0:4}}
# fill to fit width
local FILL=$((WIDTH - ${#REPLY}))
((FILL > 0)) && REPLY=$REPLY${BLANKS:0:$FILL}
MAP="$MAP$REPLY"
((++I > HEIGHT)) && break
done
# fill the remaining lines of the map
for ((; I < HEIGHT; ++I))
do
MAP="$MAP${BLANKS:0:WIDTH}"
done
}
# Download and map file
download() {
echo 'Downloading challenge ...'
local TMP="${0##*/}_challenge_$$"
curl "${URL%#*}" > "$TMP" || exit 1
local OFFSET=${URL##*#L}
map_file < "$TMP"
rm -f "$TMP"
}
# Manage the quiz
lobby() {
declare -a URLS=(
# encode URLs to save the curious reader from spoiling the quiz
'\x68\x74\x74\x70\x73\x3a\x2f\x2f\x72\x61\x77\x2e\x67\x69\x74\x68\x75\x62\x75\x73\x65\x72\x63\x6f\x6e\x74\x65\x6e\x74\x2e\x63\x6f\x6d\x2f\x6e\x75\x6d\x70\x79\x2f\x6e\x75\x6d\x70\x79\x2f\x6d\x61\x73\x74\x65\x72\x2f\x6e\x75\x6d\x70\x79\x2f\x6d\x61\x74\x6c\x69\x62\x2e\x70\x79\x23\x4c\x31\x36'
'\x68\x74\x74\x70\x73\x3a\x2f\x2f\x72\x61\x77\x2e\x67\x69\x74\x68\x75\x62\x75\x73\x65\x72\x63\x6f\x6e\x74\x65\x6e\x74\x2e\x63\x6f\x6d\x2f\x70\x6f\x69\x6e\x74\x66\x72\x65\x65\x63\x6f\x2f\x73\x77\x69\x66\x74\x2d\x73\x6e\x61\x70\x73\x68\x6f\x74\x2d\x74\x65\x73\x74\x69\x6e\x67\x2f\x6d\x61\x73\x74\x65\x72\x2f\x53\x6f\x75\x72\x63\x65\x73\x2f\x53\x6e\x61\x70\x73\x68\x6f\x74\x54\x65\x73\x74\x69\x6e\x67\x2f\x53\x6e\x61\x70\x73\x68\x6f\x74\x54\x65\x73\x74\x43\x61\x73\x65\x2e\x73\x77\x69\x66\x74\x23\x4c\x31\x30'
'\x68\x74\x74\x70\x73\x3a\x2f\x2f\x72\x61\x77\x2e\x67\x69\x74\x68\x75\x62\x75\x73\x65\x72\x63\x6f\x6e\x74\x65\x6e\x74\x2e\x63\x6f\x6d\x2f\x4a\x61\x76\x61\x63\x6f\x72\x64\x2f\x4a\x61\x76\x61\x63\x6f\x72\x64\x2f\x6d\x61\x73\x74\x65\x72\x2f\x6a\x61\x76\x61\x63\x6f\x72\x64\x2d\x63\x6f\x72\x65\x2f\x73\x72\x63\x2f\x6d\x61\x69\x6e\x2f\x6a\x61\x76\x61\x2f\x6f\x72\x67\x2f\x6a\x61\x76\x61\x63\x6f\x72\x64\x2f\x63\x6f\x72\x65\x2f\x75\x74\x69\x6c\x2f\x46\x69\x6c\x65\x43\x6f\x6e\x74\x61\x69\x6e\x65\x72\x2e\x6a\x61\x76\x61\x23\x4c\x32\x38'
'\x68\x74\x74\x70\x73\x3a\x2f\x2f\x72\x61\x77\x2e\x67\x69\x74\x68\x75\x62\x75\x73\x65\x72\x63\x6f\x6e\x74\x65\x6e\x74\x2e\x63\x6f\x6d\x2f\x66\x6c\x75\x74\x74\x65\x72\x2f\x66\x6c\x75\x74\x74\x65\x72\x2f\x6d\x61\x73\x74\x65\x72\x2f\x70\x61\x63\x6b\x61\x67\x65\x73\x2f\x66\x6c\x75\x74\x74\x65\x72\x2f\x6c\x69\x62\x2f\x73\x72\x63\x2f\x77\x69\x64\x67\x65\x74\x73\x2f\x61\x73\x79\x6e\x63\x2e\x64\x61\x72\x74\x23\x4c\x33\x37\x31'
'\x68\x74\x74\x70\x73\x3a\x2f\x2f\x72\x61\x77\x2e\x67\x69\x74\x68\x75\x62\x75\x73\x65\x72\x63\x6f\x6e\x74\x65\x6e\x74\x2e\x63\x6f\x6d\x2f\x66\x61\x63\x65\x62\x6f\x6f\x6b\x2f\x72\x65\x61\x63\x74\x2f\x6d\x61\x73\x74\x65\x72\x2f\x70\x61\x63\x6b\x61\x67\x65\x73\x2f\x72\x65\x61\x63\x74\x2d\x69\x73\x2f\x73\x72\x63\x2f\x52\x65\x61\x63\x74\x49\x73\x2e\x6a\x73\x23\x4c\x36\x34'
)
local URLS_SIZE=${#URLS[@]}
# show instructions
tput clear
cat << EOL
Explore a random snippet from $URLS_SIZE different popular open source
projects like a dungeon in a Roguelike.
Can you guess what programming language or project a file is from?
Move the cursor with WASD or HJKL and press Enter as soon as you are sure.
You will see a couple of options to pick from.
The character '˙' is shown beyond the end of a line.
All your steps are counted.
EOL
read -r -n 1 -p 'Hit a key to start ...'
echo
# decode URLs
for ((I=0; I < URLS_SIZE; ++I))
do
URLS[$I]=$(echo -e "${URLS[$I]}")
done
# run the quiz
local WIDTH=${COLUMNS:-$(tput cols)}
local HEIGHT=${LINES:-$(tput lines)}
local MOVES=0
local HITS=0
local MAP
local URL
for URL in "${URLS[@]}"
do
download
inspect
query
done
echo
echo "You got $HITS of $URLS_SIZE correct and made $MOVES moves."
}
if [ "${BASH_SOURCE[0]}" == "$0" ]
then
lobby "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.