Skip to content

Instantly share code, notes, and snippets.

Keith Irwin zentrope

Block or report user

Report or block zentrope

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
View drafts-to-oo.js
let blurb = { "text": "", "date": "", "link": "", "title": "" }
const isValidDate = (d) => d instanceof Date && !isNaN(d)
draft.content.split("\n").forEach((text) => {
const line = text.trim()
if (line.startsWith("text:")) {
blurb.text = line.replace("text:", "").trim()
if (blurb.text.trim() == "") {
View splitview-grabber.swift
override func splitView(_ splitView: NSSplitView, additionalEffectiveRectOfDividerAt dividerIndex: Int) -> NSRect {
super.splitView(splitView, additionalEffectiveRectOfDividerAt: dividerIndex)
assert(splitViewItems.count == 2)
if splitViewItems.count != 2 { return .zero }
if !splitViewItems[1].isCollapsed { return .zero }
return NSMakeRect(splitView.bounds.maxX-6, 0, 6, splitView.bounds.height)
}
View format.swift
private func handleMessage(_ message: IRC.Message) {
if let storage = chatTextView.textStorage,
let render = format(message) {
storage.append(render)
//storage.insert(render, at: storage.length)
}
chatTextView.scrollToEndOfDocument(self)
}
private let chatFont = NSFont.systemFont(ofSize: 14)
View activity3.swift
import Cocoa
class ActivityManager {
var activityUpdate: NSBackgroundActivityScheduler?
var serialize = DispatchQueue(label: "sync", qos: .background)
func backgroundActivitiesSetup() {
print("Setting up activity runner.")
activityUpdate = NSBackgroundActivityScheduler(identifier: "")
View activity2.swift
var activityUpdate: NSBackgroundActivityScheduler?
func backgroundActivitiesSetup() {
activityUpdate = NSBackgroundActivityScheduler(identifier: "")
activityUpdate?.interval = 1 * 60 // every 1 minute
activityUpdate?.repeats = true
activityUpdate?.schedule { completion in
updateNewItems(completion)
}
}
View activity.swift
var activity: NSBackgroundActivityScheduler?
func runActivity() {
activity = NSBackgroundActivityScheduler(identifier: "lkasjdlaksjdaslkjd")
activity?.repeats = true
activity?.interval = 15
activity?.schedule { completion in
// This doesn't make sense: defer should be periodically checked in
// the middle of a long job.
@zentrope
zentrope / sema.swift
Last active Jan 19, 2020
Use a semaphore to block
View sema.swift
func xmlDocument(url: String) -> Document? {
var doc: Document?
let semaphore = DispatchSemaphore(value: 0)
AF.request(url).response { response in
if let data = response.data, let xml = String(data: data, encoding: .utf8) {
do {
doc = try SwiftSoup.parse(xml, "", Parser.xmlParser())
} catch {
print("error: \(error.localizedDescription)")
@zentrope
zentrope / OverlapViewController.swift
Created Aug 25, 2019
Overlapping views in a view controller
View OverlapViewController.swift
override func loadView() {
let view = NSView(frame: .zero)
view.addSubview(vfxView)
view.addSubview(webView)
vfxView.translatesAutoresizingMaskIntoConstraints = false
webView.configuration.preferences.javaScriptEnabled = true
webView.setValue(false, forKey: "drawsBackground")
@zentrope
zentrope / AppDelegate.swift
Last active Jun 19, 2019
Open a window programmatically
View AppDelegate.swift
//
// AppDelegate.swift
//
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
private var preferencesWindow = NSWindowController()
@zentrope
zentrope / webviewInject.swift
Last active Dec 23, 2018
Set dark/light for web content
View webviewInject.swift
private func setMode(_ isDark: Bool) {
let mode = isDark ? "add" : "remove"
let script = "document.body.classList.\(mode)(\"dark\");"
webView.evaluateJavaScript(script) { result, error in
if let err = error {
os_log("%{public}s", log: logger, type: .error, "js error: \(err).")
}
}
}
You can’t perform that action at this time.