mirror of
https://github.com/phranck/TUIkit.git
synced 2026-05-21 09:50:35 +00:00
3ec23004b93e9cc505fdcb86cd76cfd8d3a642e3
- Replace #if os(Linux) with #if canImport(Glibc/Musl/Darwin) import guards - Add TermFlag typealias to fix termios flag width mismatch (UInt vs UInt32) - Add platform import guard to SignalManager for POSIX signal visibility - Replace DispatchQueue.global with Task.detached in AppStorage - Add XDG_CONFIG_HOME fallback for config directory resolution - Add Linux Build & Test CI job (swift:6.0 container on ubuntu-latest)
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
Viewprotocol — the core building block, mirroring SwiftUI'sView@ViewBuilder— result builder for declarative view composition@State— reactive state management with automatic re-rendering@Environment— dependency injection for theme, focus manager, status barAppprotocol — app lifecycle with signal handling and run loop
Views & Components
- Primitive views —
Text,EmptyView,Spacer,Divider - Layout containers —
VStack,HStack,ZStackwith alignment and spacing - Interactive —
Buttonwith focus states,Menuwith keyboard navigation - Containers —
Alert,Dialog,Panel,Box,Card StatusBar— context-sensitive keyboard shortcutsForEach— iterate over collections, ranges, orIdentifiabledata
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 — 8 predefined themes (Phosphor variants, ncurses, Dark/Light)
- Border styles — rounded, line, double, thick, ASCII, and more
Advanced
- Lifecycle modifiers —
.onAppear(),.onDisappear(),.task() - Storage —
@AppStorage,@SceneStoragewith 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 8 predefined themes inspired by classic terminals:
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
.environment(\.theme, GreenPhosphorTheme()) // Classic green terminal
}
}
Available themes:
DefaultTheme— Standard ANSI colorsGreenPhosphorTheme— Classic green CRTAmberPhosphorTheme— Amber monochromeWhitePhosphorTheme— White on blackRedPhosphorTheme— Red terminalNCursesTheme— ncurses-inspired colorsDarkTheme— Modern dark themeLightTheme— Light background
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/ 181 tests across 27 test suites
Requirements
- Swift 6.0+
- macOS 10.15+ or Linux
Developer Notes
- Tests use Swift Testing (
@Test,#expect) — run withswift test - All 181 tests run in parallel
- The
Terminalclass handles raw mode and cursor control via POSIXtermios
License
MIT
Languages
Swift
84.5%
C
14.1%
Shell
1.4%
