diff --git a/discord-if-distinct.sh b/discord-if-distinct.sh index 122d294..1c64bab 100755 --- a/discord-if-distinct.sh +++ b/discord-if-distinct.sh @@ -6,11 +6,34 @@ source "${LOCATION}/config.sh" HOOK_ARG="$1" BOBBY="$2" -MESSAGE=`echo "${@:3}" | jq -aRs .` +# sed removes spaces before each line +MESSAGE=`echo "${@:3}" | sed 's/^ *//'` + +# sed removes blank lines from the end of the message +FULL_MESSAGE=`echo "${MESSAGE}" | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}'` + +# if message is too long, split it, and after curl, recursively call with remainder +# 2k max, but we must account for escaping text in json, so limit to 90% capacity +EXTRA="${MESSAGE:1800}" + +if [ -n "${EXTRA}" ]; then + MESSAGE="${MESSAGE:0:1800}" +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'` HOOK="general" HOOK_URL=$DISCORD_GENERAL_HOOK case $HOOK_ARG in + block) + # force block format without color + if [[ "$MESSAGE" != *"\\u001b"* ]]; then + MESSAGE="\"\`\`\`${MESSAGE:1:-1}\n\`\`\`\"" + # just here to resolve formatting issue in vim "`" + fi + ;; restake) HOOK="restake" HOOK_URL=$DISCORD_RESTAKE_HOOK @@ -25,13 +48,24 @@ case $HOOK_ARG in ;; esac +# if color detected, update message from "" to "```ansi ```", so it gets displayed correctly +if [[ "$MESSAGE" == *"\\u001b"* ]]; then + MESSAGE="\"\`\`\`ansi\n${MESSAGE:1:-1}\n\`\`\`\"" + # just here to resolve formatting issue in vim "`" +fi + OLD_MSG="" if [ -f "${LOCATION}/distinct/${BOBBY}.msg" ]; then OLD_MSG=`cat "${LOCATION}/distinct/${BOBBY}.msg"` fi -if [ "${MESSAGE}" != "${OLD_MSG}" ]; then - echo "${MESSAGE}" > "${LOCATION}/distinct/${BOBBY}.msg" +if [ "${FULL_MESSAGE}" != "${OLD_MSG}" ]; then + echo "${FULL_MESSAGE}" > "${LOCATION}/distinct/${BOBBY}.msg" curl -s -H "Content-Type: application/json" -X POST -d "{\"username\": \"${DISCORD_SERVER_NAME}\", \"content\": ${MESSAGE}}" $HOOK_URL | jq -r '.id' + + if [ -n "${EXTRA}" ]; then + "${LOCATION}/discord.sh" "${HOOK}" "${EXTRA}" + fi fi + diff --git a/discord.sh b/discord.sh index d3eef82..2ec1542 100755 --- a/discord.sh +++ b/discord.sh @@ -5,7 +5,8 @@ LOCATION=`dirname "$0"` source "${LOCATION}/config.sh" HOOK_ARG="$1" -MESSAGE=`echo "${@:2}"` +# sed removes spaces before each line +MESSAGE=`echo "${@:2}" | sed 's/^ *//'` # if message is too long, split it, and after curl, recursively call with remainder # 2k max, but we must account for escaping text in json, so limit to 90% capacity @@ -16,11 +17,19 @@ if [ -n "${EXTRA}" ]; then 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'` HOOK="general" HOOK_URL=$DISCORD_GENERAL_HOOK case $HOOK_ARG in + block) + # force block format without color + if [[ "$MESSAGE" != *"\\u001b"* ]]; then + MESSAGE="\"\`\`\`${MESSAGE:1:-1}\n\`\`\`\"" + # just here to resolve formatting issue in vim "`" + fi + ;; restake) HOOK="restake" HOOK_URL=$DISCORD_RESTAKE_HOOK diff --git a/one-time-stuff/basic-settings.sh b/one-time-stuff/basic-settings.sh index 14c33bc..9b4ca03 100755 --- a/one-time-stuff/basic-settings.sh +++ b/one-time-stuff/basic-settings.sh @@ -104,7 +104,7 @@ shopt -s histappend # append to the history file, don't overwrite it HISTSIZE=10000 # cache the last 10,000 commands HISTFILESIZE=100000 # remember the last 100,000 commands HISTCONTROL="ignoredups" # ignore duplicates -HISTIGNORE="l:la:ll:ls *:clear:history:pwd" # ignore select simple commands +HISTIGNORE="l:la:ll:ls:cd:clear:history:pwd" # ignore select simple commands HISTFILE="${HOME}/.bash_history/$(tty | sed 's/\//-/g;s/^-//g')" # bash_history as folder split by tty # if we have already sourced this file, don't do it again