
Xterm/VT100 Terminal emulator library in Swift
SwiftTerm is a VT100/Xterm terminal emulator library for Swift applications that can be
以下正文同步自 Hacker News 最新,版权归原站所有,已转换为易读排版。
SwiftTerm is a VT100/Xterm terminal emulator library for Swift applications that can be embedded into macOS, iOS applications, text-based, headless applications or other custom scenarios. It has been used in several commercially available SSH clients, including Secure Shellfish, La Terminal and CodeEdit
This README and the API documentation describe the new SwiftTerm 2.0 API. To use the SwiftTerm 1.x API, use the v1.x branch or a tagged 1.x release.
If you use SwiftTerm 1.0, read the SwiftTerm 2.0 migration guide.
This repository contains both a terminal emulator engine that is UI agnostic, as well as front-ends for this engine for iOS using UIKit, and macOS using AppKit. A curses-based terminal emulator (to emulate an xterm inside a console application) is available as part of the TermKit library.
Discord server
GitHub Discussions
GitHub Issues
Companion Apps
Tecolot is a MacOS Terminal.app replacement.
SwiftTermApp builds an actual iOS app that uses this library and is more complete than the testing apps in this module and provides a proper configuration UI. It is a proof of concept for what you would need to do.
Pane is a terminal multiplexor, similar to tmux.
Sample Code There are a couple of minimal sample apps for Mac and iOS showing how to use the library inside the TerminalApp directory.
This is a port of my original XtermSharp, which was itself based on xterm.js. At this point, I consider SwiftTerm to be a more advanced terminal emulator than both of those (modulo Selection/Accessibility) as it handles UTF, Unicode and grapheme clusters better than those and has a more complete coverage of terminal emulation. XtermSharp is generally attempting to keep up, but has lagged behind.
Plenty of test cases have been extracted from xterm.js and Ghostty and this also relies extensively on esctest to ensure compatibility.
Pretty decent terminal emulation, on or better than XtermSharp and xterm.js (and more comprehensive in many ways)
Unicode rendering (including Emoji, and combining characters and emoji)
Bidirectional text (Arabic, Hebrew) following the terminal-wg BiDi recommendation, with Arabic contextual shaping
Reusable and pluggable engine allows multiple user interfaces to be built on top of it:
Bundled MacOS and iOS
Bundled Headless terminal.
TermKit contains a terminal-over-a-terminal
Pane implements a terminal multiplexor
Selection engine (with macOS support in the view)
Search support with a built-in macOS find bar and programmable search APIs
Supports colors (ANSI, 256, TrueColor)
Supports text attributes including bold, italic, underline, strikethrough, and dim/faint (SGR 2)
Supports mouse events
Supports terminal resizing operations (controlled by remote host, or locally)
Hyperlinks in terminal output
Local process and SSH connection support (some assembly required for the last one)
Proper CoreText rendering can munch through the hardened Unicode test suites.
Graphics support:
Sixel (Use img2sixel to test)
iTerm2-style graphic rendering (Use imgcat to test)
Kitty graphics (Use kittyimg to test)
Terminal session recording and playback with termcast
Thread-safe Terminal instances
Fuzzed and abused
Optional GPU-accelerated rendering via Metal (macOS, iOS, visionOS)
Seems pretty fast to me
Image formats
SwiftTerm supports these image data formats:
For iTerm2 images, the Apple views use the system image decoder. Other image formats that the target platform can decode can also work, but PNG and JPEG are the tested formats.
The SwiftTerm library itself contains the source code for both the engine and the front-ends. The front-ends are conditionally compiled based on the target platform.
SwiftTerm has a Foundation-free, headless core for Embedded Swift. Select it with the Embedded package trait. It omits the Apple views, local-process host, image decoders, and Kitty Clipboard. Embedded builds require Swift 6.3 or later. The portable host APIs are documented in the DocC guide. See the Embedded Swift build and integration guide for trait, compiler, and target settings.
SwiftTerm supplies a browser and Node.js terminal engine with ABI version 1, copied render snapshots, generated reply bytes, and host-event queues. The TypeScript package loads either the Full or Embedded WASM reactor. The host supplies a renderer and PTY transport. The Full build supports PNG, Kitty, Sixel, iTerm graphics, and Kitty Clipboard; Embedded keeps terminal input, selection, scrollback, and bracketed paste while omitting graphics and clipboard support. A Canvas 2D example is included.
See the WebAssembly build and browser guide for the pinned compiler/SDK pair, build commands, API use, limits, and tests.
The Web terminal sample is a SwiftPM package with a Hummingbird server, WebSocket transport, and a local shell PTY. Run TerminalApp/WebTerminal/run.sh to start it on localhost.
The engine is in this directory, while code for macOS lives under Mac, and code for iOS, lives under iOS. Given that those two share a lot of common traits, the shared code is under Apple.
SwiftTerm uses the Swift Package Manager for its build, and you can add the library to your project by using the url for this project or a fork of it.
The macOS AppKit NSView implementation TerminalView is a reusable NSView control that can be connected to any source by implementing the TerminalViewDelegate.
I anticipate that a common scenario will be to host a local Unix command, so I have included LocalProcessTerminalView which is an implementation that connects the TerminalView to a Unix pseudo-terminal and runs a command there.
There is an equivalent UIKit UIView implementation for TerminalView which like its NSView companion is an embeddable and reusable view that can be connected to your application by implementing the same TerminalViewDelegate. Unlike the NSView case running on a Mac, where a common scenario will be to run local commands, given that iOS does not offer access to processes, the most common scenario will be to wire up this terminal to a remote host. And the safest way of connecting to a remote system is with SSH.
The iOS and UIKit code share a lot of the code, that code lives under the Apple directory.
Both AppKit and UIKit TerminalView expose linkReporting:
linkReporting controls link discovery/tracking. Link activation is additionally gated by linkHighlightMode.
When the user activates a link, TerminalView calls TerminalViewDelegate.requestOpenLink(source:link:params:). For explicit OSC 8 hyperlinks, params includes parsed key/value metadata (if provided); implicit links use empty params. On macOS, the default delegate implementation opens links via NSWorkspace. On iOS/visionOS, handle requestOpenLink in your delegate.
The core library currently does not provide a convenient way to connect to SSH, purely to avoid the additional dependency. The iOS sample app demonstrates how to integrate SSH using a modern SSH stack with swift-nio-ssh. See UIKitSshTerminalView and SSHLoginView for an example of connecting the TerminalView for iOS to an SSH connection.
SwiftTerm includes a termcast command-line tool that can record and playback terminal sessions in the asciinema .cast format. This tool is built using SwiftTerm's LocalProcess functionality.
swift run termcast record output.cast
Options:
Record an interactive shell session
swift run termcast record my-session.cast
Record a specific command
swift run termcast record -c "ls -la && echo 'Done'" command-demo.cast
Record with a 30-second timeout
swift run termcast record --timeout 30 timed-session.cast
Playing Back Sessions
To playback a recorded session:
swift run termcast playback my-session.cast
The playback will show the recorded terminal session with proper timing, including both input and output as they occurred during recording.
Full input/output capture: Records both user input and program output with precise timing
Raw terminal mode: Properly handles terminal control sequences and special keys
asciinema compatibility: Uses the standard .cast format for interoperability
Live display: Shows the session live while recording
Proper terminal handling: Maintains correct line endings and terminal state
Working on SwiftTerm
If you are using Xcode, there are two toplevel projects, one for Mac and one for iOS in the TerminalApp directory, one called "iOSTerminal.xcodeproj" and one called "MacTerminal.xcodeproj".
This is needed because Xcode does not provide code completion for iOS if you have a Mac project in the project. So I had to split them up. Both projects reference the same SwiftTerm package.
When working with these projects, if you choose the terminal application it will run this one. To run the test suite, select the 'SwiftTerm' target instead, and you can use 'SwiftTermFuzz' to run the fuzzer.
You can use swift build to build the package, and swift test to run the test suite. For better test coverage, clone the esctest repository which contains comprehensive terminal emulator tests:
make clone-esctest swift test
This clones the esctest repository (Python 3 branch) and enables the full terminal compliance test suite to run.
If using Xcode, you can select the "SwiftTerm" project, and then use Command-U to run the test suite.
SwiftTerm implements the terminal-wg BiDi recommendation for right-to-left and mixed-direction text on the Apple views (both the CoreGraphics and Metal renderers):
The details are in the BiDi documentation.
The SwiftTerm BiDi harness is an AppKit app for visual BiDi tests. It shows SwiftTerm beside a WebKit reference. Its scenarios cover paragraph reflow, terminal modes, reset behavior, box mirroring, combining marks, selection, cursor movement, and scrollback.
Tools/BidiHarness/Scripts/run-harness.sh --artifacts /tmp/bidi-artifacts
Use the controls in the app to select a scenario, move through its steps, resize the terminal, scroll, change the renderer, and save a capture. The app also has a local control socket for repeatable test runs. See the harness README for the control commands, Xcode instructions, artifact paths, and the macOS permission that Metal window capture needs.
Solid UTF-8 support, excellent rendering:
Supports hyperlinks emitted by modern apps:
Digital's VT100 User Guide
Terminal Guide - very nice and visual, but not normative
Xterm Control Sequences
VT510 Video Terminal Programmer Information
Additional and useful documents:
正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。