#!/bin/bash # bman - view man pages in browser set -o pipefail show_help() { cat </dev/null 2>&1; then echo "Error: required command '$cmd' not found." >&2 exit 1 fi done # Parse options theme="default" while [[ $# -gt 0 ]]; do case "$1" in --dark) theme="dark" shift ;; --modern) theme="modern" shift ;; -h|--help) show_help exit 0 ;; --) # end of options shift break ;; -*) echo "Error: unknown option $1" >&2 show_help exit 1 ;; *) break # non-option argument ;; esac done # If no man pages given, try fzf interactive mode if [ $# -eq 0 ]; then # Check if fzf is available if ! command -v fzf >/dev/null 2>&1; then echo "Error: fzf is not installed. Please install it (e.g., 'sudo apt install fzf' or from https://github.com/junegunn/fzf)." >&2 show_help exit 1 fi # Generate list of all man page names (strip section numbers) mapfile -t pages < <( man -k . 2>/dev/null | awk '{print $1}' | sed 's/([0-9])//' | sort -u | fzf --height=80% --marker="*" --multi --bind='tab:toggle' --reverse --prompt="Select man page(s) (TAB to toggle): " ) # If user cancels (no selection), exit gracefully if [ ${#pages[@]} -eq 0 ]; then echo "No selection made. Exiting." exit 0 fi # Replace positional parameters with the selected pages set -- "${pages[@]}" fi # Persistent cache directory for HTML files CACHE_DIR="$HOME/.cache/bman" mkdir -p "$CACHE_DIR" || { echo "Error: failed to create cache directory" >&2; exit 1; } chmod 700 "$CACHE_DIR" # Clean up HTML files older than 1 minute find "$CACHE_DIR" -name "*.html" -type f -mmin +1 -delete 2>/dev/null # Build CSS based on theme (collapse newlines to one line for sed substitution) case "$theme" in dark) css_raw=$(cat <<'CSS' CSS ) ;; modern) css_raw=$(cat <<'CSS' CSS ) ;; default|*) css_raw=$(cat <<'CSS' CSS ) ;; esac # Collapse newlines to make the CSS a single line for sed substitution css="$(echo "$css_raw" | tr '\n' ' ')" # Process each man page for man in "$@"; do # Create a temporary file in the cache directory html_file=$(mktemp -p "$CACHE_DIR" --suffix=".html" 2>/dev/null) || { echo "Warning: failed to create temporary file for '$man', skipping." >&2 continue } # Generate HTML version of the man page if ! man -Thtml "$man" > "$html_file.tmp" 2>/dev/null; then echo "Warning: man page for '$man' not found, skipping." >&2 rm -f "$html_file" "$html_file.tmp" 2>/dev/null continue fi # Insert the custom CSS before sed "s|^*|${css}|g" < "$html_file.tmp" > "$html_file" rm -f "$html_file.tmp" # Open the HTML file in the default browser xdg-open "$html_file" 2>/dev/null & done