diff --git a/phpmon/AppDelegate.swift b/phpmon/AppDelegate.swift index ed904ad..fe561d8 100644 --- a/phpmon/AppDelegate.swift +++ b/phpmon/AppDelegate.swift @@ -9,7 +9,7 @@ import Cocoa @NSApplicationMain -class AppDelegate: NSObject, NSApplicationDelegate { +class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate { // MARK: - Variables @@ -78,9 +78,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { if (self.version != nil) { // Actions menu.addItem(NSMenuItem.separator()) - menu.addItem(NSMenuItem(title: "Open php.ini in Finder", action: #selector(self.openActiveConfigFolder), keyEquivalent: "")) - // OPTIONAL - // menu.addItem(NSMenuItem(title: "Restart PHP \(self.version!.short) service", action: #selector(self.restartPhp), keyEquivalent: "")) + menu.addItem(NSMenuItem(title: "PHP configuration file (php.ini)", action: #selector(self.openActiveConfigFolder), keyEquivalent: "")) } menu.addItem(NSMenuItem.separator()) if (self.availablePhpVersions.count > 0 && !self.busy) { @@ -99,8 +97,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { menu.addItem(NSMenuItem(title: "Switching PHP versions...", action: nil, keyEquivalent: "")) menu.addItem(NSMenuItem.separator()) } - // TODO: Enable when implementation is complete - menu.addItem(NSMenuItem(title: "View terminal output", action: #selector(self.openOutput), keyEquivalent: "")) + menu.addItem(NSMenuItem(title: "View Shell Output", action: #selector(self.openOutput), keyEquivalent: "")) menu.addItem(NSMenuItem(title: "About phpmon", action: #selector(self.openAbout), keyEquivalent: "")) menu.addItem(NSMenuItem(title: "Quit phpmon", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q")) DispatchQueue.main.async { @@ -117,19 +114,15 @@ class AppDelegate: NSObject, NSApplicationDelegate { Shell.shared.delegate = vc let window = NSWindow(contentViewController: vc) window.title = "Terminal Output" + window.delegate = self self.windowController = NSWindowController(window: window) } self.windowController!.showWindow(self) NSApp.activate(ignoringOtherApps: true) - - // TODO: Send window to front (if possible) } @objc func updatePhpVersionInStatusBar() { self.version = PhpVersion() - if (Shell.shared.history.count > 0) { - _ = Shell.shared.history.popLast() - } if (self.busy) { DispatchQueue.main.async { self.setStatusBar(image: NSImage(named: NSImage.Name("StatusBarIcon"))!) @@ -176,5 +169,10 @@ class AppDelegate: NSObject, NSApplicationDelegate { } } } + + func windowWillClose(_ notification: Notification) { + self.windowController = nil + Shell.shared.delegate = nil + } } diff --git a/phpmon/Terminal/Services.swift b/phpmon/Terminal/Services.swift index 7007d02..df3f3a7 100644 --- a/phpmon/Terminal/Services.swift +++ b/phpmon/Terminal/Services.swift @@ -11,17 +11,17 @@ import AppKit class Services { public static func mysqlIsRunning() -> Bool { - let running = Shell.shared.pipe( "launchctl list | grep homebrew.mxcl.mysql") + let running = Shell.shared.pipe("launchctl list | grep homebrew.mxcl.mysql") return (running != "") } public static func nginxIsRunning() -> Bool { - let running = Shell.shared.pipe( "launchctl list | grep homebrew.mxcl.nginx") + let running = Shell.shared.pipe("launchctl list | grep homebrew.mxcl.nginx") return (running != "") } public static func detectPhpVersions() -> [String] { - let files = Shell.shared.pipe( "ls /usr/local/opt | grep php@") + let files = Shell.shared.pipe("ls /usr/local/opt | grep php@") var versions = files.components(separatedBy: "\n") // Remove all empty strings versions.removeAll { (string) -> Bool in @@ -37,20 +37,20 @@ class Services { public static func switchToPhpVersion(version: String, availableVersions: [String]) { availableVersions.forEach { (version) in - Shell.shared.run( "brew unlink php@\(version)") + Shell.shared.run("brew unlink php@\(version)") } if (availableVersions.contains("7.3")) { - Shell.shared.run( "brew link php@7.3") + Shell.shared.run("brew link php@7.3") if (version == Constants.LatestPhpVersion) { Shell.shared.run( "valet use php") } else { - Shell.shared.run( "valet use php@\(version)") + Shell.shared.run("valet use php@\(version)") } } } public static func restartPhp(version: String) { - Shell.shared.run( "brew services restart php@\(version)") + Shell.shared.run("brew services restart php@\(version)") } public static func openPhpConfigFolder(version: String) { diff --git a/phpmon/Terminal/Shell.swift b/phpmon/Terminal/Shell.swift index ed7e282..b184efe 100644 --- a/phpmon/Terminal/Shell.swift +++ b/phpmon/Terminal/Shell.swift @@ -60,6 +60,9 @@ class Shell { let historyItem = ShellHistoryItem(command: command, output: output) history.append(historyItem) + // Keep the last 100 items + history = history.suffix(100) + print(history.count) delegate?.didCompleteCommand(historyItem: historyItem) return output diff --git a/phpmon/View Controllers/LogViewController.swift b/phpmon/View Controllers/LogViewController.swift index 11f648a..73be213 100644 --- a/phpmon/View Controllers/LogViewController.swift +++ b/phpmon/View Controllers/LogViewController.swift @@ -52,4 +52,8 @@ class LogViewController: NSViewController, ShellDelegate { @IBAction func pressed(_ sender: Any) { self.view.window?.windowController?.close() } + + deinit { + print("VC deallocated") + } }