@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
swift 28 lines 955 Bytes
Raw
1 import Foundation
2
3 // Keep only replay history for intentional surface recreation (font/theme changes).
4 final class TerminalOutputBuffer {
5 private var storage = Data()
6 private var retainedStart = 0
7 private let limit = 512 * 1024
8
9 var data: Data { storage.subdata(in: retainedStart..<storage.count) }
10
11 func write(_ incoming: Data, reset: Bool) {
12 if reset || incoming.count >= limit {
13 storage = incoming.subdata(in: max(0, incoming.count - limit)..<incoming.count)
14 retainedStart = 0
15 } else {
16 storage.append(incoming)
17 retainedStart = max(retainedStart, storage.count - limit)
18 }
19 while retainedStart < storage.count && storage[retainedStart] & 0xC0 == 0x80 {
20 retainedStart += 1
21 }
22 // Compact once per history window, rather than copying 512 KiB on every append.
23 if retainedStart >= limit {
24 storage = storage.subdata(in: retainedStart..<storage.count)
25 retainedStart = 0
26 }
27 }
28 }