Quiet dnf: spinner + package counts, full log kept for failures #7

Merged
yuri merged 4 commits from feat/quiet-dnf into feat/gum-tui 2026-10-27 19:49:14 +01:00
3 changed files with 63 additions and 19 deletions
Showing only changes of commit 9dd1ecada8 - Show all commits
+47 -3
View File
@@ -71,15 +71,59 @@ pause_if_debug() {
#------------------------------------------
# Package helpers (dnf5)
#------------------------------------------
# True when gum is installed AND we are interactive (stdin tty + /dev/tty
# writable). stdout must NOT be checked: it is a pipe inside $(...).
# Unattended runs never get the TUI (see ui_quiet_run).
have_gum() {
command -v gum >/dev/null 2>&1 || return 1
[[ "${ASSUME_YES:-false}" == "true" ]] && return 1
[ -t 0 ] || return 1
[ -w /dev/tty ] || return 1
return 0
}
# Quiet dnf transaction: spinner while it runs, one ok-line after, full output
# appended to $SETUP_DNF_LOG and printed (tail) only on failure. Unattended or
# no-gum runs execute plainly with normal output (logs matter there).
ui_quiet_run() {
local msg="$1"
shift
local log="${SETUP_DNF_LOG:-/tmp/fedora-setup-dnf.log}"
local before n
if ! have_gum; then
"$@"
return $?
fi
before=$(wc -l < "$log" 2>/dev/null || echo 0)
if LOGFILE="$log" gum spin --title "$msg" -- \
bash -c 'exec "$@" >>"$LOGFILE" 2>&1' _ "$@"; then
n=$(sed -n "$((before+1)),\$p" "$log" 2>/dev/null | grep -c '^Installing:' || true)
local plural="s"
(( n == 1 )) && plural=""
ok "$msg (${n} package${plural})"
return 0
else
local rc=$?
color_red " Failed: $msg"
color_red " Last lines from $log:"
sed -n "$((before+1)),\$p" "$log" 2>/dev/null | tail -n 20 | sed 's/^/ /' >&2
return "$rc"
fi
}
install_packages() {
# -y : no prompts
# weak deps on : keep default behaviour, stated explicitly so it is greppable
ui_quiet_run "Installing: $*" \
sudo dnf install -y --setopt=install_weak_deps=True "$@"
}
# Same, but a missing/renamed package warns instead of aborting the whole run.
# Use for anything that is a nice-to-have rather than load-bearing.
install_optional() {
ui_quiet_run "Installing (optional): $*" \
sudo dnf install -y --setopt=install_weak_deps=True "$@" \
|| warn "Optional packages not installed: $*"
}
@@ -114,7 +158,7 @@ remove_packages() {
done
if [ ${#packages_to_remove[@]} -gt 0 ]; then
info "Removing packages: ${packages_to_remove[*]}"
ui_quiet_run "Removing: ${packages_to_remove[*]}" \
sudo dnf remove -y "${packages_to_remove[@]}"
fi
}
@@ -129,7 +173,7 @@ install_group() {
dnf group list --hidden 2>/dev/null | sed 's/^/ /' >&2 || true
return 1
fi
sudo dnf group install -y "$group"
ui_quiet_run "Installing group: @$group" sudo dnf group install -y "$group"
}
# Try each candidate group/environment in turn; first one that exists wins.
@@ -138,7 +182,7 @@ install_group_any() {
for g in "$@"; do
if dnf group info "$g" >/dev/null 2>&1; then
info "Using dnf group: @$g"
sudo dnf group install -y "$g"
ui_quiet_run "Installing group: @$g" sudo dnf group install -y "$g"
return 0
fi
done
+7 -11
View File
@@ -16,16 +16,7 @@ if [[ -n "${_SB_UI_LOADED:-}" ]]; then
fi
_SB_UI_LOADED=1
have_gum() {
command -v gum >/dev/null 2>&1 || return 1
# stdin must be interactive — it is inherited by $(...) subshells, so
# menus still get the real terminal. stdout must NOT be checked: it is
# a pipe by definition inside $(ui_choose ...). gum draws its UI on
# /dev/tty and prints only the answer to stdout (which we capture).
[ -t 0 ] || return 1
[ -w /dev/tty ] || return 1
return 0
}
# have_gum lives in lib/common.sh (install_packages needs it too).
# Single choice from a list.
# ui_choose "<header>" "<prompt>" "<default>" item1 item2 ...
@@ -62,7 +53,8 @@ ui_choose() {
local n=${#items[@]} answer
while true; do
if ! read -t 90 -rp "$prompt [1-$n] (default: shown above): " answer; then
printf "\n"; answer=""
printf "\n" >&2
answer=""
fi
# empty/timeout -> the default item (its position in the list)
if [[ -z "$answer" ]]; then
@@ -74,6 +66,10 @@ ui_choose() {
echo "${items[-1]}"
return 0
fi
if [[ "${answer,,}" == "x" ]]; then
echo "$default"
return 0
fi
if [[ "$answer" =~ ^[0-9]+$ ]] && (( answer >= 1 && answer <= n )); then
echo "${items[$((answer-1))]}"
return 0
+6 -2
View File
@@ -13,6 +13,10 @@ source "$SCRIPT_DIR/lib/session.sh"
# shellcheck source=lib/ui.sh
source "$SCRIPT_DIR/lib/ui.sh"
# Full dnf output from quiet transactions lands here (shown on failure).
export SETUP_DNF_LOG="${SETUP_DNF_LOG:-/tmp/fedora-setup-dnf-$(id -u).log}"
: >> "$SETUP_DNF_LOG"
#------------------------------------------
# Defaults + argument parsing
# DEBUG_MODE must be initialised before anything reads it under set -u.
@@ -185,8 +189,8 @@ fi
info "Ensuring Flathub is available (system-wide)..."
sudo flatpak remote-add --if-not-exists --system flathub https://dl.flathub.org/repo/flathub.flatpakrepo
# Update the system (spinner when gum is present; output shown on failure)
ui_run "Updating the system" sudo dnf update -y
# Update the system (spinner + one-line summary; full output in $SETUP_DNF_LOG)
ui_quiet_run "Updating the system" sudo dnf update -y
pause_if_debug
#------------------------------------------