Architecture

Module Overview

leaf is written in Rust (Edition 2021) and organized into these modules:

ModuleResponsibility
main.rsEntrypoint, CLI options, initial document or picker
cli.rsCommand-line argument definitions
config.rsConfiguration file loading and defaults
app/Application state (document, TOC, search, pickers, code blocks, links, goto line)
markdown/Parsing, rendering, LaTeX, Mermaid, tables, TOC extraction, frontmatter
render/TUI layout with ratatui (content, popups, status bar, TOC)
runtime/Event loop with mode-aware keyboard.rs and mouse.rs handlers
theme/Theme types, presets, resolution, and TOML deserialization
editor.rsEditor detection, classification (terminal vs GUI), and launch
completions.rsShell completion installation for Bash, Zsh, Fish, PowerShell
clipboard.rsCross-platform URL opening and OSC 52 clipboard copy
inline.rsNon-interactive stdout rendering (--inline)
terminal.rsRaw mode / alternate screen lifecycle
update.rsSelf-update with SHA256 verification

Key Dependencies

  • ratatui : terminal UI framework
  • crossterm : cross-platform terminal handling
  • pulldown-cmark : Markdown parsing
  • syntect : syntax highlighting
  • mmdflux : Mermaid diagram rendering
  • unicodeit : LaTeX-to-Unicode conversion

Event Loop

The main event loop in runtime/ runs at ~20 FPS (50ms ticks):

  1. Process pending picker queue and spawn loading threads
  2. Poll async file picker loading (non-blocking try_recv)
  3. Check file changes (watch mode)
  4. Handle keyboard/mouse input via mode-aware branching (help → pickers → goto line → search → normal)
  5. Redraw the TUI
  6. Handle terminal resize (recompute width, reparse Markdown)

Building

cargo build --release
cargo test