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
+50 -35
View File
@@ -1,15 +1,19 @@
#!/usr/bin/bash
set -euo pipefail
# 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 "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"
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
skip_watermark=false
if [ "$1" = "--skip-watermark" ]; then
if [ "${1:-}" = "--skip-watermark" ]; then
skip_watermark=true
fi
@@ -23,8 +27,8 @@ if [ "$skip_watermark" = false ]; then
|___/
--------------- bat Install Script ---------------
BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE UNDER THE GPL-2.0 LICENCE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. See the LICENCE for more detail
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 LICENSE for more detail
'
fi
@@ -50,20 +54,24 @@ if ! command -v git >/dev/null || ! command -v curl >/dev/null || ! command -v w
fi
# Install bat
fresh_bat_install=false
if ! command -v bat >/dev/null && ! command -v batcat >/dev/null; then
if command -v pacman >/dev/null; then
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
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
echo "No supported package manager found (pacman/apt)."
echo "Please install bat manually, then re-run this script."
exit 1
fi
fresh_bat_install=true
fi
# 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
fi
if [ "$fresh_bat_install" = true ]; then
# 1. Detect the actual active shell name (not just the login default)
CURRENT_SHELL=$(basename -- "$SHELL")
# 2. Source the correct configuration file safely
if [ "$CURRENT_SHELL" = "bash" ] && [ -f ~/.bashrc ]; then
source ~/.bashrc
elif [ "$CURRENT_SHELL" = "zsh" ] && [ -f ~/.zshrc ]; then
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"
# Install the Catppuccin Mocha theme (downloaded and configured names must match)
BAT_CONFIG_DIR="$(bat --config-dir)"
mkdir -p "$BAT_CONFIG_DIR/themes"
if [ ! -f "$BAT_CONFIG_DIR/themes/Catppuccin Mocha.tmTheme" ]; then
if ! wget -q -O "$BAT_CONFIG_DIR/themes/Catppuccin Mocha.tmTheme" \
"https://github.com/catppuccin/bat/raw/main/themes/Catppuccin%20Mocha.tmTheme"; then
echo "Failed to download the Catppuccin Mocha theme."
exit 1
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
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
CURRENT_SHELL=$(basename "$0" 2>/dev/null || echo "$0")
CURRENT_SHELL="${CURRENT_SHELL#-}"
# Configure the MANPAGER for the user's actual login shell ($SHELL, not $0)
CURRENT_SHELL=$(basename -- "${SHELL:-/bin/bash}")
case "$CURRENT_SHELL" in
"bash") [ -f ~/.bashrc ] && echo "export MANPAGER="bat -plman"" >>~/.bashrc ;;
"zsh") [ -f ~/.zshrc ] && echo "export MANPAGER="bat -plman"" >>~/.zshrc ;;
"fish") exec fish -c "echo "export MANPAGER="bat -plman"" >> ~/.config/fish/config.fish" ;;
*) echo "Unsupported shell: $CURRENT_SHELL" ;;
bash)
if [ -f "$HOME/.bashrc" ] && ! grep -qxF 'export MANPAGER="bat -plman"' "$HOME/.bashrc"; then
printf '%s\n' 'export MANPAGER="bat -plman"' >>"$HOME/.bashrc"
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
echo "bat configured with the Catppuccin Mocha theme."