updated a lot of stuff
This commit is contained in:
parent
0ba9201bc7
commit
a1a414ce83
2 changed files with 113 additions and 61 deletions
|
@ -8,18 +8,29 @@
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
struct ContentView: View {
|
struct ContentView: View {
|
||||||
@State public var output: String = "Please select an option \n"
|
@ObservedObject var appState = AppState.shared
|
||||||
private let maxLines = 300
|
private let maxLines = 300
|
||||||
@State private var showCancelButton: Bool = false
|
@State private var showCancelButton: Bool = false
|
||||||
|
@State private var showReinitPopup = false
|
||||||
|
@State private var showCancelPopup = false
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
VStack {
|
VStack {
|
||||||
Image(systemName: "globe")
|
// Image(systemName: "globe")
|
||||||
.imageScale(.large)
|
// .imageScale(.large)
|
||||||
.foregroundStyle(.tint)
|
// .foregroundStyle(.tint)
|
||||||
|
let appIcon = NSImage(named: NSImage.applicationIconName)
|
||||||
|
|
||||||
|
// Use SwiftUI's Image to display the icon
|
||||||
|
Image(nsImage: appIcon ?? NSImage())
|
||||||
|
.resizable() // Optional: if you want to resize the icon
|
||||||
|
.scaledToFit() // Optional: makes sure the aspect ratio is maintained
|
||||||
|
|
||||||
|
|
||||||
ScrollViewReader { proxy in
|
ScrollViewReader { proxy in
|
||||||
ScrollView {
|
ScrollView {
|
||||||
VStack(alignment: .leading) {
|
VStack(alignment: .leading) {
|
||||||
TextEditor(text: $output)
|
TextEditor(text: $appState.output)
|
||||||
.font(.system(.body, design: .monospaced))
|
.font(.system(.body, design: .monospaced))
|
||||||
.frame(minWidth: 750, minHeight: 300)
|
.frame(minWidth: 750, minHeight: 300)
|
||||||
.padding()
|
.padding()
|
||||||
|
@ -29,7 +40,7 @@ struct ContentView: View {
|
||||||
.id("bottom")
|
.id("bottom")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.onChange(of: output) { _, _ in
|
.onChange(of: appState.output) { _, _ in
|
||||||
trimLinesIfNeeded()
|
trimLinesIfNeeded()
|
||||||
withAnimation {
|
withAnimation {
|
||||||
proxy.scrollTo("bottom", anchor: .bottom)
|
proxy.scrollTo("bottom", anchor: .bottom)
|
||||||
|
@ -41,41 +52,64 @@ struct ContentView: View {
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
self.showCancelButton = true
|
self.showCancelButton = true
|
||||||
}
|
}
|
||||||
runShellCommandAsync(command: "msg machine init", outputText: $output, showButton: $showCancelButton)
|
runShellCommandAsync(command: "msg machine init", outputText: $appState.output, showButton: $showCancelButton)
|
||||||
|
|
||||||
}) {
|
}) {
|
||||||
Text("Init")
|
Text("Init")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Button(action: {
|
Button(action: {
|
||||||
DispatchQueue.main.async {
|
showReinitPopup.toggle()
|
||||||
self.showCancelButton = true
|
}) {
|
||||||
}
|
|
||||||
runShellCommandAsync(command: "rm -rf ~/.guix && msg machine init", outputText: $output, showButton: nil) }) {
|
|
||||||
Text("Reinit")
|
Text("Reinit")
|
||||||
|
}.confirmationDialog("Are you sure?", isPresented: $showReinitPopup, titleVisibility: .visible) {
|
||||||
|
Button("Confirm", role: .destructive) {
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
self.showCancelButton = true
|
||||||
|
runShellCommandAsync(command: "rm -rf ~/.guix && msg machine init", outputText: $appState.output, showButton: $showCancelButton)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
Button("Cancel", role: .cancel) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Button(action: {
|
Button(action: {
|
||||||
runShellCommandAsync(command: "msg machine start", outputText: $output, showButton: nil) }) {
|
runShellCommandAsync(command: "msg machine start", outputText: $appState.output, showButton: nil) }) {
|
||||||
Text("Start")
|
Text("Start")
|
||||||
}
|
}
|
||||||
if showCancelButton {
|
if showCancelButton {
|
||||||
Button(action: {
|
Button(action: {
|
||||||
runShellCommandAsync(command: "kill $(ps aux | grep '[g]uile' | awk '{print $2}')", outputText: $output, showButton: nil)
|
showCancelPopup.toggle()
|
||||||
runShellCommandAsync(command: "kill $(ps aux | grep '[q]emu' | awk '{print $2}')", outputText: $output, showButton: nil)
|
}) {
|
||||||
DispatchQueue.main.async {
|
|
||||||
self.showCancelButton = false
|
|
||||||
}}) {
|
|
||||||
Text("Cancel")
|
Text("Cancel")
|
||||||
|
}.confirmationDialog("Are you sure?", isPresented: $showCancelPopup, titleVisibility: .visible) {
|
||||||
|
Button("Confirm", role: .destructive) {
|
||||||
|
|
||||||
|
runShellCommandAsync(command: "kill $(ps aux | grep '[g]uile' | awk '{print $2}')", outputText: $appState.output, showButton: nil)
|
||||||
|
runShellCommandAsync(command: "kill $(ps aux | grep '[q]emu' | awk '{print $2}')", outputText: $appState.output, showButton: nil)
|
||||||
|
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
|
||||||
|
self.showCancelButton = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Button("Cancel", role: .cancel) {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if !showCancelButton{
|
if !showCancelButton{
|
||||||
Button(action: {
|
Button(action: {
|
||||||
runShellCommandAsync(command: "msg machine stop", outputText: $output, showButton: nil) }) {
|
runShellCommandAsync(command: "msg machine stop", outputText: $appState.output, showButton: nil) }) {
|
||||||
Text("Stop")
|
Text("Stop")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Button(action: {
|
Button(action: {
|
||||||
runShellCommandAsync(command: "osascript -e 'tell application \"Terminal\" to do script \"msg shell\"' -e 'tell application \"Terminal\" to activate'", outputText: $output, showButton: nil)
|
runShellCommandAsync(command: "osascript -e 'tell application \"Terminal\" to do script \"msg shell\"' -e 'tell application \"Terminal\" to activate'", outputText: $appState.output, showButton: nil)
|
||||||
|
|
||||||
}) {
|
}) {
|
||||||
Text("Shell")
|
Text("Shell")
|
||||||
|
@ -90,12 +124,12 @@ struct ContentView: View {
|
||||||
|
|
||||||
|
|
||||||
private func trimLinesIfNeeded() {
|
private func trimLinesIfNeeded() {
|
||||||
let lines = output.split(separator: "\n")
|
let lines = AppState.shared.output.split(separator: "\n")
|
||||||
|
|
||||||
if lines.count > maxLines {
|
if lines.count > maxLines {
|
||||||
_ = lines.count - maxLines
|
_ = lines.count - maxLines
|
||||||
let trimmedLines = lines.suffix(maxLines) // Keep only the last 300 lines
|
let trimmedLines = lines.suffix(maxLines)
|
||||||
output = trimmedLines.joined(separator: "\n") // Join back to a single string
|
AppState.shared.output = trimmedLines.joined(separator: "\n")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,25 +6,10 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
import AppKit
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
func oldshell(_ command: String) -> String {
|
func runShellCommandAsync(command: String, outputText: Binding<String>?, showButton: Binding<Bool>?) {
|
||||||
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 {
|
DispatchQueue.global(qos: .background).async {
|
||||||
let task = Process()
|
let task = Process()
|
||||||
let pipe = Pipe()
|
let pipe = Pipe()
|
||||||
|
@ -53,7 +38,9 @@ func runShellCommandAsync(command: String, outputText: Binding<String>, showButt
|
||||||
let data = fileHandle.availableData
|
let data = fileHandle.availableData
|
||||||
if let output = String(data: data, encoding: .utf8), !output.isEmpty {
|
if let output = String(data: data, encoding: .utf8), !output.isEmpty {
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
outputText.wrappedValue += output
|
if let outputText = outputText {
|
||||||
|
outputText.wrappedValue += output
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fileHandle.waitForDataInBackgroundAndNotify()
|
fileHandle.waitForDataInBackgroundAndNotify()
|
||||||
|
@ -71,34 +58,65 @@ func runShellCommandAsync(command: String, outputText: Binding<String>, showButt
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func shell(_ command: String) -> String {
|
class AppDelegate: NSObject, NSApplicationDelegate {
|
||||||
let task = Process()
|
func applicationWillTerminate(_ notification: Notification) {
|
||||||
let pipe = Pipe()
|
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.standardOutput = pipe
|
func saveAppState() {
|
||||||
task.standardError = pipe
|
print("App is terminating. Saving state...")
|
||||||
task.arguments = ["-c", command]
|
UserDefaults.standard.set(Date(), forKey: "lastClosed")
|
||||||
task.launchPath = "/bin/zsh"
|
}
|
||||||
task.standardInput = nil
|
|
||||||
|
|
||||||
task.environment = [
|
var statusBarItem: NSStatusItem!
|
||||||
"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()
|
func applicationDidFinishLaunching(_ notification: Notification) {
|
||||||
|
statusBarItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
|
||||||
|
|
||||||
let data = pipe.fileHandleForReading.readDataToEndOfFile()
|
if let button = statusBarItem.button {
|
||||||
let output = String(data: data, encoding: .utf8) ?? "Error reading output"
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return output.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class AppState: ObservableObject {
|
||||||
|
@Published var output: String = "Please select an option \n"
|
||||||
|
|
||||||
|
static let shared = AppState()
|
||||||
|
}
|
||||||
|
|
||||||
@main
|
@main
|
||||||
struct MSG_DesktopApp: App {
|
struct MSG_DesktopApp: App {
|
||||||
|
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
|
||||||
|
|
||||||
var body: some Scene {
|
var body: some Scene {
|
||||||
WindowGroup {
|
WindowGroup {
|
||||||
ContentView()
|
ContentView()
|
||||||
|
|
Loading…
Add table
Reference in a new issue