Skip to content

Commit

Permalink
Merge pull request #26 from Ji4n1ng/develop
Browse files Browse the repository at this point in the history
feat(Core): Support Alacritty
  • Loading branch information
Ji4n1ng authored Apr 25, 2019
2 parents 9d7e17d + f5daafd commit 31d045a
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<string>com.apple.Finder</string>
<string>com.googlecode.iterm2</string>
<string>co.zeit.hyper</string>
<string>io.alacritty</string>
</array>
</dict>
</plist>
4 changes: 4 additions & 0 deletions OpenInTerminal.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
4725258A226812AB003C50DF /* OpenInTerminalFinderExtension.appex in Embed App Extensions */ = {isa = PBXBuildFile; fileRef = 4725254A22680FEB003C50DF /* OpenInTerminalFinderExtension.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
4725259022681424003C50DF /* OpenInTerminalCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4725251122680E87003C50DF /* OpenInTerminalCore.framework */; };
47A9A93C22697BFA00E92B54 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 47A9A92122697A2200E92B54 /* Main.storyboard */; };
950A4E692271EEC8002A9548 /* AlacrittyApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 950A4E682271EEC8002A9548 /* AlacrittyApp.swift */; };
95163CD22269979E007C006F /* Notifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95163CD12269979E007C006F /* Notifier.swift */; };
95163CD4226998ED007C006F /* OpenNotifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95163CD3226998ED007C006F /* OpenNotifier.swift */; };
95163CD52269A09D007C006F /* Notifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95163CD12269979E007C006F /* Notifier.swift */; };
Expand Down Expand Up @@ -117,6 +118,7 @@
4725257E2268112F003C50DF /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
4725257F2268112F003C50DF /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
47A9A92222697A2200E92B54 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
950A4E682271EEC8002A9548 /* AlacrittyApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlacrittyApp.swift; sourceTree = "<group>"; };
95163CD12269979E007C006F /* Notifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Notifier.swift; sourceTree = "<group>"; };
95163CD3226998ED007C006F /* OpenNotifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OpenNotifier.swift; sourceTree = "<group>"; };
9521EDC9226B3F93008445D1 /* Preferences.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = Preferences.storyboard; sourceTree = "<group>"; };
Expand Down Expand Up @@ -200,6 +202,7 @@
4725253822680F4B003C50DF /* TerminalApp.swift */,
4725253A22680F4B003C50DF /* iTermApp.swift */,
4725253922680F4B003C50DF /* HyperApp.swift */,
950A4E682271EEC8002A9548 /* AlacrittyApp.swift */,
);
path = Terminals;
sourceTree = "<group>";
Expand Down Expand Up @@ -468,6 +471,7 @@
9590B695226C6E7400362B93 /* EditorManager.swift in Sources */,
4725254122680F5A003C50DF /* Finder.swift in Sources */,
9590B697226C6E8600362B93 /* VSCodeApp.swift in Sources */,
950A4E692271EEC8002A9548 /* AlacrittyApp.swift in Sources */,
9590B699226C6E9600362B93 /* AtomApp.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
2 changes: 2 additions & 0 deletions OpenInTerminalCore/Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ extension DefaultsKeys {
static let terminalVisible = DefaultsKey<String>("OIT_TerminalVisible")
static let iTermVisible = DefaultsKey<String>("OIT_iTermVisible")
static let hyperVisible = DefaultsKey<String>("OIT_HyperVisible")
static let alacrittyVisible = DefaultsKey<String>("OIT_AlacrittyVisible")
static let vscodeVisible = DefaultsKey<String>("OIT_VSCodeVisible")
static let atomVisible = DefaultsKey<String>("OIT_AtomVisible")
static let sublimeVisible = DefaultsKey<String>("OIT_SublimeVisible")
Expand All @@ -40,6 +41,7 @@ extension DefaultsKeys {
static let terminalNewOption = DefaultsKey<String>("OIT_TerminalNewOption")
static let iTermNewOption = DefaultsKey<String>("OIT_iTermNewOption")
static let hyperNewOption = DefaultsKey<String>("OIT_HyperNewOption")
static let alacrittyNewOption = DefaultsKey<String>("OIT_AlacrittyNewOption")

static let copyPathToClipboardVisible = DefaultsKey<String>("OIT_CopyPathToClipboardVisible")

Expand Down
3 changes: 3 additions & 0 deletions OpenInTerminalCore/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ enum OITError: Error {
case cannotAccessTerminal
case cannotAccessIterm
case cannotAccessHyper
case cannotAccessAlacritty
case wrongUrl

}
Expand All @@ -32,6 +33,8 @@ extension OITError : CustomStringConvertible {
return "Cannot access iTerm, please check permissions."
case .cannotAccessHyper:
return "Cannot access Hyper, please check permissions."
case .cannotAccessAlacritty:
return "Cannot access Alacritty, please check permissions."
case .wrongUrl:
return "Oops, got a wrong url"
}
Expand Down
2 changes: 2 additions & 0 deletions OpenInTerminalCore/FinderManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ public class FinderManager {
return self.applicationExists(TerminalType.iTerm.name)
case .hyper:
return self.applicationExists(TerminalType.hyper.name)
case .alacritty:
return self.applicationExists(TerminalType.alacritty.name)
}
}
}
57 changes: 57 additions & 0 deletions OpenInTerminalCore/Terminals/AlacrittyApp.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// AlacrittyApp.swift
// OpenInTerminalCore
//
// Created by Jianing Wang on 2019/4/25.
// Copyright © 2019 Jianing Wang. All rights reserved.
//

import Foundation

final class AlacrittyApp : Openable {

func open(_ path: String, _ newOption: NewOptionType) throws {

guard let url = URL(string: path) else {
throw OITError.wrongUrl
}

let source = """
do shell script "open -na alacritty --args --working-directory \(url.path.alacrittyEscaped)"
"""

let script = NSAppleScript(source: source)!

var error: NSDictionary?

script.executeAndReturnError(&error)

if error != nil {
log(error, .error)
throw OITError.cannotAccessHyper
}
}

}


fileprivate extension String {

// FIXME: if path contains "\" or """, application will crash.
// Special symbols have been tested, except for backslashes and double quotes.
var alacrittyEscaped: String {

var result = ""
let set: [Character] = [" ", "(", ")", "&", "|", ";",
"\"", "'", "<", ">", "`"]

for char in self {
if set.contains(char) {
result += "\\\\"
}
result.append(char)
}

return result
}
}
8 changes: 8 additions & 0 deletions OpenInTerminalCore/Terminals/TerminalManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public class TerminalManager {
option = Defaults[.iTermNewOption]
case .hyper:
option = Defaults[.hyperNewOption]
case .alacritty:
option = Defaults[.alacrittyNewOption]
}

return option.map(NewOptionType.init(rawValue: )) ?? nil
Expand All @@ -57,6 +59,8 @@ public class TerminalManager {
Defaults[.iTermNewOption] = newOption.rawValue
case .hyper:
Defaults[.hyperNewOption] = newOption.rawValue
case .alacritty:
Defaults[.alacrittyNewOption] = newOption.rawValue
}
}

Expand All @@ -69,6 +73,8 @@ public class TerminalManager {
visible = Defaults[.iTermVisible]
case .hyper:
visible = Defaults[.hyperVisible]
case .alacritty:
visible = Defaults[.alacrittyVisible]
}

return visible.map(VisibleType.init(rawValue: )) ?? nil
Expand All @@ -83,6 +89,8 @@ public class TerminalManager {
Defaults[.iTermVisible] = visible.rawValue
case .hyper:
Defaults[.hyperVisible] = visible.rawValue
case .alacritty:
Defaults[.alacrittyVisible] = visible.rawValue
}
}

Expand Down
5 changes: 5 additions & 0 deletions OpenInTerminalCore/Terminals/TerminalType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum TerminalType: String {
case terminal = "com.apple.Terminal"
case iTerm = "com.googlecode.iterm2"
case hyper = "co.zeit.hyper"
case alacritty = "io.alacritty"

public var name: String {
switch self {
Expand All @@ -22,6 +23,8 @@ public enum TerminalType: String {
return "iTerm"
case .hyper:
return "Hyper"
case .alacritty:
return "Alacritty"
}
}

Expand All @@ -33,6 +36,8 @@ public enum TerminalType: String {
return iTermApp()
case .hyper:
return HyperApp()
case .alacritty:
return AlacrittyApp()
}
}

Expand Down

0 comments on commit 31d045a

Please sign in to comment.