Skip to content

Commit e22e1f7

Browse files
committed
fix(mobile): allow copying iOS terminal output
1 parent 9efb016 commit e22e1f7

2 files changed

Lines changed: 98 additions & 9 deletions

File tree

‎apps/mobile/modules/t3-terminal/ios/T3TerminalView.swift‎

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ public final class T3TerminalView: ExpoView, UITextFieldDelegate {
201201
private let inputField = TerminalInputField()
202202
private let focusTapGesture = UITapGestureRecognizer()
203203
private let scrollPanGesture = UIPanGestureRecognizer()
204+
private let selectTextGesture = UILongPressGestureRecognizer()
204205
private var lastViewportSize: CGSize = .zero
205206
private var lastContentScale: CGFloat = 0
206207
private var lastReportedGrid: (cols: Int, rows: Int)?
@@ -219,15 +220,7 @@ public final class T3TerminalView: ExpoView, UITextFieldDelegate {
219220
var captureRequest: Double = 0 {
220221
didSet {
221222
guard captureRequest > 0, captureRequest != oldValue else { return }
222-
guard let surface else { onCapture(["text": ""]); return }
223-
let selection = ghostty_selection_s(
224-
top_left: ghostty_point_s(tag: GHOSTTY_POINT_VIEWPORT, coord: GHOSTTY_POINT_COORD_TOP_LEFT, x: 0, y: 0),
225-
bottom_right: ghostty_point_s(tag: GHOSTTY_POINT_VIEWPORT, coord: GHOSTTY_POINT_COORD_BOTTOM_RIGHT, x: 0, y: 0),
226-
rectangle: false)
227-
var captured = ghostty_text_s()
228-
guard ghostty_surface_read_text(surface, selection, &captured) else { onCapture(["text": ""]); return }
229-
defer { ghostty_surface_free_text(surface, &captured) }
230-
let text = captured.text.flatMap { String(bytes: UnsafeBufferPointer(start: UnsafeRawPointer($0).assumingMemoryBound(to: UInt8.self), count: Int(captured.text_len)), encoding: .utf8) } ?? ""
223+
let text = readViewportText() ?? ""
231224
// Android joins snapshot rows with "\n" and trims each row's trailing whitespace, so do
232225
// the same here: identical terminal content must capture identically on both platforms.
233226
let normalized = text.split(separator: "\n", omittingEmptySubsequences: false)
@@ -355,6 +348,9 @@ public final class T3TerminalView: ExpoView, UITextFieldDelegate {
355348
scrollPanGesture.maximumNumberOfTouches = 1
356349
scrollPanGesture.cancelsTouchesInView = false
357350
terminalViewport.addGestureRecognizer(scrollPanGesture)
351+
selectTextGesture.addTarget(self, action: #selector(handleSelectText(_:)))
352+
terminalViewport.addGestureRecognizer(selectTextGesture)
353+
focusTapGesture.require(toFail: selectTextGesture)
358354

359355
addSubview(terminalViewport)
360356
addSubview(inputField)
@@ -426,6 +422,40 @@ public final class T3TerminalView: ExpoView, UITextFieldDelegate {
426422
requestKeyboardFocus()
427423
}
428424

425+
@objc
426+
private func handleSelectText(_ gesture: UILongPressGestureRecognizer) {
427+
guard gesture.state == .began,
428+
let presenter = appContext?.utilities?.currentViewController(),
429+
let text = readViewportText(),
430+
!text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
431+
else { return }
432+
433+
// Capture before dismissing the keyboard: doing so can resize the remote PTY.
434+
// UIKit owns selection in this snapshot, so live output cannot move its endpoints.
435+
inputField.resignFirstResponder()
436+
let controller = TerminalTextSelectionViewController(text: text, fontSize: fontSize)
437+
let navigation = UINavigationController(rootViewController: controller)
438+
navigation.modalPresentationStyle = .pageSheet
439+
presenter.present(navigation, animated: !UIAccessibility.isReduceMotionEnabled)
440+
}
441+
442+
private func readViewportText() -> String? {
443+
guard let surface else { return nil }
444+
let selection = ghostty_selection_s(
445+
top_left: ghostty_point_s(tag: GHOSTTY_POINT_VIEWPORT, coord: GHOSTTY_POINT_COORD_TOP_LEFT, x: 0, y: 0),
446+
bottom_right: ghostty_point_s(tag: GHOSTTY_POINT_VIEWPORT, coord: GHOSTTY_POINT_COORD_BOTTOM_RIGHT, x: 0, y: 0),
447+
rectangle: false)
448+
var captured = ghostty_text_s()
449+
guard ghostty_surface_read_text(surface, selection, &captured) else { return nil }
450+
defer { ghostty_surface_free_text(surface, &captured) }
451+
guard let text = captured.text else { return nil }
452+
return String(
453+
bytes: UnsafeBufferPointer(
454+
start: UnsafeRawPointer(text).assumingMemoryBound(to: UInt8.self),
455+
count: Int(captured.text_len)),
456+
encoding: .utf8)
457+
}
458+
429459
@objc
430460
private func handleViewportPan(_ gesture: UIPanGestureRecognizer) {
431461
guard let surface else { return }
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import UIKit
2+
3+
/// A frozen viewport lets UIKit provide selection handles without competing with
4+
/// terminal scrolling or changing the remote shell's input.
5+
final class TerminalTextSelectionViewController: UIViewController {
6+
private let text: String
7+
private let fontSize: CGFloat
8+
9+
init(text: String, fontSize: CGFloat) {
10+
self.text = text
11+
self.fontSize = fontSize
12+
super.init(nibName: nil, bundle: nil)
13+
}
14+
15+
required init?(coder: NSCoder) {
16+
return nil
17+
}
18+
19+
override func viewDidLoad() {
20+
super.viewDidLoad()
21+
title = "Terminal text"
22+
view.backgroundColor = .systemBackground
23+
24+
navigationItem.leftBarButtonItem = UIBarButtonItem(
25+
title: "Copy All", style: .plain, target: self, action: #selector(copyAll))
26+
navigationItem.rightBarButtonItem = UIBarButtonItem(
27+
barButtonSystemItem: .done, target: self, action: #selector(close))
28+
29+
let textView = UITextView()
30+
textView.text = text
31+
textView.font = .monospacedSystemFont(ofSize: max(fontSize, 14), weight: .regular)
32+
textView.textColor = .label
33+
textView.backgroundColor = .systemBackground
34+
textView.isEditable = false
35+
textView.isSelectable = true
36+
textView.accessibilityLabel = "Terminal output"
37+
textView.textContainerInset = UIEdgeInsets(top: 16, left: 12, bottom: 16, right: 12)
38+
textView.translatesAutoresizingMaskIntoConstraints = false
39+
view.addSubview(textView)
40+
41+
NSLayoutConstraint.activate([
42+
textView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
43+
textView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
44+
textView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
45+
textView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
46+
])
47+
}
48+
49+
@objc
50+
private func copyAll() {
51+
UIPasteboard.general.string = text
52+
close()
53+
}
54+
55+
@objc
56+
private func close() {
57+
dismiss(animated: !UIAccessibility.isReduceMotionEnabled)
58+
}
59+
}

0 commit comments

Comments
 (0)