|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +## +-----------------------------------+-----------------------------------+ |
| 4 | +## | | |
| 5 | +## | Copyright (c) 2019, Andres Gongora <[email protected]>. | |
| 6 | +## | | |
| 7 | +## | This program is free software: you can redistribute it and/or modify | |
| 8 | +## | it under the terms of the GNU General Public License as published by | |
| 9 | +## | the Free Software Foundation, either version 3 of the License, or | |
| 10 | +## | (at your option) any later version. | |
| 11 | +## | | |
| 12 | +## | This program is distributed in the hope that it will be useful, | |
| 13 | +## | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 14 | +## | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 15 | +## | GNU General Public License for more details. | |
| 16 | +## | | |
| 17 | +## | You should have received a copy of the GNU General Public License | |
| 18 | +## | along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 19 | +## | | |
| 20 | +## +-----------------------------------------------------------------------+ |
| 21 | + |
| 22 | + |
| 23 | +## |
| 24 | +## |
| 25 | +## |
| 26 | +## |
| 27 | +## |
| 28 | +## |
| 29 | +## |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | +##============================================================================== |
| 35 | +## COPNFIGURATION |
| 36 | +##============================================================================== |
| 37 | + |
| 38 | +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" |
| 39 | +if [ "$(type -t getFormatCode)" != 'function' ]; then |
| 40 | + source "$DIR/color.sh" |
| 41 | +fi |
| 42 | +unset DIR |
| 43 | + |
| 44 | + |
| 45 | +UIO_FC_DECO=$(getFormatCode -c none -e bold) |
| 46 | +UIO_FC_TEXT=$(getFormatCode -c none -e none) |
| 47 | +UIO_FC_HEAD=$(getFormatCode -c none -e bold) |
| 48 | +UIO_FC_SUCC=$(getFormatCode -c green -e bold) |
| 49 | +UIO_FC_INFO=$(getFormatCode -c blue -e bold) |
| 50 | +UIO_FC_WARN=$(getFormatCode -c yellow -e bold) |
| 51 | +UIO_FC_CRIT=$(getFormatCode -c red -e bold) |
| 52 | +UIO_FC_ERRO=$(getFormatCode -c red -e bold -e blink) |
| 53 | +UIO_FC_PRMT=$(getFormatCode -c yellow -e bold) |
| 54 | +UIO_FC_NONE=$(getFormatCode -c none -e none) |
| 55 | + |
| 56 | +UIO_SIGN_SUCC="OK" |
| 57 | +UIO_SIGN_INFO="··" |
| 58 | +UIO_SIGN_WARN="!!" |
| 59 | +UIO_SIGN_CRIT="><" |
| 60 | +UIO_SIGN_ERRO="><" |
| 61 | +UIO_SIGN_PRMT="??" |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +##============================================================================== |
| 67 | +## OUTPUT |
| 68 | +##============================================================================== |
| 69 | + |
| 70 | +_printBase() |
| 71 | +{ |
| 72 | + local sign_fc=$1 |
| 73 | + local sign=$2 |
| 74 | + local text="${@:3}" |
| 75 | + |
| 76 | + if [ ! -z "$sign" ]; then |
| 77 | + printf " ${UIO_FC_DECO}[${sign_fc}${sign}${UIO_FC_DECO}]" |
| 78 | + fi |
| 79 | + |
| 80 | + printf "\t${UIO_FC_TEXT}${text}" |
| 81 | + printf "${UIO_FC_NONE}\n" |
| 82 | +} |
| 83 | + |
| 84 | + |
| 85 | +printHeader() |
| 86 | +{ |
| 87 | + _printBase "$UIO_FC_HEAD" "" "\n\t${UIO_FC_HEAD}$@\n" |
| 88 | +} |
| 89 | + |
| 90 | + |
| 91 | +printText() |
| 92 | +{ |
| 93 | + _printBase "$UIO_FC_TEXT" "" "$@" |
| 94 | +} |
| 95 | + |
| 96 | + |
| 97 | +printSuccess() |
| 98 | +{ |
| 99 | + _printBase "$UIO_FC_SUCC" "$UIO_SIGN_SUCC" "$@" |
| 100 | +} |
| 101 | + |
| 102 | + |
| 103 | +printInfo() |
| 104 | +{ |
| 105 | + _printBase "$UIO_FC_INFO" "$UIO_SIGN_INFO" "$@" |
| 106 | +} |
| 107 | + |
| 108 | + |
| 109 | +printWarn() |
| 110 | +{ |
| 111 | + _printBase "$UIO_FC_WARN" "$UIO_SIGN_WARN" "$@" |
| 112 | +} |
| 113 | + |
| 114 | + |
| 115 | +printCrit() |
| 116 | +{ |
| 117 | + _printBase "$UIO_FC_CRIT" "$UIO_SIGN_CRIT" "$@" |
| 118 | +} |
| 119 | + |
| 120 | + |
| 121 | +printError() |
| 122 | +{ |
| 123 | + _printBase "$UIO_FC_ERRO" "$UIO_SIGN_ERRO" "$@" |
| 124 | +} |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | +##============================================================================== |
| 132 | +## INPUT |
| 133 | +##============================================================================== |
| 134 | + |
| 135 | +promptUser() |
| 136 | +{ |
| 137 | + local text="$1" |
| 138 | + local options="$2" |
| 139 | + local valid_options="$3" |
| 140 | + local default_option="$4" |
| 141 | + local user_choice= |
| 142 | + |
| 143 | + |
| 144 | + ## PROMPT USER |
| 145 | + printf " ${UIO_FC_DECO}[${UIO_FC_PRMT}${UIO_SIGN_PRMT}${UIO_FC_DECO}]" > /dev/tty |
| 146 | + printf "\t${UIO_FC_TEXT}${text}" > /dev/tty |
| 147 | + if [ ! -z "$options" ]; then |
| 148 | + printf "\n\t${UIO_FC_NONE}${options}: " > /dev/tty |
| 149 | + else |
| 150 | + printf ": " > /dev/tty |
| 151 | + fi |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | + while [ -z "$user_choice" ]; do |
| 156 | + |
| 157 | + ## READ USER INPUT |
| 158 | + ## -n 1: read only 1 character |
| 159 | + ## -s: do not echo |
| 160 | + local input= |
| 161 | + read -s -n 1 input |
| 162 | + |
| 163 | + |
| 164 | + ## CHECK IF USER CHOICE IS VALID |
| 165 | + ## - In no input (enter), but default options set, use that one |
| 166 | + ## - If no valid options specified, return whatever |
| 167 | + ## - If valid options specified, check input against them |
| 168 | + ## - Else, do nothing and repeat the while loop |
| 169 | + if [ "$input" == "" ] && [ ! -z "$default_option" ]; then |
| 170 | + local user_choice="$default_option" |
| 171 | + echo "$user_choice" > /dev/tty |
| 172 | + |
| 173 | + elif [ -z "$valid_options" ]; then |
| 174 | + local user_choice="$input" |
| 175 | + echo "$user_choice" > /dev/tty |
| 176 | + |
| 177 | + elif [ ! -z $(echo "$input"|sed -n '/['"$valid_options"']/p') ]; then |
| 178 | + local user_choice="$input" |
| 179 | + echo "$user_choice" > /dev/tty |
| 180 | + |
| 181 | + else |
| 182 | + echo "$user_choice is not a valid option." > /dev/tty |
| 183 | + printf "\t${UIO_FC_NONE}${options}: " > /dev/tty |
| 184 | + fi |
| 185 | + done |
| 186 | + |
| 187 | + |
| 188 | + ## EXIT |
| 189 | + echo "$user_choice" |
| 190 | +} |
| 191 | + |
| 192 | + |
| 193 | + |
| 194 | + |
| 195 | + |
| 196 | + |
| 197 | +##============================================================================== |
| 198 | +## TEST |
| 199 | +##============================================================================== |
| 200 | + |
| 201 | +#printHeader "Hello" |
| 202 | +#printText "Normal text" |
| 203 | +#printSuccess "Menssage" |
| 204 | +#printInfo "Menssage" |
| 205 | +#printWarn "Menssage" |
| 206 | +#printCrit "Menssage" |
| 207 | +#printError "Menssage" |
| 208 | + |
| 209 | +#var=$(promptUser "Please make a choice now about random stuff" "[h] Hug a koala, [j] jump from a bridge, [n] nothing" "jasd") && echo "You chose: $var" |
| 210 | + |
| 211 | + |
0 commit comments