Fix bat.sh config target, theme name, MANPAGER quoting, shell detection

- set -euo pipefail with audit: every tolerated failure is guarded
- write --theme to bat's config file, not into the themes/ directory (was: append to a directory, failed silently)
- download and configure the same theme (Catppuccin Mocha; was: Macchiato downloaded, Mocha configured)
- preserve MANPAGER quoting via printf (was: collapsed to export MANPAGER=bat -plman, invalid when sourced)
- detect shell via $SHELL instead of $0 (was: always matched *) and reported 'Unsupported shell: bat.sh')
- fish branch appends fish syntax, no more exec of a mangled command string
- check pacman/apt bat install results (was: unchecked, continued on failure)
- idempotent appends (grep guard), tty-guarded fish warning
- remove broken rc-sourcing block (sourced .zshrc/config.fish from bash)
This commit is contained in:
alvinlollo
2026-10-08 14:04:49 +11:00
parent e215b85d90
commit 49e00110bf
+51 -36
View File
@@ -1,15 +1,19 @@
#!/usr/bin/bash #!/usr/bin/bash
set -euo pipefail
# Detect fish shell and ask user to switch to bash (bash-only syntax) # Detect fish shell and ask user to switch to bash (bash-only syntax)
if [ -n "${FISH_VERSION:-}" ] || case "$SHELL" in *fish*) ;; *) false ;; esac; then if [ -n "${FISH_VERSION:-}" ] || case "${SHELL:-}" in *fish*) ;; *) false ;; esac; then
echo "Warning: fish shell detected." echo "Warning: fish shell detected."
echo "This script is written for bash and may have syntax issues under fish." echo "This script is written for bash and may have syntax issues under fish."
echo "Please switch to bash first and re-run it, e.g.: bash $0" echo "Please switch to bash first and re-run it, e.g.: bash $0"
read -r -p "Press Enter to continue anyway, or Ctrl+C to cancel..." </dev/tty || true if [ -t 0 ] && [ -e /dev/tty ]; then
read -r -p "Press Enter to continue anyway, or Ctrl+C to cancel..." </dev/tty || true
fi
fi fi
skip_watermark=false skip_watermark=false
if [ "$1" = "--skip-watermark" ]; then if [ "${1:-}" = "--skip-watermark" ]; then
skip_watermark=true skip_watermark=true
fi fi
@@ -23,8 +27,8 @@ if [ "$skip_watermark" = false ]; then
|___/ |___/
--------------- bat Install Script --------------- --------------- bat Install Script ---------------
BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE UNDER THE GPL-2.0 LICENCE, THERE IS NO WARRANTY BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE UNDER THE GPL-2.0 LICENSE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. See the LICENCE for more detail FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. See the LICENSE for more detail
' '
fi fi
@@ -50,20 +54,24 @@ if ! command -v git >/dev/null || ! command -v curl >/dev/null || ! command -v w
fi fi
# Install bat # Install bat
fresh_bat_install=false
if ! command -v bat >/dev/null && ! command -v batcat >/dev/null; then if ! command -v bat >/dev/null && ! command -v batcat >/dev/null; then
if command -v pacman >/dev/null; then if command -v pacman >/dev/null; then
echo "pacman detected. Installing bat" echo "pacman detected. Installing bat"
sudo pacman -S --needed --noconfirm bat bat-extras if ! sudo pacman -S --needed --noconfirm bat bat-extras; then
echo "Failed to install bat. Install it manually, then re-run this script."
exit 1
fi
elif command -v apt >/dev/null; then elif command -v apt >/dev/null; then
echo "apt detected. Installing bat" echo "apt detected. Installing bat"
sudo apt install bat -y if ! sudo apt install bat -y; then
echo "Failed to install bat. Install it manually, then re-run this script."
exit 1
fi
else else
echo "No supported package manager found (pacman/apt)." echo "No supported package manager found (pacman/apt)."
echo "Please install bat manually, then re-run this script." echo "Please install bat manually, then re-run this script."
exit 1 exit 1
fi fi
fresh_bat_install=true
fi fi
# Debian/Ubuntu ship the binary as batcat, provide bat for the commands below # Debian/Ubuntu ship the binary as batcat, provide bat for the commands below
@@ -76,38 +84,45 @@ if ! command -v bat >/dev/null; then
exit 1 exit 1
fi fi
if [ "$fresh_bat_install" = true ]; then # Install the Catppuccin Mocha theme (downloaded and configured names must match)
# 1. Detect the actual active shell name (not just the login default) BAT_CONFIG_DIR="$(bat --config-dir)"
CURRENT_SHELL=$(basename -- "$SHELL") mkdir -p "$BAT_CONFIG_DIR/themes"
if [ ! -f "$BAT_CONFIG_DIR/themes/Catppuccin Mocha.tmTheme" ]; then
# 2. Source the correct configuration file safely if ! wget -q -O "$BAT_CONFIG_DIR/themes/Catppuccin Mocha.tmTheme" \
if [ "$CURRENT_SHELL" = "bash" ] && [ -f ~/.bashrc ]; then "https://github.com/catppuccin/bat/raw/main/themes/Catppuccin%20Mocha.tmTheme"; then
source ~/.bashrc echo "Failed to download the Catppuccin Mocha theme."
elif [ "$CURRENT_SHELL" = "zsh" ] && [ -f ~/.zshrc ]; then exit 1
source ~/.zshrc
elif [ "$CURRENT_SHELL" = "fish" ] && [ -f ~/.config/fish/config.fish ]; then
source ~/.config/fish/config.fish
else
echo "Unsupported or unreadable shell configuration: $CURRENT_SHELL"
fi fi
fi fi
# Install Catppuccin themes
source ~/.bashrc
mkdir -p "$(bat --config-dir)/themes"
wget -P "$(bat --config-dir)/themes" https://github.com/catppuccin/bat/raw/main/themes/Catppuccin%20Macchiato.tmTheme
bat cache --build bat cache --build
echo "--theme="Catppuccin Mocha"" >>"$(bat --config-dir)/themes" # Set the default theme in bat's config file (the themes/ directory only holds themes)
if ! grep -qxF -- '--theme="Catppuccin Mocha"' "$BAT_CONFIG_DIR/config" 2>/dev/null; then
printf '%s\n' '--theme="Catppuccin Mocha"' >>"$BAT_CONFIG_DIR/config"
fi
# Detect shell name via POSIX standard # Configure the MANPAGER for the user's actual login shell ($SHELL, not $0)
CURRENT_SHELL=$(basename "$0" 2>/dev/null || echo "$0") CURRENT_SHELL=$(basename -- "${SHELL:-/bin/bash}")
CURRENT_SHELL="${CURRENT_SHELL#-}"
case "$CURRENT_SHELL" in case "$CURRENT_SHELL" in
"bash") [ -f ~/.bashrc ] && echo "export MANPAGER="bat -plman"" >>~/.bashrc ;; bash)
"zsh") [ -f ~/.zshrc ] && echo "export MANPAGER="bat -plman"" >>~/.zshrc ;; if [ -f "$HOME/.bashrc" ] && ! grep -qxF 'export MANPAGER="bat -plman"' "$HOME/.bashrc"; then
"fish") exec fish -c "echo "export MANPAGER="bat -plman"" >> ~/.config/fish/config.fish" ;; printf '%s\n' 'export MANPAGER="bat -plman"' >>"$HOME/.bashrc"
*) echo "Unsupported shell: $CURRENT_SHELL" ;; fi
;;
zsh)
if [ -f "$HOME/.zshrc" ] && ! grep -qxF 'export MANPAGER="bat -plman"' "$HOME/.zshrc"; then
printf '%s\n' 'export MANPAGER="bat -plman"' >>"$HOME/.zshrc"
fi
;;
fish)
if [ -f "$HOME/.config/fish/config.fish" ] && ! grep -qxF 'set -gx MANPAGER "bat -plman"' "$HOME/.config/fish/config.fish"; then
printf '%s\n' 'set -gx MANPAGER "bat -plman"' >>"$HOME/.config/fish/config.fish"
fi
;;
*)
echo "Unsupported shell: $CURRENT_SHELL (skipping MANPAGER setup)"
;;
esac esac
echo "bat configured with the Catppuccin Mocha theme."