Skip to content

Instantly share code, notes, and snippets.

View palmin's full-sized avatar

Anders Borum palmin

View GitHub Profile
// check if folder contains file with given name after having performed
// file coordination
func fileExists(_ filename: String, in folder: URL) async throws -> Bool {
let found: Bool = try await withCheckedThrowingContinuation { cont in
let array = NSMutableArray()
@Sendable func complete(_ ok: Bool, _ error: Error?) {
objc_sync_enter(array)
let completed = array.count > 0
array.add(1)
objc_sync_exit(self)
//
// Promise.swift
// ShellFish
//
// Created by Anders Borum on 25/02/2019.
// Copyright © 2019 Anders Borum. All rights reserved.
//
import Foundation
import SwiftUI
#if os(iOS)
import UIKit
struct BranchPickerButton: UIViewRepresentable {
public var title: String = ""
public let producer: () async throws -> [(String, String)]
@Binding public var value: String
extension Array where Element == UIImage {
func gifData(frameDelay: TimeInterval = 0.2) -> Data? {
let data = NSMutableData()
guard let destination = CGImageDestinationCreateWithData(data, kUTTypeGIF, self.count, nil) else {
return nil
}
let frameProperties: CFDictionary = [kCGImagePropertyGIFDictionary as String: [(kCGImagePropertyGIFDelayTime as String): frameDelay]] as CFDictionary
let fileProperties: CFDictionary = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFLoopCount as String: 0]] as CFDictionary
CGImageDestinationSetProperties(destination, fileProperties)
private var constantFloatSelection = NSMakeRange(0, 0)
public override func beginFloatingCursor(at point: CGPoint) {
// remember how it starts out
constantFloatSelection = selectedRange
super.beginFloatingCursor(at: point)
}
public override func updateFloatingCursor(at point: CGPoint) {
@palmin
palmin / .shellfishrc
Last active September 25, 2022 07:54
# include this from .bashrc, .zshrc or
# another shell startup file
# this script does nothing outside ShellFish
if [ "$LC_TERMINAL" = "ShellFish" ]; then
printURIComponent() {
awk 'BEGIN {while (y++ < 125) z[sprintf("%c", y)] = y
    while (y = substr(ARGV[1], ++j, 1))
    q = y ~ /[a-zA-Z0-9]/ ? q y : q sprintf("%%%02X", z[y])
    printf("%s", q)}' "$1"
// Keep polling the memory usage (through number of wired pages) every millisecond to discover if usage
// stays below some limit.
//
// let limit = MemoryGrowthLimit(megabytes: 5)
// hardWorkingMemoryConsumingMethodCall()
// XCTAssertFalse(limit.exceeded)
//
class MemoryGrowthLimit {
private let pollingInterval = 0.001
console.log("Hello World!")
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: pink; icon-glyph: user;
// lookup gravatar for contacts without images.
var MD5 = function (string) {
function RotateLeft(lValue, iShiftBits) {
return (lValue<<iShiftBits) | (lValue>>>(32-iShiftBits));
}