Skip to content

Instantly share code, notes, and snippets.

@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_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 / swiftncurses_package_example.swift
Created September 9, 2022 16:09
terminus_medium_article
// swift-tools-version:5.5
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
...
let package = Package(
name: "SwiftNCurses",
products: [,
targets: [
@jbadger3
jbadger3 / PostData.swift
Last active October 1, 2021 22:17
Gists for the Medium post 'Paving A Road Between JSON And CoreData'
struct PostData: Decodable {
let userId: Int
let id: Int
let title: String
let body: String
}
@jbadger3
jbadger3 / parsing_character_entities_NSAttributedString.swift
Last active March 19, 2024 15:24
Gist for the article 'Parsing Character Entities from HTML/XML Content In Swift' on Medium.com
extension String {
init?(htmlEncodedString: String) {
guard let data = htmlEncodedString.data(using: .utf8) else {
return nil
}
let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [
.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue
]
guard let attributedString = try? NSAttributedString(data: data, options: options, documentAttributes: nil) else {
@jbadger3
jbadger3 / NumericTextField.swift
Last active April 13, 2021 14:55
Working with TextField Views in SwiftUI
struct NumericTextField<T>: UIViewRepresentable {
private var title: String
@Binding var value: T
private var formatter: NumberFormatter
@State var errorMessage = ""
private var keyboardType: UIKeyboardType
init(title: String = "", value: Binding<T>, numberFormatter: NumberFormatter, keyboardType: UIKeyboardType) {
self.title = title
self._value = value
@jbadger3
jbadger3 / SwiftUI_overlapping_navigation_titles_content_view.swift
Created December 30, 2020 15:56
SwiftUI: Overlapping Navigation Titles
struct FoodGroup {
var name: String
var examples: [String]
}
struct ContentView: View {
var foodGroups: [FoodGroup] {
let fruits = FoodGroup(name: "Fruit", examples: ["Apple", "Banana", "Pear", "Peach", "Mango", "Orange", "Strawberry", "Watermelon", "Pineapple", "Lemon", "Lime", "Cherry", "Date", "Plum", "Apricot", "Blueberry", "Blackberry", "Cranberry", "Kiwi", "Nectarine"])
let vegetables = FoodGroup(name: "Vegetables", examples: ["Lettuce", "Carrot", "Beet", "Broccoli", "Corn", "Celery", "Chicory", "Kale", "Spinach", "Yarrow", "Brussels sprouts", "Arugula", "Cauliflower", "Turnip", "Sweet Potato"])
return [fruits, vegetables]
@jbadger3
jbadger3 / NavigableListButtonFix.swift
Last active November 19, 2020 19:10
NavigableListWithInteractiveCells
Button(action: {
currentFavorite = teamMember
}, label: {
Image(systemName: (teamMember == currentFavorite ? "heart.fill" : "heart"))
.foregroundColor(.pink)
})
.buttonStyle(PlainButtonStyle())
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
let contentView = ContentView()
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
@main
struct TestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.onAppear(perform: UIApplication.shared.addTapGestureRecognizer)
}
}
}