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

@ -8,18 +8,29 @@
import SwiftUI
struct ContentView: View {
@State public var output: String = "Please select an option \n"
@ObservedObject var appState = AppState.shared
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 {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
// Image(systemName: "globe")
// .imageScale(.large)
// .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
ScrollView {
VStack(alignment: .leading) {
TextEditor(text: $output)
TextEditor(text: $appState.output)
.font(.system(.body, design: .monospaced))
.frame(minWidth: 750, minHeight: 300)
.padding()
@ -29,7 +40,7 @@ struct ContentView: View {
.id("bottom")
}
}
.onChange(of: output) { _, _ in
.onChange(of: appState.output) { _, _ in
trimLinesIfNeeded()
withAnimation {
proxy.scrollTo("bottom", anchor: .bottom)
@ -41,41 +52,64 @@ struct ContentView: View {
DispatchQueue.main.async {
self.showCancelButton = true
}
runShellCommandAsync(command: "msg machine init", outputText: $output, showButton: $showCancelButton)
runShellCommandAsync(command: "msg machine init", outputText: $appState.output, showButton: $showCancelButton)
}) {
Text("Init")
}
Button(action: {
DispatchQueue.main.async {
self.showCancelButton = true
}
runShellCommandAsync(command: "rm -rf ~/.guix && msg machine init", outputText: $output, showButton: nil) }) {
showReinitPopup.toggle()
}) {
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: {
runShellCommandAsync(command: "msg machine start", outputText: $output, showButton: nil) }) {
runShellCommandAsync(command: "msg machine start", outputText: $appState.output, showButton: nil) }) {
Text("Start")
}
if showCancelButton {
Button(action: {
runShellCommandAsync(command: "kill $(ps aux | grep '[g]uile' | awk '{print $2}')", outputText: $output, showButton: nil)
runShellCommandAsync(command: "kill $(ps aux | grep '[q]emu' | awk '{print $2}')", outputText: $output, showButton: nil)
DispatchQueue.main.async {
self.showCancelButton = false
}}) {
showCancelPopup.toggle()
}) {
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{
Button(action: {
runShellCommandAsync(command: "msg machine stop", outputText: $output, showButton: nil) }) {
runShellCommandAsync(command: "msg machine stop", outputText: $appState.output, showButton: nil) }) {
Text("Stop")
}
}
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")
@ -90,12 +124,12 @@ struct ContentView: View {
private func trimLinesIfNeeded() {
let lines = output.split(separator: "\n")
let lines = AppState.shared.output.split(separator: "\n")
if lines.count > maxLines {
_ = lines.count - maxLines
let trimmedLines = lines.suffix(maxLines) // Keep only the last 300 lines
output = trimmedLines.joined(separator: "\n") // Join back to a single string
let trimmedLines = lines.suffix(maxLines)
AppState.shared.output = trimmedLines.joined(separator: "\n")
}
}
}