Skip to content

Instantly share code, notes, and snippets.

@brunomiguel
Last active November 28, 2022 01:40
Show Gist options
  • Save brunomiguel/a65cfdf34ebff1f43f80ed8d909540fe to your computer and use it in GitHub Desktop.
Save brunomiguel/a65cfdf34ebff1f43f80ed8d909540fe to your computer and use it in GitHub Desktop.
Toot with images from ZSH using the toot command line client for Mastodon (I'm using it as a function on my .zshrc)
# Toot with images from the command line
# License: GPLv3 (https://www.gnu.org/licenses/gpl-3.0.txt)
function tooti {
# Allow using trap inside a function
set -o localoptions -o localtraps
# Check if toot is in $PATH
if ! type toot > /dev/null; then
echo -e "\n\e[0;93mWarning: toot not found in \$PATH\n"
echo -e "Please install it using your package manager or from https://github.com/ihabunek/toot\n"
fi
# Set commands and message to display when pressing Ctrl+C
trap 'echo -e "\n\n\e[0;93mInterrupted..."; return 1' INT
local img=""
local desc=""
local msg=""
# Ask for user input and set it as variables
vared -p 'Write the image path: ' -c img
vared -p 'Write the image description: ' -c desc
vared -p 'Write your message: ' -c msg
# Check if variables have values and toot if they all have
if [ "$img" != "" ] || [ "$desc" != "" ] || [ "$msg" != "" ]; then
echo
toot post -m "$img" -d "${desc}" "${msg}"
echo -e "\ndone"
# Display an error message if any of the variables are empy
else
echo
tput bold; echo -e '\e[0;93mWarning'; tput sgr0
echo -e "You forgot the media, media description or the message"
fi
}
@brunomiguel
Copy link
Author

This enables you to toot images and text from the command line

@brunomiguel
Copy link
Author

brunomiguel commented Nov 28, 2022

Alternative version, with positional arguments and no interactive input

# Toot with images from the command line
# License: GPLv3 (https://www.gnu.org/licenses/gpl-3.0.txt)
function tooti {

		# Check if toot is in $PATH
		if ! type toot > /dev/null; then
			echo -e "\n\e[0;93mWarning: toot not found in \$PATH\n"
			echo -e "Please install it using your package manager or from https://github.com/ihabunek/toot\n"
		fi

		if [ $# -lt 3 ]; then
			tput bold; echo -e "Usage: $funcstack[1] <image-path> <image-description> <message>"; tput sgr0
			return
		fi

		toot post -m "${1}" -d "${2}" "${3}"
		echo -e "\ndone"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment