Skip to content

Instantly share code, notes, and snippets.

@sumpygump
Last active April 3, 2025 10:36

Revisions

  1. sumpygump revised this gist Nov 20, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions lsaber
    Original file line number Diff line number Diff line change
    @@ -172,4 +172,5 @@ fi

    # Move cursor down to bottom of screen
    tput cup $HEIGHT 0
    tput op
    exit 0
  2. sumpygump revised this gist Nov 20, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion lsaber
    Original file line number Diff line number Diff line change
    @@ -29,7 +29,7 @@ displayUsage() {
    echo " -l Length of the lightsaber"
    echo " -c Color of blade (valid colors: ${COLORNAMES[@]})"
    echo
    echo "Example: lsaber -l 44 'Everything's gonna be okay'"
    echo "Example: lsaber -l 44 -c green 'May the force be with you'"
    }

    # Handle --help arg
  3. sumpygump created this gist Nov 20, 2015.
    175 changes: 175 additions & 0 deletions lsaber
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,175 @@
    #!/bin/bash

    ######################################################################
    # The ASCII lightsaber #
    # #
    # By: Jansen Price <[email protected]> #
    # November 19, 2015 #
    # ▁▁▁▁▁▁▁▁▁▁▁▁▁ ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ #
    # |▍░▐░░▣░▒░▒░▒▕| ▌ #
    # ▔▔▔▔▔▔▔▔▔▝▔▔▔ ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔ #
    ######################################################################

    lsaberVersion="lsaber-1.0"

    # Determine width and height of terminal
    WIDTH=`tput cols`
    HEIGHT=`tput lines`

    HILT=(" ▁▁▁▁▁▁▁▁▁▁▁▁▁ " "▐▍░▐░░▣░▒░▒░▒▕|" " ▔▔▔▔▔▔▔▔▔▝▔▔▔ ")
    BLADE=("" "" "")
    TIP=(" " "" " ")

    COLORNAMES=("red" "green" "yellow" "blue" "purple" "cyan")
    COLORCODES=("\E[31m" "\E[32m" "\E[33m" "\E[34m" "\E[35m" "\E[36m")

    displayUsage() {
    echo "$lsaberVersion - An animated ASCII lightsaber"
    echo "Usage: lsaber [-l <length>] [message]"
    echo " -l Length of the lightsaber"
    echo " -c Color of blade (valid colors: ${COLORNAMES[@]})"
    echo
    echo "Example: lsaber -l 44 'Everything's gonna be okay'"
    }

    # Handle --help arg
    if [ -n "$1" -a "$1" == "--help" ]; then
    displayUsage
    exit 1
    fi

    # Default blade length and color
    bladeLength=55
    bladeColor=red

    # Process user options
    while getopts "l:c:" opt; do
    case $opt in
    l)
    # Set the bladelength (option -l <length>)
    bladeLength=$OPTARG
    ;;
    c)
    # Set the color (option -c <color>)
    bladeColor=$OPTARG
    ;;
    esac
    done

    # Shift off the used args
    shift $((OPTIND-1))

    # Get message
    if [ -n "$1" ]; then
    message="$@"
    fi

    # Define hilt length
    hiltLength=${#HILT}

    # Get blade length
    maxSaberLength=$((($WIDTH-$hiltLength)/2))
    isNumber='^[0-9]+$'
    if ! [[ $bladeLength =~ $isNumber ]] ; then
    echo "Error: Blade length must be a number" >&2
    exit 1
    fi
    if [ "$bladeLength" -gt "$maxSaberLength" ]; then
    bladeLength=$maxSaberLength
    fi

    # Define saber length
    saberLength=$((${#HILT}+$bladeLength))

    # Get blade color
    if [[ ${COLORNAMES[@]} =~ $bladeColor ]]; then
    for c in $(seq 0 $((${#COLORNAMES[@]} - 1))); do
    if [ "${COLORNAMES[c]}" == "$bladeColor" ]; then
    bladeColorCode=${COLORCODES[c]}
    fi
    done
    else
    echo "Error: Invalid color name for blade" >&2; echo
    displayUsage
    exit 1
    fi

    drawHilt() {
    local row=$1
    local col=$2

    # Preserve whitespace in strings
    local IFS='%'

    tput cup $(($row-1)) $col
    echo ${HILT[0]}

    tput cup $row $col
    echo ${HILT[1]}

    tput cup $(($row+1)) $col
    echo ${HILT[2]}

    # Reset whitespace preserving directive
    unset IFS
    }

    animateBlade() {
    local row=$1
    local col=$2
    local hiltLength=$3
    local bladeLength=$4

    bladeStart=$(($col+$hiltLength))

    # Hide the cursor
    tput civis

    for (( i=0; i<$bladeLength; i++ ))
    do
    tput cup $(($row-1)) $(($bladeStart+$i)); echo ${BLADE[0]}
    tput cup $row $(($bladeStart+$i)); echo ${BLADE[1]}
    tput cup $(($row+1)) $(($bladeStart+$i)); echo ${BLADE[2]}

    tput cup $(($row-1)) $(($bladeStart+$i+1)); echo ${TIP[0]}
    tput cup $row $(($bladeStart+$i+1)); echo ${TIP[1]}
    tput cup $(($row+1)) $(($bladeStart+$i+1)); echo ${TIP[2]}

    sleep 0.001
    done

    # Display the cursor
    tput cnorm
    }

    showMessage() {
    local row=$1
    local message=$2

    messageLength=${#message}
    center=$((($WIDTH-$messageLength)/2))

    tput cup $(($row+3)) $center
    echo "$message"
    }

    # ------------------------------------------------------
    # Main program execution

    # Clear the screen
    tput clear

    # Calculate center of screen
    centerRow=$(($HEIGHT/2))
    centerCol=$((($WIDTH-$saberLength)/2))

    drawHilt $centerRow $centerCol
    printf "$bladeColorCode"
    animateBlade $centerRow $centerCol $hiltLength $bladeLength
    if [ -n "$message" ]; then
    showMessage $centerRow "$message"
    fi

    # Move cursor down to bottom of screen
    tput cup $HEIGHT 0
    exit 0