@@ -83,6 +83,7 @@ struct ScriptCommand: Codable {
83
83
let needsConfirmation : Bool ?
84
84
let refreshTime : String ?
85
85
let path : String
86
+ let language : String
86
87
87
88
private( set) var leadingPath : String = " "
88
89
@@ -102,6 +103,7 @@ struct ScriptCommand: Codable {
102
103
case needsConfirmation
103
104
case refreshTime
104
105
case path
106
+ case language
105
107
}
106
108
107
109
mutating func setLeadingPath( _ value: String ) {
@@ -111,9 +113,20 @@ struct ScriptCommand: Codable {
111
113
extension ScriptCommand {
112
114
typealias Authors = [ Author ]
113
115
114
- struct Author : Codable {
116
+ struct Author : Codable , CustomStringConvertible {
115
117
let name : String ?
116
118
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
+ }
117
130
}
118
131
}
119
132
extension ScriptCommand {
@@ -125,6 +138,39 @@ extension ScriptCommand {
125
138
}
126
139
}
127
140
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
+
128
174
// MARK: - Extensions
129
175
130
176
extension ScriptCommand {
@@ -136,8 +182,9 @@ extension ScriptCommand {
136
182
}
137
183
138
184
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)
141
188
}
142
189
}
143
190
extension ScriptCommand : Comparable {
@@ -162,8 +209,28 @@ extension Group: Comparable {
162
209
// MARK: - Int Extension
163
210
164
211
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 "
167
234
let counter = String ( format: format, self )
168
235
169
236
return counter
@@ -202,8 +269,8 @@ extension String {
202
269
}
203
270
204
271
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 ) )
207
274
else {
208
275
return nil
209
276
}
@@ -225,19 +292,12 @@ final class ScriptCommandsStore {
225
292
case emptyData
226
293
}
227
294
295
+ private var totalScriptCommands : Int = 0
296
+
228
297
private var scriptCommands : ScriptCommands = [ ]
229
298
230
299
private let extensionsURL = URL ( string: " https://raw.githubusercontent.com/raycast/script-commands/master/commands/extensions.json " )
231
300
232
- private lazy var separator : String = {
233
- var separator = " "
234
- for i in 0 ... 106 {
235
- separator += " - "
236
- }
237
-
238
- return separator
239
- } ( )
240
-
241
301
private func githubURL( for path: String ) -> String {
242
302
" https://github.com/raycast/script-commands/blob/master/commands/ \( path) "
243
303
}
@@ -276,6 +336,8 @@ final class ScriptCommandsStore {
276
336
func searchCommand( using query: String ) {
277
337
do {
278
338
let data = try loadData ( )
339
+ totalScriptCommands = data. totalScriptCommands
340
+
279
341
search ( for: query, in: data. groups)
280
342
281
343
if scriptCommands. count > 0 {
@@ -333,11 +395,20 @@ final class ScriptCommandsStore {
333
395
}
334
396
335
397
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 "
337
401
338
402
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) "
341
412
342
413
if contentString. count > 0 {
343
414
contentString += . newLine + . newLine
@@ -350,6 +421,9 @@ final class ScriptCommandsStore {
350
421
contentString += details. clearMarkdownLink ( )
351
422
}
352
423
424
+ contentString += . newLine
425
+ contentString += " Language: \u{001B} [0;31m \( scriptCommand. language. capitalized) \u{001B} [0m "
426
+
353
427
contentString += . newLine
354
428
contentString += githubURL ( for: scriptCommand. leadingPath)
355
429
}
@@ -360,12 +434,12 @@ final class ScriptCommandsStore {
360
434
361
435
if CommandLine . arguments. count > 1 {
362
436
let query = CommandLine . arguments [ 1 ] . lowercased ( ) . trimmedText
363
-
437
+
364
438
if query. isEmpty {
365
439
print ( " Query must not be empty " )
366
440
}
367
441
else {
368
442
let store = ScriptCommandsStore ( )
369
443
store. searchCommand ( using: query)
370
444
}
371
- }
445
+ }
0 commit comments