Initial Push
This commit is contained in:
72
SnippetManager.swift
Normal file
72
SnippetManager.swift
Normal file
@@ -0,0 +1,72 @@
|
||||
import Foundation
|
||||
|
||||
// MARK: - Command Snippet Manager
|
||||
class SnippetManager {
|
||||
static let shared = SnippetManager()
|
||||
private var snippets: [Snippet] = []
|
||||
|
||||
struct Snippet: Codable {
|
||||
let id: UUID
|
||||
var name: String
|
||||
var command: String
|
||||
var description: String
|
||||
var tags: [String]
|
||||
var category: String
|
||||
|
||||
init(name: String, command: String, description: String = "", tags: [String] = [], category: String = "General") {
|
||||
self.id = UUID()
|
||||
self.name = name
|
||||
self.command = command
|
||||
self.description = description
|
||||
self.tags = tags
|
||||
self.category = category
|
||||
}
|
||||
}
|
||||
|
||||
init() {
|
||||
loadSnippets()
|
||||
// Start with empty snippets - user will add via right-click
|
||||
}
|
||||
|
||||
func addSnippet(_ snippet: Snippet) {
|
||||
snippets.append(snippet)
|
||||
saveSnippets()
|
||||
}
|
||||
|
||||
func removeSnippet(_ id: UUID) {
|
||||
snippets.removeAll { $0.id == id }
|
||||
saveSnippets()
|
||||
}
|
||||
|
||||
func searchSnippets(_ query: String) -> [Snippet] {
|
||||
if query.isEmpty { return snippets }
|
||||
return snippets.filter {
|
||||
$0.name.localizedCaseInsensitiveContains(query) ||
|
||||
$0.command.localizedCaseInsensitiveContains(query) ||
|
||||
$0.category.localizedCaseInsensitiveContains(query) ||
|
||||
$0.tags.contains { $0.localizedCaseInsensitiveContains(query) }
|
||||
}
|
||||
}
|
||||
|
||||
func getAllSnippets() -> [Snippet] {
|
||||
return snippets
|
||||
}
|
||||
|
||||
private func addDefaultSnippets() {
|
||||
// Removed - snippets now start empty
|
||||
// Users can add snippets via right-click context menu
|
||||
}
|
||||
|
||||
private func saveSnippets() {
|
||||
if let data = try? JSONEncoder().encode(snippets) {
|
||||
UserDefaults.standard.set(data, forKey: "terminal_snippets")
|
||||
}
|
||||
}
|
||||
|
||||
private func loadSnippets() {
|
||||
if let data = UserDefaults.standard.data(forKey: "terminal_snippets"),
|
||||
let loaded = try? JSONDecoder().decode([Snippet].self, from: data) {
|
||||
snippets = loaded
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user