Host-managed I/O
InMemoryTerminalSession connects Ghostty’s host-managed backend to your own process, emulator, or command engine. The host supplies output with receive, and Ghostty sends user input through the write closure.
Create a session
let session = InMemoryTerminalSession(
write: { data in
backend.handleInput(data)
},
resize: { viewport in
backend.resize(columns: viewport.columns, rows: viewport.rows)
}
)
terminal.configuration = TerminalSurfaceOptions(
backend: .inMemory(session)
)
Send host output
Use UTF-8 strings for simple demos and Data for exact byte streams.
session.receive("hello from host\r\n")
session.receive(Data([0x1B, 0x5B, 0x32, 0x4A])) // ESC [ 2 J
Read viewport text
readViewportText() returns the visible grid as a UTF-8 string. It reads viewport rows and separates them with \n.
if let text = session.readViewportText() {
print(text)
}
Finish a process
Tell Ghostty that the host-managed process has exited so lifecycle delegates and shell integration receive the close event.
session.finish(exitCode: 0, runtimeMilliseconds: 1234)