From 928a8c5463704be9a1a0deba1b3e0e35546c9c96 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Thu, 25 Aug 2011 19:45:18 -0400 Subject: [PATCH] Added multi-word action support to process-input --- test/test-process-input | 22 ++++++++++++---------- util/process-input | 23 +++++++++-------------- 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/test/test-process-input b/test/test-process-input index 85ec811..936f1fb 100755 --- a/test/test-process-input +++ b/test/test-process-input @@ -30,11 +30,11 @@ process-input "foo bar" 2>/dev/null <<< "" && { scene_opendoor="opendoor" doorspeak="Yo. I'm a door." -multiactions=" - MSG foo +multiactions="MSG foo WAIT - GO somewhereelse -" + GO somewhereelse" + +kickinmsg="MSG Doesn't look like it's going to budge" testscene=" TYPE input @@ -47,6 +47,8 @@ OBJECT door UNKNOWNCMD foo ACTION multi $multiactions + ACTION kick in + $kickinmsg OBJECT foo ACTION bar @@ -64,11 +66,6 @@ tryinput() 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 assert-equal "$( tryinput "open door" )" "GO $scene_opendoor" || { 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) -expected=$( sed 's/ \+//' <<< "$multiactions" ) +expected=$( sed 's/^ \+//' <<< "$multiactions" ) assert-equal "$( tryinput "multi door" )" "$expected" || { 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" } +# multi-word actions should be supported +assert-equal "$( tryinput "kick in door" )" "$kickinmsg" || { + fail "Multi-word actions should be supported" +} + diff --git a/util/process-input b/util/process-input index ef28e78..df69efd 100755 --- a/util/process-input +++ b/util/process-input @@ -22,10 +22,10 @@ words="$1" max_words=2 -data=$( cat - ) +scene=$( cat - ) # scene data should be provided via stdin -[ "$data" ] || { +[ "$scene" ] || { echo "No scene data provided" >&2 exit 1 } @@ -33,19 +33,15 @@ data=$( cat - ) # our processing method depends heavily on the word count count=$( grep -o '\w\+' <<< "$words" | wc -l ) -# no use in allowing more words than we're able to interpret -if [ $count -gt $max_words ]; then - echo "Too many words" >&2 - exit 2 -fi - -action=$( cut -d' ' -f1 <<< "$words" ) -object=$( cut -s -d' ' -f2 <<< "$words" ) +# the action is every word except the last, and the object being acted upon is +# the final word +action=$( sed 's/^\(.\+\) .\+$/\1/' <<< "$words" ) +object=$( grep -o '[^ ]\+$' <<< "$words" ) # if only one word was given, then we need to ask what to do if [ "$count" -eq 1 ]; then # is it a known object? - grep -q "$action" <<< "$scene" && { + grep -q "^OBJECT $action" <<< "$scene" && { echo "MSG What about $action?" exit 2 } @@ -55,11 +51,10 @@ if [ "$count" -eq 1 ]; then exit 2 fi - # check for am action on a known object and output the associated commands, left # trimmed awk " - /OBJECT $object/ { + /^OBJECT $object$/ { found = 1 getline } @@ -77,6 +72,6 @@ awk " } } } - " <<< "$data" \ + " <<< "$scene" \ | sed 's/^[ \t]\+//'