Skip to content

Instantly share code, notes, and snippets.

@erica

erica/pggrep.swift

Last active Nov 30, 2018
Embed
What would you like to do?
Find files in playgrounds, which are otherwise not indexed by spotlight
//
// main.swift
// pggrep - updated 12/4/15
// Created by Erica Sadun on 6/17/15.
// Copyright © 2015 Erica Sadun. All rights reserved.
//
import Foundation
extension String {
var ns: NSString {return self as NSString}
var enboxed: String {
let border = String(count: characters.count, repeatedValue: Character("-"))
return border + "\n" + self + "\n" + border
}
}
extension NSString {
var trimmed: NSString {return self.stringByTrimmingCharactersInSet(.whitespaceAndNewlineCharacterSet())}
}
func PlaygroundList() -> [String] {
let task = NSTask()
task.launchPath = "/usr/bin/mdfind"
task.arguments = ["kMDItemDisplayName == *.playground"]
let pipe = NSPipe()
task.standardOutput = pipe
task.launch()
task.waitUntilExit()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
guard let s = NSString(data: data, encoding: NSUTF8StringEncoding) else {return []}
return s.trimmed.componentsSeparatedByString("\n")
}
func SwiftList() -> [String] {
let task = NSTask()
task.launchPath = "/usr/bin/mdfind"
task.arguments = ["kMDItemDisplayName == *.swift"]
let pipe = NSPipe()
task.standardOutput = pipe
task.launch()
task.waitUntilExit()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
guard var s = NSString(data: data, encoding: NSUTF8StringEncoding) else {return []}
s = s.stringByTrimmingCharactersInSet(.whitespaceAndNewlineCharacterSet())
return s.componentsSeparatedByString("\n")
}
func PerformQuery(query: String, path: String) -> NSString {
let task = NSTask()
task.launchPath = "/usr/bin/grep"
task.arguments = ["-iR", query, path]
let pipe = NSPipe()
task.standardOutput = pipe
task.launch()
task.waitUntilExit()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
guard var s = NSString(data: data, encoding: NSUTF8StringEncoding) else {return ""}
s = s.stringByTrimmingCharactersInSet(.whitespaceAndNewlineCharacterSet())
return s
}
func grep(query: String) {
print("Searching for \(query)")
// Iterate through each playground
let pgPaths = PlaygroundList()
for path in pgPaths {
// Fetch results
let s = PerformQuery(query, path: path)
if s.length == 0 {continue}
// Defer printing playground info until a match is found
var isFirst = true
// Decompose the grep results into lines
let array = s.componentsSeparatedByString("\n")
for line in array {
// Ignore any "Binary" files
if line.hasPrefix("Binary") {continue}
// Split the rest into fileName:match
let splits = line.characters
.split(1, isSeparator: {$0 == ":"})
.map({String($0)})
let fname = splits[0].ns.lastPathComponent
if !fname.hasSuffix("swift") {continue}
if splits.count < 2 {continue}
// If this is a first match, show the playground info
if isFirst {
let playgroundName = path.ns.lastPathComponent.ns.stringByDeletingPathExtension
print(playgroundName.enboxed)
print("\(path.ns.stringByDeletingLastPathComponent)")
isFirst = false
}
// Print the file name and the match
let rest = splits[1]
print(" \(fname):\n \(rest)")
}
// Leave spaces between playground results
if !isFirst {print("")}
}
}
var args = Array(Process.arguments.dropFirst())
if args.isEmpty {
print("Must suply query")
exit(-1)
}
grep(args.joinWithSeparator(" "))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.