Skip to content

Instantly share code, notes, and snippets.

@d11wtq
Last active August 29, 2015 13:56
Show Gist options
  • Save d11wtq/9248289 to your computer and use it in GitHub Desktop.
Save d11wtq/9248289 to your computer and use it in GitHub Desktop.

Revisions

  1. d11wtq revised this gist Feb 27, 2014. 1 changed file with 46 additions and 15 deletions.
    61 changes: 46 additions & 15 deletions spinner.sh
    Original file line number Diff line number Diff line change
    @@ -3,34 +3,68 @@
    TTY=/dev/tty

    spin() {
    width=5
    width=20
    delta=1
    "$@" >/dev/null &
    child=$!
    chars=("\\" "|" "/" "-")
    #fwd_chars=("\\" "|" "/" "-")
    #rev_chars=("/" "|" "\\" "-")
    fwd_chars=">"
    rev_chars="<"
    space=0
    while kill -0 $child 2>/dev/null

    "$@" >/dev/null &
    pid=$!

    tput civis
    while kill -0 $pid 2>/dev/null
    do
    for ((i=0; i<space; i++))
    if ((delta>0))
    then
    chars=(${fwd_chars[@]})
    else
    chars=(${rev_chars[@]})
    fi

    # make a line of dashes
    for ((i=0; i<width; i++))
    do
    echo -ne " " >$TTY
    echo -ne "-" >$TTY
    done

    # move to the right offset
    for ((i=0; i<width-space; i++))
    do
    echo -ne "\b" >$TTY
    done

    # animate
    for c in ${chars[@]}
    do
    echo -ne "$c" >$TTY
    sleep 0.1
    sleep 0.075
    echo -ne "\b" >$TTY
    done

    # move back to the left
    for ((i=0; i<space; i++))
    do
    echo -ne "\b" >$TTY
    done

    # clear the line
    for ((i=0; i<width; i++))
    do
    echo -ne " " >$TTY
    done

    # back to the left
    for ((i=0; i<width; i++))
    do
    echo -ne "\b" >$TTY
    done

    ((space+=delta))

    if ((space>width))
    if ((space>=width))
    then
    delta=-1
    ((space+=delta))
    @@ -42,12 +76,9 @@ spin() {
    ((space+=delta))
    fi
    done
    wait $child
    }

    example() {
    sleep 5
    tput cnorm
    wait $pid
    }

    spin example
    spin sleep 15
    echo "Done."
  2. d11wtq created this gist Feb 27, 2014.
    53 changes: 53 additions & 0 deletions spinner.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    #!/bin/bash

    TTY=/dev/tty

    spin() {
    width=5
    delta=1
    "$@" >/dev/null &
    child=$!
    chars=("\\" "|" "/" "-")
    space=0
    while kill -0 $child 2>/dev/null
    do
    for ((i=0; i<space; i++))
    do
    echo -ne " " >$TTY
    done

    for c in ${chars[@]}
    do
    echo -ne "$c" >$TTY
    sleep 0.1
    echo -ne "\b" >$TTY
    done

    for ((i=0; i<space; i++))
    do
    echo -ne "\b" >$TTY
    done

    ((space+=delta))

    if ((space>width))
    then
    delta=-1
    ((space+=delta))
    ((space+=delta))
    elif ((space<0))
    then
    delta=1
    ((space+=delta))
    ((space+=delta))
    fi
    done
    wait $child
    }

    example() {
    sleep 5
    }

    spin example
    echo "Done."