diff options
author | EuAndreh <eu@euandre.org> | 2021-01-24 17:45:34 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2021-01-24 17:49:12 -0300 |
commit | b6f8c46c0ee6e17d0bc9a63a6fe3b45e35107bac (patch) | |
tree | da3b03a57430ef3cd9b20c26a00b5a85b05713e7 | |
parent | remembering: Refator to use head/tail instead of read (diff) | |
download | remembering-b6f8c46c0ee6e17d0bc9a63a6fe3b45e35107bac.tar.gz remembering-b6f8c46c0ee6e17d0bc9a63a6fe3b45e35107bac.tar.xz |
remembering: Fix behaviour where STDIN is smaller than profile
Now the profile will be an ever growing list, remembering every possible
option ever presented to it.
The tests in tests/ranking.sh already expected this behaviour and were
broken, but now they work.
-rwxr-xr-x | remembering | 25 | ||||
-rwxr-xr-x | tests/ranking.sh | 7 |
2 files changed, 23 insertions, 9 deletions
diff --git a/remembering b/remembering index bb3dcd2..2f39385 100755 --- a/remembering +++ b/remembering @@ -73,12 +73,21 @@ cat | sort > "$SORTED_STDIN" MERGED='' MERGEDF="$(mktemp)" +FILTERED='' +FILTEREDF="$(mktemp)" + append() { + FILTERED="$FILTERED${FILTERED:+ +}$1" +} + +accumulate() { MERGED="$MERGED${MERGED:+ }$1" } end() { + echo "$FILTERED" > "$FILTEREDF" echo "$MERGED" > "$MERGEDF" exit } @@ -87,6 +96,8 @@ get_left() { LEFT="$(echo "$LEFT_L" | head -n1)" LEFT_L="$(echo "$LEFT_L" | tail -n+2)" if [ -z "$LEFT" ]; then + accumulate "$RIGHT_RANKED" + accumulate "$RIGHT_RANKED_L" end fi } @@ -95,8 +106,9 @@ get_right() { RIGHT_RANKED="$(echo "$RIGHT_RANKED_L" | head -n1)" RIGHT_RANKED_L="$(echo "$RIGHT_RANKED_L" | tail -n+2)" if [ -z "$RIGHT_RANKED" ]; then - MERGED="$MERGED${MERGED:+ -}$(echo "$LEFT_L" | awk '{print "0:" $0}')" + STDIN_LEFTOVER="$(echo "$LEFT_L" | awk '/./{print "0:" $0}')" + append "$STDIN_LEFTOVER" + accumulate "$STDIN_LEFTOVER" end fi } @@ -110,6 +122,7 @@ get_right() { RIGHT="$(echo "$RIGHT_RANKED" | cut -d: -f2-)" if [ "$LEFT" = "$RIGHT" ]; then append "$RIGHT_RANKED" + accumulate "$RIGHT_RANKED" get_right get_left continue @@ -117,16 +130,18 @@ get_right() { if [ "$(expr "$LEFT" \< "$RIGHT")" = 1 ]; then append "0:$LEFT" + accumulate "0:$LEFT" get_left continue else get_right + accumulate "$RIGHT_RANKED" continue fi done ) -CHOICE="$(sort -t: -k1nr,1 -k2,2 < "$MERGEDF" | \ +CHOICE="$(sort -t: -k1nr,1 -k2,2 < "$FILTEREDF" | \ cut -d: -f2- | \ sh -c "$COMMAND")" @@ -142,7 +157,7 @@ if [ -n "$CHOICE" ]; then echo "$LINE_RANKED" >> "$NEW_PROFILE" fi done < "$MERGEDF" + mv "$NEW_PROFILE" "$PROFILE" + echo "$CHOICE" fi -mv "$NEW_PROFILE" "$PROFILE" -echo "$CHOICE" diff --git a/tests/ranking.sh b/tests/ranking.sh index d7df451..370c61b 100755 --- a/tests/ranking.sh +++ b/tests/ranking.sh @@ -56,7 +56,7 @@ pick_x() { echo "${2:-$INPUT}" | \ sh remembering \ -p "$PROFILE" \ - -c "tee -a /dev/stderr | grep '$PICK'" \ + -c "tee -a /dev/stderr | grep \"$PICK\"" \ 1>"$OUT" 2>"$ERR" STATUS=$? assert_status 0 @@ -66,8 +66,8 @@ pick_x() { assert_first() { FIRST="$(head -n1 "$ERR")" if [ "$FIRST" != "$1" ]; then - echo 'Previous choice did not appear at the beginning of the list' - printf '\nexpected: %s\ngot: %s\n' "$1" "$FIRST" + printf '\nERR: Previous choice did not appear at the beginning of the list:\n\nexpected: %s\ngot: %s\n' \ + "$1" "$FIRST" >&2 exit 1 fi } @@ -216,7 +216,6 @@ test_stdin_is_smaller_than_profile() { echo "$BASE_PROFILE" > "$XDG_DATA_HOME/$PROFILE" INPUT='a' pick_x a "$INPUT" - # echo "err is: $(cat "$ERR")" assert_profile "$PROFILE" "$BASE_PROFILE_A_PICKED" test_ok } |