diff --git a/bat.sh b/bat.sh index c5a0056..e78280b 100755 --- a/bat.sh +++ b/bat.sh @@ -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" - read -r -p "Press Enter to continue anyway, or Ctrl+C to cancel..." /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."