初始提交:terminalX 可运行态(M0/M1/M1.5 已真机验证)

- M0: libghostty SSH 终端(渲染/输入/连接)+ 白屏修复(OutputGate) + 会话状态机/自动重连
- M1: tsnet 用户态组网 + SSH-over-tsnet(fd 桥),shell 级真机验证;R5(Go+gvisor+C+++Swift 同进程) retire
- M1.5: tmux -CC 原生 tab(MVP)
- 结构: packages/(TXCore·TXTransport), apps/TerminalX, vendor/(libghostty-spm/libssh2/mbedtls/tsnet-bridge), artifacts/
- 文档: CLAUDE.md + docs/HANDOFF.md(新会话入口)
- 环境: 认证代理→依赖 vendor 本地化;Go 在 ~/.local/go;仅模拟器/未签名
- 待续: M2 mosh, tmux 多 pane, M4 安全(host key/SE)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
kid
2026-07-24 10:20:46 +08:00
commit aa92d0e676
2761 changed files with 803505 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
import GhosttyTerminal
import UIKit
@main
final class AppDelegate: UIResponder, UIApplicationDelegate {
func application(
_: UIApplication,
didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]? = nil
) -> Bool {
TerminalDebugLog.enable(.standard)
TerminalDebugLog.sink = { message in
NSLog("%@", message)
}
return true
}
func application(
_: UIApplication,
configurationForConnecting connectingSceneSession: UISceneSession,
options _: UIScene.ConnectionOptions
) -> UISceneConfiguration {
let config = UISceneConfiguration(
name: "Default Configuration",
sessionRole: connectingSceneSession.role
)
config.delegateClass = SceneDelegate.self
return config
}
}

View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--Generator: Apple Native CoreSVG 341-->
<!DOCTYPE svg
PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 403.625 361.875">
<g>
<rect height="361.875" opacity="0" width="403.625" x="0" y="0"/>
<path d="M93.5 332.625C146.625 332.625 185.75 302.875 185.75 249.25C185.75 201.25 154 181.875 110.625 168.625L86 161.25C56.125 152.25 31.5 137.5 31.5 105.125C31.5 70.375 59.625 51.5 90.875 51.5C121.125 51.5 148.25 68 154.25 100.75C155.75 107.75 159.875 111.375 166.875 111.375C173.875 111.375 179.25 107 179.25 99C179.25 96.375 178.875 93.25 178.125 90.25C168.875 49.625 132.375 27.25 91.25 27.25C42.75 27.25 4.625 58.625 4.625 106.5C4.625 151.75 34 173.5 77.5 186.625L102.25 194.25C134.25 203.875 158.75 218.375 158.75 251.75C158.75 287.25 130.625 308.375 94.625 308.375C58.625 308.375 31 289.5 24.75 255C22.875 247.375 18.75 244.25 12.125 244.25C4.875 244.25 0 249.5 0 257C0 261.75 0.625 265.75 1.75 269.625C11.875 309.75 49.25 332.625 93.5 332.625ZM314.375 322.625C362.875 322.625 403.625 297.125 403.625 248C403.625 204 372.25 186 334.125 171.75L306.625 161.5C281.75 152.5 254.5 137.75 254.5 108C254.5 77.25 282.5 60.875 313.5 60.875C345.75 60.875 367.625 78.25 373.25 110.25C375 117.25 379.25 121.625 386.5 121.625C394.25 121.625 399 116.875 399 109.25C399 107.25 398.875 103.75 398.125 100.125C390.25 58.5 354.75 37.375 313.5 37.375C268.125 37.375 228.125 63.375 228.125 109.375C228.125 149.5 257.75 170.25 295.875 184.5L323.375 194.875C352.375 205.375 377.5 218.75 377.5 250.125C377.5 282.875 350.25 299.125 314.5 299.125C278.625 299.125 253.875 282 248.875 246.75C247.625 239.75 242.875 235.125 235.625 235.125C228.625 235.125 223 239.75 223 247.875C223 249.75 223.25 251.875 223.5 253.75C231.125 300.625 269.375 322.625 314.375 322.625ZM313.25 361.75C318.25 361.75 322.375 357.625 322.375 352.75L322.375 9C322.375 4.125 318.25 0 313.25 0C308.25 0 304.125 4.125 304.125 9L304.125 352.75C304.125 357.625 308.25 361.75 313.25 361.75Z" fill="white" fill-opacity="0.85"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 2.1 KiB

View File

@@ -0,0 +1,30 @@
{
"fill" : {
"automatic-gradient" : "srgb:1.00000,0.54098,0.84731,1.00000"
},
"groups" : [
{
"layers" : [
{
"glass" : false,
"image-name" : "singaporedollarsign.svg",
"name" : "singaporedollarsign"
}
],
"shadow" : {
"kind" : "neutral",
"opacity" : 0.5
},
"translucency" : {
"enabled" : true,
"value" : 0.5
}
}
],
"supported-platforms" : {
"circles" : [
"watchOS"
],
"squares" : "shared"
}
}

View File

@@ -0,0 +1,29 @@
import UIKit
@MainActor
enum ApplicationExitController {
private static var hasRequestedExit = false
static func requestExit() {
guard !hasRequestedExit else {
return
}
hasRequestedExit = true
suspendIfAvailable()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
exit(EXIT_SUCCESS)
}
}
private static func suspendIfAvailable() {
let selector = NSSelectorFromString("suspend")
let application = UIApplication.shared
guard application.responds(to: selector) else {
return
}
_ = application.perform(selector)
}
}

View File

@@ -0,0 +1,21 @@
import UIKit
final class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(
_ scene: UIScene,
willConnectTo _: UISceneSession,
options _: UIScene.ConnectionOptions
) {
guard let windowScene = scene as? UIWindowScene else {
return
}
let window = UIWindow(windowScene: windowScene)
window.backgroundColor = .systemBackground
window.rootViewController = UINavigationController(rootViewController: ViewController())
window.makeKeyAndVisible()
self.window = window
}
}

View File

@@ -0,0 +1,86 @@
import UIKit
final class TerminalSelectionViewController: UIViewController {
private let pendingText: String
private let pendingAnchorRange: NSRange?
var onDone: (() -> Void)?
private lazy var textView: UITextView = {
let view = UITextView()
view.isEditable = false
view.isSelectable = true
view.accessibilityIdentifier = "terminal.selectionTextView"
view.alwaysBounceVertical = true
view.font = .monospacedSystemFont(ofSize: 14, weight: .regular)
view.backgroundColor = .clear
view.textColor = .label
view.textContainerInset = .init(top: 12, left: 12, bottom: 12, right: 12)
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
init(text: String, anchorRange: NSRange?) {
pendingText = text
pendingAnchorRange = anchorRange
super.init(nibName: nil, bundle: nil)
}
@available(*, unavailable)
required init?(coder _: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .clear
textView.text = pendingText
view.addSubview(textView)
// Pin to view edges (not safe area) so scrolled content extends
// under the transparent navigation bar / home indicator the way
// iOS sheets expect. UIKit's contentInsetAdjustmentBehavior keeps
// the initial scroll position below the nav bar via
// adjustedContentInset.
NSLayoutConstraint.activate([
textView.topAnchor.constraint(equalTo: view.topAnchor),
textView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
textView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
textView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
navigationItem.rightBarButtonItem = UIBarButtonItem(
barButtonSystemItem: .done,
target: self,
action: #selector(handleDone)
)
navigationItem.rightBarButtonItem?.accessibilityIdentifier = "terminal.selectionDoneButton"
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.view.backgroundColor = .clear
navigationController?.navigationBar.standardAppearance.configureWithTransparentBackground()
navigationController?.navigationBar.scrollEdgeAppearance = navigationController?.navigationBar.standardAppearance
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
textView.becomeFirstResponder()
let nsText = textView.text as NSString
if let range = pendingAnchorRange, NSMaxRange(range) <= nsText.length {
textView.selectedRange = range
textView.scrollRangeToVisible(range)
} else {
textView.selectAll(nil)
}
}
@objc private func handleDone() {
dismiss(animated: true) { [weak self] in
self?.onDone?()
}
}
}

View File

@@ -0,0 +1,262 @@
import GhosttyTerminal
import GhosttyTheme
import ShellCraftKit
import UIKit
final class ViewController: UIViewController {
private static let lightThemeKey = "SelectedTheme.light"
private static let darkThemeKey = "SelectedTheme.dark"
private lazy var terminalView: TerminalView = .init(frame: .zero)
private lazy var shellSession: ShellSession = .init(shell: defaultSandboxShell)
private lazy var controller: TerminalController = .init(
theme: Self.savedTerminalTheme()
) { builder in
builder.withBackgroundOpacity(0)
}
override func viewDidLoad() {
super.viewDidLoad()
title = "Terminal"
view.backgroundColor = .systemBackground
view.isOpaque = true
configureTerminalView()
configureThemeMenu()
applyBackgroundForCurrentAppearance()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
activateTerminal()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
terminalView.fitToSize()
}
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
guard traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) else {
return
}
controller.setTheme(Self.savedTerminalTheme())
applyBackgroundForCurrentAppearance()
}
private func configureTerminalView() {
terminalView.delegate = self
terminalView.isAccessibilityElement = true
terminalView.accessibilityIdentifier = "terminal.surface"
terminalView.accessibilityLabel = "Terminal Surface"
terminalView.configuration = TerminalSurfaceOptions(
backend: .inMemory(shellSession.terminalSession)
)
terminalView.controller = controller
terminalView.backgroundColor = .clear
terminalView.isOpaque = false
terminalView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(terminalView)
NSLayoutConstraint.activate([
terminalView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
terminalView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
terminalView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
terminalView.bottomAnchor.constraint(equalTo: view.keyboardLayoutGuide.topAnchor),
])
}
private func activateTerminal() {
terminalView.becomeFirstResponder()
shellSession.start()
}
// MARK: - Persistence
private static func savedTerminalTheme() -> TerminalTheme {
let lightConfig = savedThemeDefinition(forKey: lightThemeKey)?
.toTerminalConfiguration() ?? .alabaster
let darkConfig = savedThemeDefinition(forKey: darkThemeKey)?
.toTerminalConfiguration() ?? .afterglow
return TerminalTheme(light: lightConfig, dark: darkConfig)
}
private static func savedThemeDefinition(
forKey key: String
) -> GhosttyThemeDefinition? {
guard let name = UserDefaults.standard.string(forKey: key) else {
return nil
}
return GhosttyThemeCatalog.theme(named: name)
}
private var isDarkMode: Bool {
traitCollection.userInterfaceStyle == .dark
}
private func saveTheme(_ theme: GhosttyThemeDefinition) {
let key = isDarkMode ? Self.darkThemeKey : Self.lightThemeKey
UserDefaults.standard.set(theme.name, forKey: key)
}
private func applyBackgroundForCurrentAppearance() {
let key = isDarkMode ? Self.darkThemeKey : Self.lightThemeKey
guard let theme = Self.savedThemeDefinition(forKey: key) else { return }
if let bgColor = UIColor(hexString: theme.background) {
view.backgroundColor = bgColor
}
}
// MARK: - Theme Menu
private func configureThemeMenu() {
navigationItem.rightBarButtonItem = UIBarButtonItem(
image: UIImage(systemName: "paintpalette"),
menu: buildThemeMenu()
)
navigationItem.rightBarButtonItem?.accessibilityIdentifier = "terminal.themeButton"
}
private func buildThemeMenu() -> UIMenu {
let popular = buildSubmenu(
title: "Popular",
themes: [
"Dracula", "Catppuccin Mocha", "Catppuccin Latte",
"Nord", "Solarized Dark", "Solarized Light",
"Gruvbox Dark", "Gruvbox Light", "Tokyo Night",
"One Half Dark", "One Half Light", "Rose Pine",
"Monokai Pro", "GitHub Dark", "GitHub Light",
]
)
let dark = UIMenu(
title: "Dark",
image: UIImage(systemName: "moon.fill"),
children: alphabeticalSubmenus(
themes: GhosttyThemeCatalog.allThemes.filter(\.isDark)
)
)
let light = UIMenu(
title: "Light",
image: UIImage(systemName: "sun.max.fill"),
children: alphabeticalSubmenus(
themes: GhosttyThemeCatalog.allThemes.filter { !$0.isDark }
)
)
return UIMenu(title: "Theme", children: [popular, dark, light])
}
private func buildSubmenu(
title: String,
themes names: [String]
) -> UIMenu {
let actions = names.compactMap { name -> UIAction? in
guard let theme = GhosttyThemeCatalog.theme(named: name) else {
return nil
}
return themeAction(for: theme)
}
return UIMenu(
title: title,
image: UIImage(systemName: "star.fill"),
children: actions
)
}
private func alphabeticalSubmenus(
themes: [GhosttyThemeDefinition]
) -> [UIMenu] {
var grouped: [String: [GhosttyThemeDefinition]] = [:]
for theme in themes {
let letter = String(theme.name.prefix(1)).uppercased()
let key = letter.first?.isLetter == true ? letter : "#"
grouped[key, default: []].append(theme)
}
return grouped.keys.sorted().map { key in
UIMenu(
title: key,
children: grouped[key]!.map { themeAction(for: $0) }
)
}
}
private func themeAction(for theme: GhosttyThemeDefinition) -> UIAction {
UIAction(title: theme.name) { [weak self] _ in
self?.applyTheme(theme)
}
}
private func applyTheme(_ theme: GhosttyThemeDefinition) {
saveTheme(theme)
controller.setTheme(Self.savedTerminalTheme())
if let bgColor = UIColor(hexString: theme.background) {
view.backgroundColor = bgColor
}
}
}
// MARK: - Terminal Callbacks
extension ViewController:
TerminalSurfaceTitleDelegate,
TerminalSurfaceResizeDelegate,
TerminalSurfaceCloseDelegate,
TerminalSurfaceTextSelectionRequestDelegate,
UIAdaptivePresentationControllerDelegate
{
func terminalDidChangeTitle(_ title: String) {
self.title = title
}
func terminalDidResize(columns _: Int, rows _: Int) {}
func terminalDidClose(processAlive _: Bool) {
ApplicationExitController.requestExit()
}
func terminalDidRequestTextSelection(_ request: TerminalTextSelectionRequest) {
let selectionVC = TerminalSelectionViewController(
text: request.text,
anchorRange: request.anchorRange
)
selectionVC.onDone = { [weak self] in
self?.terminalView.becomeFirstResponder()
}
let nav = UINavigationController(rootViewController: selectionVC)
nav.modalPresentationStyle = .pageSheet
nav.sheetPresentationController?.detents = [.medium(), .large()]
nav.sheetPresentationController?.prefersGrabberVisible = true
nav.presentationController?.delegate = self
present(nav, animated: true)
}
/// Covers the user-gesture (grabber swipe) dismiss path only —
/// programmatic dismiss does not trigger this callback, so the Done
/// button restores focus via `onDone` instead.
func presentationControllerDidDismiss(_: UIPresentationController) {
terminalView.becomeFirstResponder()
}
}
// MARK: - UIColor Hex
private extension UIColor {
convenience init?(hexString: String) {
let hex = hexString.hasPrefix("#") ? String(hexString.dropFirst()) : hexString
guard hex.count == 6,
let r = UInt8(hex.prefix(2), radix: 16),
let g = UInt8(hex.dropFirst(2).prefix(2), radix: 16),
let b = UInt8(hex.dropFirst(4).prefix(2), radix: 16)
else { return nil }
self.init(
red: CGFloat(r) / 255,
green: CGFloat(g) / 255,
blue: CGFloat(b) / 255,
alpha: 1
)
}
}