|
| 1 | +#!/bin/bash |
| 2 | +# check_os.sh |
| 3 | + |
| 4 | +# shellcheck disable=SC2034 |
| 5 | +_OS="Unknown" |
| 6 | +_DISTRO="Unknown" |
| 7 | + |
| 8 | +function check_os::init() { |
| 9 | + if check_os::is_linux; then |
| 10 | + _OS="Linux" |
| 11 | + if check_os::is_ubuntu; then |
| 12 | + _DISTRO="Ubuntu" |
| 13 | + elif check_os::is_alpine; then |
| 14 | + _DISTRO="Alpine" |
| 15 | + else |
| 16 | + _DISTRO="Other" |
| 17 | + fi |
| 18 | + elif check_os::is_macos; then |
| 19 | + _OS="OSX" |
| 20 | + elif check_os::is_windows; then |
| 21 | + _OS="Windows" |
| 22 | + else |
| 23 | + _OS="Unknown" |
| 24 | + _DISTRO="Unknown" |
| 25 | + fi |
| 26 | +} |
| 27 | + |
| 28 | +function check_os::is_ubuntu() { |
| 29 | + command -v apt > /dev/null |
| 30 | +} |
| 31 | + |
| 32 | +function check_os::is_alpine() { |
| 33 | + command -v apk > /dev/null |
| 34 | +} |
| 35 | + |
| 36 | +function check_os::is_linux() { |
| 37 | + [[ "$(uname)" == "Linux" ]] |
| 38 | +} |
| 39 | + |
| 40 | +function check_os::is_macos() { |
| 41 | + [[ "$(uname)" == "Darwin" ]] |
| 42 | +} |
| 43 | + |
| 44 | +function check_os::is_windows() { |
| 45 | + [[ "$(uname)" == *"MINGW"* ]] |
| 46 | +} |
| 47 | + |
| 48 | +function check_os::is_busybox() { |
| 49 | + |
| 50 | + case "$_DISTRO" in |
| 51 | + |
| 52 | + "Alpine") |
| 53 | + return 0 |
| 54 | + ;; |
| 55 | + *) |
| 56 | + return 1 |
| 57 | + ;; |
| 58 | + esac |
| 59 | +} |
| 60 | + |
| 61 | +check_os::init |
| 62 | + |
| 63 | +export _OS |
| 64 | +export _DISTRO |
| 65 | +export -f check_os::is_alpine |
| 66 | +export -f check_os::is_busybox |
| 67 | +export -f check_os::is_ubuntu |
| 68 | + |
| 69 | +# str.sh |
| 70 | + |
| 71 | +function str::rpad() { |
| 72 | + local left_text="$1" |
| 73 | + local right_word="$2" |
| 74 | + local width_padding="${3:-$TERMINAL_WIDTH}" |
| 75 | + # Subtract 1 more to account for the extra space |
| 76 | + local padding=$((width_padding - ${#right_word} - 1)) |
| 77 | + |
| 78 | + # Remove ANSI escape sequences (non-visible characters) for length calculation |
| 79 | + # shellcheck disable=SC2155 |
| 80 | + local clean_left_text=$(echo -e "$left_text" | sed 's/\x1b\[[0-9;]*m//g') |
| 81 | + |
| 82 | + local is_truncated=false |
| 83 | + # If the visible left text exceeds the padding, truncate it and add "..." |
| 84 | + if [[ ${#clean_left_text} -gt $padding ]]; then |
| 85 | + local truncation_length=$((padding - 3)) # Subtract 3 for "..." |
| 86 | + clean_left_text="${clean_left_text:0:$truncation_length}" |
| 87 | + is_truncated=true |
| 88 | + fi |
| 89 | + |
| 90 | + # Rebuild the text with ANSI codes intact, preserving the truncation |
| 91 | + local result_left_text="" |
| 92 | + local i=0 |
| 93 | + local j=0 |
| 94 | + while [[ $i -lt ${#clean_left_text} && $j -lt ${#left_text} ]]; do |
| 95 | + local char="${clean_left_text:$i:1}" |
| 96 | + local original_char="${left_text:$j:1}" |
| 97 | + |
| 98 | + # If the current character is part of an ANSI sequence, skip it and copy it |
| 99 | + if [[ "$original_char" == $'\x1b' ]]; then |
| 100 | + while [[ "${left_text:$j:1}" != "m" && $j -lt ${#left_text} ]]; do |
| 101 | + result_left_text+="${left_text:$j:1}" |
| 102 | + ((j++)) |
| 103 | + done |
| 104 | + result_left_text+="${left_text:$j:1}" # Append the final 'm' |
| 105 | + ((j++)) |
| 106 | + elif [[ "$char" == "$original_char" ]]; then |
| 107 | + # Match the actual character |
| 108 | + result_left_text+="$char" |
| 109 | + ((i++)) |
| 110 | + ((j++)) |
| 111 | + else |
| 112 | + ((j++)) |
| 113 | + fi |
| 114 | + done |
| 115 | + |
| 116 | + local remaining_space |
| 117 | + if $is_truncated ; then |
| 118 | + result_left_text+="..." |
| 119 | + # 1: due to a blank space |
| 120 | + # 3: due to the appended ... |
| 121 | + remaining_space=$((width_padding - ${#clean_left_text} - ${#right_word} - 1 - 3)) |
| 122 | + else |
| 123 | + # Copy any remaining characters after the truncation point |
| 124 | + result_left_text+="${left_text:$j}" |
| 125 | + remaining_space=$((width_padding - ${#clean_left_text} - ${#right_word} - 1)) |
| 126 | + fi |
| 127 | + |
| 128 | + # Ensure the right word is placed exactly at the far right of the screen |
| 129 | + # filling the remaining space with padding |
| 130 | + if [[ $remaining_space -lt 0 ]]; then |
| 131 | + remaining_space=0 |
| 132 | + fi |
| 133 | + |
| 134 | + printf "%s%${remaining_space}s %s\n" "$result_left_text" "" "$right_word" |
| 135 | +} |
| 136 | + |
| 137 | +# globals.sh |
| 138 | +set -euo pipefail |
| 139 | + |
| 140 | +# This file provides a set of global functions to developers. |
| 141 | + |
| 142 | +function current_dir() { |
| 143 | + dirname "${BASH_SOURCE[1]}" |
| 144 | +} |
| 145 | + |
| 146 | +function current_filename() { |
| 147 | + basename "${BASH_SOURCE[1]}" |
| 148 | +} |
| 149 | + |
| 150 | +function caller_filename() { |
| 151 | + dirname "${BASH_SOURCE[2]}" |
| 152 | +} |
| 153 | + |
| 154 | +function caller_line() { |
| 155 | + echo "${BASH_LINENO[1]}" |
| 156 | +} |
| 157 | + |
| 158 | +function current_timestamp() { |
| 159 | + date +"%Y-%m-%d %H:%M:%S" |
| 160 | +} |
| 161 | + |
| 162 | +function is_command_available() { |
| 163 | + command -v "$1" >/dev/null 2>&1 |
| 164 | +} |
| 165 | + |
| 166 | +function random_str() { |
| 167 | + local length=${1:-6} |
| 168 | + local chars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' |
| 169 | + local str='' |
| 170 | + for (( i=0; i<length; i++ )); do |
| 171 | + str+="${chars:RANDOM%${#chars}:1}" |
| 172 | + done |
| 173 | + echo "$str" |
| 174 | +} |
| 175 | + |
| 176 | +function temp_file() { |
| 177 | + local prefix=${1:-bashunit} |
| 178 | + mkdir -p /tmp/bashunit/tmp && chmod -R 777 /tmp/bashunit/tmp |
| 179 | + mktemp /tmp/bashunit/tmp/"$prefix".XXXXXXX |
| 180 | +} |
| 181 | + |
| 182 | +function temp_dir() { |
| 183 | + local prefix=${1:-bashunit} |
| 184 | + mkdir -p /tmp/bashunit/tmp && chmod -R 777 /tmp/bashunit/tmp |
| 185 | + mktemp -d /tmp/bashunit/tmp/"$prefix".XXXXXXX |
| 186 | +} |
| 187 | + |
| 188 | +function cleanup_temp_files() { |
| 189 | + rm -rf /tmp/bashunit/tmp/* |
| 190 | +} |
| 191 | + |
| 192 | +# shellcheck disable=SC2145 |
| 193 | +function log() { |
| 194 | + if ! env::is_dev_mode_enabled; then |
| 195 | + return |
| 196 | + fi |
| 197 | + |
| 198 | + local level="$1" |
| 199 | + shift |
| 200 | + |
| 201 | + case "$level" in |
| 202 | + info|INFO) level="INFO" ;; |
| 203 | + debug|DEBUG) level="DEBUG" ;; |
| 204 | + warning|WARNING) level="WARNING" ;; |
| 205 | + critical|CRITICAL) level="CRITICAL" ;; |
| 206 | + error|ERROR) level="ERROR" ;; |
| 207 | + *) set -- "$level $@"; level="INFO" ;; |
| 208 | + esac |
| 209 | + |
| 210 | + echo "$(current_timestamp) [$level]: $@" >> "$BASHUNIT_DEV_LOG" |
| 211 | +} |
| 212 | + |
| 213 | +# dependencies.sh |
| 214 | +set -euo pipefail |
| 215 | + |
| 216 | +function dependencies::has_perl() { |
| 217 | + command -v perl >/dev/null 2>&1 |
| 218 | +} |
| 219 | + |
| 220 | +function dependencies::has_powershell() { |
| 221 | + command -v powershell > /dev/null 2>&1 |
| 222 | +} |
| 223 | + |
| 224 | +function dependencies::has_adjtimex() { |
| 225 | + command -v adjtimex >/dev/null 2>&1 |
| 226 | +} |
| 227 | + |
| 228 | +function dependencies::has_bc() { |
| 229 | + command -v bc >/dev/null 2>&1 |
| 230 | +} |
| 231 | + |
| 232 | +function dependencies::has_awk() { |
| 233 | + command -v awk >/dev/null 2>&1 |
| 234 | +} |
| 235 | + |
| 236 | +function dependencies::has_git() { |
| 237 | + command -v git >/dev/null 2>&1 |
| 238 | +} |
| 239 | + |
| 240 | +function dependencies::has_curl() { |
| 241 | + command -v curl >/dev/null 2>&1 |
| 242 | +} |
| 243 | + |
| 244 | +function dependencies::has_wget() { |
| 245 | + command -v wget >/dev/null 2>&1 |
| 246 | +} |
| 247 | + |
| 248 | +# io.sh |
| 249 | + |
| 250 | +function io::download_to() { |
| 251 | + local url="$1" |
| 252 | + local output="$2" |
| 253 | + if dependencies::has_curl; then |
| 254 | + curl -L -J -o "$output" "$url" 2>/dev/null |
| 255 | + elif dependencies::has_wget; then |
| 256 | + wget -q -O "$output" "$url" 2>/dev/null |
| 257 | + else |
| 258 | + return 1 |
| 259 | + fi |
| 260 | +} |
| 261 | + |
| 262 | +# math.sh |
0 commit comments