Skip to content

Commit

Permalink
chore: optimize code structure, completion docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Saiya committed Apr 27, 2022
1 parent 0f099f4 commit 6439ff2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 35 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@ launch alfred then input `st` and `keyword` separated with a space to search boo
> press `cmd` + `enter` to reveal the repo in Finder
## Optimize for Intel Chip Mac
If you are using old Intel Chip Mac, you may experience the unbearable lagging, that's because of Swift JIT Compiler are really slow on Intel Chip.
If you are using old Intel Chip Mac, you may experience the unbearable lagging, that's because of Swift JIT Compiler is pretty slow on Intel Chip.

You can follow the following steps to compile the workflow script to binary to speed up its response speed.

> enter `st $compile` in Alfred then press `enter` key
*If you are using Apple Silicon Macs(like M1, M1 Pro), you can also compile the workflow, but only a little bit faster*


## Contributions and Support
I'm new to swift, feel free to make a pull request if you are willing to improve the code quality or its functions.
Binary file modified SourceTree.alfredworkflow
Binary file not shown.
69 changes: 35 additions & 34 deletions workflow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,29 @@ class Workflow {

// MARK: Alfred Structs

struct AlfredResult: Codable {
let items: [AlfredItem]
}

struct AlfredItem: Codable {
var title: String
var subtitle: String
var match: String?
var arg: String?
var mods: AlfredMods?
}

struct AlfredMods: Codable {
var cmd: AlfredItemModItem?
var alt:AlfredItemModItem?
}

struct AlfredItemModItem: Codable {
var valid: Bool
var arg: String
var subtitle: String
extension Workflow {
struct AlfredResult: Codable {
let items: [AlfredItem]
}

struct AlfredItem: Codable {
var title: String
var subtitle: String
var match: String?
var arg: String?
var mods: AlfredMods?
}

struct AlfredMods: Codable {
var cmd: AlfredItemModItem?
var alt:AlfredItemModItem?
}

struct AlfredItemModItem: Codable {
var valid: Bool
var arg: String
var subtitle: String
}
}


Expand All @@ -82,15 +84,15 @@ extension Encodable {

// MARK: convert AlfredItem to AlfredResult

extension AlfredItem {
func toAlfredResult() -> AlfredResult {
return AlfredResult(items: [self])
extension Workflow.AlfredItem {
func toAlfredResult() -> Workflow.AlfredResult {
return Workflow.AlfredResult(items: [self])
}
}

extension Array where Element == AlfredItem {
func toAlfredResult() -> AlfredResult {
return AlfredResult(items: self)
extension Array where Element == Workflow.AlfredItem {
func toAlfredResult() -> Workflow.AlfredResult {
return Workflow.AlfredResult(items: self)
}
}

Expand Down Expand Up @@ -146,14 +148,13 @@ extension Workflow {
}
}


// MARK: SourceTree Workflow implements

class SourceTree: Workflow {
override init() {
super.init()

emptyMessage = AlfredItem(title: "Your SourceTree Bookmark Is Empty ", subtitle: "Please add repos to SourceTree first")


guard let data = try? Data(contentsOf: Self.plistPath) else {
errorMessage = AlfredItem(title: "SourceTree not installed", subtitle: "Press enter to open SourceTree homepage and download it", arg: "open \"https://sourcetreeapp.com/\"")
Expand Down Expand Up @@ -183,8 +184,8 @@ class SourceTree: Workflow {
let sourceFile = "\(destFile).swift"
if query == "$compile" {
list.append(AlfredItem(
title: "Compile script",
subtitle: "compile script to speed up workflow response time",
title: "Compile workflow script",
subtitle: "Compile workflow script to binary to speed up its response time",
arg: "swiftc \"\(sourceFile)\" -O -o \"\(destFile)\""
))
}
Expand Down Expand Up @@ -257,7 +258,7 @@ extension SourceTree.SourceTreePlist {
self.objects = objects
}

func toAlfredItems() -> [AlfredItem] {
func toAlfredItems() -> [Workflow.AlfredItem] {
var namePathGroups: [(name: String, path: String)] = []
var name = ""
objects.forEach { str in
Expand All @@ -273,8 +274,8 @@ extension SourceTree.SourceTreePlist {
}

return namePathGroups.map { (name, path) in
let mod = AlfredItemModItem(valid: true, arg: "open \"\(path)\"", subtitle: "Reveal in Finder")
return AlfredItem(title: name, subtitle: path, arg: path, mods: AlfredMods(cmd: mod))
let mod = Workflow.AlfredItemModItem(valid: true, arg: "open \"\(path)\"", subtitle: "Reveal in Finder")
return Workflow.AlfredItem(title: name, subtitle: path, arg: path, mods: Workflow.AlfredMods(cmd: mod))
}
}
}
Expand Down

0 comments on commit 6439ff2

Please sign in to comment.