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
This commit is contained in:
neon443
2025-06-21 17:07:28 +01:00
parent c33e22cd3d
commit ed1e7ab25d
3 changed files with 50 additions and 20 deletions

View File

@@ -321,31 +321,25 @@ class SSHHandler: ObservableObject {
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)
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() { private func logSshGetError() {
logger.critical("\(String(cString: ssh_get_error(&self.session)))") 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_open(channel) != 0 else { return }
guard ssh_channel_is_eof(channel) == 0 else { return } guard ssh_channel_is_eof(channel) == 0 else { return }
var buffer: [CChar] = Array(repeating: 65, count: 256) var buffer: [CChar] = Array(string.utf8CString)
var nbytes: Int let nwritten = Int(ssh_channel_write(channel, &buffer, UInt32(buffer.count)))
var nwritten: Int
// readFromChannel() if nwritten != buffer.count {
// nbytes = Int(read(0, &buffer, buffer.count)) print("partial write!!")
nbytes = buffer.count
guard nbytes > 0 else {
return
} }
if nbytes > 0 {
nwritten = Int(ssh_channel_write(channel, &buffer, UInt32(nbytes)))
guard nwritten == nbytes else { return }
}
// readFromChannel()
} }
} }

View File

@@ -14,9 +14,9 @@ struct TerminalView: View {
var body: some View { var body: some View {
Button("write") { Button("write") {
handler.writeToChannel() handler.writeToChannel("top\n")
} }
TextViewController(text: $handler.terminal) TextViewController(handler: handler)
.toolbar { .toolbar {
ToolbarItem(placement: .cancellationAction) { ToolbarItem(placement: .cancellationAction) {
Button("reload") { Button("reload") {

View File

@@ -12,23 +12,28 @@ import Runestone
import TreeSitterBashRunestone import TreeSitterBashRunestone
struct TextViewController: UIViewRepresentable { struct TextViewController: UIViewRepresentable {
@Binding var text: String @ObservedObject var handler: SSHHandler
func makeUIView(context: Context) -> TextView { func makeUIView(context: Context) -> TextView {
let languageMode = TreeSitterLanguageMode(language: .bash) let languageMode = TreeSitterLanguageMode(language: .bash)
let textView = TextView() let textView = TextView()
setTextViewState(on: textView) setTextViewState(on: textView)
var editorDelegate = textView.editorDelegate
editorDelegate = TerminalViewDelegate(handler: handler)
textView.editorDelegate = editorDelegate
textView.translatesAutoresizingMaskIntoConstraints = false textView.translatesAutoresizingMaskIntoConstraints = false
textView.backgroundColor = .systemBackground textView.backgroundColor = .systemBackground
return textView return textView
} }
func updateUIView(_ textView: TextView, context: Context) { func updateUIView(_ textView: TextView, context: Context) {
textView.text = text textView.text = handler.terminal
} }
private func setTextViewState(on textView: TextView) { private func setTextViewState(on textView: TextView) {
let text = self.text let text = handler.terminal
DispatchQueue.global(qos: .userInitiated).async { DispatchQueue.global(qos: .userInitiated).async {
let state = TextViewState(text: text, language: .bash) let state = TextViewState(text: text, language: .bash)
DispatchQueue.main.async { 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
}
}