Cut some text into smaller snippets without cutting words in half
#!/usr/bin/env bash
# Cut some text into smaller snippets without cutting words in half
#
# @param ... - text to cut (optional)
snip()
{
local TEXT=${*:-$(cat)} LIMIT=${LIMIT:-140} GLUE=${GLUE:-'..'}
(( LIMIT -= ${#GLUE} ))
local N
for (( N=0; ${#TEXT} > LIMIT; ++N ))
do
local S=${TEXT:0:$LIMIT}
local U=${S%[ $'\n']*}
(( N > 0 )) && echo -n "$GLUE"
if [ "$U" ]
then
echo -n "$U"
TEXT=${TEXT:${#U}}
else
echo -n "$S"
TEXT=${TEXT:$LIMIT}
fi
echo "$GLUE"
(( N == 0 )) && (( LIMIT -= ${#GLUE} ))
done
(( N > 0 )) && echo -n "$GLUE"
echo "$TEXT"
}
if [ "${BASH_SOURCE[0]}" == "$0" ]
then
snip "$@"
fi