Skip to content

Instantly share code, notes, and snippets.

@jbadger3
jbadger3 / terminus_text_and_color.swift
Created September 9, 2022 17:28
on_command_line_applications_in_swift
import Terminus
let terminal = Terminal.shared
terminal.write("I am bold and underlined.\n", attributes: [.bold, .underline])
let greenColor = Color(r:0, g:255, b:0)
terminal.write("Grass is green.\n", attributes: [.color(greenColor)])
let palette = XTermPalette()
let blueOneYellow = ColorPair(foreground: palette.Blue1, background: palette.Yellow1)
@jbadger3
jbadger3 / terminus_attributed_strings.swift
Created September 9, 2022 17:35
on_command_line_applications_in_swift
import Foundation
import Terminus
let terminal = Terminal.shared
var attributedString = AttributedString("Hello, bold, underlined, world.")
if let boldRange = attributedString.range(of: "bold") {
attributedString[boldRange].terminalTextAttributes = [.bold]
}
if let underlinedRange = attributedString.range(of: "underlined") {
@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
@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 / 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 / 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 / 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 / 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_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.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