diff --git a/setup.sh b/setup.sh old mode 100644 new mode 100755 index e69de29..a5fbdc8 --- a/setup.sh +++ b/setup.sh @@ -0,0 +1,42 @@ +#!/bin/env bash + +source ./src/scripts/utils/source.sh +source ./src/scripts/utils/progressbar.sh + +while getopts 'v' OPTION; do + case "$OPTION" in + v) + _V=1 + ;; + ?) + echo "usage: ./setup.sh [-v]" >&2 + exit 1 + ;; + esac +done + +draw_progress_bar 0 +log '.: Setting up sono-os v0.1.0 :.' + +sleep 1 +draw_progress_bar 5 +log 'Installing scripts ...' +# TODO +sleep 1 + +draw_progress_bar 45 +log 'Installing config files ...' +# TODO +sleep 1 + +draw_progress_bar 65 +log 'Configuring Logger ...' +# TODO +sleep 1 + +draw_progress_bar 85 +log 'Copying rules to udev ...' +# TODO +sleep 1 + +draw_progress_bar 100 \ No newline at end of file diff --git a/src/scripts/utils/progressbar.sh b/src/scripts/utils/progressbar.sh new file mode 100644 index 0000000..2a75440 --- /dev/null +++ b/src/scripts/utils/progressbar.sh @@ -0,0 +1,205 @@ +#!/bin/bash +# https://github.com/pollev/bash_progress_bar - See license at end of file + +# Usage: +# Source this script +# enable_trapping <- optional to clean up properly if user presses ctrl-c +# setup_scroll_area <- create empty progress bar +# draw_progress_bar 10 <- advance progress bar +# draw_progress_bar 40 <- advance progress bar +# block_progress_bar 45 <- turns the progress bar yellow to indicate some action is requested from the user +# draw_progress_bar 90 <- advance progress bar +# destroy_scroll_area <- remove progress bar + +# Constants +CODE_SAVE_CURSOR="\033[s" +CODE_RESTORE_CURSOR="\033[u" +CODE_CURSOR_IN_SCROLL_AREA="\033[1A" +COLOR_FG="\e[30m" +COLOR_BG="\e[42m" +COLOR_BG_BLOCKED="\e[43m" +RESTORE_FG="\e[39m" +RESTORE_BG="\e[49m" + +# Variables +PROGRESS_BLOCKED="false" +TRAPPING_ENABLED="false" +TRAP_SET="false" + +CURRENT_NR_LINES=0 + +setup_scroll_area() { + # If trapping is enabled, we will want to activate it whenever we setup the scroll area and remove it when we break the scroll area + if [ "$TRAPPING_ENABLED" = "true" ]; then + trap_on_interrupt + fi + + lines=$(tput lines) + CURRENT_NR_LINES=$lines + let lines=$lines-1 + # Scroll down a bit to avoid visual glitch when the screen area shrinks by one row + log -en "\n" + + # Save cursor + log -en "$CODE_SAVE_CURSOR" + # Set scroll region (this will place the cursor in the top left) + log -en "\033[0;${lines}r" + + # Restore cursor but ensure its inside the scrolling area + log -en "$CODE_RESTORE_CURSOR" + log -en "$CODE_CURSOR_IN_SCROLL_AREA" + + # Start empty progress bar + draw_progress_bar 0 +} + +destroy_scroll_area() { + lines=$(tput lines) + # Save cursor + log -en "$CODE_SAVE_CURSOR" + # Set scroll region (this will place the cursor in the top left) + log -en "\033[0;${lines}r" + + # Restore cursor but ensure its inside the scrolling area + log -en "$CODE_RESTORE_CURSOR" + log -en "$CODE_CURSOR_IN_SCROLL_AREA" + + # We are done so clear the scroll bar + clear_progress_bar + + # Scroll down a bit to avoid visual glitch when the screen area grows by one row + log -en "\n\n" + + # Once the scroll area is cleared, we want to remove any trap previously set. Otherwise, ctrl+c will exit our shell + if [ "$TRAP_SET" = "true" ]; then + trap - INT + fi +} + +draw_progress_bar() { + percentage=$1 + lines=$(tput lines) + let lines=$lines + + # Check if the window has been resized. If so, reset the scroll area + if [ "$lines" -ne "$CURRENT_NR_LINES" ]; then + setup_scroll_area + fi + + # Save cursor + log -en "$CODE_SAVE_CURSOR" + + # Move cursor position to last row + log -en "\033[${lines};0f" + + # Clear progress bar + tput el + + # Draw progress bar + PROGRESS_BLOCKED="false" + print_bar_text $percentage + + # Restore cursor position + log -en "$CODE_RESTORE_CURSOR" +} + +block_progress_bar() { + percentage=$1 + lines=$(tput lines) + let lines=$lines + # Save cursor + log -en "$CODE_SAVE_CURSOR" + + # Move cursor position to last row + log -en "\033[${lines};0f" + + # Clear progress bar + tput el + + # Draw progress bar + PROGRESS_BLOCKED="true" + print_bar_text $percentage + + # Restore cursor position + log -en "$CODE_RESTORE_CURSOR" +} + +clear_progress_bar() { + lines=$(tput lines) + let lines=$lines + # Save cursor + log -en "$CODE_SAVE_CURSOR" + + # Move cursor position to last row + log -en "\033[${lines};0f" + + # clear progress bar + tput el + + # Restore cursor position + log -en "$CODE_RESTORE_CURSOR" +} + +print_bar_text() { + local percentage=$1 + local cols=$(tput cols) + let bar_size=$cols-17 + + local color="${COLOR_FG}${COLOR_BG}" + if [ "$PROGRESS_BLOCKED" = "true" ]; then + color="${COLOR_FG}${COLOR_BG_BLOCKED}" + fi + + # Prepare progress bar + let complete_size=($bar_size*$percentage)/100 + let remainder_size=$bar_size-$complete_size + progress_bar=$(log -ne "["; log -en "${color}"; printf_new "#" $complete_size; log -en "${RESTORE_FG}${RESTORE_BG}"; printf_new "." $remainder_size; log -ne "]"); + + # Print progress bar + log -ne " Progress ${percentage}% ${progress_bar}" +} + +enable_trapping() { + TRAPPING_ENABLED="true" +} + +trap_on_interrupt() { + # If this function is called, we setup an interrupt handler to cleanup the progress bar + TRAP_SET="true" + trap cleanup_on_interrupt INT +} + +cleanup_on_interrupt() { + destroy_scroll_area + exit +} + +printf_new() { + str=$1 + num=$2 + v=$(printf "%-${num}s" "$str") + log -ne "${v// /$str}" +} + + +# SPDX-License-Identifier: MIT +# +# Copyright (c) 2018--2020 Polle Vanhoof +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. diff --git a/src/scripts/utils/source.sh b/src/scripts/utils/source.sh index 8a63a7d..1e64e2c 100644 --- a/src/scripts/utils/source.sh +++ b/src/scripts/utils/source.sh @@ -391,4 +391,10 @@ function run_as_root() { fi } +function log () { + if [[ $_V -eq 1 ]]; then + echo "$@" + fi +} + # vim: syntax=sh cc=80 tw=79 ts=4 sw=4 sts=4 et sr \ No newline at end of file