Simple Git & GitHub CLI Shell Scripts
1#!/bin/bash
2
3# Function to animate loading with a command and wait
4function animate_loading {
5 loading_pid="$1"
6 loading_text="$2 ... "
7 animation_chars="/-\|"
8 i=0
9
10 # Keep spinning until the background process finishes
11 while kill -0 $loading_pid 2>/dev/null; do
12 printf "\r$loading_text${animation_chars:i:1} ${RESET}"
13 i=$(((i + 1) % ${#animation_chars}))
14 sleep 0.1
15 done
16
17 # Wait for the background process to finish and
18 # then clear the loading character
19 wait $loading_pid
20 printf "\r$loading_text"
21}
22
23# Function to execute any command with a loading animation
24function execute_with_loading {
25 # All arguments except the last one
26 loading_message="${@:1:$(($#-1))}"
27
28 # The last argument is the command to run
29 command_to_run="${!#}"
30
31 # Run the command in the background and capture its PID
32 $command_to_run > /dev/null 2>&1 &
33 command_pid=$!
34
35 # Run the animation while the command is executing
36 animate_loading $command_pid "$loading_message"
37
38 # Capture the exit status of the command
39 wait $command_pid
40 exit_status=$?
41
42 # Assign the status symbol based on the exit status
43 status=$([ $exit_status -eq 0 ] && echo "${GREEN} " || echo "${RED}✘")
44
45 echo -e "${BOLD}$status ${RESET}"
46
47 return $exit_status
48}
49
50# Function to animate loading, then delete the line after 1 second
51function load_and_delete {
52 # All arguments except the last one
53 loading_message="${@:1:$(($#-1))}"
54
55 # The last argument is the command to run
56 command_to_run="${!#}"
57
58 # Run the command in the background and capture its PID
59 $command_to_run > /dev/null 2>&1 &
60 command_pid=$!
61
62 # Run the animation while the command is executing
63 animate_loading $command_pid "$loading_message"
64
65 # Capture the exit status of the command
66 wait $command_pid
67 exit_status=$?
68
69 # Assign the status symbol based on the exit status
70 status=$([ $exit_status -eq 0 ] && echo "${GREEN} " || echo "${RED}✘")
71
72 # Show the status briefly before deleting the line
73 printf "${BOLD}$status ${RESET}"
74
75 sleep 1
76
77 printf "\r\033[2K"
78
79 return $exit_status
80}