Skip to content

Instantly share code, notes, and snippets.

💙
Loving Life

Ben Leggiero BenLeggiero

💙
Loving Life
Block or report user

Report or block BenLeggiero

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@BenLeggiero
BenLeggiero / runAnimationGroup shim.swift
Created May 7, 2019
NSAnimationContext.runAnimationGroup in OS X 10.11 and older
View runAnimationGroup shim.swift
// By Ben Leggiero on 2019-05-07
// License is BH-1-PS: https://github.com/BlueHuskyStudios/Licenses/blob/master/Licenses/BH-1-PS.txt
import AppKit
public extension NSAnimationContext {
@BenLeggiero
BenLeggiero / CompilationException
Last active Dec 24, 2018
Kotlin Compiler Crashes
View CompilationException
<Exception from standalone Kotlin compiler>
Kotlin: [Internal Error] org.jetbrains.kotlin.codegen.CompilationException: Back-end (JVM) Internal error: wrong code generated
org.jetbrains.kotlin.codegen.CompilationException Back-end (JVM) Internal error: Couldn't transform method node:
reposition (Lorg/bh/tools/base/math/geometry/FractionRect;Lorg/bh/tools/base/math/geometry/FractionRect;)Lorg/bh/tools/base/math/geometry/FractionRect;:
@Lorg/jetbrains/annotations/NotNull;() // invisible
@Lorg/jetbrains/annotations/NotNull;() // invisible, parameter 0
@Lorg/jetbrains/annotations/NotNull;() // invisible, parameter 1
L0
ALOAD 1
LDC "rect"
@BenLeggiero
BenLeggiero / Array.joinedWithComma.swift
Last active Sep 11, 2018
Slight tweaks to Cortis Clark's joinedWithComma function
View Array.joinedWithComma.swift
extension Array where Iterator.Element == String {
/// Originally by Cortis Clark on StackOverflow, modified by Ben Leggiero for an example
/// - SeeAlso: https://stackoverflow.com/a/52266604/3939277
func joinedWithComma(useOxfordComma: Bool = true, maxItemCount: UInt8? = nil) -> String {
let result: String
if let maxItemCount = maxItemCount, count > maxItemCount {
result = self[0 ..< maxItemCount].joined(separator: ", ") + ", etc."
} else if count >= 2 {
let lastIndex = count - 1
let extraComma = (useOxfordComma && count > 2) ? "," : ""
@BenLeggiero
BenLeggiero / Elevate Permissions.swift
Created Aug 8, 2018
Elevates permissions in non-sandboxed Swift apps
View Elevate Permissions.swift
import Foundation
private func gainPermissions() {
var authorizationRef: AuthorizationRef? = nil
var authItem = AuthorizationItem(name: kSMRightBlessPrivilegedHelper, valueLength: 0, value: nil, flags: 0)
var authRights = AuthorizationRights(count: 1, items: &authItem)
let flags: AuthorizationFlags = [.interactionAllowed, .preAuthorize, .extendRights]
var environment = AuthorizationEnvironment()
@BenLeggiero
BenLeggiero / QuickSorter.swift
Last active Mar 6, 2018
General-use Quicksort in Swift
View QuickSorter.swift
/// Uses quicksort with the Lomuto partition scheme.
///
/// By Ben Leggiero, written on 2018-03-05. Copyright BH-0-PD
/// https://github.com/BlueHuskyStudios/Licenses/blob/master/Licenses/BH-0-PD.txt
struct QuickSorter {
/// Performs a quicksort with Lomuto partition using the given array and returns the result
func quicksorting<C: Comparable>(_ unsorted: [C]) -> [C] {
var arrayCopy = unsorted
QuickSorter.quicksort(&arrayCopy)
@BenLeggiero
BenLeggiero / What is a Monad?.swift
Created Nov 27, 2017
This file is an illustration of the concepts described in the Computerphile video "What is a Monad?", but in Swift rather than the video's Haskell. https://www.youtube.com/watch?v=t1e8gqXLbsU
View What is a Monad?.swift
/// This file is an illustration of the concepts described in the Computerphile video "What is a Monad?", but in Swift rather than the video's Haskell.
/// https://www.youtube.com/watch?v=t1e8gqXLbsU
import Cocoa
// MARK: - Stuff we need in order to make our code look like the code in the video
@BenLeggiero
BenLeggiero / NSWindow+Fade.swift
Last active Apr 3, 2019 — forked from indragiek/NSWindow+Fade.h
NSWindow+Fade - Animator proxy based NSWindow fading
View NSWindow+Fade.swift
//
// NSWindow+Fade.swift
// BH Bezel Notification
//
// Created by Ben Leggiero on 2017-11-09.
// Translated to Swift 4 from the original (ObjC): https://gist.github.com/indragiek/1397050
// Copyright © 2017 Ben Leggiero. All rights reserved.
//
import Foundation
@BenLeggiero
BenLeggiero / Swift range conformance check.swift
Created Nov 6, 2017
A proof-of-concept that attempts to create a syntax like "left < center < right", which ensures that "center" is both greater than "left" and less than "right".
View Swift range conformance check.swift
//
// Swift range conformance check.swift
// Requires Swift 4
//
// Created by Ben Leggiero on 2017-11-06.
// Copyright © 2017 Ben Leggiero using BH-1-PS license
//
// This is a proof-of-concept that attempts to create a syntax like "left < center < right", which ensures that
// "center" is both greater than "left" and less than "right".
@BenLeggiero
BenLeggiero / Swift 4 - NSApp Relaunch.swift
Last active Aug 13, 2018
Allows you to re-launch a Cocoa app
View Swift 4 - NSApp Relaunch.swift
import Cocoa
public extension NSApplication {
public func relaunch(afterDelay seconds: TimeInterval = 0.5) -> Never {
let task = Process()
task.launchPath = "/bin/sh"
task.arguments = ["-c", "sleep \(seconds); open \"\(Bundle.main.bundlePath)\""]
You can’t perform that action at this time.