This document describes the high-level architecture and core data flow of the bat system. It explains how the main components interact to process input files, apply syntax highlighting, and generate formatted output. bat is designed both as a feature-rich CLI tool and a reusable Rust library via its PrettyPrinter API.
bat follows a pipeline architecture where input (files, stdin, or byte buffers) is processed through a series of components before being displayed. The system maintains a clean separation between configuration, asset management (syntaxes/themes), and the printing logic.
Sources: src/bin/bat/main.rs23-43 src/lib.rs1-9 src/controller.rs22-37 src/assets.rs33-40 src/printer.rs83-102
bat is structured to serve two primary audiences:
src/bin/bat/ handles argument parsing via clap, environment variable resolution, and configuration file loading. For details, see Application Entry Point and Configuration.PrettyPrinter struct in src/pretty_printer.rs provides a builder-pattern API for Rust developers to integrate bat's highlighting into their own applications. For details, see API and Library Usage.Both interfaces eventually initialize a Config struct and pass it to the Controller.
Sources: src/pretty_printer.rs38-48 src/bin/bat/main.rs23-43 src/lib.rs1-9
The entry point parses command-line arguments and merges them with configuration files and environment variables (like BAT_THEME or BAT_STYLE). This results in a unified Config object that dictates the behavior of the entire pipeline.
App (src/bin/bat/app.rs) and Config (src/config.rs).The Controller orchestrates the flow. It iterates through the provided inputs, determines the correct syntax to use, and manages the lifecycle of the Printer. It also handles the logic for line ranges (e.g., bat -r 5:10).
Controller (src/controller.rs22-27).bat abstracts input sources (files, stdin, or raw bytes) into an Input type. When opened, it performs content inspection to detect binary data, encoding (UTF-8/UTF-16), and strips Byte Order Marks (BOM).
Input (src/input.rs94-98) and InputReader (src/input.rs210-212).The HighlightingAssets struct manages the syntect SyntaxSet and ThemeSet. It handles loading these from a binary cache for performance or from custom user directories. It also performs the complex task of mapping a file path or first-line content to a specific syntax.
HighlightingAssets (src/assets.rs33-39) and SyntaxMapping (src/syntax_mapping/mod.rs).The Printer trait defines how headers, footers, and individual lines are rendered.
InteractivePrinter: Handles full terminal formatting, including line numbers, Git change markers, and grid borders.SimplePrinter: A lightweight implementation for "loop-through" mode (e.g., when piping bat to another command).Printer trait (src/printer.rs83-102), InteractivePrinter (src/printer.rs201-216), and SimplePrinter (src/printer.rs104-107).The following diagram bridges the natural language concepts to the specific code entities involved in a single execution of bat.
Sources: src/controller.rs112-136 src/controller.rs141-190 src/printer.rs83-102 src/output.rs74-89
For deeper dives into these components, please refer to the child pages listed in the sidebar.
Refresh this wiki