-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathXTermView.swift
More file actions
100 lines (86 loc) · 3.8 KB
/
XTermView.swift
File metadata and controls
100 lines (86 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import SwiftUI
import Logger
import WebKit
import Terminal
struct XTermView: NSViewRepresentable {
@ObservedObject var terminalSession: TerminalSession
var onTerminalInput: (String) -> Void
var terminalOutput: String {
terminalSession.terminalOutput
}
func makeNSView(context: Context) -> WKWebView {
let webpagePrefs = WKWebpagePreferences()
webpagePrefs.allowsContentJavaScript = true
let preferences = WKWebViewConfiguration()
preferences.defaultWebpagePreferences = webpagePrefs
preferences.userContentController.add(context.coordinator, name: "terminalInput")
let webView = WKWebView(frame: .zero, configuration: preferences)
webView.navigationDelegate = context.coordinator
#if DEBUG
webView.configuration.preferences.setValue(true, forKey: "developerExtrasEnabled")
#endif
// Load the terminal bundle resources
let terminalBundleBaseURL = Bundle.main.bundleURL.appendingPathComponent("Contents/Resources/webViewDist/terminal")
let htmlFileURL = terminalBundleBaseURL.appendingPathComponent("terminal.html")
webView.loadFileURL(htmlFileURL, allowingReadAccessTo: terminalBundleBaseURL)
return webView
}
func updateNSView(_ webView: WKWebView, context: Context) {
// When terminalOutput changes, send the new data to the terminal
if context.coordinator.lastOutput != terminalOutput {
let newOutput = terminalOutput.suffix(from:
terminalOutput.index(terminalOutput.startIndex,
offsetBy: min(context.coordinator.lastOutput.count, terminalOutput.count)))
if !newOutput.isEmpty {
context.coordinator.lastOutput = terminalOutput
if context.coordinator.isWebViewLoaded {
context.coordinator.writeToTerminal(text: String(newOutput), webView: webView)
} else {
context.coordinator.pendingOutput = (context.coordinator.pendingOutput ?? "") + String(newOutput)
}
}
}
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, WKNavigationDelegate, WKScriptMessageHandler {
var parent: XTermView
var lastOutput: String = ""
var isWebViewLoaded = false
var pendingOutput: String?
init(_ parent: XTermView) {
self.parent = parent
super.init()
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
isWebViewLoaded = true
if let pending = pendingOutput {
writeToTerminal(text: pending, webView: webView)
pendingOutput = nil
}
}
func writeToTerminal(text: String, webView: WKWebView) {
let escapedOutput = text
.replacingOccurrences(of: "\\", with: "\\\\")
.replacingOccurrences(of: "'", with: "\\'")
.replacingOccurrences(of: "\n", with: "\\r\\n")
.replacingOccurrences(of: "\r", with: "\\r")
let jsCode = "writeToTerminal('\(escapedOutput)');"
DispatchQueue.main.async {
webView.evaluateJavaScript(jsCode) { _, error in
if let error = error {
Logger.client.info("XTerm: Error writing to terminal: \(error)")
}
}
}
}
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if message.name == "terminalInput", let input = message.body as? String {
DispatchQueue.main.async {
self.parent.onTerminalInput(input)
}
}
}
}
}