elenapan/Scripts/scrolling_text.sh
elena 912d000d9a scrolling text now handles short strings by justifying them (center)
Former-commit-id: bf71bc0ad2
Former-commit-id: 0e344565b6665c0d2fedd509c3ae6b28ffd133a5
Former-commit-id: 0535973731572793ed726c9df6dd2447784956bc
2017-09-28 20:32:01 +03:00

46 lines
No EOL
1.3 KiB
Bash
Executable file

# Scrolling text
LIMIT=12
SEPERATOR=" ~~~ "
SEPERATOR_LENGTH="$(echo ${#SEPERATOR})"
i=1
while :
do
# Get input and create the full string that will be used
INPUT="$@"
INPUT_LENGTH="$(echo ${#INPUT})"
LOOPTAIL="$(echo $INPUT | cut -c -$LIMIT)"
FULLSTRING="$(echo "$INPUT$SEPERATOR$LOOPTAIL")"
if [ $INPUT_LENGTH -le $LIMIT ]; then
# Song has a smaller title than the limit, so no need to scroll.
echo -n "  "
#echo -e -n "$INPUT"
#Justify - left
#SPACES=$(($LIMIT-$INPUT_LENGTH+1))
#echo -n "$INPUT" | awk -v spaces=$SPACES '{printf "%s%*.s\n", $0, spaces, " "}'
#Justify - center
SPACES_AFTER=$(($LIMIT-$(((${#INPUT}+$LIMIT)/2))))
printf "%*s\n" $(((${#INPUT}+$LIMIT)/2 + 1)) "$INPUT" | awk -v spaces=$SPACES_AFTER '{printf "%s%*.s\n", $0, spaces, " "}'
else
LOOPTAIL="$(echo $INPUT | cut -c -$LIMIT)"
FULLSTRING="$(echo "$INPUT$SEPERATOR$LOOPTAIL")"
echo -n "  "
# Show only LIMIT characters, from i to i+LIMIT. Output scrolls due to incrementing i
echo -n $FULLSTRING | cut -c $i-$(($LIMIT+$i))
#echo -n $FULLSTRING | awk -v charlimit="$LIMIT" -v start="$i" '{print substr($0,start,charlimit)}'
# If we have shown the full string, start over
if [ $i -eq $(($INPUT_LENGTH+$SEPERATOR_LENGTH)) ]; then
i=0
fi
i=$(($i+1))
fi
sleep 1
done