phranck dc34b0fa55 Refactor: Line-level diff rendering — only write changed terminal lines
Introduce FrameDiffWriter that stores the previous frame's output and
compares line-by-line on each render. Only lines that actually differ
are written to the terminal, reducing I/O by ~94% for mostly-static UI.

- Extract output line building from WindowGroup.renderScene into
  FrameDiffWriter.buildOutputLines() pure function
- WindowGroup.renderScene now returns FrameBuffer instead of writing
  directly to terminal
- RenderLoop (now final class) owns FrameDiffWriter for state tracking
- Content and status bar are diffed independently
- SIGWINCH invalidates diff cache for full repaint on resize
- SignalManager gains separate consumeResizeFlag() for resize detection
- 13 new tests for diff computation and output line building
2026-02-02 21:33:43 +01:00
2026-01-30 20:29:29 +01:00
2026-01-30 22:37:27 +01:00

CI Tests Swift 6.0 Platforms License

TUIkit Banner

TUIkit

A SwiftUI-like framework for building Terminal User Interfaces in Swift — no ncurses, no C dependencies, just pure Swift.

What is this?

TUIkit lets you build TUI apps using the same declarative syntax you already know from SwiftUI. Define your UI with View, compose views with VStack, HStack, and ZStack, style text with modifiers like .bold() and .foregroundColor(.red), and run it all in your terminal.

import TUIkit

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

struct ContentView: View {
    @State var count = 0
    
    var body: some View {
        VStack(spacing: 1) {
            Text("Hello, TUIkit!")
                .bold()
                .foregroundColor(.cyan)
            
            Text("Count: \(count)")
            
            Button("Increment") {
                count += 1
            }
        }
        .statusBarItems {
            StatusBarItem(shortcut: "q", label: "quit")
        }
    }
}

Features

Core

  • View protocol — the core building block, mirroring SwiftUI's View
  • @ViewBuilder — result builder for declarative view composition
  • @State — reactive state management with automatic re-rendering
  • @Environment — dependency injection for theme, focus manager, status bar
  • App protocol — app lifecycle with signal handling and run loop

Views & Components

  • Primitive viewsText, EmptyView, Spacer, Divider
  • Layout containersVStack, HStack, ZStack with alignment and spacing
  • InteractiveButton with focus states, Menu with keyboard navigation
  • ContainersAlert, Dialog, Panel, Box, Card
  • StatusBar — context-sensitive keyboard shortcuts
  • ForEach — iterate over collections, ranges, or Identifiable data

Styling

  • Text styling — bold, italic, underline, strikethrough, dim, blink, inverted
  • Full color support — ANSI colors, 256-color palette, 24-bit RGB, hex values, HSL
  • Theming — 7 predefined palettes (Green, Amber, White, Red, NCurses, Generated)
  • Border styles — rounded, line, double, thick, ASCII, and more

Advanced

  • Lifecycle modifiers.onAppear(), .onDisappear(), .task()
  • Storage@AppStorage, @SceneStorage with JSON backend
  • Preferences — bottom-up data flow with PreferenceKey
  • Focus system — Tab/Shift+Tab navigation between interactive elements

Run the Example App

swift run TUIkitExample

Press q or ESC to exit.

Installation

Add TUIkit to your Package.swift:

dependencies: [
    .package(url: "https://github.com/phranck/TUIkit.git", branch: "main")
]

Then add it to your target:

.target(
    name: "YourApp",
    dependencies: ["TUIkit"]
)

Theming

TUIkit includes predefined palettes inspired by classic terminals:

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        .palette(GreenPalette())  // Classic green terminal
    }
}

Available palettes:

  • GreenPalette — Classic green CRT (default)
  • AmberPalette — Amber monochrome
  • WhitePalette — White on black
  • RedPalette — Red terminal
  • NCursesPalette — ncurses-inspired colors
  • GeneratedPalette — Algorithmic palette from a single hue

Architecture

  • No singletons for state — All state flows through the Environment system
  • Pure ANSI rendering — No ncurses or other C dependencies
  • Linux compatible — Works on macOS and Linux (XDG paths supported)
  • Value types — Views are structs, just like SwiftUI

Project Structure

Sources/
├── TUIkit/
│   ├── App/              App, Scene, WindowGroup
│   ├── Core/             View, ViewBuilder, State, Environment, Color, Theme
│   ├── Modifiers/        Border, Frame, Padding, Overlay, Lifecycle
│   ├── Rendering/        Terminal, ANSIRenderer, ViewRenderer, FrameBuffer
│   └── Views/            Text, Stacks, Button, Menu, Alert, StatusBar, ...
└── TUIkitExample/        Example app (executable target)

Tests/
└── TUIkitTests/          424 tests across 73 test suites

Requirements

  • Swift 6.0+
  • macOS 10.15+ or Linux

Developer Notes

  • Tests use Swift Testing (@Test, #expect) — run with swift test
  • All 424 tests run in parallel
  • The Terminal class handles raw mode and cursor control via POSIX termios

License

This repository has been published under the CC-BY-NC-SA 4.0 license.

Languages
Swift 84.5%
C 14.1%
Shell 1.4%