To connect SwiftTerm to a non-local data source like an SSH connection or a network socket, use TerminalView directly and implement the TerminalViewDelegate.
The Core Pattern:
- Outgoing Data: Implement
TerminalViewDelegate/send(source:data:) to forward user keystrokes/input from the terminal to your backend (e.g., an SSH channel). - Incoming Data: When your backend receives data from the remote host, call
TerminalView/feed(byteArray:) to display it in the terminal.
This pattern works for both macOS (AppKit) and iOS (UIKit).
class MyTerminalController: NSViewController, TerminalViewDelegate {
var terminalView: TerminalView!
override func viewDidLoad() {
super.viewDidLoad()
terminalView = TerminalView(frame: view.bounds)
terminalView.terminalDelegate = self
view.addSubview(terminalView)
}
func send(source: TerminalView, data: ArraySlice<UInt8>) {
// Send data to your backend (SSH channel, socket, etc.)
}
// Feed incoming data from the backend into the terminal:
func onDataReceived(_ data: ArraySlice<UInt8>) {
terminalView.feed(byteArray: data)
}
func sizeChanged(source: TerminalView, newCols: Int, newRows: Int) {}
func setTerminalTitle(source: TerminalView, title: String) {}
func hostCurrentDirectoryUpdate(source: TerminalView, directory: String?) {}
func scrolled(source: TerminalView, position: Double) {}
func requestOpenLink(source: TerminalView, link: String, params: [String: String]) {}
func clipboardCopy(source: TerminalView, content: Data) {}
func rangeChanged(source: TerminalView, startY: Int, endY: Int) {}
}