mirror of
https://github.com/neon443/ShhShell.git
synced 2026-03-11 13:26:16 +00:00
add addtohistory function
update formathistory made history have an array of history made ts codabel
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
struct History: Identifiable {
|
||||
struct History: Identifiable, Codable {
|
||||
var id: UUID = UUID()
|
||||
|
||||
var host: Host
|
||||
|
||||
@@ -24,7 +24,7 @@ class HostsManager: ObservableObject, @unchecked Sendable {
|
||||
|
||||
@Published var snippets: [Snippet] = []
|
||||
|
||||
@Published var history: [Host] = []
|
||||
@Published var history: [History] = []
|
||||
|
||||
var tint: SwiftUI.Color {
|
||||
selectedTheme.ansi[selectedAnsi].suiColor
|
||||
@@ -40,22 +40,27 @@ class HostsManager: ObservableObject, @unchecked Sendable {
|
||||
|
||||
func loadHistory() {
|
||||
guard let data = userDefaults.data(forKey: "history") else { return }
|
||||
guard let decoded = try? JSONDecoder().decode([Host].self, from: data) else { return }
|
||||
guard let decoded = try? JSONDecoder().decode([History].self, from: data) else { return }
|
||||
withAnimation { self.history = decoded }
|
||||
}
|
||||
|
||||
func formatHistory() -> [History] {
|
||||
func addToHistory(_ host: Host) {
|
||||
history.append(History(host: host, count: 1))
|
||||
formatHistory()
|
||||
}
|
||||
|
||||
func formatHistory() {
|
||||
var result: [History] = []
|
||||
for host in history {
|
||||
if result.last?.host == host {
|
||||
guard var lastOne = result.popLast() else { return result }
|
||||
for item in history {
|
||||
if result.last?.host == item.host {
|
||||
guard var lastOne = result.popLast() else { continue }
|
||||
lastOne.count += 1
|
||||
result.append(lastOne)
|
||||
} else {
|
||||
result.append(History(host: host, count: 1))
|
||||
result.append(History(host: item.host, count: 1))
|
||||
}
|
||||
}
|
||||
return result
|
||||
withAnimation { self.history = result }
|
||||
}
|
||||
|
||||
func saveHistory() {
|
||||
|
||||
@@ -146,7 +146,7 @@ Hostkey fingerprint is \(handler.getHostkey() ?? "nil")
|
||||
handler.go()
|
||||
showTerminal = checkShell(handler.state)
|
||||
if showTerminal {
|
||||
hostsManager.history.append(handler.host)
|
||||
hostsManager.addToHistory(handler.host)
|
||||
handler.writeToChannel(hostsManager.snippets.first(where: { $0.id == handler.host.startupSnippetID })?.content)
|
||||
}
|
||||
} label: {
|
||||
|
||||
@@ -14,7 +14,7 @@ struct RecentsView: View {
|
||||
var body: some View {
|
||||
if !hostsManager.history.isEmpty {
|
||||
Section("Recents") {
|
||||
ForEach(hostsManager.formatHistory()) { history in
|
||||
ForEach(hostsManager.history) { history in
|
||||
NavigationLink() {
|
||||
ConnectionView(
|
||||
handler: SSHHandler(
|
||||
|
||||
Reference in New Issue
Block a user