mirror of
https://github.com/nicoverbruggen/phpmon.git
synced 2025-11-07 21:20:07 +01:00
✨ Allows quitting via menu bar item
This commit is contained in:
@@ -12,6 +12,7 @@ import Cocoa
|
||||
class AppDelegate: NSObject, NSApplicationDelegate {
|
||||
|
||||
var timer: Timer?
|
||||
var version: PhpVersionExtractor? = nil
|
||||
|
||||
let statusItem = NSStatusBar.system.statusItem(
|
||||
withLength: 40
|
||||
@@ -37,8 +38,21 @@ class AppDelegate: NSObject, NSApplicationDelegate {
|
||||
}
|
||||
|
||||
@objc func updatePhpVersionInStatusBar() {
|
||||
let version = Shell.extractPhpVersion()
|
||||
self.setStatusBarImage(version: version)
|
||||
self.version = PhpVersionExtractor()
|
||||
self.setStatusBarImage(version: self.version!.short)
|
||||
self.updateMenu()
|
||||
}
|
||||
|
||||
func updateMenu() {
|
||||
let menu = NSMenu()
|
||||
var string = "We are not sure what version of PHP you are running."
|
||||
if (version != nil) {
|
||||
string = "You are running PHP \(version!.long)"
|
||||
}
|
||||
menu.addItem(NSMenuItem(title: string, action: nil, keyEquivalent: ""))
|
||||
menu.addItem(NSMenuItem.separator())
|
||||
menu.addItem(NSMenuItem(title: "Quit phpmon", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q"))
|
||||
statusItem.menu = menu
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
29
phpmon/PhpVersionExtractor.swift
Normal file
29
phpmon/PhpVersionExtractor.swift
Normal file
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// PhpVersionExtractor.swift
|
||||
// phpmon
|
||||
//
|
||||
// Created by Nico Verbruggen on 11/06/2019.
|
||||
// Copyright © 2019 Nico Verbruggen. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
class PhpVersionExtractor {
|
||||
|
||||
var short : String = "???"
|
||||
var long : String = "???"
|
||||
|
||||
init() {
|
||||
// Get the info about the PHP installation
|
||||
let output = Shell.execute(command: "php -v")
|
||||
// Get everything before "(cli)" (PHP X.X.X (cli) ...)
|
||||
var version = output!.components(separatedBy: " (cli)")[0]
|
||||
// Strip away the text before the version number
|
||||
version = version.components(separatedBy: "PHP ")[1]
|
||||
self.long = version
|
||||
// Next up, let's strip away the minor version number
|
||||
let segments = version.components(separatedBy: ".")
|
||||
// Get the first two elements
|
||||
self.short = segments[0...1].joined(separator: ".")
|
||||
}
|
||||
}
|
||||
@@ -24,18 +24,4 @@ class Shell {
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
public static func extractPhpVersion() -> String
|
||||
{
|
||||
// Get the info about the PHP installation
|
||||
let output = self.execute(command: "php -v")
|
||||
// Get everything before "(cli)" (PHP X.X.X (cli) ...)
|
||||
var version = output!.components(separatedBy: " (cli)")[0]
|
||||
// Strip away the text before the version number
|
||||
version = version.components(separatedBy: "PHP ")[1]
|
||||
// Next up, let's strip away the minor version number
|
||||
let segments = version.components(separatedBy: ".")
|
||||
// Get the first two elements
|
||||
return segments[0...1].joined(separator: ".")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user