mirror of
https://github.com/neon443/ShhShell.git
synced 2026-03-11 13:26:16 +00:00
addedfunctions to check state
large code cleanup thanks to this
This commit is contained in:
@@ -20,8 +20,7 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
|
||||
@Published var title: String = ""
|
||||
@Published var state: SSHState = .idle
|
||||
var connected: Bool {
|
||||
return !(state == .idle || state == .connecting)
|
||||
// return state == .authorized || state == .shellOpen || state == .authorizing
|
||||
return checkConnected(state)
|
||||
}
|
||||
// @Published var connected: Bool = false
|
||||
// @Published var authorized: Bool = false
|
||||
@@ -57,6 +56,7 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
|
||||
|
||||
func go() {
|
||||
guard !connected else {
|
||||
withAnimation { state = .idle }
|
||||
Task {
|
||||
await disconnect()
|
||||
}
|
||||
@@ -130,8 +130,6 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
|
||||
func disconnect() async {
|
||||
await MainActor.run {
|
||||
withAnimation { state = .idle }
|
||||
// withAnimation { connected = false }
|
||||
// withAnimation { authorized = false }
|
||||
withAnimation { testSuceeded = nil }
|
||||
}
|
||||
|
||||
@@ -171,7 +169,7 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
|
||||
}
|
||||
}
|
||||
|
||||
if state == .authorized {} else {
|
||||
if !checkAuth(state) {
|
||||
go()
|
||||
}
|
||||
|
||||
@@ -180,7 +178,7 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
|
||||
return
|
||||
}
|
||||
|
||||
guard state == .authorized else {
|
||||
guard checkAuth(state) else {
|
||||
withAnimation { testSuceeded = false }
|
||||
return
|
||||
}
|
||||
@@ -378,7 +376,7 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
|
||||
status = ssh_channel_request_shell(self.channel)
|
||||
guard status == SSH_OK else { return }
|
||||
|
||||
|
||||
withAnimation { state = .shellOpen }
|
||||
}
|
||||
|
||||
func readFromChannel() -> String? {
|
||||
|
||||
@@ -14,3 +14,15 @@ enum SSHState {
|
||||
case authorized
|
||||
case shellOpen
|
||||
}
|
||||
|
||||
func checkConnected(_ state: SSHState) -> Bool {
|
||||
return !(state == .idle || state == .connecting)
|
||||
}
|
||||
|
||||
func checkAuth(_ state: SSHState) -> Bool {
|
||||
return state == .authorized || state == .shellOpen
|
||||
}
|
||||
|
||||
func checkShell(_ state: SSHState) -> Bool {
|
||||
return state == .shellOpen
|
||||
}
|
||||
|
||||
@@ -24,13 +24,14 @@ struct ConnectionView: View {
|
||||
var body: some View {
|
||||
NavigationStack {
|
||||
List {
|
||||
Text("\(handler.state)")
|
||||
Section {
|
||||
HStack {
|
||||
Text(handler.connected ? "connected" : "not connected")
|
||||
.modifier(foregroundColorStyle(handler.connected ? .green : .red))
|
||||
|
||||
Text(handler.state == .authorized ? "authorized" : "unauthorized")
|
||||
.modifier(foregroundColorStyle(handler.state == .authorized ? .green : .red))
|
||||
Text(checkAuth(handler.state) ? "authorized" : "unauthorized")
|
||||
.modifier(foregroundColorStyle(checkAuth(handler.state) ? .green : .red))
|
||||
}
|
||||
TextField("address", text: $handler.host.address)
|
||||
.textFieldStyle(.roundedBorder)
|
||||
@@ -91,7 +92,7 @@ struct ConnectionView: View {
|
||||
} label: {
|
||||
Label("Show Terminal", systemImage: "apple.terminal")
|
||||
}
|
||||
.disabled(!handler.connected || !(handler.state == .authorized))
|
||||
.disabled(!checkShell(handler.state))
|
||||
|
||||
Button() {
|
||||
handler.testExec()
|
||||
@@ -123,7 +124,7 @@ struct ConnectionView: View {
|
||||
ToolbarItem() {
|
||||
Button() {
|
||||
handler.go()
|
||||
showTerminal = handler.connected && handler.state == .authorized
|
||||
showTerminal = checkShell(handler.state)
|
||||
} label: {
|
||||
Label(
|
||||
handler.connected ? "Disconnect" : "Connect",
|
||||
@@ -134,6 +135,7 @@ struct ConnectionView: View {
|
||||
}
|
||||
}
|
||||
.fullScreenCover(isPresented: $showTerminal) {
|
||||
Text("\(handler.state)")
|
||||
ShellView(handler: handler)
|
||||
}
|
||||
.onDisappear {
|
||||
|
||||
Reference in New Issue
Block a user