Skip to content

Commit d096216

Browse files
unnameddPitNikola
authored andcommitted
Add more informations in the result of the search
1 parent 5cec27a commit d096216

File tree

1 file changed

+95
-21
lines changed

1 file changed

+95
-21
lines changed

commands/developer-utils/search-script-command.swift

Lines changed: 95 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ struct ScriptCommand: Codable {
8383
let needsConfirmation: Bool?
8484
let refreshTime: String?
8585
let path: String
86+
let language: String
8687

8788
private(set) var leadingPath: String = ""
8889

@@ -102,6 +103,7 @@ struct ScriptCommand: Codable {
102103
case needsConfirmation
103104
case refreshTime
104105
case path
106+
case language
105107
}
106108

107109
mutating func setLeadingPath(_ value: String) {
@@ -111,9 +113,20 @@ struct ScriptCommand: Codable {
111113
extension ScriptCommand {
112114
typealias Authors = [Author]
113115

114-
struct Author: Codable {
116+
struct Author: Codable, CustomStringConvertible {
115117
let name: String?
116118
let url: String?
119+
120+
var description: String {
121+
if let name = name {
122+
return "\u{001B}[0;33m\(name.trimmedText)\u{001B}[0m"
123+
}
124+
else if let url = url {
125+
return url
126+
}
127+
128+
return .empty
129+
}
117130
}
118131
}
119132
extension ScriptCommand {
@@ -125,6 +138,39 @@ extension ScriptCommand {
125138
}
126139
}
127140

141+
// MARK: - Author Extension
142+
143+
extension Array where Element == ScriptCommand.Author {
144+
var authorDescription: String {
145+
var authors = String.empty
146+
147+
for author in self {
148+
let separator = self.separator(for: author.name ?? .empty)
149+
authors += separator + author.description
150+
}
151+
152+
return authors
153+
}
154+
155+
func separator(for currentName: String) -> String {
156+
if let firstAuthor = first, currentName == firstAuthor.name {
157+
return .empty
158+
}
159+
else if let lastAuthor = last, currentName == lastAuthor.name {
160+
return Separator.and
161+
}
162+
163+
return Separator.comma
164+
}
165+
}
166+
167+
extension ScriptCommand.Authors {
168+
enum Separator {
169+
static let and = " and "
170+
static let comma = ", "
171+
}
172+
}
173+
128174
// MARK: - Extensions
129175

130176
extension ScriptCommand {
@@ -136,8 +182,9 @@ extension ScriptCommand {
136182
}
137183

138184
return title.lowercased().contains(query)
139-
|| filename.lowercased().contains(query)
140-
|| description.lowercased().contains(query)
185+
|| filename.lowercased().contains(query)
186+
|| description.lowercased().contains(query)
187+
|| language.lowercased().contains(query)
141188
}
142189
}
143190
extension ScriptCommand: Comparable {
@@ -162,8 +209,28 @@ extension Group: Comparable {
162209
// MARK: - Int Extension
163210

164211
extension Int {
165-
func prependZeros(by total: Int) -> String {
166-
let format = "%0\(total)d"
212+
enum Unit: Int {
213+
case units = 1
214+
case tens = 2
215+
case hundreds = 3
216+
case thousands = 4
217+
}
218+
219+
var unitForTotal: Unit {
220+
switch self {
221+
case 0..<10:
222+
return .units
223+
case 10..<100:
224+
return .tens
225+
case 100..<1000:
226+
return .hundreds
227+
default:
228+
return .thousands
229+
}
230+
}
231+
232+
func prependZeros(for unit: Unit) -> String {
233+
let format = "%0\(unit.rawValue)d"
167234
let counter = String(format: format, self)
168235

169236
return counter
@@ -202,8 +269,8 @@ extension String {
202269
}
203270

204271
guard
205-
let match = content.value(of: result.range(at: 0)),
206-
let value = content.value(of: result.range(at: 1))
272+
let match = content.value(of: result.range(at: 0)),
273+
let value = content.value(of: result.range(at: 1))
207274
else {
208275
return nil
209276
}
@@ -225,19 +292,12 @@ final class ScriptCommandsStore {
225292
case emptyData
226293
}
227294

295+
private var totalScriptCommands: Int = 0
296+
228297
private var scriptCommands: ScriptCommands = []
229298

230299
private let extensionsURL = URL(string: "https://raw.githubusercontent.com/raycast/script-commands/master/commands/extensions.json")
231300

232-
private lazy var separator: String = {
233-
var separator = ""
234-
for i in 0...106 {
235-
separator += "-"
236-
}
237-
238-
return separator
239-
}()
240-
241301
private func githubURL(for path: String) -> String {
242302
"https://github.com/raycast/script-commands/blob/master/commands/\(path)"
243303
}
@@ -276,6 +336,8 @@ final class ScriptCommandsStore {
276336
func searchCommand(using query: String) {
277337
do {
278338
let data = try loadData()
339+
totalScriptCommands = data.totalScriptCommands
340+
279341
search(for: query, in: data.groups)
280342

281343
if scriptCommands.count > 0 {
@@ -333,11 +395,20 @@ final class ScriptCommandsStore {
333395
}
334396

335397
private func renderOutput(for scriptCommands: ScriptCommands) -> String {
336-
var contentString = "Total Script Commands found: \u{001B}[0;32m\(scriptCommands.count)\u{001B}[0m" //String.empty
398+
let total = scriptCommands.count
399+
let unit = total.unitForTotal
400+
var contentString = "Script Commands found: \u{001B}[0;32m\(total)\u{001B}[0m in \u{001B}[0;32m\(totalScriptCommands)\u{001B}[0m"
337401

338402
for (index, scriptCommand) in scriptCommands.enumerated() {
339-
let position = (index + 1).prependZeros(by: 2)
340-
let title = "\(position)) \u{001B}[0;31m\(scriptCommand.title.clearMarkdownLink())\u{001B}[0m"
403+
var title = String.empty
404+
var author = String.empty
405+
406+
if let value = scriptCommand.authors {
407+
author = "(by \(value.authorDescription))"
408+
}
409+
410+
let position = Int(index + 1).prependZeros(for: unit)
411+
title = "\(position)) \u{001B}[0;31m\(scriptCommand.title.clearMarkdownLink())\u{001B}[0m \(author)"
341412

342413
if contentString.count > 0 {
343414
contentString += .newLine + .newLine
@@ -350,6 +421,9 @@ final class ScriptCommandsStore {
350421
contentString += details.clearMarkdownLink()
351422
}
352423

424+
contentString += .newLine
425+
contentString += "Language: \u{001B}[0;31m\(scriptCommand.language.capitalized)\u{001B}[0m"
426+
353427
contentString += .newLine
354428
contentString += githubURL(for: scriptCommand.leadingPath)
355429
}
@@ -360,12 +434,12 @@ final class ScriptCommandsStore {
360434

361435
if CommandLine.arguments.count > 1 {
362436
let query = CommandLine.arguments[1].lowercased().trimmedText
363-
437+
364438
if query.isEmpty {
365439
print("Query must not be empty")
366440
}
367441
else {
368442
let store = ScriptCommandsStore()
369443
store.searchCommand(using: query)
370444
}
371-
}
445+
}

0 commit comments

Comments
 (0)