107 lines
3.3 KiB
Swift
107 lines
3.3 KiB
Swift
//
|
|
// MSG_DesktopApp.swift
|
|
// MSG Desktop
|
|
//
|
|
// Created by Chad Nelson on 3/8/25.
|
|
//
|
|
|
|
import SwiftUI
|
|
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>?) {
|
|
DispatchQueue.global(qos: .background).async {
|
|
let task = Process()
|
|
let pipe = Pipe()
|
|
|
|
task.standardOutput = pipe
|
|
task.standardError = pipe
|
|
task.arguments = ["-c", command]
|
|
task.launchPath = "/bin/zsh"
|
|
task.standardInput = 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"
|
|
]
|
|
|
|
task.launch()
|
|
|
|
let fileHandle = pipe.fileHandleForReading
|
|
fileHandle.waitForDataInBackgroundAndNotify()
|
|
|
|
var observer: NSObjectProtocol?
|
|
|
|
observer = NotificationCenter.default.addObserver(forName: .NSFileHandleDataAvailable, object: fileHandle, queue: nil) { _ in
|
|
let data = fileHandle.availableData
|
|
if let output = String(data: data, encoding: .utf8), !output.isEmpty {
|
|
DispatchQueue.main.async {
|
|
outputText.wrappedValue += output
|
|
}
|
|
}
|
|
fileHandle.waitForDataInBackgroundAndNotify()
|
|
}
|
|
|
|
task.waitUntilExit()
|
|
|
|
if let observer = observer {
|
|
NotificationCenter.default.removeObserver(observer)
|
|
}
|
|
|
|
if showButton != nil {
|
|
showButton?.wrappedValue = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
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
|
|
|
|
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"
|
|
]
|
|
|
|
task.launch()
|
|
|
|
let data = pipe.fileHandleForReading.readDataToEndOfFile()
|
|
let output = String(data: data, encoding: .utf8) ?? "Error reading output"
|
|
|
|
return output.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
}
|
|
|
|
|
|
@main
|
|
struct MSG_DesktopApp: App {
|
|
var body: some Scene {
|
|
WindowGroup {
|
|
ContentView()
|
|
}
|
|
}
|
|
}
|