Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/AppListProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ class AppListProvider: ListProvider {
var appDirDict = [String: Bool]()

var appList = [URL]()

let exitFlag: Bool

init() {
init(exitFlag: Bool) {
self.exitFlag = exitFlag

let applicationDir = NSSearchPathForDirectoriesInDomains(
.applicationDirectory, .localDomainMask, true)[0]

Expand All @@ -41,7 +45,7 @@ class AppListProvider: ListProvider {
appDirDict["/System/Library/CoreServices/"] = false

initFileWatch(Array(appDirDict.keys))
updateAppList()
updateAppList()
}

func initFileWatch(_ dirs: [String]) {
Expand Down Expand Up @@ -93,8 +97,12 @@ class AppListProvider: ListProvider {
NSLog("Cannot do action on item \(item.name)")
return
}
DispatchQueue.main.async {
DispatchQueue.main.async { [weak self] in
NSWorkspace.shared.launchApplication(app.path)

if self?.exitFlag ?? false {
NSApplication.shared.terminate(self)
}
}
}
}
3 changes: 3 additions & 0 deletions src/CommandArguments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ import ArgumentParser
struct DmenuMac: ParsableArguments {
@Option(name: .shortAndLong, help: "Show a prompt instead of the search input.")
var prompt: String?

@Flag(name: .shortAndLong, help: "Exit the process after an item is selected.")
var exit: Bool = false
}
17 changes: 8 additions & 9 deletions src/SearchViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class SearchViewController: NSViewController, NSTextFieldDelegate,
var hotkey: DDHotKey?
var listProvider: ListProvider?
var promptValue = ""
var exitFlag = false

struct Shortcut {
let keycode: UInt16
Expand All @@ -55,16 +56,16 @@ class SearchViewController: NSViewController, NSTextFieldDelegate,
object: nil
)

let options = DmenuMac.parseOrExit()
if options.prompt != nil {
promptValue = options.prompt!
}

let stdinStr = ReadStdin.read()
if stdinStr.count > 0 {
listProvider = PipeListProvider(str: stdinStr)
} else {
listProvider = AppListProvider()
}

let options = DmenuMac.parseOrExit()
if options.prompt != nil {
promptValue = options.prompt!
listProvider = AppListProvider(exitFlag: options.exit ?? false)
}

clearFields()
Expand Down Expand Up @@ -207,9 +208,7 @@ Ensure you are using a unique, valid shortcut.

func closeApp() {
clearFields()
if promptValue == "" {
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change isn't necessary, just saw this could be simplified as PipeListProvider.doAction terminates the app before this code is reached -- promptValue == "" will always evaluate to true.

NSApplication.shared.hide(nil)
}
NSApplication.shared.hide(nil)
}

@IBAction func openSettings(_ sender: AnyObject) {
Expand Down