apply DRY and enhance progress in update.sh
All checks were successful
CI Pipeline / shasums (push) Successful in 7s
git-sync-with-mirror / git-sync (push) Successful in 9s
CI Pipeline / build (push) Successful in 43s
CI Pipeline / tests (push) Successful in 28s
CI Pipeline / performance (push) Successful in 8s

- Centralize constants and remove code duplication
- Implement dynamic progress bar with real file count
- Remove artificial delays for improved performance
This commit is contained in:
2025-11-05 21:34:18 -05:00
parent 0d88c91fc2
commit 175736caa2
2 changed files with 89 additions and 62 deletions

View File

@@ -1,5 +1,13 @@
#!/bin/bash #!/bin/bash
# shellcheck source=/dev/null # shellcheck source=/dev/null
# Constants
HYPERTERM_DIR="${HYPERTERM_DIR:-$HOME/.hyperterm}"
URL_1="${URL_1:-https://git.fridu.us/heckyel/hyperterm}"
URL_2="${URL_2:-https://c.fridu.us/software/hyperterm.git}"
REPO_ERROR_MSG_ES="${REPO_ERROR_MSG_ES:-El repositorio no está disponible o no hay conexión a Internet.}"
REPO_ERROR_MSG_EN="${REPO_ERROR_MSG_EN:-The repository is unavailable or there\'s no internet connection.}"
#---------------------------- #----------------------------
# Check if a command exists # Check if a command exists
#---------------------------- #----------------------------
@@ -15,22 +23,16 @@ function _url_exists() {
} }
#------------------ #------------------
# Set working URLs # Get working URLs
#------------------ #------------------
function _urls() { function _get_repo_urls() {
URL_1="https://git.fridu.us/heckyel/hyperterm"
URL_2="https://c.fridu.us/software/hyperterm.git"
if [ "$(_url_exists "$URL_1")" -eq 200 ]; then if [ "$(_url_exists "$URL_1")" -eq 200 ]; then
URL="$URL_1" echo "$URL_1|$URL_1/raw/branch/master"
RAW="$URL_1/raw/branch/master"
elif [ "$(_url_exists "$URL_2")" -eq 200 ]; then elif [ "$(_url_exists "$URL_2")" -eq 200 ]; then
URL="$URL_2" echo "$URL_2|$URL_2/plain"
RAW="$URL_2/plain"
else else
msg_err "El repositorio no está disponible o no hay conexión a Internet." \ msg_err "$REPO_ERROR_MSG_ES" "$REPO_ERROR_MSG_EN"
"The repository is unavailable or there's no internet connection." return 1
exit 1
fi fi
} }
@@ -40,44 +42,73 @@ function _urls() {
function download_file() { function download_file() {
local remote_path=$1 local remote_path=$1
local local_path=$2 local local_path=$2
mkdir -p "$(dirname "$local_path")" local repo_info
curl -Ls "$RAW/$remote_path" -o "$local_path" local raw_url
repo_info=$(_get_repo_urls) || return 1
raw_url="${repo_info#*|}"
install -d "$(dirname "$local_path")"
curl -Ls "$raw_url/$remote_path" -o "$local_path"
} }
#---------------------------- #--------------------------------
# Download _custom.sh if missing # Download _custom.sh if missing
#---------------------------- #--------------------------------
function ifexists_custom() { function ifexists_custom() {
_urls local custom_path="$HYPERTERM_DIR/_custom.sh"
local custom_path="$HOME/.hyperterm/_custom.sh"
if [ ! -e "$custom_path" ]; then if [ ! -e "$custom_path" ]; then
download_file "hyperterm/_custom.sh" "$custom_path" download_file "hyperterm/_custom.sh" "$custom_path"
fi fi
} }
#---------------------------- #-------------------
# Show progress bar # Show progress bar
#---------------------------- #-------------------
function show_progress() { function show_progress() {
case $1 in local current=$1
1) printf '%s\r' "##### (33%)" ;; local total=$2
2) printf '%s\r' "############# (66%)" ;; local message=${3:-"Procesando"}
3) printf '%s\n' "####################### (100%) done!" ;;
esac if [[ $total -eq 0 ]]; then
printf '\r%s... ' "$message"
return
fi
local percentage=$((current * 100 / total))
local filled=$((percentage / 5)) # 20 chars max
local empty=$((20 - filled))
# Truncate long filenames for better display
local display_message="$message"
if [[ ${#message} -gt 30 ]]; then
display_message="...${message: -27}"
fi
# Clear the line first, then show progress
printf '\r\033[K[%*s%*s] %d%% (%d/%d) %s' \
"$filled" "$(printf '#%.0s' $(seq 1 $filled))" \
"$empty" "" \
"$percentage" "$current" "$total" "$display_message"
[[ $current -eq $total ]] && printf '\n'
} }
#---------------------------- #------------------------------
# Update HyperTerm environment # Update HyperTerm environment
#---------------------------- #------------------------------
function updbashrc() { function updbashrc() {
_urls local repo_info
local url
repo_info=$(_get_repo_urls) || return 1
url="${repo_info%|*}"
# Step 1: Download checksum and _custom.sh # Step 1: Download checksum and _custom.sh
download_file "hyperterm/hyperterm.sha512" "$HOME/.hyperterm/hyperterm.sha512" download_file "hyperterm/hyperterm.sha512" "$HYPERTERM_DIR/hyperterm.sha512"
ifexists_custom ifexists_custom
# Step 2: Verify checksum # Step 2: Verify checksum
(cd "$HOME/.hyperterm/" && sha512sum -c hyperterm.sha512 &> /dev/null) (cd "$HYPERTERM_DIR" && sha512sum -c hyperterm.sha512 &> /dev/null)
local _integer=$? local _integer=$?
if [[ "$_integer" -eq 0 ]]; then if [[ "$_integer" -eq 0 ]]; then
@@ -99,21 +130,22 @@ function updbashrc() {
"Get your copy at: https://c.fridu.us/software/hyperterm.git" "Get your copy at: https://c.fridu.us/software/hyperterm.git"
printf '%b\n' "$RESET" printf '%b\n' "$RESET"
else else
msg "Fallo de checksum. Re-descargando archivos desde: $URL" \ msg "Fallo de checksum. Re-descargando archivos desde: $url" \
"Checksum failed. Re-downloading files from: $URL" "Checksum failed. Re-downloading files from: $url"
show_progress 1
sleep 1 # Count total files to download
local total_files
total_files=$(wc -l < "$HYPERTERM_DIR/hyperterm.sha512")
local current_file=0
# Step 3: Download all files from hyperterm.sha512 like list # Step 3: Download all files from hyperterm.sha512 like list
while IFS= read -r line; do while IFS= read -r line; do
file=$(echo "$line" | cut -d' ' -f2) file=$(echo "$line" | cut -d' ' -f2)
file=${file#./} file=${file#./}
download_file "hyperterm/$file" "$HOME/.hyperterm/$file" ((current_file++))
done < "$HOME/.hyperterm/hyperterm.sha512" show_progress "$current_file" "$total_files" "Descargando $file"
show_progress 2 download_file "hyperterm/$file" "$HYPERTERM_DIR/$file"
sleep 1 done < "$HYPERTERM_DIR/hyperterm.sha512"
show_progress 3
_colors_bash "$@" _colors_bash "$@"
source "$HOME/.bashrc" source "$HOME/.bashrc"
fi fi
@@ -123,25 +155,20 @@ function updbashrc() {
# Overwrite _custom.sh interactively # Overwrite _custom.sh interactively
#------------------------------------ #------------------------------------
function updbashrc_custom() { function updbashrc_custom() {
_urls _get_repo_urls > /dev/null || return 1
if [ "$(_url_exists "$URL")" -eq 200 ]; then
while true; do while true; do
question=$(msg "¿Estás seguro de sobre-escribir _custom.sh? [s/N]: " \ question=$(msg "¿Estás seguro de sobre-escribir _custom.sh? [s/N]: " \
"Are you sure to overwrite _custom.sh? [y/N]: ") "Are you sure to overwrite _custom.sh? [y/N]: ")
read -r -p "$question" input read -r -p "$question" input
case "$input" in case "$input" in
[yY]|[sS]) [yY]|[sS])
download_file "hyperterm/_custom.sh" "$HOME/.hyperterm/_custom.sh" download_file "hyperterm/_custom.sh" "$HYPERTERM_DIR/_custom.sh"
source "$HOME/.bashrc" source "$HOME/.bashrc"
break ;; break ;;
[nN]|"") break ;; [nN]|"") break ;;
*) msg "Por favor responde sí o no." \ *) msg "Por favor responde sí o no." \
"Please answer yes or no." ;; "Please answer yes or no." ;;
esac esac
done done
else
msg_err "El repositorio no está disponible o no hay conexión a Internet." \
"The repository is unavailable or there's no internet connection."
return 1
fi
} }

View File

@@ -1,6 +1,6 @@
cdfe049ec07f02a1893cda29c13085d06709e09a30b0c2e1111585278315f03139d61080c883cb3fd87f2bf64e05d9b5e5eaaad84c97ced890d83c73eb399fcb ../.bash_profile cdfe049ec07f02a1893cda29c13085d06709e09a30b0c2e1111585278315f03139d61080c883cb3fd87f2bf64e05d9b5e5eaaad84c97ced890d83c73eb399fcb ../.bash_profile
f363606f41a2c2c8f1cc44110c64fe23b1c8feb4c788ee006222db0f5c7a3adeac2b0948626b313adc985e9b8d303a0b9ce1c5ba42746810accb54efddcd4b84 ./hyperterm.sh f363606f41a2c2c8f1cc44110c64fe23b1c8feb4c788ee006222db0f5c7a3adeac2b0948626b313adc985e9b8d303a0b9ce1c5ba42746810accb54efddcd4b84 ./hyperterm.sh
b760a908a3f6222b974abc1f7464bde0f5427f120f1e7ef1c6d97ae61769e552ef3b5cb88e193e955da72a592f07eadb812413dd50a691cd3dbb33e3da581ea6 ./core/update.sh c625e97dd0feb1d956f7f9bbe77e238eb57effc2951b320d20c652ca2d7b0ecfd18a772af61326d93ac5627316c8a2047a1cfe47fb90647fc3613623c7051941 ./core/update.sh
1cfba599047d84a17ff92b695ebf527a505a30acc9ec21a2b9f410a7ea6dde4b23b5cf62e557d82f2fe9a8980649942424b879ca53baae4d4cb3057681baa7b6 ./core/colors.sh 1cfba599047d84a17ff92b695ebf527a505a30acc9ec21a2b9f410a7ea6dde4b23b5cf62e557d82f2fe9a8980649942424b879ca53baae4d4cb3057681baa7b6 ./core/colors.sh
c5ae12ee08361d1264fde5c73868fd0a80fd8810ac4e614cc333335679ad68d629d9bb88a04d27a8002d62f454105f1168df9ea63c7300c9272380b6d5311278 ./core/git.sh c5ae12ee08361d1264fde5c73868fd0a80fd8810ac4e614cc333335679ad68d629d9bb88a04d27a8002d62f454105f1168df9ea63c7300c9272380b6d5311278 ./core/git.sh
f3e00b2aa8ab9f3ab44570adaa2520408ed66fd00f551654d60b64a4be3546ec781b7efa39bcd774937e654b6ffb4c7af3f21eeb36caf9c01f82f85cf28e2b4d ./core/languages.sh f3e00b2aa8ab9f3ab44570adaa2520408ed66fd00f551654d60b64a4be3546ec781b7efa39bcd774937e654b6ffb4c7af3f21eeb36caf9c01f82f85cf28e2b4d ./core/languages.sh