diff options
author | jakob.stendahl <jakob.stendahl@infomedia.dk> | 2022-12-17 21:31:41 +0100 |
---|---|---|
committer | Jakob Stendahl <jakob.stendahl@outlook.com> | 2022-12-17 21:31:41 +0100 |
commit | 1e588718a855ae2871a8841f6c6e621f49795454 (patch) | |
tree | 6599b3959554b307a571a73373114cb2d34a98ef /bin/bashfuncs.sh | |
parent | 6c37c28d7044a813fcde9ef80bf8852529b8305f (diff) | |
download | Luxcena-Neo-1e588718a855ae2871a8841f6c6e621f49795454.tar.gz Luxcena-Neo-1e588718a855ae2871a8841f6c6e621f49795454.zip |
Start moving to esm, work on updater
Diffstat (limited to 'bin/bashfuncs.sh')
-rw-r--r-- | bin/bashfuncs.sh | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/bin/bashfuncs.sh b/bin/bashfuncs.sh new file mode 100644 index 0000000..a1559a2 --- /dev/null +++ b/bin/bashfuncs.sh @@ -0,0 +1,96 @@ +#!/bin/bash + +function die() { + tput setaf 1 + printf "\n\nInstall failed, successfull steps not reversed.\n" + tput sgr0 + exit 1 +} + + +function TPUT() { + if [ -t 1 ]; then + if [ "$1" = "tput" ]; then + shift + fi + tput $@ + fi +} + +function header() { + TPUT setaf 3 + if [ -t 1 ]; then + printf "\n[ ] $1" + else + printf "\n- $1" + fi + TPUT sgr0 +} + +function commandError() { + trap - 1 + cat $1 + rm $1 + + TPUT setaf 1 + printf "\n\nInstall failed.\n" + TPUT sgr0 + TPUT cnorm + exit 1 +} + +function spinner() { + i=1 + sp="/-\|" + while ps a | awk '{print $1}' | grep -q "$1"; do + TPUT cub $(tput cols) + TPUT cuf 1 + printf "${sp:i++%${#sp}:1}" + TPUT cuf $(tput cols) + sleep 0.09 + done + + TPUT cub $(tput cols) + TPUT cuf 1 +} + +function execCommand() { + TPUT sc + TPUT setaf 4 + if [ -t 1 ]; then + printf " ($1)" + else + printf "\n>> $1 " + fi + TPUT sgr0 + log=$(mktemp) + bash -c "$1 > $log 2>&1" & + + PID=$! + + if [ -t 1 ]; then + spinner $PID + fi + + wait $PID + commandSucc=$? + if [ $commandSucc -eq 0 ]; then + TPUT setaf 2 + printf "✓" + TPUT sgr0 + TPUT rc + TPUT el + else + TPUT setaf 1 + printf "x" + TPUT sgr0 + TPUT cuf $(tput cols) + printf "\n" + if [ $# -eq 1 ] || [ $2 -eq "0" ]; then + commandError $log + fi + fi + rm $log +} + +TPUT civis |