Created
June 20, 2024 17:02
-
-
Save kybernetyk/b68aaef40eebe60496a6dd9d9c410c43 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// PowerFinder.swift | |
// powerfinder | |
// | |
// Created by jls on 16.06.24. | |
// | |
import Foundation | |
import Cocoa | |
enum PowerFinderError : Error { | |
case emptyQuery | |
case cantBuildURL | |
case invalidSearchEngine | |
} | |
struct SearchEngine { | |
var name: String | |
var searchURL: String | |
var token: String | |
} | |
class PowerFinder { | |
var engines: [SearchEngine] = [SearchEngine(name: "Google", | |
searchURL: "https://google.com/search?q=", | |
token: "!g"), | |
SearchEngine(name: "DuckDuckGo", | |
searchURL: "https://duckduckgo.com/?q=", | |
token: "!d"), | |
SearchEngine(name: "ChatGTP", | |
searchURL: "https://chat.openai.com/chat?q=", | |
token: "!ai"), | |
SearchEngine(name: "Wikipedia", | |
searchURL: "https://en.wikipedia.org/w/index.php?search=", | |
token: "!w"), | |
] | |
var defaultSearchEngine : SearchEngine { | |
return self.engines.first! | |
} | |
func searchEngineForQuery(_ qry: String) -> SearchEngine { | |
let tokens = qry.components(separatedBy: .whitespaces) | |
guard let t0 = tokens.first else { | |
return self.defaultSearchEngine | |
} | |
if !t0.starts(with: "!") { | |
return self.defaultSearchEngine | |
} | |
let e = self.engines.first { | |
$0.token.lowercased() == t0.lowercased() | |
} ?? self.defaultSearchEngine | |
return e | |
} | |
func removeCommandTokenFromQuery(_ qry: String) -> String { | |
let tokens = qry.components(separatedBy: .whitespaces) | |
guard let t0 = tokens.first else { | |
return qry | |
} | |
let firstTokenIsCommand = t0.starts(with: "!") | |
if !firstTokenIsCommand { | |
return qry | |
} | |
var t = tokens | |
let _ = t.remove(at: 0) | |
let query = t.joined(separator: " ") | |
return query | |
} | |
func performSearchForQuery(_ qry: String) throws { | |
if qry.isEmpty { | |
throw PowerFinderError.emptyQuery | |
} | |
let tokens = qry.components(separatedBy: .whitespaces) | |
guard let t0 = tokens.first else { | |
throw PowerFinderError.emptyQuery | |
} | |
let searchEngine = self.searchEngineForQuery(qry) | |
let query = self.removeCommandTokenFromQuery(qry) | |
if let url = URL(string: "\(searchEngine.searchURL)\(query)") { | |
NSWorkspace.shared.open(url) | |
} else { | |
throw PowerFinderError.cantBuildURL | |
} | |
} | |
func predictSearchEngineNameForQuery(_ qry: String) -> String { | |
if qry.isEmpty { | |
return self.defaultSearchEngine.name | |
} | |
let searchEngine = self.searchEngineForQuery(qry) | |
return searchEngine.name | |
} | |
} | |
extension CustomStringConvertible { | |
func log(_ file: String = #file, function: String = #function, line: Int = #line) { | |
let df = DateFormatter() | |
df.dateFormat = "H:mm:ss.SSSS" | |
let time = df.string(from: Date.now) | |
let fileString: NSString = NSString(string: file) | |
let str = "\(time): \(fileString.lastPathComponent):\(line) \(function) - \(self)".replacingOccurrences(of: "\\\"", with: "") | |
print(str) | |
} | |
func logRet(_ file: String = #file, function: String = #function, line: Int = #line) -> String { | |
let df = DateFormatter() | |
df.dateFormat = "H:mm:ss.SSSS" | |
let time = df.string(from: Date.now) | |
let fileString: NSString = NSString(string: file) | |
let str = "\(time): \(fileString.lastPathComponent):\(line) \(function) - \(self)".replacingOccurrences(of: "\\\"", with: "") | |
return str | |
} | |
} | |
extension String: LocalizedError { | |
public var errorDescription: String? { return self } | |
public var recoverySuggestion: String? { return self } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment