|
| 1 | +#!/usr/bin/env zsh |
| 2 | +# Source: https://github.com/molovo/revolver |
| 3 | +# License: MIT — Copyright (c) 2016 Joe Letchford |
| 4 | +# Maintained by z-shell/src — https://github.com/z-shell/src |
| 5 | +# |
| 6 | + |
| 7 | +local -A _revolver_spinners |
| 8 | +_revolver_spinners=( |
| 9 | + 'dots' '0.08 ⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏' |
| 10 | + 'dots2' '0.08 ⣾ ⣽ ⣻ ⢿ ⡿ ⣟ ⣯ ⣷' |
| 11 | + 'dots3' '0.08 ⠋ ⠙ ⠚ ⠞ ⠖ ⠦ ⠴ ⠲ ⠳ ⠓' |
| 12 | + 'dots4' '0.08 ⠄ ⠆ ⠇ ⠋ ⠙ ⠸ ⠰ ⠠ ⠰ ⠸ ⠙ ⠋ ⠇ ⠆' |
| 13 | + 'dots5' '0.08 ⠋ ⠙ ⠚ ⠒ ⠂ ⠂ ⠒ ⠲ ⠴ ⠦ ⠖ ⠒ ⠐ ⠐ ⠒ ⠓ ⠋' |
| 14 | + 'dots6' '0.08 ⠁ ⠉ ⠙ ⠚ ⠒ ⠂ ⠂ ⠒ ⠲ ⠴ ⠤ ⠄ ⠄ ⠤ ⠴ ⠲ ⠒ ⠂ ⠂ ⠒ ⠚ ⠙ ⠉ ⠁' |
| 15 | + 'dots7' '0.08 ⠈ ⠉ ⠋ ⠓ ⠒ ⠐ ⠐ ⠒ ⠖ ⠦ ⠤ ⠠ ⠠ ⠤ ⠦ ⠖ ⠒ ⠐ ⠐ ⠒ ⠓ ⠋ ⠉ ⠈' |
| 16 | + 'dots8' '0.08 ⠁ ⠁ ⠉ ⠙ ⠚ ⠒ ⠂ ⠂ ⠒ ⠲ ⠴ ⠤ ⠄ ⠄ ⠤ ⠠ ⠠ ⠤ ⠦ ⠖ ⠒ ⠐ ⠐ ⠒ ⠓ ⠋ ⠉ ⠈ ⠈' |
| 17 | + 'dots9' '0.08 ⢹ ⢺ ⢼ ⣸ ⣇ ⡧ ⡗ ⡏' |
| 18 | + 'dots10' '0.08 ⢄ ⢂ ⢁ ⡁ ⡈ ⡐ ⡠' |
| 19 | + 'dots11' '0.1 ⠁ ⠂ ⠄ ⡀ ⢀ ⠠ ⠐ ⠈' |
| 20 | + 'dots12' '0.08 "⢀⠀" "⡀⠀" "⠄⠀" "⢂⠀" "⡂⠀" "⠅⠀" "⢃⠀" "⡃⠀" "⠍⠀" "⢋⠀" "⡋⠀" "⠍⠁" "⢋⠁" "⡋⠁" "⠍⠉" "⠋⠉" "⠋⠉" "⠉⠙" "⠉⠙" "⠉⠩" "⠈⢙" "⠈⡙" "⢈⠩" "⡀⢙" "⠄⡙" "⢂⠩" "⡂⢘" "⠅⡘" "⢃⠨" "⡃⢐" "⠍⡐" "⢋⠠" "⡋⢀" "⠍⡁" "⢋⠁" "⡋⠁" "⠍⠉" "⠋⠉" "⠋⠉" "⠉⠙" "⠉⠙" "⠉⠩" "⠈⢙" "⠈⡙" "⠈⠩" "⠀⢙" "⠀⡙" "⠀⠩" "⠀⢘" "⠀⡘" "⠀⠨" "⠀⢐" "⠀⡐" "⠀⠠" "⠀⢀" "⠀⡀"' |
| 21 | + 'line' '0.13 - \\ | /' |
| 22 | + 'line2' '0.1 ⠂ - – — – -' |
| 23 | + 'pipe' '0.1 ┤ ┘ ┴ └ ├ ┌ ┬ ┐' |
| 24 | + 'simpleDots' '0.4 ". " ".. " "..." " "' |
| 25 | + 'simpleDotsScrolling' '0.2 ". " ".. " "..." " .." " ." " "' |
| 26 | + 'star' '0.07 ✶ ✸ ✹ ✺ ✹ ✷' |
| 27 | + 'star2' '0.08 + x *' |
| 28 | + 'flip' "0.07 _ _ _ - \` \` ' ´ - _ _ _" |
| 29 | + 'hamburger' '0.1 ☱ ☲ ☴' |
| 30 | + 'growVertical' '0.12 ▁ ▃ ▄ ▅ ▆ ▇ ▆ ▅ ▄ ▃' |
| 31 | + 'growHorizontal' '0.12 ▏ ▎ ▍ ▌ ▋ ▊ ▉ ▊ ▋ ▌ ▍ ▎' |
| 32 | + 'balloon' '0.14 " " "." "o" "O" "@" "*" " "' |
| 33 | + 'balloon2' '0.12 . o O ° O o .' |
| 34 | + 'noise' '0.14 ▓ ▒ ░' |
| 35 | + 'bounce' '0.1 ⠁ ⠂ ⠄ ⠂' |
| 36 | + 'boxBounce' '0.12 ▖ ▘ ▝ ▗' |
| 37 | + 'boxBounce2' '0.1 ▌ ▀ ▐ ▄' |
| 38 | + 'triangle' '0.05 ◢ ◣ ◤ ◥' |
| 39 | + 'arc' '0.1 ◜ ◠ ◝ ◞ ◡ ◟' |
| 40 | + 'circle' '0.12 ◡ ⊙ ◠' |
| 41 | + 'squareCorners' '0.18 ◰ ◳ ◲ ◱' |
| 42 | + 'circleQuarters' '0.12 ◴ ◷ ◶ ◵' |
| 43 | + 'circleHalves' '0.05 ◐ ◓ ◑ ◒' |
| 44 | + 'squish' '0.1 ╫ ╪' |
| 45 | + 'toggle' '0.25 ⊶ ⊷' |
| 46 | + 'toggle2' '0.08 ▫ ▪' |
| 47 | + 'toggle3' '0.12 □ ■' |
| 48 | + 'toggle4' '0.1 ■ □ ▪ ▫' |
| 49 | + 'toggle5' '0.1 ▮ ▯' |
| 50 | + 'toggle6' '0.3 ဝ ၀' |
| 51 | + 'toggle7' '0.08 ⦾ ⦿' |
| 52 | + 'toggle8' '0.1 ◍ ◌' |
| 53 | + 'toggle9' '0.1 ◉ ◎' |
| 54 | + 'toggle10' '0.1 ㊂ ㊀ ㊁' |
| 55 | + 'toggle11' '0.05 ⧇ ⧆' |
| 56 | + 'toggle12' '0.12 ☗ ☖' |
| 57 | + 'toggle13' '0.08 = * -' |
| 58 | + 'arrow' '0.1 ← ↖ ↑ ↗ → ↘ ↓ ↙' |
| 59 | + 'arrow2' '0.12 ▹▹▹▹▹ ▸▹▹▹▹ ▹▸▹▹▹ ▹▹▸▹▹ ▹▹▹▸▹ ▹▹▹▹▸' |
| 60 | + 'bouncingBar' '0.08 "[ ]" "[ =]" "[ ==]" "[ ===]" "[====]" "[=== ]" "[== ]" "[= ]"' |
| 61 | + 'bouncingBall' '0.08 "( ● )" "( ● )" "( ● )" "( ● )" "( ●)" "( ● )" "( ● )" "( ● )" "( ● )" "(● )"' |
| 62 | + 'pong' '0.08 "▐⠂ ▌" "▐⠈ ▌" "▐ ⠂ ▌" "▐ ⠠ ▌" "▐ ⡀ ▌" "▐ ⠠ ▌" "▐ ⠂ ▌" "▐ ⠈ ▌" "▐ ⠂ ▌" "▐ ⠠ ▌" "▐ ⡀ ▌" "▐ ⠠ ▌" "▐ ⠂ ▌" "▐ ⠈ ▌" "▐ ⠂▌" "▐ ⠠▌" "▐ ⡀▌" "▐ ⠠ ▌" "▐ ⠂ ▌" "▐ ⠈ ▌" "▐ ⠂ ▌" "▐ ⠠ ▌" "▐ ⡀ ▌" "▐ ⠠ ▌" "▐ ⠂ ▌" "▐ ⠈ ▌" "▐ ⠂ ▌" "▐ ⠠ ▌" "▐ ⡀ ▌" "▐⠠ ▌"' |
| 63 | + 'shark' '0.12 "▐|\\____________▌" "▐_|\\___________▌" "▐__|\\__________▌" "▐___|\\_________▌" "▐____|\\________▌" "▐_____|\\_______▌" "▐______|\\______▌" "▐_______|\\_____▌" "▐________|\\____▌" "▐_________|\\___▌" "▐__________|\\__▌" "▐___________|\\_▌" "▐____________|\\▌" "▐____________/|▌" "▐___________/|_▌" "▐__________/|__▌" "▐_________/|___▌" "▐________/|____▌" "▐_______/|_____▌" "▐______/|______▌" "▐_____/|_______▌" "▐____/|________▌" "▐___/|_________▌" "▐__/|__________▌" "▐_/|___________▌" "▐/|____________▌"' |
| 64 | +) |
| 65 | + |
| 66 | +### |
| 67 | +# Output usage information and exit |
| 68 | +### |
| 69 | +function _revolver_usage() { |
| 70 | + echo "\033[0;33mUsage:\033[0;m" |
| 71 | + echo " revolver [options] <command> <message>" |
| 72 | + echo |
| 73 | + echo "\033[0;33mOptions:\033[0;m" |
| 74 | + echo " -h, --help Output help text and exit" |
| 75 | + echo " -v, --version Output version information and exit" |
| 76 | + echo " -s, --style Set the spinner style" |
| 77 | + echo |
| 78 | + echo "\033[0;33mCommands:\033[0;m" |
| 79 | + echo " start <message> Start the spinner" |
| 80 | + echo " update <message> Update the message" |
| 81 | + echo " stop Stop the spinner" |
| 82 | + echo " demo Display an demo of each style" |
| 83 | +} |
| 84 | + |
| 85 | +### |
| 86 | +# The main revolver process, which contains the loop |
| 87 | +### |
| 88 | +function _revolver_process() { |
| 89 | + local dir statefile state msg pid="$1" spinner_index=0 |
| 90 | + |
| 91 | + # Find the directory and load the statefile |
| 92 | + dir=${REVOLVER_DIR:-"${ZDOTDIR:-$HOME}/.revolver"} |
| 93 | + statefile="$dir/$pid" |
| 94 | + |
| 95 | + # The frames that, when animated, will make up |
| 96 | + # our spinning indicator |
| 97 | + frames=(${(@z)_revolver_spinners[$style]}) |
| 98 | + interval=${(@z)frames[1]} |
| 99 | + shift frames |
| 100 | + |
| 101 | + # Create a never-ending loop |
| 102 | + while [[ 1 -eq 1 ]]; do |
| 103 | + # If the statefile has been removed, exit the script |
| 104 | + # to prevent it from being orphaned |
| 105 | + if [[ ! -f $statefile ]]; then |
| 106 | + exit 1 |
| 107 | + fi |
| 108 | + |
| 109 | + # Check for the existence of the parent process |
| 110 | + $(kill -s 0 $pid 2&>/dev/null) |
| 111 | + |
| 112 | + # If process doesn't exist, exit the script |
| 113 | + # to prevent it from being orphaned |
| 114 | + if [[ $? -ne 0 ]]; then |
| 115 | + exit 1 |
| 116 | + fi |
| 117 | + |
| 118 | + # Load the current state, and parse it to get |
| 119 | + # the message to be displayed |
| 120 | + state=($(cat $statefile)) |
| 121 | + |
| 122 | + msg="${(@)state:1}" |
| 123 | + |
| 124 | + # Output the current spinner frame, and add a |
| 125 | + # slight delay before the next one |
| 126 | + _revolver_spin |
| 127 | + sleep ${interval:-"0.1"} |
| 128 | + done |
| 129 | +} |
| 130 | + |
| 131 | +### |
| 132 | +# Output the spinner itself, along with a message |
| 133 | +### |
| 134 | +function _revolver_spin() { |
| 135 | + local dir statefile state pid frame |
| 136 | + |
| 137 | + # ZSH arrays start at 1, so we need to bump the index if it's 0 |
| 138 | + if [[ $spinner_index -eq 0 ]]; then |
| 139 | + spinner_index+=1 |
| 140 | + fi |
| 141 | + |
| 142 | + # Calculate the screen width |
| 143 | + lim=$(tput cols) |
| 144 | + |
| 145 | + # Clear the line and move the cursor to the start |
| 146 | + printf ' %.0s' {1..$lim} |
| 147 | + echo -n "\r" |
| 148 | + |
| 149 | + # Echo the current frame and message, and overwrite |
| 150 | + # the rest of the line with white space |
| 151 | + msg="\033[0;38;5;242m${msg}\033[0;m" |
| 152 | + frame="${${(@z)frames}[$spinner_index]//\"}" |
| 153 | + printf '%*.*b' ${#msg} $lim "$frame $msg$(printf '%0.1s' " "{1..$lim})" |
| 154 | + |
| 155 | + # Return to the beginning of the line |
| 156 | + echo -n "\r" |
| 157 | + |
| 158 | + # Set the spinner index to the next frame |
| 159 | + spinner_index=$(( $(( $spinner_index + 1 )) % $(( ${#frames} + 1 )) )) |
| 160 | +} |
| 161 | + |
| 162 | +### |
| 163 | +# Stop the current spinner process |
| 164 | +### |
| 165 | +function _revolver_stop() { |
| 166 | + local dir statefile state pid |
| 167 | + |
| 168 | + # Find the directory and load the statefile |
| 169 | + dir=${REVOLVER_DIR:-"${ZDOTDIR:-$HOME}/.revolver"} |
| 170 | + statefile="$dir/$PPID" |
| 171 | + |
| 172 | + # If the statefile does not exist, raise an error. |
| 173 | + # The spinner process itself performs the same check |
| 174 | + # and kills itself, so it should never be orphaned |
| 175 | + if [[ ! -f $statefile ]]; then |
| 176 | + echo '\033[0;31mRevolver process could not be found\033[0;m' |
| 177 | + exit 1 |
| 178 | + fi |
| 179 | + |
| 180 | + # Get the current state, and parse it to find the PID |
| 181 | + # of the spinner process |
| 182 | + state=($(cat $statefile)) |
| 183 | + pid="$state[1]" |
| 184 | + |
| 185 | + # Clear the line and move the cursor to the start |
| 186 | + printf ' %.0s' {1..$(tput cols)} |
| 187 | + echo -n "\r" |
| 188 | + |
| 189 | + # If a PID has been found, kill the process |
| 190 | + [[ ! -z $pid ]] && kill "$pid" > /dev/null |
| 191 | + unset pid |
| 192 | + |
| 193 | + # Remove the statefile |
| 194 | + rm $statefile |
| 195 | +} |
| 196 | + |
| 197 | +### |
| 198 | +# Update the message being displayed |
| 199 | +function _revolver_update() { |
| 200 | + local dir statefile state pid msg="$1" |
| 201 | + |
| 202 | + # Find the directory and load the statefile |
| 203 | + dir=${REVOLVER_DIR:-"${ZDOTDIR:-$HOME}/.revolver"} |
| 204 | + statefile="$dir/$PPID" |
| 205 | + |
| 206 | + # If the statefile does not exist, raise an error. |
| 207 | + # The spinner process itself performs the same check |
| 208 | + # and kills itself, so it should never be orphaned |
| 209 | + if [[ ! -f $statefile ]]; then |
| 210 | + echo '\033[0;31mRevolver process could not be found\033[0;m' |
| 211 | + exit 1 |
| 212 | + fi |
| 213 | + |
| 214 | + # Get the current state, and parse it to find the PID |
| 215 | + # of the spinner process |
| 216 | + state=($(cat $statefile)) |
| 217 | + pid="$state[1]" |
| 218 | + |
| 219 | + # Clear the line and move the cursor to the start |
| 220 | + printf ' %.0s' {1..$(tput cols)} |
| 221 | + echo -n "\r" |
| 222 | + |
| 223 | + # Echo the new message to the statefile, to be |
| 224 | + # picked up by the spinner process |
| 225 | + echo "$pid $msg" >! $statefile |
| 226 | +} |
| 227 | + |
| 228 | +### |
| 229 | +# Create a new spinner with the specified message |
| 230 | +### |
| 231 | +function _revolver_start() { |
| 232 | + local dir statefile msg="$1" |
| 233 | + |
| 234 | + # Find the directory and create it if it doesn't exist |
| 235 | + dir=${REVOLVER_DIR:-"${ZDOTDIR:-$HOME}/.revolver"} |
| 236 | + if [[ ! -d $dir ]]; then |
| 237 | + mkdir -p $dir |
| 238 | + fi |
| 239 | + |
| 240 | + # Create the filename for the statefile |
| 241 | + statefile="$dir/$PPID" |
| 242 | + |
| 243 | + touch $statefile |
| 244 | + if [[ ! -f $statefile ]]; then |
| 245 | + echo '\033[0;31mRevolver process could not create state file\033[0;m' |
| 246 | + echo "Check that the directory $dir is writable" |
| 247 | + exit 1 |
| 248 | + fi |
| 249 | + |
| 250 | + # Start the spinner process in the background |
| 251 | + _revolver_process $PPID &! |
| 252 | + |
| 253 | + # Save the current state to the statefile |
| 254 | + echo "$! $msg" >! $statefile |
| 255 | +} |
| 256 | + |
| 257 | +### |
| 258 | +# Demonstrate each of the included spinner styles |
| 259 | +### |
| 260 | +function _revolver_demo() { |
| 261 | + for style in "${(@k)_revolver_spinners[@]}"; do |
| 262 | + revolver --style $style start $style |
| 263 | + sleep 2 |
| 264 | + revolver stop |
| 265 | + done |
| 266 | +} |
| 267 | + |
| 268 | +### |
| 269 | +# Handle command input |
| 270 | +### |
| 271 | +function _revolver() { |
| 272 | + # Get the context from the first parameter |
| 273 | + local help version style ctx="$1" |
| 274 | + |
| 275 | + # Parse CLI options |
| 276 | + zparseopts -D \ |
| 277 | + h=help -help=help \ |
| 278 | + v=version -version=version \ |
| 279 | + s:=style -style:=style |
| 280 | + |
| 281 | + # Output usage information and exit |
| 282 | + if [[ -n $help ]]; then |
| 283 | + _revolver_usage |
| 284 | + exit 0 |
| 285 | + fi |
| 286 | + |
| 287 | + # Output version information and exit |
| 288 | + if [[ -n $version ]]; then |
| 289 | + echo '0.2.0' |
| 290 | + exit 0 |
| 291 | + fi |
| 292 | + |
| 293 | + if [[ -z $style ]]; then |
| 294 | + style='dots' |
| 295 | + fi |
| 296 | + |
| 297 | + if [[ -n $style ]]; then |
| 298 | + shift style |
| 299 | + ctx="$1" |
| 300 | + fi |
| 301 | + |
| 302 | + if [[ -z $_revolver_spinners[$style] ]]; then |
| 303 | + echo $(color red "Spinner '$style' is not recognised") |
| 304 | + exit 1 |
| 305 | + fi |
| 306 | + |
| 307 | + case $ctx in |
| 308 | + start|update|stop|demo) |
| 309 | + # Check if a valid command is passed, |
| 310 | + # and if so, run it |
| 311 | + _revolver_${ctx} "${(@)@:2}" |
| 312 | + ;; |
| 313 | + *) |
| 314 | + # If the context is not recognised, |
| 315 | + # throw an error and exit |
| 316 | + echo "Command $ctx is not recognised" |
| 317 | + exit 1 |
| 318 | + ;; |
| 319 | + esac |
| 320 | +} |
| 321 | + |
| 322 | +_revolver "$@" |
0 commit comments