From ed1e7ab25d4634bd355c90932b2b3e78114471f5 Mon Sep 17 00:00:00 2001 From: neon443 <69979447+neon443@users.noreply.github.com> Date: Sat, 21 Jun 2025 17:07:28 +0100 Subject: [PATCH] commiting atp pressing write sends ls\n terminal view gets sshhandler instead of js text sumplify writetochannel, takes a string implemented a terminalviewdelegate that some rason never gets to do anythoing --- ShhShell/SSH/SSHHandler.swift | 24 ++++------- ShhShell/Views/Terminal/TerminalView.swift | 4 +- .../Views/Terminal/TextViewController.swift | 42 +++++++++++++++++-- 3 files changed, 50 insertions(+), 20 deletions(-) diff --git a/ShhShell/SSH/SSHHandler.swift b/ShhShell/SSH/SSHHandler.swift index a6aa3e3..fe45992 100644 --- a/ShhShell/SSH/SSHHandler.swift +++ b/ShhShell/SSH/SSHHandler.swift @@ -321,31 +321,25 @@ class SSHHandler: ObservableObject { write(1, buffer, Int(nbytes)) let data = Data(bytes: buffer, count: buffer.count) - self.terminal.append(String(data: data, encoding: .utf8)!) + if let string = String(data: data, encoding: .utf8) { + self.terminal.append(string) + terminal = self.terminal.replacingOccurrences(of: "[K", with: "\n") + } } private func logSshGetError() { logger.critical("\(String(cString: ssh_get_error(&self.session)))") } - func writeToChannel() { + func writeToChannel(_ string: String) { guard ssh_channel_is_open(channel) != 0 else { return } guard ssh_channel_is_eof(channel) == 0 else { return } - var buffer: [CChar] = Array(repeating: 65, count: 256) - var nbytes: Int - var nwritten: Int + var buffer: [CChar] = Array(string.utf8CString) + let nwritten = Int(ssh_channel_write(channel, &buffer, UInt32(buffer.count))) -// readFromChannel() -// nbytes = Int(read(0, &buffer, buffer.count)) - nbytes = buffer.count - guard nbytes > 0 else { - return + if nwritten != buffer.count { + print("partial write!!") } - if nbytes > 0 { - nwritten = Int(ssh_channel_write(channel, &buffer, UInt32(nbytes))) - guard nwritten == nbytes else { return } - } -// readFromChannel() } } diff --git a/ShhShell/Views/Terminal/TerminalView.swift b/ShhShell/Views/Terminal/TerminalView.swift index 1294e9b..db75fb3 100644 --- a/ShhShell/Views/Terminal/TerminalView.swift +++ b/ShhShell/Views/Terminal/TerminalView.swift @@ -14,9 +14,9 @@ struct TerminalView: View { var body: some View { Button("write") { - handler.writeToChannel() + handler.writeToChannel("top\n") } - TextViewController(text: $handler.terminal) + TextViewController(handler: handler) .toolbar { ToolbarItem(placement: .cancellationAction) { Button("reload") { diff --git a/ShhShell/Views/Terminal/TextViewController.swift b/ShhShell/Views/Terminal/TextViewController.swift index 2049688..65cdf39 100644 --- a/ShhShell/Views/Terminal/TextViewController.swift +++ b/ShhShell/Views/Terminal/TextViewController.swift @@ -12,23 +12,28 @@ import Runestone import TreeSitterBashRunestone struct TextViewController: UIViewRepresentable { - @Binding var text: String + @ObservedObject var handler: SSHHandler func makeUIView(context: Context) -> TextView { let languageMode = TreeSitterLanguageMode(language: .bash) let textView = TextView() setTextViewState(on: textView) + + var editorDelegate = textView.editorDelegate + editorDelegate = TerminalViewDelegate(handler: handler) + textView.editorDelegate = editorDelegate + textView.translatesAutoresizingMaskIntoConstraints = false textView.backgroundColor = .systemBackground return textView } func updateUIView(_ textView: TextView, context: Context) { - textView.text = text + textView.text = handler.terminal } private func setTextViewState(on textView: TextView) { - let text = self.text + let text = handler.terminal DispatchQueue.global(qos: .userInitiated).async { let state = TextViewState(text: text, language: .bash) DispatchQueue.main.async { @@ -37,3 +42,34 @@ struct TextViewController: UIViewRepresentable { } } } + +class TerminalViewDelegate: TextViewDelegate { + @ObservedObject var handler: SSHHandler + + init(handler: SSHHandler) { + self.handler = handler + } + + func textViewDidChangeGutterWidth(_ textView: TextView) { + print(textView.gutterWidth) + } + + func textViewDidChange(_ textView: TextView) { + handler.writeToChannel(textView.text) + } + + func textView(_ textView: TextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { + print(range) + print(text) + return true + } + + func textViewShouldBeginEditing(_ textView: TextView) -> Bool { + print("can edit") + return true + } + + func textView(_ textView: TextView, shouldInsert characterPair: any CharacterPair, in range: NSRange) -> Bool { + return false + } +}