Skip to content

Instantly share code, notes, and snippets.

@boredzo
boredzo / with_scope.py
Created September 13, 2015 20:53
Experiment in making a “with” object that injects variables into the subjected scope and removes them at the end. One fatal flaw—see last comment. Inspired by: https://twitter.com/SwedishForSize/status/643146539897090048
class scope(object):
def __init__(self, **kargs):
self.values = kargs
def __enter__(self):
import inspect
frame, filename, lineno, module, code_context, idx = inspect.stack()[1]
caller_args, caller_fargs, caller_kargs, caller_locals = inspect.getargvalues(frame)
dest = caller_kargs if caller_kargs is not None else caller_locals
self.saved = dict(dest)
dest.update(self.values)
@boredzo
boredzo / git-ff.zsh
Created July 4, 2015 18:07
git helpers
#!/bin/zsh -f
exec git merge --ff-only $*
@boredzo
boredzo / PRHFrameGrabber+DraggingSource.m
Last active March 11, 2016 19:26
Dragging source code for promising files from my never-released movie player app.
//PRHFrameGrabber grabs frame images from the movie.
//It wraps a couple of AVAssetImageGenerators, one of which is allowed to round to a more convenient time (e.g., a keyframe).
//Thus, the temporally lax generator will return an image first, while we wait for the temporally strict generator, which may take a significant fraction of a second.
//PRHFrameGrabber is also an NSPasteboardWriter. It's what the movie view feeds to the dragging session when the user tries to drag a frame; the frame grabber fulfills its NSPasteboardWriter duties by returning the grabbed frame.
@interface PRHFrameGrabber ()
@property(readwrite, copy) NSImage *image;
@end
@implementation PRHFrameGrabber
@boredzo
boredzo / balloon-tail.ps
Last active August 29, 2015 14:03
Demonstration of how to draw a speech balloon tail, based on advice in http://stackoverflow.com/a/16782469/30461 .
%!PS-Adobe-3.0 EPSF-3.0
%%BoundingBox: 0 0 120 260
/pt1 { 20 100 } def
/pt2 { 60 80 } def
/pt5 { 100 60 } def
/pt3 { 20 20 } def
/pt4 { 60 40 } def
/yoffset { 30 add } def
@boredzo
boredzo / README.md
Last active August 29, 2015 14:03
indexheaders/findheader

indexheaders/findheader

Ever wish you could do something like view $(findheader signal.h)—and have it be fast? Now you can.

(open -h is one alternative, but that opens it in Xcode, rather than a terminal-based or other editor.)

Basically, it's like “Open Quickly” for the command line.

Installation

@boredzo
boredzo / gist:edfba48ca9b52d7c09d8
Created May 11, 2014 21:25
Card dealing command-line program
#import <Foundation/Foundation.h>
int main(int argc, char *argv[]) {
@autoreleasepool {
NSArray *suits = @[
@"♠", @"♡", @"♢", @"♣"
];
NSArray *ranks = @[
@"A",
@"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9",
@boredzo
boredzo / gist:ae47404dedec7930152a
Last active August 29, 2015 14:01
On the importance of an *explicit* code of conduct

First, some context.

I urged the AltConf organizers to publish a Code of Conduct for their attendees and speakers. In that Twitter conversation, I repeatedly cited Ashe Dryden's Code of Conduct 101 + FAQ.

This is what they published on their website as their code of conduct (as of 2014-05-06):

AltConf is provided free of charge, on a first-come, first-served basis, without warranty or liability, by volunteers, for the good of our community.

To participate in our conference in any way (attendee, speaker, sponsor, etc), you must agree to abide by the instructions of the volunteers and the decisions of the organizers, and acknowledge that you may be barred from participation at any time, for any reason.

@boredzo
boredzo / gist:8911626
Created February 10, 2014 07:11
Guess the bug!
/draw_rows
gsave
1 dict begin
/num_rows exch def
1 1 num_rows {
5 draw_one_row
} for
end
grestore
} def
@boredzo
boredzo / gist:8661176
Created January 28, 2014 02:04
The difference between fmod and remainder
NSTimeInterval days = 0.0;
NSTimeInterval hours = 1.0;
NSTimeInterval minutes = 49.0;
NSTimeInterval seconds = 6.215;
NSTimeInterval interval =
days * 3600.0 * 24.0
+ hours * 3600.0
+ minutes * 60.0
+ seconds * 1.0;
NSLog(@"%f", fmod(interval / 60.0, 60.0)); // 49.103583
@boredzo
boredzo / gist:8640626
Last active April 23, 2016 03:37
Category on NSString to enumerate a string's substrings that match another string
#import <Foundation/Foundation.h>
@interface NSString (PRHEnumerateMatches)
- (NSUInteger) enumerateSubstringsMatchingString_PRH:(NSString *)otherString
options:(NSStringCompareOptions)mask
usingBlock:(void (^)(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop))block;
@end