Skip to content

Instantly share code, notes, and snippets.

View MrMage's full-sized avatar

Daniel Gräfe MrMage

View GitHub Profile
import Cocoa
struct TaskTitleArray: Equatable {
let title: String
let children: [TaskTitleArray]
init(title: String, children: [TaskTitleArray]) {
self.title = title
self.children = children
}
public struct NonemptySequence<Element> {
fileprivate let _storage: [Element]
public init(first: Element, rest: [Element]) {
_storage = [first] + rest
}
}
extension NonemptySequence: Sequence {
// call through to _storage here
@MrMage
MrMage / photos_add_photos_with_filenames.js
Created November 29, 2017 20:50
JXA (JavaScript for Automation) to add photos with a specific filename to a Photos album
var photos = Application("Photos");
var al = photos.albums.whose({ name: 'ALBUM_NAME' })[0].get();
photos.add([photos.mediaItems.whose({ filename: 'FILE_NAME.jpg' })[0].get()], { to: al });
// ...
@MrMage
MrMage / echo.grpc.swift
Created February 21, 2018 21:52
Result of exploratory codegen changes to improve the testability of SwiftGRPC generated code (see https://github.com/grpc/grpc-swift/issues/120).
/*
* DO NOT EDIT.
*
* Generated by the protocol buffer compiler.
* Source: Sync/Proto/echo.proto
*
*/
/*
* Copyright 2018, SwiftGRPC Authors All rights reserved.
@MrMage
MrMage / echo.grpc.swift
Created February 23, 2018 18:59
Example for generated code without a package name set
/*
* DO NOT EDIT.
*
* Generated by the protocol buffer compiler.
* Source: echo.proto
*
*/
/*
* Copyright 2018, gRPC Authors All rights reserved.
import NIOConcurrencyHelpers
import Vapor
public protocol CloseableResource: class {
var eventLoop: EventLoop { get }
var isClosed: Bool { get }
func close()
}
import FluentPostgreSQL
extension QueryBuilder where Result: Model,
Result.Database == Database,
Database.Query: FluentSQLQuery,
Database.QueryField: SQLColumnIdentifier,
Database.QueryField: Hashable,
Database.QueryData == Dictionary<String, Database.Query.Expression> {
public func update<T>(_ keyPath: WritableKeyPath<Result, T>, to value: T) -> Future<Void> where T: Encodable {
Database.queryDataSet(Database.queryField(.keyPath(keyPath)), to: value, on: &query)
extension Collection where Element: FutureType {
/// Flattens an array of futures into a future with an array of results.
/// - note: the order of the results will match the order of the futures in the input array.
public func flatten(on worker: Worker) -> Future<[Element.Expectation]> {
let eventLoop = worker.eventLoop
// Avoid unnecessary work
guard count > 0 else {
return eventLoop.newSucceededFuture(result: [])
}
public func attributeMarkdownString(_ string: String, defaultAttributes: [NSAttributedString.Key: Any]) -> NSAttributedString {
let result = NSMutableAttributedString(string: string, attributes: defaultAttributes)
let linkExpression = try! NSRegularExpression(pattern: "\\[([^]]+)\\]\\(([^)]+)\\)")
while let linkMatch = linkExpression.firstMatch(in: result.string, range: NSRange(location: 0, length: result.length)) {
let linkString = NSMutableAttributedString(attributedString: result.attributedSubstring(from: linkMatch.range(at: 1)))
linkString.addAttributes([
.foregroundColor: Appearance.colors.link,
.link: result.attributedSubstring(from: linkMatch.range(at: 2)).string,
], range: NSRange(location: 0, length: linkString.length))
import Foundation
import Logging
import NIOConcurrencyHelpers
import NIO
import Async
import Service
public protocol CloseableResource: AnyObject {
var eventLoop: EventLoop { get }