mirror of
https://github.com/neon443/ShhShell.git
synced 2026-03-11 05:19:13 +00:00
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:
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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") {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user