Skip to content

Instantly share code, notes, and snippets.

@nathan-fiscaletti
Created January 5, 2018 20:20
Show Gist options
  • Save nathan-fiscaletti/f4662029fa8571b295e8f47cd256f8cc to your computer and use it in GitHub Desktop.
Save nathan-fiscaletti/f4662029fa8571b295e8f47cd256f8cc to your computer and use it in GitHub Desktop.
A bash script for controlling the cursor (with a few tests)
#!/bin/bash
# A library of bash functions for moving the cursor around.
function main()
{
# Run the basic test
run_test
echo ""
# Run the progress bar test
#
# The only cursor control used
# for the progress bar is
# the c_clear_up function.
run_progress_bar_test
}
# Cursor movement functions
### Clears the current line
### Starting with the cursor
function c_clear()
{
printf '\033[K';
}
### Moves the cursor to the
### beginning of a line.
function c_start()
{
printf '\r';
}
### Moves the cursor to the
### beginning of a line and
### clears the whole line.
function c_start_clear()
{
c_start
c_clear
}
### Moves the cursor up
### $1 lines.
function c_up()
{
for i in `seq 1 $1`; do
printf '\033[1A'
done;
}
### Moves the cursor up $1
### lines and clears each
### line as it goes.
### Cursor will end up
### at the begging of the
### last line.
function c_clear_up()
{
c_start
c_clear
for i in `seq 1 $1`; do
printf '\033[1A'
c_clear
done;
}
### Moves the cursor back
### $1 columns.
function c_back()
{
printf "\033[$1D"
}
### Moves the cursor back
### $1 columns and then
### clears the line from
### the cursor to the end
### of the line.
function c_clear_back()
{
c_back $1
c_clear
}
### Moves the cursor to
### line $1 and column $2.
function c_move()
{
printf '\033[$1;$2H'
}
### Tests
function run_test()
{
echo "Basic Tests..."
echo ""
echo "This is the first line."
echo "This is the second line, I will be replaced in 2s..."
sleep 2
c_clear_up 1
echo "I've been replaced, but now i will count down.."
echo -n "3"
sleep 2
c_start_clear
echo -n "2"
sleep 2
c_start_clear
echo -n "1"
sleep 2
c_start_clear
echo -n "0"
sleep 2
c_start_clear
echo "The countdown is finished."
echo -n "This line will be written twice starting here: First line"
sleep 3
c_clear_back 10
echo "Second line"
echo -n "This line will be fully re-written"
sleep 3
c_start_clear
echo "See? It's been fully re-written."
}
function run_progress_bar_test()
{
echo "Progress bar test..."
PROGRESS_BAR_LENGTH=20
PROGRESS_BAR_VALUE=0
# We start with a new line in order
# to add a nicer spacing between our
# progress bar and everything else.
echo ""
# Then we add another new line since
# after each time we print our our progress
# bar we are writing a new line.
echo ""
# Define the last char (We'll make this dance.)
# Integers correspond to charactesr.
LAST_CHAR=0
while [ $PROGRESS_BAR_VALUE -lt `expr $PROGRESS_BAR_LENGTH + 1` ]; do
# Move up one line, and clear the full line.
c_clear_up 1
# Begin printing the progress bar
echo -n " Loading ["
# Print the current value of the progress bar
for i in `seq 0 $(expr $PROGRESS_BAR_VALUE - 1)`; do
echo -n ":"
done
# Print the last character, the dancing character, for the progress bar
if [[ "$PROGRESS_BAR_VALUE" == "$PROGRESS_BAR_LENGTH" ]]; then
echo -n ":"
else
# Print last char in bar based on the integer value
if [[ "$LAST_CHAR" == "0" ]]; then
echo -n "/"
elif [[ "$LAST_CHAR" == "1" ]]; then
echo -n "-"
elif [[ "$LAST_CHAR" == "2" ]]; then
echo -n "\\"
elif [[ "$LAST_CHAR" == "3" ]]; then
echo -n "|"
elif [[ "$LAST_CHAR" == "4" ]]; then
echo -n "/"
elif [[ "$LAST_CHAR" == "5" ]]; then
echo -n "-"
elif [[ "$LAST_CHAR" == "6" ]]; then
echo -n "\\"
elif [[ "$LAST_CHAR" == "7" ]]; then
echo -n "|"
fi
# Move to the next frame in the animation
LAST_CHAR=`expr $LAST_CHAR + 1`
# Check if we have reached the end of our animation
# and if so, go back to the beginning.
if [[ "$LAST_CHAR" == "8" ]]; then
LAST_CHAR=0
fi
fi
# Print the remaining space in the progress bar
for i in `seq 1 $(expr $PROGRESS_BAR_LENGTH - $PROGRESS_BAR_VALUE)`; do
echo -n ' '
done
# Finalize the progress bar
echo -n "] "
# Print the percentage
let PERCENT="(100 * $PROGRESS_BAR_VALUE) / $PROGRESS_BAR_LENGTH"
if [[ "$PERCENT" == "100" ]]; then
PERCENT="$PERCENT% DONE!"
else
PERCENT="$PERCENT%"
fi
echo $PERCENT
# Update the progress bar value
PROGRESS_BAR_VALUE=`expr $PROGRESS_BAR_VALUE + 1`
# Sleep
sleep 0.2
done;
# Print out another new line in order to
# add a buffer under the progress bar.
echo ""
}
# Initialize the Main function
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment