# sharkdp/bat architecture

> Architecture diagram of the GitHub repository sharkdp/bat, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sharkdp/bat
- Repository: https://github.com/sharkdp/bat
- Video tour (about a minute): https://gitdiagram.com/sharkdp/bat/video

## Overview

bat is a terminal file viewer and cat replacement. A user invokes the CLI with files or piped input; the application configures a controller, which opens inputs, optionally preprocesses them, and prints highlighted or plain output. Output may include Git change markers, non-printable notation, line selection and decorations, and can go to stdout or a pager. The README documents these capabilities; some subsystem links below rely on that documentation because their implementation paths were not sampled.

Main entry points: [`src/controller.rs`](https://github.com/sharkdp/bat/blob/master/src/controller.rs) (Run controller), [`src/printer.rs`](https://github.com/sharkdp/bat/blob/master/src/printer.rs) (Content printer), [`src/bin/bat/main.rs`](https://github.com/sharkdp/bat/blob/master/src/bin/bat/main.rs) (CLI entry), [`src/output.rs`](https://github.com/sharkdp/bat/blob/master/src/output.rs) (Output routing)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_cli["CLI and orchestration"]
  node_main["CLI entry<br/>[main.rs]"]
  node_app["CLI application<br/>[app.rs]"]
  node_config["Configuration<br/>[config.rs]"]
  node_controller["Run controller<br/>[controller.rs]"]
end

subgraph group_content["Input and highlighting"]
  node_input["Input reader<br/>[input.rs]"]
  node_lessopen["LESSOPEN adapter<br/>[lessopen.rs]"]
  node_assets["Highlight assets<br/>[assets.rs]"]
  node_syntax["Syntax mapping<br/>[syntax_mapping.rs]"]
end

subgraph group_render["Rendering and features"]
  node_printer["Content printer<br/>[printer.rs]"]
  node_preprocessor["Text preprocessing<br/>[preprocessor.rs]"]
  node_diff["Git changes<br/>[diff.rs]"]
  node_ranges["Line ranges<br/>[line_range.rs]"]
  node_style["Style components<br/>[style.rs]"]
end

subgraph group_delivery["Output and paging"]
  node_output["Output routing<br/>[output.rs]"]
  node_pager["Pager selection<br/>[pager.rs]"]
end

node_user(("User"))
node_terminal(("Terminal"))
node_git["Git"]

node_user -->|"invokes"| node_main
node_main -->|"dispatches"| node_app
node_main -->|"runs"| node_controller
node_app -->|"configures"| node_config
node_controller -->|"uses"| node_config
node_controller -->|"opens"| node_input
node_controller -.->|"optionally opens"| node_lessopen
node_controller -->|"prints"| node_printer
node_controller -->|"gets changes"| node_diff
node_assets -->|"maps syntax"| node_syntax
node_printer -->|"selects highlighting"| node_assets
node_printer -->|"sanitizes text"| node_preprocessor
node_printer -->|"limits lines"| node_ranges
node_printer -->|"formats components"| node_style
node_printer -->|"renders changes"| node_diff
node_controller -->|"writes output"| node_output
node_output -->|"selects pager"| node_pager
node_output -->|"writes to"| node_terminal
node_diff -.->|"reads index"| node_git

click node_main "https://github.com/sharkdp/bat/blob/master/src/bin/bat/main.rs"
click node_app "https://github.com/sharkdp/bat/blob/master/src/bin/bat/app.rs"
click node_config "https://github.com/sharkdp/bat/blob/master/src/config.rs"
click node_controller "https://github.com/sharkdp/bat/blob/master/src/controller.rs"
click node_input "https://github.com/sharkdp/bat/blob/master/src/input.rs"
click node_lessopen "https://github.com/sharkdp/bat/blob/master/src/lessopen.rs"
click node_assets "https://github.com/sharkdp/bat/blob/master/src/assets.rs"
click node_syntax "https://github.com/sharkdp/bat/blob/master/src/syntax_mapping.rs"
click node_printer "https://github.com/sharkdp/bat/blob/master/src/printer.rs"
click node_preprocessor "https://github.com/sharkdp/bat/blob/master/src/preprocessor.rs"
click node_diff "https://github.com/sharkdp/bat/blob/master/src/diff.rs"
click node_ranges "https://github.com/sharkdp/bat/blob/master/src/line_range.rs"
click node_style "https://github.com/sharkdp/bat/blob/master/src/style.rs"
click node_output "https://github.com/sharkdp/bat/blob/master/src/output.rs"
click node_pager "https://github.com/sharkdp/bat/blob/master/src/pager.rs"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_app,node_config,node_controller,node_user toneBlue
class node_input,node_lessopen,node_assets,node_syntax toneAmber
class node_printer,node_preprocessor,node_diff,node_ranges,node_style toneMint
class node_output,node_pager toneRose
class node_terminal,node_git toneIndigo
```

## Components

### CLI and orchestration

- [CLI entry](https://github.com/sharkdp/bat/blob/master/src/bin/bat/main.rs): `src/bin/bat/main.rs`
- [CLI application](https://github.com/sharkdp/bat/blob/master/src/bin/bat/app.rs): `src/bin/bat/app.rs`
- [Configuration](https://github.com/sharkdp/bat/blob/master/src/config.rs): `src/config.rs`
- [Run controller](https://github.com/sharkdp/bat/blob/master/src/controller.rs): `src/controller.rs`

### Input and highlighting

- [Input reader](https://github.com/sharkdp/bat/blob/master/src/input.rs): `src/input.rs`
- [LESSOPEN adapter](https://github.com/sharkdp/bat/blob/master/src/lessopen.rs): `src/lessopen.rs`
- [Highlight assets](https://github.com/sharkdp/bat/blob/master/src/assets.rs): `src/assets.rs`
- [Syntax mapping](https://github.com/sharkdp/bat/blob/master/src/syntax_mapping.rs): `src/syntax_mapping.rs`

### Rendering and features

- [Content printer](https://github.com/sharkdp/bat/blob/master/src/printer.rs): `src/printer.rs`
- [Text preprocessing](https://github.com/sharkdp/bat/blob/master/src/preprocessor.rs): `src/preprocessor.rs`
- [Git changes](https://github.com/sharkdp/bat/blob/master/src/diff.rs): `src/diff.rs`
- [Line ranges](https://github.com/sharkdp/bat/blob/master/src/line_range.rs): `src/line_range.rs`
- [Style components](https://github.com/sharkdp/bat/blob/master/src/style.rs): `src/style.rs`

### Output and paging

- [Output routing](https://github.com/sharkdp/bat/blob/master/src/output.rs): `src/output.rs`
- [Pager selection](https://github.com/sharkdp/bat/blob/master/src/pager.rs): `src/pager.rs`

### Other components

- **User**
- **Terminal**
- **Git**

## Connections

- User → CLI entry: invokes (evidence: `README.md`)
- CLI entry → CLI application: dispatches (evidence: `src/bin/bat/main.rs`)
- CLI entry → Run controller: runs (evidence: `src/bin/bat/main.rs`)
- CLI application → Configuration: configures (evidence: `src/bin/bat/app.rs`)
- Run controller → Configuration: uses (evidence: `src/controller.rs`)
- Run controller → Input reader: opens (evidence: `src/controller.rs`)
- Run controller → LESSOPEN adapter: optionally opens (evidence: `src/controller.rs`)
- Run controller → Content printer: prints (evidence: `src/controller.rs`)
- Run controller → Git changes: gets changes (evidence: `src/controller.rs`)
- Highlight assets → Syntax mapping: maps syntax (evidence: `src/assets.rs`)
- Content printer → Highlight assets: selects highlighting (evidence: `src/printer.rs`)
- Content printer → Text preprocessing: sanitizes text (evidence: `src/printer.rs`)
- Content printer → Line ranges: limits lines (evidence: `src/printer.rs`)
- Content printer → Style components: formats components (evidence: `src/printer.rs`)
- Content printer → Git changes: renders changes (evidence: `src/printer.rs`)
- Run controller → Output routing: writes output (evidence: `src/controller.rs`)
- Output routing → Pager selection: selects pager (evidence: `src/output.rs`)
- Output routing → Terminal: writes to (evidence: `README.md`)
- Git changes → Git: reads index (evidence: `README.md`)
