# thewtex/tmux-mem-cpu-load architecture

> Architecture diagram of the GitHub repository thewtex/tmux-mem-cpu-load, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-11-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/thewtex/tmux-mem-cpu-load
- Repository: https://github.com/thewtex/tmux-mem-cpu-load

## Overview

##### Project type and purpose
- Type: A small native command-line monitoring tool written in modern C++.
- Purpose: Gather CPU, memory, and load-average metrics from the host OS; render them as a textual bar graph and statistics line for embedding in tmux status lines.

##### File structure
- Top level:
- builder/         → Shell scripts for CMake and Makefile–based builds
- common/          → Core logic and abstractions shared across OS targets
- freebsd/, linux/, netbsd/, openbsd/, osx/, windows/
→ OS-specific implementations for CPU, memory, load
- CMakeLists.txt  → CMake build configuration
- Key patterns:
- “common” holds interfaces, utility code, main entry point
- Each OS directory overrides CPU/memory functions via polymorphic linkage
- builder scripts automate cmake/make sequences
- No frontend/backend split – it’s a single binary that interacts with OS services and outputs to stdout.

##### Extract architecture details from the README
- Outputs a combined line: memory usage, CPU bar graph, CPU percent, 1/5/15-minute load averages
- Integrates with tmux via plugin scripts (tmux and zsh plugins), but the core is the CLI binary.

##### Components & interactions
a. Main components:
1. CLI Argument Parser
2. MetricCollector
– CPU Module (abstract interface)
– Memory Module (abstract interface)
– LoadAverage Module (via common/getsysctl or OS syscalls)
3. OS Adapter Layer
– One implementation per OS directory (linux/cpu.cc, windows/memory.cc, etc.)
4. Graph & Formatter
– LUT generator (common/generate-luts.py + common/luts.h)
– GraphRenderer (common/graph.cc)
– OutputFormatter (common/main.cc)
5. Stdout / tmux status line
b. Relationships / data flow:
- CLI Parser reads flags → invokes MetricCollector
- MetricCollector calls OS Adapter Layer → returns raw metrics
- GraphRenderer + LUTs → create text bars
- tmux captures stdout → displays in status line
c. Architectural patterns:
- Strategy/Interface pattern for OS-specific modules
- Modular separation of data gathering vs. rendering vs. CLI logic
d. Key technologies:
- C++11
- CMake build system (+ optional Makefiles via builder/)
- Shell scripts for automated builds
- tmux plugin integration (external)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Build System
    subgraph "Build System"
        direction TB
        BuildScripts["Shell Build Scripts"]:::build
        CMake["CMakeLists.txt"]:::build
        BuildScripts -->|"invoke cmake"| CMake
        CMake -->|"generate binary"| Binary["tmux-mem-cpu-load (Executable)"]:::build
    end

    %% Core Modules
    subgraph "Core Modules" 
        direction TB
        Parser["CLI Argument Parser"]:::core
        Collector["MetricCollector"]:::core
        CPUInterface["CPU Module (interface)"]:::core
        MemoryInterface["Memory Module (interface)"]:::core
        LoadInterface["LoadAverage Module (interface)"]:::core
        LUTGen["LUT Generator Script"]:::core
        LUTDef["LUT Definitions"]:::core
        GraphRenderer["GraphRenderer"]:::core
        OutputFormatter["OutputFormatter"]:::core

        Parser -->|"parse flags"| Collector
        Collector -->|"uses interface"| CPUInterface
        Collector -->|"uses interface"| MemoryInterface
        Collector -->|"uses interface"| LoadInterface
        GraphRenderer -->|"uses LUTs"| LUTGen
        GraphRenderer -->|"uses LUTs"| LUTDef
        Collector -->|"fetch metrics"| GraphRenderer
        GraphRenderer -->|"render bars"| OutputFormatter
        Collector -->|"send metrics"| OutputFormatter
        OutputFormatter -->|"write ANSI to stdout"| Stdout["Stdout"]:::core
        Binary -->|"executes"| Parser
    end

    %% OS Adapter Layer
    subgraph "OS Adapter Layer" 
        direction TB
        FreeBSDAdapter["FreeBSD Adapter"]:::os
        LinuxAdapter["Linux Adapter"]:::os
        NetBSDAdapter["NetBSD Adapter"]:::os
        OpenBSDAdapter["OpenBSD Adapter"]:::os
        MacOSAdapter["macOS Adapter"]:::os
        WindowsAdapter["Windows Adapter"]:::os

        CPUInterface -->|“link at build”| FreeBSDAdapter
        CPUInterface -->|“link at build”| LinuxAdapter
        CPUInterface -->|“link at build”| NetBSDAdapter
        CPUInterface -->|“link at build”| OpenBSDAdapter
        CPUInterface -->|“link at build”| MacOSAdapter
        CPUInterface -->|“link at build”| WindowsAdapter

        MemoryInterface -->|“link at build”| FreeBSDAdapter
        MemoryInterface -->|“link at build”| LinuxAdapter
        MemoryInterface -->|“link at build”| NetBSDAdapter
        MemoryInterface -->|“link at build”| OpenBSDAdapter
        MemoryInterface -->|“link at build”| MacOSAdapter
        MemoryInterface -->|“link at build”| WindowsAdapter

        LoadInterface -->|“link at build”| WindowsAdapter
    end

    %% External Integration
    subgraph "External Integration"
        direction TB
        Stdout -->|"captured by tmux"| TmuxPlugin["tmux plugin script"]:::ext
        Stdout -->|"captured by zsh"| ZshPlugin["zsh plugin script"]:::ext
    end

    classDef core fill:#cce5ff,stroke:#007bff
    classDef os fill:#d4edda,stroke:#28a745
    classDef build fill:#fff3cd,stroke:#ffc107
    classDef ext fill:#e2e3e5,stroke:#6c757d

    %% Click Events
    click Parser "https://github.com/thewtex/tmux-mem-cpu-load/blob/master/common/main.cc"
    click CPUInterface "https://github.com/thewtex/tmux-mem-cpu-load/blob/master/common/cpu.h"
    click MemoryInterface "https://github.com/thewtex/tmux-mem-cpu-load/blob/master/common/memory.h"
    click LoadInterface "https://github.com/thewtex/tmux-mem-cpu-load/blob/master/common/load.h"
    click LUTGen "https://github.com/thewtex/tmux-mem-cpu-load/blob/master/common/generate-luts.py"
    click LUTDef "https://github.com/thewtex/tmux-mem-cpu-load/blob/master/common/luts.h"
    click GraphRenderer "https://github.com/thewtex/tmux-mem-cpu-load/blob/master/common/graph.cc"
    click OutputFormatter "https://github.com/thewtex/tmux-mem-cpu-load/blob/master/common/powerline.cc"
    click CMake "https://github.com/thewtex/tmux-mem-cpu-load/blob/master/CMakeLists.txt"
    click BuildScripts "https://github.com/thewtex/tmux-mem-cpu-load/tree/master/builder/"
    click FreeBSDAdapter "https://github.com/thewtex/tmux-mem-cpu-load/blob/master/freebsd/cpu.cc"
    click LinuxAdapter "https://github.com/thewtex/tmux-mem-cpu-load/blob/master/linux/cpu.cc"
    click NetBSDAdapter "https://github.com/thewtex/tmux-mem-cpu-load/blob/master/netbsd/cpu.cc"
    click OpenBSDAdapter "https://github.com/thewtex/tmux-mem-cpu-load/blob/master/openbsd/cpu.cc"
    click MacOSAdapter "https://github.com/thewtex/tmux-mem-cpu-load/blob/master/osx/cpu.cc"
    click WindowsAdapter "https://github.com/thewtex/tmux-mem-cpu-load/blob/master/windows/cpu.cc"
    click TmuxPlugin "https://github.com/thewtex/tmux-mem-cpu-load/blob/master/tmux-mem-cpu-load.plugin.tmux"
    click ZshPlugin "https://github.com/thewtex/tmux-mem-cpu-load/blob/master/tmux-mem-cpu-load.plugin.zsh"
```
