Skip to content

Instantly share code, notes, and snippets.

@dillera
Created January 5, 2025 19:44
Show Gist options
  • Save dillera/62226e425c47cca362bec90b529b4745 to your computer and use it in GitHub Desktop.
Save dillera/62226e425c47cca362bec90b529b4745 to your computer and use it in GitHub Desktop.
TNSF OSX APP
import SwiftUI
import Foundation
// TNFSController handles the TNFS process management
class TNFSController: ObservableObject {
@Published var isRunning = false
@Published var sharedDirectory = FileManager.default.homeDirectoryForCurrentUser.path
@Published var statusMessage = "TNFS Service Stopped"
private var process: Process?
func startService() {
guard !isRunning else { return }
let process = Process()
process.executableURL = URL(fileURLWithPath: "/path/to/tnfs") // Update with actual path
process.arguments = [sharedDirectory]
let pipe = Pipe()
process.standardOutput = pipe
process.standardError = pipe
do {
try process.run()
self.process = process
isRunning = true
statusMessage = "TNFS Service Running - Sharing \(sharedDirectory)"
} catch {
statusMessage = "Error starting TNFS: \(error.localizedDescription)"
}
}
func stopService() {
guard isRunning else { return }
process?.terminate()
process = nil
isRunning = false
statusMessage = "TNFS Service Stopped"
}
func selectDirectory() {
let panel = NSOpenPanel()
panel.canChooseFiles = false
panel.canChooseDirectories = true
panel.allowsMultipleSelection = false
panel.directoryURL = FileManager.default.homeDirectoryForCurrentUser
if panel.runModal() == .OK {
if let url = panel.url {
sharedDirectory = url.path
if isRunning {
stopService()
startService()
}
}
}
}
}
// Main view for the application
struct ContentView: View {
@StateObject private var controller = TNFSController()
var body: some View {
VStack(spacing: 20) {
Text("TNFS Controller")
.font(.title)
.padding()
HStack {
Text("Shared Directory:")
Text(controller.sharedDirectory)
.truncationMode(.head)
.frame(maxWidth: .infinity, alignment: .leading)
Button("Browse") {
controller.selectDirectory()
}
}
.padding(.horizontal)
HStack(spacing: 20) {
Button(action: {
controller.startService()
}) {
Text("Start Service")
.frame(width: 100)
}
.disabled(controller.isRunning)
Button(action: {
controller.stopService()
}) {
Text("Stop Service")
.frame(width: 100)
}
.disabled(!controller.isRunning)
}
Text(controller.statusMessage)
.padding()
.foregroundColor(controller.isRunning ? .green : .red)
}
.frame(width: 500, height: 200)
}
}
// App entry point
@main
struct TNFSControllerApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
.windowResizable(false)
}
}
@dillera
Copy link
Author

dillera commented Jan 5, 2025

This SwiftUI application provides a user-friendly interface to control the TNFS service. Here are the key components and features:

The TNFSController class manages the TNFS process, handling start/stop operations and directory selection.
The ContentView provides the user interface with:

A directory selector that defaults to the user's home directory
Start and Stop buttons that are appropriately enabled/disabled based on service status
Status display showing the current state of the service
A fixed window size for a clean appearance

To use this code, you'll need to:

Replace /path/to/tnfs in the TNFSController class with the actual path to your TNFS binary
Create a new Xcode project (macOS App with SwiftUI interface)
Replace the default ContentView.swift content with this code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment