Dotfiles/setup/modules/lib/logging.sh

108 lines
6.2 KiB
Bash

#!/bin/bash
# ╔══════════════════════════════════════════════════════════════════════════════╗
# ║ setup/modules/lib/logging.sh — Shared logging helpers ║
# ║ ║
# ║ PURPOSE: ║
# ║ Provides colored console output functions used by every module script. ║
# ║ Source this file at the top of any module with: ║
# ║ source "$(dirname "${BASH_SOURCE[0]}")/lib/logging.sh" ║
# ║ ║
# ║ WHY A SHARED LIB: ║
# ║ Centralizes formatting so all modules have consistent output. One change ║
# ║ here affects every module's output style. ║
# ║ ║
# ║ ALSO PROVIDES: ║
# ║ - ensure_flatpak: bootstrap Flatpak + Flathub for any app module ║
# ║ - apply_flatpak_theme: apply the cyberqueer GTK theme to a Flatpak app ║
# ╚══════════════════════════════════════════════════════════════════════════════╝
# Shared logging helpers — source this in every module
# ── ANSI color codes ──────────────────────────────────────────────────────────
# These are escape sequences that terminal emulators interpret as colors.
# Format: \e[<code>m where code 31=red, 32=green, 33=yellow, 0=reset.
GREEN="\e[32m"
YELLOW="\e[33m"
RED="\e[31m"
RESET="\e[0m"
# ── Logging functions ──────────────────────────────────────────────────────────
# Each function takes a message string and formats it with a colored prefix.
# WHY: Consistent, colored output makes it easy to scan install logs at a glance
# and immediately identify success, skips, warnings, or errors.
# log: normal progress message — green [+] prefix
# Used for: "doing X now", successful completions
log() { printf "${GREEN}[+] %s${RESET}\n" "$*"; }
# skip: indicates something was already done — yellow [~] prefix
# Used for: idempotency checks ("X already installed")
skip() { printf "${YELLOW}[~] %s${RESET}\n" "$*"; }
# warn: non-fatal warning — yellow [!] prefix to stderr
# Used for: recoverable issues that don't abort the module
warn() { printf "${YELLOW}[!] %s${RESET}\n" "$*" >&2; }
# err: fatal error — red [✖] prefix to stderr
# Used for: unrecoverable failures (caller decides whether to exit)
err() { printf "${RED}[✖] %s${RESET}\n" "$*" >&2; }
# ── ensure_flatpak ─────────────────────────────────────────────────────────────
# WHY: Many optional app modules install via Flatpak. Rather than duplicating
# the bootstrap code in every app script, this function handles it once.
# WHAT: Installs flatpak if missing, adds Flathub remote if not already present.
ensure_flatpak() {
# Check if flatpak binary is available; install it via pacman if not
if ! command -v flatpak &>/dev/null; then
log "Installing flatpak..."
sudo pacman -S --noconfirm --needed flatpak
fi
# Add the Flathub remote at --user scope. A system-scope remote (and the
# matching `flatpak install` below) needs the org.freedesktop.Flatpak
# SystemHelper D-Bus service + polkit, which are not activatable in a chroot
# or bare-TTY install — that is the "The name is not activatable" failure.
# User scope needs neither, so it works during install and is the right place
# for per-user app installs anyway (apply_flatpak_theme already uses --user).
if ! flatpak remotes --user 2>/dev/null | grep -q flathub; then
log "Adding Flathub remote (user)..."
flatpak remote-add --user --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo
fi
}
# ── apply_flatpak_theme ────────────────────────────────────────────────────────
# WHY: Flatpak apps run in sandboxes and can't access the host ~/.themes directory
# by default. We must explicitly grant filesystem access AND set GTK_THEME.
# WHAT: Copies the cyberqueer GTK theme into ~/.themes, grants the Flatpak app
# read-only access to that directory, and overrides the GTK_THEME env var.
#
# Usage: apply_flatpak_theme <app-id>
# e.g. apply_flatpak_theme org.gnome.gedit
apply_flatpak_theme() {
local app_id="$1"
local theme_name="cyberqueer"
# The theme lives in the dotfiles repo under gtk-themes/cyberqueer/
local theme_src="$HOME/Dotfiles/gtk-themes/$theme_name"
local themes_dir="$HOME/.themes"
# Guard: if the theme source doesn't exist, skip gracefully with a warning
if [[ ! -d "$theme_src" ]]; then
warn "Cyberqueer theme not found at $theme_src — skipping Flatpak theme override."
return 0
fi
# Copy the theme into the user's ~/.themes/ so it's accessible
mkdir -p "$themes_dir"
cp -r "$theme_src" "$themes_dir/$theme_name"
# Grant the Flatpak app read-only access to ~/.themes using a per-user override.
# This avoids needing a global system-wide Flatpak override.
flatpak override --user --filesystem="$themes_dir":ro "$app_id"
# Set GTK_THEME env var for this app so GTK picks up the correct theme name
flatpak override --user --env=GTK_THEME="$theme_name" "$app_id"
log "Cyberqueer theme applied to $app_id."
}