Skip to content

Instantly share code, notes, and snippets.

@GeorgeLyon
Last active July 4, 2025 02:15
Show Gist options
  • Select an option

  • Save GeorgeLyon/bbd443dcabef5ca5ae71ae83db6524ef to your computer and use it in GitHub Desktop.

Select an option

Save GeorgeLyon/bbd443dcabef5ca5ae71ae83db6524ef to your computer and use it in GitHub Desktop.

Revisions

  1. GeorgeLyon revised this gist Jul 8, 2020. 1 changed file with 6 additions and 11 deletions.
    17 changes: 6 additions & 11 deletions swift-ui-commandline-tool.swift
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,18 @@
    #!/usr/bin/env xcrun -sdk macosx swift

    // Displays UI which can interact with the commandline
    // Displays UI in an NSWindow which can interact with the commandline
    // Usage: `echo "Bar" | ./swift-ui-commandline-tool.swift`

    import Foundation
    import SwiftUI

    extension CommandLine {
    static let input: String = { AnyIterator { readLine(strippingNewline: false) }.joined() }()
    static let input: String = { AnyIterator { readLine() }.joined() }()
    }

    struct ContentView: View {
    var body: some View {
    struct App: SwiftUI.App {
    var body: some Scene {
    WindowGroup {
    VStack {
    Text("Hello, George!")
    Button("Print \"Foo\"") { print("Foo") }
    @@ -20,13 +21,7 @@ struct ContentView: View {
    }
    .padding(100)
    }
    }

    struct App: SwiftUI.App {
    var body: some SwiftUI.Scene {
    WindowGroup {
    ContentView()
    }
    .windowStyle(HiddenTitleBarWindowStyle())
    }
    }
    App.main()
  2. GeorgeLyon revised this gist Jul 8, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion swift-ui-commandline-tool.swift
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/usr/bin/env xcrun -sdk macosx swift

    // Displays UI in an NSWindow which can interact with the commandline
    // Displays UI which can interact with the commandline
    // Usage: `echo "Bar" | ./swift-ui-commandline-tool.swift`

    import Foundation
  3. GeorgeLyon revised this gist Jul 8, 2020. 1 changed file with 6 additions and 25 deletions.
    31 changes: 6 additions & 25 deletions swift-ui-commandline-tool.swift
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,6 @@
    // Usage: `echo "Bar" | ./swift-ui-commandline-tool.swift`

    import Foundation
    import Cocoa
    import SwiftUI

    extension CommandLine {
    @@ -23,29 +22,11 @@ struct ContentView: View {
    }
    }

    class AppDelegate: NSObject, NSApplicationDelegate {
    var window: NSWindow!
    func applicationDidFinishLaunching(_ aNotification: Notification) {
    let contentView = ContentView()
    let window = NSWindow(
    contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
    styleMask: [.titled, .closable, .fullSizeContentView],
    backing: .buffered, defer: false)
    window.center()
    window.contentView = NSHostingView(rootView: contentView)
    window.titlebarAppearsTransparent = true
    window.level = .modalPanel
    // The default close action causes a crash, and this way we can set the exit status
    let closeButton = window.standardWindowButton(.closeButton)!
    closeButton.target = self
    closeButton.action = #selector(close)
    window.makeKeyAndOrderFront(nil)
    }
    @objc func close() {
    exit(1)
    struct App: SwiftUI.App {
    var body: some SwiftUI.Scene {
    WindowGroup {
    ContentView()
    }
    }
    }
    let application = NSApplication.shared
    let delegate = AppDelegate()
    application.delegate = delegate
    application.run()
    App.main()
  4. GeorgeLyon revised this gist Sep 19, 2019. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions swift-ui-commandline-tool.swift
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    #!/usr/bin/env xcrun -sdk macosx swift

    // Displays UI in an NSWindow which can interact with the commandline
    // Usage: `echo "Bar" | ./swift-ui-commandline-tool.swift`

    import Foundation
  5. GeorgeLyon revised this gist Sep 19, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion swift-ui-commandline-tool.swift
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ import Cocoa
    import SwiftUI

    extension CommandLine {
    static let input: String = { AnyIterator { readLine() }.joined() }()
    static let input: String = { AnyIterator { readLine(strippingNewline: false) }.joined() }()
    }

    struct ContentView: View {
  6. GeorgeLyon revised this gist Sep 19, 2019. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions swift-ui-commandline-tool.swift
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    #!/usr/bin/env xcrun -sdk macosx swift

    // Usage: `echo "Bar" | ./swift-ui-commandline-tool.swift`

    import Foundation
    import Cocoa
    import SwiftUI
  7. GeorgeLyon revised this gist Sep 19, 2019. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions swift-ui-commandline-tool.swift
    Original file line number Diff line number Diff line change
    @@ -5,9 +5,7 @@ import Cocoa
    import SwiftUI

    extension CommandLine {

    static let input: String = { AnyIterator { readLine() }.joined() }()

    }

    struct ContentView: View {
  8. GeorgeLyon revised this gist Sep 19, 2019. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions swift-ui-commandline-tool.swift
    Original file line number Diff line number Diff line change
    @@ -11,12 +11,10 @@ extension CommandLine {
    }

    struct ContentView: View {

    var body: some View {
    VStack {
    Text("Hello, George!")
    Button("Print \"Foo\"") { print("Foo") }

    Button("Echo Input") { print(CommandLine.input) }
    Button("Done") { exit(0) }
    }
  9. GeorgeLyon revised this gist Sep 19, 2019. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions swift-ui-commandline-tool.swift
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,23 @@
    #!/usr/bin/env xcrun -sdk macosx swift

    import Foundation
    import Cocoa
    import SwiftUI

    extension CommandLine {

    static let input: String = { AnyIterator { readLine() }.joined() }()

    }

    struct ContentView: View {

    var body: some View {
    VStack {
    Text("Hello, George!")
    Button("Print \"Foo\"") { print("Foo") }

    Button("Echo Input") { print(CommandLine.input) }
    Button("Done") { exit(0) }
    }
    .padding(100)
  10. GeorgeLyon revised this gist Sep 19, 2019. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions swift-ui-commandline-tool.swift
    Original file line number Diff line number Diff line change
    @@ -7,11 +7,10 @@ struct ContentView: View {
    var body: some View {
    VStack {
    Text("Hello, George!")
    .frame(maxWidth: .infinity, maxHeight: .infinity)
    Button("Print \"Foo\"") { print("Foo") }
    Button("Done") { exit(0) }
    .padding(.bottom, 10)
    }
    .padding(100)
    }
    }

  11. GeorgeLyon created this gist Sep 19, 2019.
    43 changes: 43 additions & 0 deletions swift-ui-commandline-tool.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    #!/usr/bin/env xcrun -sdk macosx swift

    import Cocoa
    import SwiftUI

    struct ContentView: View {
    var body: some View {
    VStack {
    Text("Hello, George!")
    .frame(maxWidth: .infinity, maxHeight: .infinity)
    Button("Print \"Foo\"") { print("Foo") }
    Button("Done") { exit(0) }
    .padding(.bottom, 10)
    }
    }
    }

    class AppDelegate: NSObject, NSApplicationDelegate {
    var window: NSWindow!
    func applicationDidFinishLaunching(_ aNotification: Notification) {
    let contentView = ContentView()
    let window = NSWindow(
    contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
    styleMask: [.titled, .closable, .fullSizeContentView],
    backing: .buffered, defer: false)
    window.center()
    window.contentView = NSHostingView(rootView: contentView)
    window.titlebarAppearsTransparent = true
    window.level = .modalPanel
    // The default close action causes a crash, and this way we can set the exit status
    let closeButton = window.standardWindowButton(.closeButton)!
    closeButton.target = self
    closeButton.action = #selector(close)
    window.makeKeyAndOrderFront(nil)
    }
    @objc func close() {
    exit(1)
    }
    }
    let application = NSApplication.shared
    let delegate = AppDelegate()
    application.delegate = delegate
    application.run()