# vim:ft=sh ## Copyright (C) 2020-2024 Aditya Shakya # archcraft.cfg - Variables and functions commonly used in custom scripts # Copyright (C) 2012 Philip Newborough # Copyright (C) 2013 Aleks-Daniel Jakimenko # Copyright (C) 2015-2016 John Crawley # Copyright (C) 2016 xaos52 # Copyright (C) 2021 Aditya Shakya # # These functions can be used in user scripts too, by first sourcing this file, eg: # MENUS_LIBDIR='/usr/share/archcraft/openbox/menulib' # if ! . "$MENUS_LIBDIR/archcraft.cfg" 2> /dev/null; then # echo $"Error: Failed to locate archcraft.cfg in $MENUS_LIBDIR" >&2 # exit 1 # fi # NOTE: Do not source this file in non-Bash shell scripts. # It uses some "bashisms". ######################################################################## # Functions to generate Openbox menus. menuStart() { echo ' ' } # Usage: menuItem label command menuItem() { echo " " echo ' ' echo " $(XMLescape "$2")" echo ' ' echo ' ' } # Usage: menuSeparator [label] menuSeparator() { if [[ ${1-} ]]; then echo " " else echo ' ' fi } # Usage menuSubmenu id label # http://openbox.org/wiki/Help:Menus menuSubmenu() { echo " " } menuSubmenuEnd() { echo ' ' } menuEnd() { echo ' ' } # escape special characters XMLescape() { local string="${1//&/&}" string="${string///>}" string="${string//\"/"}" string="${string//\'/'}" printf '%s' "$string" } OBlabelEscape() { printf '%s' "${1//_/__}" } ######################################################################## # rerun script in terminal if necessary # Usage: terminalCheck [ -T title ] [arguments...] terminalCheck() { if ! [[ -t 0 && -t 1 && -t 2 ]]; then # not attached to terminal if [[ -n $DISPLAY ]]; then # running X if [[ ${1-} = '-T' ]]; then local title=${2-} shift 2 exec exo-open --launch TerminalEmulator -T "$title" -e "$0" "$@" else exec exo-open --launch TerminalEmulator -e "$0" "$@" fi else echo "$0: no terminal" >&2 exit 1 fi fi } # Usage: createFlag filename createFlag() { mkdir -p "$HOME/.config/archcraft" touch "$HOME/.config/archcraft/$1" } # Check the connection by downloading a file from ftp.debian.org. No disk space used. # Usage: connectiontest [attempts] # If attempt count is not specified or 0, then it will loop forever and exit(!) your main program with 1 exit status. connectiontest() { local TEXT_CHECKING='Checking internet connection...' local TEXT_FAILED='Internet connection test failed!' local TEXT_ASK_RETRY=$'\n\nThis script requires a working internet connection. Please configure your internet connection, then hit "Enter" to continue, else hit "q" to quit.' local TEXT_ABORT='Script aborted.' local TEXT_OK='Internet connection test passed!' local -i i attempts=${1-0} for (( i=0; i < attempts || attempts == 0; i++ )); do say "$TEXT_CHECKING" if wget -O - 'http://ftp.debian.org/debian/README' &> /dev/null; then say "$TEXT_OK" 1 return 0 fi say "$TEXT_FAILED" if (( i == attempts - 1 )); then # if last attempt return 1 elif prompt "$TEXT_ASK_RETRY" Q; then # if user wants to quit say "$TEXT_ABORT" 2 (( attempts == 0 )) && exit 1 || return 1 fi tput clear done } # Check for availability of commands used in script. # Usage: declareDependencies command [command...] declareDependencies(){ local missing_commands=() i= for i in "$@"; do hash "$i" 2>/dev/null || missing_commands+=(" $i") done [[ ${missing_commands[0]} ]] && errorExit "This script requires the following commands: ${missing_commands[*]} Please install the packages containing the missing commands and rerun the script." } ######################################################################## # User interface functions. # Usage: say text [delayAfterText|pause] say() { local width=$(tput cols 2>/dev/tty) fold -s -w $((width - 3)) <<< "${1-}" | sed 's/^/ /' # wraps text nicely and adds two leading spaces if [[ ${2-} = 'pause' ]]; then echo $'\nPress any key to continue.' read -srn1 else sleep "${2:-0}" fi } # Usage errorExit message [details] errorExit() { say $'\n'"$1"$'\n' '1' >&2 [[ ${2-} ]] && { while true; do local REPLY read -rn1 -p ' Press "d" for details (q to quit) ' [[ ${REPLY^} = Q ]] && exit 1 [[ ${REPLY^} = D ]] && { echo $'\n\n' tput setaf 1 echo "$2" >&2 tput sgr0 say " Would you like to quit now, or continue anyway? (Be careful!) Press \"q\" to quit, \"c\" to continue: " read -r [[ ${REPLY^} = Q ]] && exit 1 [[ ${REPLY^} = C ]] && return 0 } done } say 'Press any key to exit.' read -srn1 exit 1 } # Usage: prompt text [ Y | N | Q | ] prompt() { local answer prompt default if [[ ${2-} && ${2^} = Q ]]; then while true; do read -srn1 -p "$1" answer echo [[ ${answer^} = Q ]] && return 0 [[ ! $answer ]] && return 1 done fi if [[ ! ${2-} || ${2^} = Y ]]; then prompt='Y/n' default='Y' elif [[ ${2^} = N ]]; then prompt='y/N' default='N' else prompt= default= fi while true; do read -r -p "$1 ${prompt:+[$prompt] }" answer if [[ ! $prompt && ! $default ]]; then if [[ $answer = ${2-} ]]; then echo return 0 elif [[ ${answer^} = Q ]]; then echo return 1 else continue fi fi [[ ! $answer ]] && answer=$default if [[ ${answer^} = Y || ${answer^^} = YES ]]; then echo return 0 elif [[ ${answer^} = N || ${answer^^} = NO ]]; then echo return 1 fi done } ########################################################################