From 98e6447899b9cad4bcf284d570aefc1be26cd0ff Mon Sep 17 00:00:00 2001 From: Andrew Stowell Date: Wed, 13 Sep 2023 13:00:59 +0000 Subject: [PATCH] discord*.sh will now break chunks at nearest newline, when input is large. --- discord-if-distinct.sh | 9 +++++++++ discord.sh | 10 ++++++++++ 2 files changed, 19 insertions(+) diff --git a/discord-if-distinct.sh b/discord-if-distinct.sh index 1c64bab..bdba6c1 100755 --- a/discord-if-distinct.sh +++ b/discord-if-distinct.sh @@ -18,6 +18,15 @@ EXTRA="${MESSAGE:1800}" if [ -n "${EXTRA}" ]; then MESSAGE="${MESSAGE:0:1800}" + + # as an added elegance, when spliting into pieces, find the last \n newline, + # and break there instead, but only if the new smaller chunk is of sufficient size + chunk1=`echo "$MESSAGE" | head -n -1` + chunks=`echo "$MESSAGE" | tail -n 1` + if (( "${#chunk1}" >= 1000 )); then + MESSAGE="$chunk1" + EXTRA="${chunks}${EXTRA}" + fi fi # sed: rewrite json string, because "\u001b[m" needs to "\u001b[0m" instead diff --git a/discord.sh b/discord.sh index 2ec1542..8a236d3 100755 --- a/discord.sh +++ b/discord.sh @@ -14,8 +14,18 @@ EXTRA="${MESSAGE:1800}" if [ -n "${EXTRA}" ]; then MESSAGE="${MESSAGE:0:1800}" + + # as an added elegance, when spliting into pieces, find the last \n newline, + # and break there instead, but only if the new smaller chunk is of sufficient size + chunk1=`echo "$MESSAGE" | head -n -1` + chunks=`echo "$MESSAGE" | tail -n 1` + if (( "${#chunk1}" >= 1000 )); then + MESSAGE="$chunk1" + EXTRA="${chunks}${EXTRA}" + fi fi + # sed: rewrite json string, because "\u001b[m" needs to "\u001b[0m" instead # linux is fine with the missing zero, but discord needs it MESSAGE=`echo "${MESSAGE}" | jq -aRs . | sed 's/\\\\u001b\[m/\\\\u001b[0m/g'`