Added multi-word action support to process-input

master
Mike Gerwitz 2011-08-25 19:45:18 -04:00
parent ce8cf68ad8
commit 928a8c5463
2 changed files with 21 additions and 24 deletions

View File

@ -30,11 +30,11 @@ process-input "foo bar" 2>/dev/null <<< "" && {
scene_opendoor="opendoor" scene_opendoor="opendoor"
doorspeak="Yo. I'm a door." doorspeak="Yo. I'm a door."
multiactions=" multiactions="MSG foo
MSG foo
WAIT WAIT
GO somewhereelse GO somewhereelse"
"
kickinmsg="MSG Doesn't look like it's going to budge"
testscene=" testscene="
TYPE input TYPE input
@ -47,6 +47,8 @@ OBJECT door
UNKNOWNCMD foo UNKNOWNCMD foo
ACTION multi ACTION multi
$multiactions $multiactions
ACTION kick in
$kickinmsg
OBJECT foo OBJECT foo
ACTION bar ACTION bar
@ -64,11 +66,6 @@ tryinput()
process-input "$1" <<< "$testscene" process-input "$1" <<< "$testscene"
} }
# ensure it doesn't accept too many words
process-input "foo bar baz" 2>/dev/null <<< "data" && {
simplefail "Shouldn't allow more words than can be interpreted"
}
# should allow actions on known objects # should allow actions on known objects
assert-equal "$( tryinput "open door" )" "GO $scene_opendoor" || { assert-equal "$( tryinput "open door" )" "GO $scene_opendoor" || {
fail "Should be able to perform known actions on known objects" fail "Should be able to perform known actions on known objects"
@ -85,7 +82,7 @@ assert-equal "$( tryinput "fondle door" )" "" || {
} }
# all commands associated with an action should be returned (left trimmed) # all commands associated with an action should be returned (left trimmed)
expected=$( sed 's/ \+//' <<< "$multiactions" ) expected=$( sed 's/^ \+//' <<< "$multiactions" )
assert-equal "$( tryinput "multi door" )" "$expected" || { assert-equal "$( tryinput "multi door" )" "$expected" || {
fail "All commands for a multi-line action should be returned" fail "All commands for a multi-line action should be returned"
} }
@ -113,3 +110,8 @@ assert-equal "$( tryinput "tickle" )" "MSG Please elaborate." || {
fail "Naming an unknown object should prompt user to elaborate" fail "Naming an unknown object should prompt user to elaborate"
} }
# multi-word actions should be supported
assert-equal "$( tryinput "kick in door" )" "$kickinmsg" || {
fail "Multi-word actions should be supported"
}

View File

@ -22,10 +22,10 @@
words="$1" words="$1"
max_words=2 max_words=2
data=$( cat - ) scene=$( cat - )
# scene data should be provided via stdin # scene data should be provided via stdin
[ "$data" ] || { [ "$scene" ] || {
echo "No scene data provided" >&2 echo "No scene data provided" >&2
exit 1 exit 1
} }
@ -33,19 +33,15 @@ data=$( cat - )
# our processing method depends heavily on the word count # our processing method depends heavily on the word count
count=$( grep -o '\w\+' <<< "$words" | wc -l ) count=$( grep -o '\w\+' <<< "$words" | wc -l )
# no use in allowing more words than we're able to interpret # the action is every word except the last, and the object being acted upon is
if [ $count -gt $max_words ]; then # the final word
echo "Too many words" >&2 action=$( sed 's/^\(.\+\) .\+$/\1/' <<< "$words" )
exit 2 object=$( grep -o '[^ ]\+$' <<< "$words" )
fi
action=$( cut -d' ' -f1 <<< "$words" )
object=$( cut -s -d' ' -f2 <<< "$words" )
# if only one word was given, then we need to ask what to do # if only one word was given, then we need to ask what to do
if [ "$count" -eq 1 ]; then if [ "$count" -eq 1 ]; then
# is it a known object? # is it a known object?
grep -q "$action" <<< "$scene" && { grep -q "^OBJECT $action" <<< "$scene" && {
echo "MSG What about $action?" echo "MSG What about $action?"
exit 2 exit 2
} }
@ -55,11 +51,10 @@ if [ "$count" -eq 1 ]; then
exit 2 exit 2
fi fi
# check for am action on a known object and output the associated commands, left # check for am action on a known object and output the associated commands, left
# trimmed # trimmed
awk " awk "
/OBJECT $object/ { /^OBJECT $object$/ {
found = 1 found = 1
getline getline
} }
@ -77,6 +72,6 @@ awk "
} }
} }
} }
" <<< "$data" \ " <<< "$scene" \
| sed 's/^[ \t]\+//' | sed 's/^[ \t]\+//'