Skip to content

Instantly share code, notes, and snippets.

View backslash-f's full-sized avatar
💻
Keep calm and Swift

Fernando Fernandes backslash-f

💻
Keep calm and Swift
View GitHub Profile
@backslash-f
backslash-f / Swift.playground
Created January 23, 2022 19:54
Charles + Swift Playgrounds
// "Charles Enabler"
// https://stackoverflow.com/a/57253011/584548
public class NetworkEnabler: NSObject, URLSessionDelegate {
public func urlSession(_ session: URLSession,
didReceive challenge: URLAuthenticationChallenge,
completionHandler: (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
completionHandler(.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!))
}
}
@backslash-f
backslash-f / heroku-pinger.sh
Last active February 3, 2022 08:14
Keeps Heroku servers awake
#!/bin/bash
# - Curls Heroku servers indicated in the "hosts" file.
# - Logs actions to log files (unique per host), including the response HTTP status
# - Clean up log files if > 5mb
#
# Simply add the target hosts in the "hosts" file, like:
# my-server-01.herokuapp.com
# my-server-02.herokuapp.com
# NOTICE: There must be a new line at the end of the "hosts" file.
@backslash-f
backslash-f / ddns-start
Created December 10, 2021 20:40
Merlin + DuckDNS with Double NAT
#!/bin/sh
# register a subdomain at https://www.duckdns.org/ to get your token
# put 'hostname|token' in the 'Host Name' field under DDNS (custom)
# e.g. myhost|abcdefgh-1234-5678-9876-f71b0ed7a7fe
DDNS_HOSTNAME_FIELD=$(nvram get ddns_hostname_x)
SUBDOMAIN=$(echo "$DDNS_HOSTNAME_FIELD" | awk -F'|' '{print $1}')
TOKEN=$(echo "$DDNS_HOSTNAME_FIELD" | awk -F'|' '{print $2}')
IP="$(curl -fs4 https://myip.dnsomatic.com/)"
@backslash-f
backslash-f / Decodable.swift
Last active May 16, 2023 11:32
Swift Decodable example.
#!/usr/bin/swift
import Foundation
// TINY EXAMPLE
/*
let jsonData = Data(
"""
{
@backslash-f
backslash-f / .config
Last active October 19, 2023 20:39
Gist for neofetch .config
# See this wiki page for more info:
# https://github.com/dylanaraps/neofetch/wiki/Customizing-Info
#
# brew install neofetch
# mkdir ~/.config/neofetch
# touch config.conf
# pbpaste > config.conf
print_info() {
info title
@backslash-f
backslash-f / sublime-remove-zero-width-spaces
Last active October 13, 2020 09:09
[Sublime] Custom plugin to remove zero width spaces (u200b) / key bind
###
# Save this as a .py file in your user package dir, e.g.:
# /Users/<name>/Library/Application Support/Sublime Text 3/Packages/User/u200b_killer.py
###
# Source: https://rdzhou.github.io/2017/07/31/How-to-Remove-u200b/
import sublime, sublime_plugin
class ShowU200b(sublime_plugin.TextCommand):
@backslash-f
backslash-f / appendSameStringToCurrentFiles.sh
Last active September 3, 2020 13:36
Append same String in many files (e.g. @1x, @2x, @3x, etc)
# Source: https://stackoverflow.com/a/24302276/584548
for i in *; do name="${i%.*}"; mv "$i" "${name}👉🏻👉🏻👉🏻@1x👈🏻👈🏻👈🏻${i#$name}"; done
@backslash-f
backslash-f / View+Extensions.swift
Created July 28, 2020 20:49
Add "print" capabilities to SwiftUI Views
import SwiftUI
extension View {
/// Source: https://stackoverflow.com/a/59473712/584548
func log(_ vars: Any...) -> some View {
vars.forEach { print("\($0)") }
return EmptyView()
}
}
@backslash-f
backslash-f / Formatters.swift
Last active June 27, 2020 06:47
Convert Swift data types to and from their string representations.
/*
See "Displaying Human-Friendly Content"
https://developer.apple.com/documentation/foundation/formatter/displaying_human-friendly_content
*/
import Foundation
/*:
## Number Formatter
Provides localized representations of units and measurements.
*/
@backslash-f
backslash-f / Shell.swift
Last active June 13, 2023 19:11
Invoke Zsh commands via a Swift script
#!/usr/bin/swift
import Foundation
func shell(_ command: String) {
let task = Process()
task.launchPath = "/bin/zsh"
task.arguments = ["-c", command]
let pipe = Pipe()