Skip to content

Instantly share code, notes, and snippets.

View griffin-stewie's full-sized avatar

griffin-stewie griffin-stewie

View GitHub Profile
[
"#(ハッシュ)",
"#7thFestival!",
"#a",
"#b",
"#c",
"#d",
"#e",
"#f",
"#MEMO",
@griffin-stewie
griffin-stewie / template.swift
Created August 28, 2022 14:55
Template for Shortcuts.app's Swift shell script. You can develop on Xcode Playground then you paste them into Shortcuts.app.
import Cocoa
import os
// print debug helper function for Playground envoironment and Shortcuts.app
func log(_ item: String, function: String = #function) {
let isRunningOnPlayground = CommandLine.arguments.filter({ !$0.isEmpty }).isEmpty
if isRunningOnPlayground {
Swift.print("\(function) \(item)")
} else {
@griffin-stewie
griffin-stewie / Markup+writing.swift
Created October 23, 2021 02:16
Extension which write-to support for swift-markdown.
import Foundation
import Markdown
extension Markup {
func write(to destination: URL, formatOptions: MarkupFormatter.Options = .default, atomically useAuxiliaryFile: Bool = true, encoding: String.Encoding = .utf8) throws {
let string = format(options: formatOptions)
try string.write(to: destination, atomically: useAuxiliaryFile, encoding: encoding)
}
}
@griffin-stewie
griffin-stewie / CSSTokenizer.swift
Created October 11, 2021 22:12
Implemented CSS Tokenizer in Swift. Original Rust code is https://www.youtube.com/watch?v=8Y0-lrFf3Qk
import Foundation
public struct CSSTokenizer {
var position: String.Index
let input: String
public init(input: String) {
self.position = input.startIndex
self.input = input
}
/*
Copyright © 2020 Apple Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR O
@griffin-stewie
griffin-stewie / date_formatter_with_argumentparser.swift
Created April 7, 2020 13:35
ArgumentParser で Date を扱うときの例
// https://forums.swift.org/t/support-for-date/34797/2?u=griffin-stewie
func parseDate(_ formatter: DateFormatter) -> (String) throws -> Date {
{ arg in
guard let date = formatter.date(from: arg) else {
throw ValidationError("Invalid date")
}
return date
}
}
@griffin-stewie
griffin-stewie / Array+UniqueExtensions.swift
Last active September 2, 2021 14:03
Swift Command Line Tool Helpers
import Foundation
// O(n^2)
public extension Array where Element: Equatable {
/// Remove duplicated elements
/// Complexity: O(n^2), where n is the length of the sequence.
/// - Returns: A new Array containing those elements from self that are not duplicates
func unique() -> Array<Element> {
return reduce(into: []) { (array, r) in
@griffin-stewie
griffin-stewie / keymap.c
Created October 8, 2018 09:11
Mint60 qmkfirm
/* Copyright 2018 Eucalyn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@griffin-stewie
griffin-stewie / keymap.c
Last active October 9, 2018 13:19
qmk_firmware for HelixPico
#include QMK_KEYBOARD_H
#include "bootloader.h"
#include "keymap_jp.h"
#ifdef PROTOCOL_LUFA
#include "lufa.h"
#include "split_util.h"
#endif
#ifdef AUDIO_ENABLE
#include "audio.h"
#endif
import UIKit
func downsample(imageAt imageURL: URL, to pointSize: CGSize, scale: CGFloat = UIScreen.main.nativeScale) -> UIImage {
let imageSourceOptions = [kCGImageSourceShouldCache: false] as CFDictionary
let imageSource = CGImageSourceCreateWithURL(imageURL as CFURL, imageSourceOptions)!
let maxDimensionInPixels = max(pointSize.width, pointSize.height) * scale
let downsampleOptions = [
kCGImageSourceCreateThumbnailFromImageAlways: true,
kCGImageSourceShouldCacheImmediately: true,
kCGImageSourceCreateThumbnailWithTransform: true,