Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env ruby
require 'rubygems'
require 'mechanize'
$name_mapping = {
"Egwene al'Vere" => "Egwene",
"Elaida a'Roihan" => "Elaida",
"Verin Mathwin" => "Verin",
"Cadsuane Melaidhrin" => "Cadsuane",
@bjhomer
bjhomer / prompt_pwd.fish
Created February 5, 2013 20:21
My fish shell prompt_pwd.fish. Produces paths like this: `~/.../someDir/some_other_dir/`, using as many full components as possible.
function prompt_pwd --description "Print the current working directory, shortened to fit the prompt"
echo $PWD | sed -e |^$HOME|~|" | awk -F/ '{
str = $NF
currfield = NF - 1
maxlen = 50 # Well, possibly plus a few for "~/..."
while (currfield > 0 \
&& length(str) + length($currfield) + 1 < maxlen \
&& length($currfield) > 0 ) {
str = $currfield "/" str
currfield = currfield-1
@bjhomer
bjhomer / runtimeHang.m
Created February 13, 2013 22:06
Here's a hang inside the runtime. rdar://13210148
//
// AppDelegate.m
// RuntimeHang
//
// Created by BJ Homer on 2/13/13.
// Copyright (c) 2013 BJ Homer. All rights reserved.
//
#import "AppDelegate.h"
@bjhomer
bjhomer / controller.m
Last active December 14, 2015 07:59
Pattern for -init methods of controllers designed in a storyboard
- (id)init
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainInterface" bundle:nil];
self = [storyboard instantiateViewControllerWithIdentifier:@"MyControllerIdentifier"];
// Set up ivars here. 'self' won't be nil; if it is, you passed an invalid identifier
// and UIKit would have thrown an exception anyway.
return self;
}
@bjhomer
bjhomer / main.m
Last active December 15, 2015 16:59
Forward declaration of primitive variables. Turns out it's legal.
#import <Foundation/Foundation.h>
int foo;
int foo;
int foo;
int foo;
int foo = 4;
int main(int argc, const char **argv) {
NSLog(@"foo: %i", foo); // Prints '4'
@bjhomer
bjhomer / bad.m
Last active December 15, 2015 19:49
-Wformat-nonliteral is picky
NSString *template = NSLocalizedString(@"My name is %@", @"A label for displaying the user's name");
NSString *str = [NSString stringWithFormat:template, @"John"];
// warning: format string is not a string literal [-Wformat-nonliteral]
label.text = str;
@bjhomer
bjhomer / gist:5369684
Last active December 16, 2015 03:29
Explaining complex multiplication as rotation.

Imagine that a number line lives on a 2D plane. "Positive" on the number line is in the 0° direction, and "Negative" on the number line is in the 180° direction.

"1" is a vector of length 1, pointing in the direction 0°.

"-3" is a vector of length 3, pointing in the direction 180°.

Multiplication of these vectors can be understood as multiplying the lengths and adding the angle of each vector.

"1 * -3" is a vector of length (1*3 = 3), pointing in the direction (0° + 180° = 180°). In other words, "1 * -3" is a vector of length 3, pointing in the negative direction.

@bjhomer
bjhomer / config.fish
Last active March 20, 2019 21:30
This is a modified version of the built-in fish git prompt. It uses the colors I like. :)
if status --is-interactive
set -gx PATH /usr/local/bin ~/Applications/ $PATH;
set -gx EDITOR "subl -nw"
end
# My colors
set -g fish_color_autosuggestion 444444
set -g fish_color_command 00ff00
set -g fish_color_comment red
set -g fish_color_cwd cyan
@bjhomer
bjhomer / 1pxline.m
Created September 13, 2013 16:11
Drawing a 1px line on both 1x and 2x screens.
// A 1pt rect along the bottom of the view
NSRect bottomLineRect = NSMakeRect(0 , NSHeight(self.bounds) - 1, NSWidth(self.bounds), 1);
// Convert to the backing rect, and make sure that it is only 1px tall.
NSRect backingRect = [self convertRectToBacking:bottomLineRect];
backingRect.size.height = 1;
bottomLineRect = [self convertRectFromBacking:backingRect];
@bjhomer
bjhomer / after.m
Last active December 24, 2015 22:49
- (BOOL)shouldShowMenuIcon
{
return [AgentManager sharedManager].shouldShowInMenuBar;
}
- (void)setShouldShowMenuIcon:(BOOL)enabled
{
AgentManager *manager = [AgentManager sharedManager];
manager.shouldLaunchAtStartup = enabled;
manager.shouldShowInMenuBar = enabled;