Skip to content

Commit

Permalink
Fetch up-to-date services list before showing menu
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewn committed May 1, 2016
1 parent bc505d5 commit e7cc642
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
buildConfiguration = "Release"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
Expand Down
102 changes: 63 additions & 39 deletions BrewServicesMenubar/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,79 @@ class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(aNotification: NSNotification) {
let icon = NSImage(named: "statusIcon")

statusItem.image = icon
statusItem.menu = statusMenu
if let button = statusItem.button {
button.image = icon
button.action = #selector(AppDelegate.handleMenuOpen(_:))
}

services = serviceStates()
updateMenu()
queryServicesAndUpdateMenu()
}

func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}

//
// Event handlers for UI actions
//
func handleClick(sender: NSMenuItem) {
if (sender.state == NSOnState) {
sender.state = NSOffState
controlService(sender.title, state: "stop")
} else {
sender.state = NSOnState
controlService(sender.title, state: "start")
}
}

func handleQuit(sender: NSMenuItem) {
NSApplication.sharedApplication().terminate(nil)
}

func handleMenuOpen(sender: AnyObject?) {
queryServicesAndUpdateMenu()
statusItem.popUpStatusItemMenu(statusMenu)
}

//
// Update menu of services
//
func updateMenu() {
statusMenu.removeAllItems()
for service in services {
let item = NSMenuItem.init(title: service.name, action:#selector(AppDelegate.handleClick(_:)), keyEquivalent: "")
if service.state == "started" {
item.state = NSOnState
}
statusMenu.addItem(item)
}
statusMenu.addItem(NSMenuItem.separatorItem())
let quit = NSMenuItem.init(title: "Quit", action:#selector(AppDelegate.handleQuit(_:)), keyEquivalent: "q")
statusMenu.addItem(quit)
}

func queryServicesAndUpdateMenu() {
services = serviceStates()
updateMenu()
}

//
// Changes a service state
//
func controlService(name:String, state:String) {
let task = NSTask()
let outpipe = NSPipe()
task.standardOutput = outpipe

task.launchPath = "/usr/local/bin/brew"
task.arguments = ["services", state, name]
task.launch()
}

//
// Queries and parses the output of:
// brew services list
//
func serviceStates() -> [Service] {
let task = NSTask()
let outpipe = NSPipe()
Expand All @@ -79,45 +131,17 @@ class AppDelegate: NSObject, NSApplicationDelegate {
return []
}

func controlService(name:String, state:String) {
let task = NSTask()
let outpipe = NSPipe()
task.standardOutput = outpipe

task.launchPath = "/usr/local/bin/brew"
task.arguments = ["services", state, name]
task.launch()
}

let matcher = "([^ ]+)([^ ]+)"

func parseServiceList(raw: String) -> [Service] {
let r = "([^ ]+)([^ ]+)"
var services = [Service]()
let rawServices = raw.componentsSeparatedByString("\n")
for s in rawServices {
let parts = matchesForRegexInText(r, text: s)
let service = Service(name: parts[0], state: parts[1])
services.append(service)
}
return services
return rawServices[1..<rawServices.count].map(parseService)
}

func handleClick(sender: NSMenuItem) {
if (sender.state == NSOnState) {
sender.state = NSOffState
controlService(sender.title, state: "stop")
} else {
sender.state = NSOnState
controlService(sender.title, state: "start")
}
}


@IBAction func itemClicked(sender: NSMenuItem) {
if (sender.state == NSOnState) {
sender.state = NSOffState
} else {
sender.state = NSOnState
}
func parseService(raw:String) -> Service {
let parts = matchesForRegexInText(matcher, text: raw)
let service = Service(name: parts[0], state: parts[1])
return service;
}
}

0 comments on commit e7cc642

Please sign in to comment.