Skip to content

Instantly share code, notes, and snippets.

@dekarrin
Last active August 29, 2015 13:56
Show Gist options
  • Save dekarrin/8884468 to your computer and use it in GitHub Desktop.
Save dekarrin/8884468 to your computer and use it in GitHub Desktop.
Overwrites the last line of the terminal
# Outputs without a newline and overwrites anything that was already on the last line of
# the terminal
overwrite ()
{
output="$1"
if [ -z "$output" ]
then
read output
fi
printf "\r"
if [ -n "$LAST_OVERWRITE_LENGTH" ]
then
if [ "$LAST_OVERWRITE_LENGTH" -gt 0 ]
then
clear_space_count=0
while [ $clear_space_count -lt $LAST_OVERWRITE_LENGTH ]
do
printf " "
let clear_space_count++
done
printf "\r"
fi
fi
# only save length of last line
while read -r line
do
LAST_OVERWRITE_LENGTH=${#line}
printf "$line"
done <<< "$output"
}
# Outputs with a newline and overwrites anything that was already on the last line of
# the terminal
overwriteln ()
{
overwrite "$@"
printf "\n"
LAST_OVERWRITE_LENGTH=0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment