updated a lot of stuff

This commit is contained in:
Chad Nelson 2025-03-09 07:27:37 -06:00
parent 0ba9201bc7
commit a1a414ce83
2 changed files with 113 additions and 61 deletions

View file

@ -6,25 +6,10 @@
//
import SwiftUI
import AppKit
import Foundation
func oldshell(_ command: String) -> String {
let task = Process()
let pipe = Pipe()
task.standardOutput = pipe
task.standardError = pipe
task.arguments = ["-c", command]
task.launchPath = "/bin/zsh"
task.standardInput = nil
task.launch()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: .utf8)!
return output
}
func runShellCommandAsync(command: String, outputText: Binding<String>, showButton: Binding<Bool>?) {
func runShellCommandAsync(command: String, outputText: Binding<String>?, showButton: Binding<Bool>?) {
DispatchQueue.global(qos: .background).async {
let task = Process()
let pipe = Pipe()
@ -53,7 +38,9 @@ func runShellCommandAsync(command: String, outputText: Binding<String>, showButt
let data = fileHandle.availableData
if let output = String(data: data, encoding: .utf8), !output.isEmpty {
DispatchQueue.main.async {
outputText.wrappedValue += output
if let outputText = outputText {
outputText.wrappedValue += output
}
}
}
fileHandle.waitForDataInBackgroundAndNotify()
@ -71,34 +58,65 @@ func runShellCommandAsync(command: String, outputText: Binding<String>, showButt
}
}
func shell(_ command: String) -> String {
let task = Process()
let pipe = Pipe()
task.standardOutput = pipe
task.standardError = pipe
task.arguments = ["-c", command]
task.launchPath = "/bin/zsh"
task.standardInput = nil
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationWillTerminate(_ notification: Notification) {
runShellCommandAsync(command: "msg machine stop", outputText: nil, showButton: nil)
runShellCommandAsync(command: "kill $(ps aux | grep '[g]uile' | awk '{print $2}')", outputText: nil, showButton: nil)
runShellCommandAsync(command: "kill $(ps aux | grep '[q]emu' | awk '{print $2}')", outputText: nil, showButton: nil)
}
task.environment = [
"GUILE_LOAD_PATH": "/opt/homebrew/share/guile/site/3.0:/usr/local/share/guile/site/3.0/",
"GUILE_LOAD_COMPILED_PATH": "/opt/homebrew/lib/guile/3.0/site-ccache:/usr/local/lib/guile/3.0/site-ccache/",
"GUILE_SYSTEM_EXTENSIONS_PATH": "/opt/homebrew/lib/guile/3.0/extensions:/usr/local/lib/guile/3.0/extensions/",
"PATH": "$PATH:/opt/homebrew/bin:/usr/bin:/usr/local/bin:/bin"
]
func saveAppState() {
print("App is terminating. Saving state...")
UserDefaults.standard.set(Date(), forKey: "lastClosed")
}
var statusBarItem: NSStatusItem!
func applicationDidFinishLaunching(_ notification: Notification) {
statusBarItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
task.launch()
if let button = statusBarItem.button {
button.title = "MSG"
button.toolTip = "MSG App"
}
let menu = NSMenu()
menu.addItem(NSMenuItem(title: "Quit", action: #selector(quitApp), keyEquivalent: "q"))
statusBarItem.menu = menu
}
@objc func quitApp() {
let alert = NSAlert()
alert.messageText = "Are you sure you want to quit?"
alert.informativeText = "Do you want to quit the app and exit?"
alert.alertStyle = .warning
alert.addButton(withTitle: "Quit")
alert.addButton(withTitle: "Cancel")
let response = alert.runModal()
if response == .alertFirstButtonReturn {
NSApplication.shared.terminate(nil)
}
}
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: .utf8) ?? "Error reading output"
return output.trimmingCharacters(in: .whitespacesAndNewlines)
}
class AppState: ObservableObject {
@Published var output: String = "Please select an option \n"
static let shared = AppState()
}
@main
struct MSG_DesktopApp: App {
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()