Skip to content

Instantly share code, notes, and snippets.

@SamEureka
Created September 27, 2023 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SamEureka/3e61942d37256550b40d0ffe75bc22c4 to your computer and use it in GitHub Desktop.
Save SamEureka/3e61942d37256550b40d0ffe75bc22c4 to your computer and use it in GitHub Desktop.
Bash Braille Spinner
#!/usr/bin/env bash
# Define an array of Braille patterns for a spinner
six_dot_cell_pattern=("⠙" "⠹" "⠸" "⠼" "⠴" "⠦" "⠧" "⠇")
eight_dot_cell_pattern=("⣾" "⢿" "⡿" "⣷" "⣯" "⢟" "⡻" "⣽")
# Set the pattern
braille_spinner=("${eight_dot_cell_pattern[@]}")
# Set the duration for each spinner frame (in seconds)
frame_duration=0.1
# Function to start the spinner in the background
start_spinner() {
(
spinner_index=0
while :; do
printf "\r%s " "${braille_spinner[spinner_index]}"
spinner_index=$(( (spinner_index + 1) % ${#braille_spinner[@]} ))
sleep "$frame_duration"
done
) &
spinner_pid=$!
disown
}
# Function to stop the spinner with U+2800
stop_spinner() {
kill -9 "$spinner_pid" # Stop the spinner loop
printf "\r%s " "⠀" # Print U+2800 (Braille Pattern Blank) and move to the next line
display_message "$1"
}
# Function to simulate a long-running task
long_running_function() {
echo "Starting long-running function..."
sleep 5 # Simulate work (replace with your actual work)
}
display_message() {
if [ -n "$1" ]; then
echo -e "\n$1"
else
echo -e "\nDone, Bitches!"
fi
}
# Call the start_spinner function before a long-running function
start_spinner
# Run your long-running function(s) here
long_running_function
# Call stop_spinner to stop the spinner when the long-running function is done
# You can pass a string with the function call to add a custom message
stop_spinner "Your functions are complete"
# Continue with other echo statements or tasks
echo "Other tasks..."
#!/usr/bin/env bash
six_dot_cell_pattern=("⠙" "⠹" "⠸" "⠼" "⠴" "⠦" "⠧" "⠇")
eight_dot_cell_pattern=("⣾" "⢿" "⡿" "⣷" "⣯" "⢟" "⡻" "⣽")
braille_spinner=("${eight_dot_cell_pattern[@]}")
frame_duration=0.1
start_spinner() { ( spinner_index=0; while :; do printf "\r%s " "${braille_spinner[spinner_index]}"; spinner_index=$(( (spinner_index + 1) % ${#braille_spinner[@]} )); sleep "$frame_duration"; done ) & spinner_pid=$!; disown; }
stop_spinner() { kill -9 "$spinner_pid"; printf "\r%s " "⠀"; display_message "$1"; }
long_running_function() { echo "Starting long-running function..."; sleep 5; }
display_message() { if [ -n "$1" ]; then echo -e "\n$1"; else echo -e "\nDone, Bitches!"; fi; }
start_spinner; long_running_function; stop_spinner "Your functions are complete"; echo "Other tasks..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment