# everesh/wanikani-tui architecture

> Architecture diagram of the GitHub repository everesh/wanikani-tui, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/everesh/wanikani-tui
- Repository: https://github.com/everesh/wanikani-tui

## Overview

##### Project type and purpose
- From the file tree and naming conventions, this is a Ruby‐based command‐line full-stack application with a terminal UI (TUI).
- It interacts with the external WaniKani REST API, persists data to a local database, normalizes and queries that data, and presents review sessions in a curses-style interface.
- Key use case: fetch review data (vocabulary/kanji) from WaniKani, store it locally, and guide the user through spaced-repetition reviews in the terminal.

##### High-level architecture layers
- Presentation layer (the TUI)
- Application/Domain layer (business logic, review engine)
- Persistence layer (database abstractions)
- External integration (WaniKani API)
- Utility/Glue code (data normalization, CJK rendering bridge)

##### Main components
a. Entry point
- bin/tui.rb (starts the TUI application)
b. Terminal UI (TUI)
- tui/main.rb (bootstraps the UI)
- tui/components/* (menus, spinners)
- tui/windows/* (status lines, title screens)
- CJK renderer submodule (Python script + Ruby bridge: tui/cjk_renderer)
c. Core engine
- lib/engine.rb (coordinates workflows: fetch → persist → review)
- lib/review.rb (review session logic)
d. Persistence layer
- lib/db/database.rb (DB connection, schema init/drop via drop.sql & init.sql)
- lib/db/persister.rb (writes fetched data into DB)
- lib/db/common_query.rb (shared query logic)
e. Utilities
- lib/util/data_normalizer.rb (cleans/structures API data)
- lib/util/data_dir.rb (manages local data paths)
f. External service
- lib/wanikani_api.rb (HTTP client for WaniKani endpoints)
- tmp/mock_wanikani_api.rb (simulated API for testing)
g. Database

##### Relationships & data flow
- The TUI entry point invokes the Engine
- Engine calls the WaniKani API client → receives JSON → passes through DataNormalizer → hands off to Persister → writes into the local SQLite DB
- For a review session, Engine loads Review objects via CommonQuery → Review logic structures prompts and answers → TUI calls CJKRenderer (via bridge) when heavy CJK rendering is needed → displays to user
- User input flows back through the TUI into the Engine → marks items correct/incorrect → Persister updates review intervals in DB

##### Architectural patterns & design principles
- Layered architecture: UI ↔ Engine ↔ Persistence ↔ External API
- Single Responsibility: each lib/ folder contains components strictly for one concern (db, error types, util, api, review logic)
- Testable units: extensive test suite under test/ and tmp/ mock data
- Bridge pattern: Ruby-to-Python CJK renderer

##### Technologies & frameworks
- Ruby (core language)
- Bundler/Gems (Gemfile)
- SQLite (local DB via SQL scripts)
- Curses-style TUI (likely using Ruby’s io/console or similar)
- Python (for advanced CJK rendering)
- Rake (build/test tasks)

##### Final checklist
- [ ] All major modules/components labeled
- [ ] Notes on data formats and protocols (HTTP/JSON, SQL, STDIN/STDOUT for the renderer)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Presentation Layer
    subgraph "Presentation Layer"
        direction TB
        EP["bin/tui.rb"]:::presentation
        UI_Main["tui/main.rb"]:::presentation
        Menu["tui/components/main_menu.rb"]:::presentation
        Spinner["tui/components/spinner.rb"]:::presentation
        StatusLine["tui/windows/status_line.rb"]:::presentation
        TitleScreen["tui/windows/title_screen.rb"]:::presentation
        Bridge["tui/cjk_renderer/cjk_renderer_bridge.rb"]:::external
        PyRenderer["tui/cjk_renderer/cjk_renderer.py"]:::external
    end

    %% Application/Domain Layer
    subgraph "Domain Layer"
        direction TB
        Engine["lib/engine.rb"]:::domain
        Review["lib/review.rb"]:::domain
    end

    %% Persistence Layer
    subgraph "Persistence Layer"
        direction TB
        Persister["lib/db/persister.rb"]:::persistence
        CommonQuery["lib/db/common_query.rb"]:::persistence
        DatabaseCode["lib/db/database.rb"]:::persistence
        InitSQL["lib/db/init.sql"]:::persistence
        DropSQL["lib/db/drop.sql"]:::persistence
        DB["SQLite DB"]:::db
    end

    %% Utilities
    subgraph "Utilities"
        direction TB
        Normalizer["lib/util/data_normalizer.rb"]:::utility
        DataDir["lib/util/data_dir.rb"]:::utility
    end

    %% External Integration
    subgraph "External Integration"
        direction TB
        WaniKaniAPI["lib/wanikani_api.rb"]:::external
        MockAPI["tmp/mock_wanikani_api.rb"]:::external
        CloudAPI["WaniKani REST API"]:::external
    end

    %% Data Flows
    EP -->|"starts TUI"| UI_Main
    UI_Main -->|"initiates fetch"| Engine
    Engine -->|"HTTP request"| CloudAPI
    Engine -->|"HTTP request (test)"| MockAPI
    CloudAPI -->|"JSON response"| Normalizer
    MockAPI -->|"JSON response"| Normalizer
    Normalizer -->|"Normalized data"| Persister
    Persister -->|"SQL writes"| DB
    UI_Main -->|"start review"| Engine
    Engine -->|"review session"| Review
    Review -->|"uses query"| CommonQuery
    CommonQuery -->|"SQL queries"| DB
    UI_Main -->|"prompt/render"| Bridge
    Bridge -->|"STDIN/STDOUT"| PyRenderer
    PyRenderer -->|"Rendered CJK text"| Bridge
    Bridge -->|"text"| UI_Main
    Engine -->|"manages data dir"| DataDir

    %% Click Events
    click EP "https://github.com/everesh/wanikani-tui/blob/main/bin/tui.rb"
    click UI_Main "https://github.com/everesh/wanikani-tui/blob/main/tui/main.rb"
    click Menu "https://github.com/everesh/wanikani-tui/blob/main/tui/components/main_menu.rb"
    click Spinner "https://github.com/everesh/wanikani-tui/blob/main/tui/components/spinner.rb"
    click StatusLine "https://github.com/everesh/wanikani-tui/blob/main/tui/windows/status_line.rb"
    click TitleScreen "https://github.com/everesh/wanikani-tui/blob/main/tui/windows/title_screen.rb"
    click Bridge "https://github.com/everesh/wanikani-tui/blob/main/tui/cjk_renderer/cjk_renderer_bridge.rb"
    click PyRenderer "https://github.com/everesh/wanikani-tui/blob/main/tui/cjk_renderer/cjk_renderer.py"
    click Engine "https://github.com/everesh/wanikani-tui/blob/main/lib/engine.rb"
    click Review "https://github.com/everesh/wanikani-tui/blob/main/lib/review.rb"
    click Persister "https://github.com/everesh/wanikani-tui/blob/main/lib/db/persister.rb"
    click CommonQuery "https://github.com/everesh/wanikani-tui/blob/main/lib/db/common_query.rb"
    click DatabaseCode "https://github.com/everesh/wanikani-tui/blob/main/lib/db/database.rb"
    click InitSQL "https://github.com/everesh/wanikani-tui/blob/main/lib/db/init.sql"
    click DropSQL "https://github.com/everesh/wanikani-tui/blob/main/lib/db/drop.sql"
    click Normalizer "https://github.com/everesh/wanikani-tui/blob/main/lib/util/data_normalizer.rb"
    click DataDir "https://github.com/everesh/wanikani-tui/blob/main/lib/util/data_dir.rb"
    click WaniKaniAPI "https://github.com/everesh/wanikani-tui/blob/main/lib/wanikani_api.rb"
    click MockAPI "https://github.com/everesh/wanikani-tui/blob/main/tmp/mock_wanikani_api.rb"

    %% Styles
    classDef presentation fill:#cce5ff,stroke:#005cbf,color:#005cbf;
    classDef domain fill:#d4edda,stroke:#155724,color:#155724;
    classDef persistence fill:#fff3cd,stroke:#856404,color:#856404;
    classDef external fill:#e2e3e5,stroke:#6c757d,color:#6c757d;
    classDef utility fill:#f8d7da,stroke:#721c24,color:#721c24;
    classDef db fill:#ffe5b4,stroke:#cc8400,color:#cc8400,stroke-width:2px,stroke-dasharray: 5 5;
    class CloudAPI external;
```
