langs a-z

This commit is contained in:
Ingy döt Net 2013-04-10 22:43:41 -07:00
parent db842d013d
commit d066446780
11389 changed files with 98361 additions and 1020 deletions

View file

@ -0,0 +1,19 @@
# choose 'choice 1' 'choice 2' ...
# Prints menu to standard error. Prompts with PS3.
# Reads REPLY from standard input. Sets CHOICE.
choose() {
CHOICE= # Default CHOICE is empty string.
[[ $# -gt 0 ]] || return # Return if "$@" is empty.
select CHOICE; do # Select from "$@".
if [[ -n $CHOICE ]]; then
break
else
echo Invalid choice.
fi
done
}
PS3='Which is from the three pigs: '
choose 'fee fie' 'huff and puff' 'mirror mirror' 'tick tock'
[[ -n $CHOICE ]] && echo You chose: $CHOICE
[[ -z $CHOICE ]] && echo No input.

View file

@ -0,0 +1,54 @@
# choose 'choice 1' 'choice 2' ...
# Prints menu to standard error. Prompts with $prompt.
# Returns choice. If no input, returns empty list.
fn choose args {
# If args are empty, return empty list.
~ $#args 0 && return
# Echo to standard error.
let (reply =; choice =; fn-menu =; fn-ch =) >[1=2] {
fn-menu = {
# Show menu.
let (i = 1) for (c = $args) {
echo $i') '$c
i = `{expr $i + 1}
}
}
fn-ch = {
# Set choice = $args($reply), but ignore error
# if $reply is not a valid index.
choice = <={catch @ e {result} {
result $args($reply)
}}
}
menu
forever {
# Show prompt, read reply.
echo -n $prompt
reply = <={%read}
# If no input, return empty list.
~ $#reply 0 && return
# Parse reply and return choice.
reply = <={%split \ \t\n $reply}
if {~ $#reply 0} {
# Empty reply: show menu again.
menu
} {~ $#reply 1 && ch; ~ $#choice 1} {
return $choice
} {
echo Invalid choice.
}
}
}
}
let (choice = <={
local (prompt = 'Which is from the three pigs: ')
choose 'fee fie' 'huff and puff' 'mirror mirror' 'tick tock'
}) {
~ $#choice 1 && echo You chose: $choice
~ $#choice 0 && echo No input.
}