improved how the terminal is updated, now just async reads aytomatically and will js not do anything if nothing recieved

terminal also starts at the top and doesnt have th wierd scroll artifacting
This commit is contained in:
neon443
2025-06-22 18:43:10 +01:00
parent b60e8f4eed
commit 9338b46760
3 changed files with 16 additions and 8 deletions

View File

@@ -311,21 +311,21 @@ class SSHHandler: ObservableObject {
// RunLoop.main.add(self.readTimer!, forMode: .common) // RunLoop.main.add(self.readTimer!, forMode: .common)
} }
func readFromChannel() -> String { func readFromChannel() -> String? {
guard ssh_channel_is_open(channel) != 0 else { return "" } guard ssh_channel_is_open(channel) != 0 else { return nil }
guard ssh_channel_is_eof(channel) == 0 else { return "" } guard ssh_channel_is_eof(channel) == 0 else { return nil }
var buffer: [CChar] = Array(repeating: 0, count: 256) var buffer: [CChar] = Array(repeating: 0, count: 16)
let nbytes = ssh_channel_read_nonblocking(channel, &buffer, UInt32(buffer.count), 0) let nbytes = ssh_channel_read_nonblocking(channel, &buffer, UInt32(buffer.count), 0)
guard nbytes > 0 else { return "" } guard nbytes > 0 else { return nil }
write(1, buffer, Int(nbytes)) write(1, buffer, Int(nbytes))
let data = Data(bytes: buffer, count: buffer.count) let data = Data(bytes: buffer, count: buffer.count)
if let string = String(data: data, encoding: .utf8) { if let string = String(data: data, encoding: .utf8) {
return string return string
} }
return "" return nil
} }
private func logSshGetError() { private func logSshGetError() {

View File

@@ -22,6 +22,16 @@ class SSHTerminalView: TerminalView, TerminalViewDelegate {
super.init(frame: frame) super.init(frame: frame)
terminalDelegate = self terminalDelegate = self
sshQueue.async {
guard let handler = self.handler else { return }
while handler.connected {
guard let handler = self.handler else { break }
guard let read = handler.readFromChannel() else { return }
DispatchQueue.main.async {
self.feed(text: read)
}
}
}
} }
required init?(coder: NSCoder) { required init?(coder: NSCoder) {

View File

@@ -23,11 +23,9 @@ struct TerminalController: UIViewRepresentable {
) )
// tv.terminalDelegate = terminalDelegate // tv.terminalDelegate = terminalDelegate
tv.getTerminal().feed(text: handler.readFromChannel())
return tv return tv
} }
func updateUIView(_ tv: TerminalView, context: Context) { func updateUIView(_ tv: TerminalView, context: Context) {
tv.getTerminal().feed(text: handler.readFromChannel())
} }
} }