Skip to content

Instantly share code, notes, and snippets.

@jbadger3
jbadger3 / PipeExample.swift
Created November 29, 2022 22:54
Example of piping together commands in swift.
#!/usr/bin/swift
import Foundation
let catProcess = Process()
catProcess.executableURL = URL(fileURLWithPath: "/bin/cat")
catProcess.arguments = ["PipeExample.swift"]
let pipe1 = Pipe()
let grepProcess = Process()
@jbadger3
jbadger3 / countdown.swift
Created November 29, 2022 21:56
A swift script used to demonstrate stream buffering and flushing
#!/usr/bin/swift
import Foundation
let arguments = CommandLine.arguments
let startingValue = Int(arguments[1])!
var flush = false
if arguments.count == 3 {
@jbadger3
jbadger3 / readabilityHandler.swift
Created November 29, 2022 21:22
Demo using readabilityHandler to read data asynchronously from FileHandle
import Foundation
let outputPipe = Pipe()
let process = Process()
process.executableURL = URL(fileURLWithPath:"/bin/bash")
process.arguments = ["-c", "ls"]
process.standardOutput = outputPipe
outputPipe.fileHandleForReading.readabilityHandler = { fileHandle in
@jbadger3
jbadger3 / FileHandle_async-await.swift
Created November 29, 2022 20:42
Demo using async-await to read from a FileHandle
import Foundation
let outputPipe = Pipe()
let process = Process()
process.executableURL = URL(fileURLWithPath:"/bin/bash")
process.arguments = ["-c", "ls"]
process.standardOutput = outputPipe
try? process.run()
@jbadger3
jbadger3 / readabilityHandler_noEOFCheck.swift
Last active November 29, 2022 21:04
FileHandle readability handler demo with no EOF check
import Foundation
let outputPipe = Pipe()
let process = Process()
process.executableURL = URL(fileURLWithPath:"/bin/bash")
process.arguments = ["-c", "ls"]
process.standardOutput = outputPipe
outputPipe.fileHandleForReading.readabilityHandler = { fileHandle in
@jbadger3
jbadger3 / FileHandle_bytes.swift
Created November 29, 2022 19:08
Use of async-await for reading data from a FileHandle
for try await line in FileHandle.standardOutput.bytes.lines {
///Do something with output
}
@jbadger3
jbadger3 / sub_process.swift
Created November 28, 2022 19:57
Example of creating a subprocess in Swift.
import Foundation
let process = Process()
process.executableURL = URL(fileURLWithPath:"/bin/bash")
process.arguments = ["-c", "ls"]
try? process.run()
let standardInputFH = process.standardInput as! FileHandle
let standardOutputFH = process.standardOutput as! FileHandle
let standardErrorFH = process.standardError as! FileHandle
@jbadger3
jbadger3 / ProcessInfo.swift
Created November 27, 2022 01:01
Getting process information and file descriptors in Swift
import Foundation
let processInfo = ProcessInfo()
print("COMMAND: \(processInfo.processName)")
print("PID: \(processInfo.processIdentifier)")
print("USER \(processInfo.userName)")
print("stdin FD: \(FileHandle.standardInput.fileDescriptor)")
print("stdout FD: \(FileHandle.standardOutput.fileDescriptor)")
print("stderr FD: \(FileHandle.standardError.fileDescriptor)")
@jbadger3
jbadger3 / line_edit_demo.swift
Created September 9, 2022 18:07
on_command_line_applications_in_swift
import Terminus
let terminal = Terminal.shared
let lineEditor = LineEditor()
lineEditor.bufferHandler = {
var shouldWriteBuffer = false
if let greenRange = lineEditor.buffer.range(of: "green") {
lineEditor.buffer[greenRange].terminalTextAttributes = [.color(Color(r: 0, g: 255, b: 0))]
shouldWriteBuffer = true
@jbadger3
jbadger3 / terminus_menu.swift
Created September 9, 2022 17:58
on_command_line_applications_in_swift
import Foundation
import Terminus
let terminal = Terminal.shared
terminal.clearScreen()
terminal.cursor.moveToHome()
let palette = XTermPalette()
let itemColor = palette.Aquamarine2