Skip to content

Instantly share code, notes, and snippets.

@ivan3bx
ivan3bx / ws_example.go
Created January 9, 2016 17:24
Simple 'echo' server with golang.org/x/net/websocket
package main
import (
"fmt"
"io"
"net/http"
"reflect"
"golang.org/x/net/websocket"
)
@ivan3bx
ivan3bx / GestureRecognizerExample.swift
Last active August 29, 2015 14:22
Q&D example of UIPanGestureRecognizer in Swift, dragging a square around
class ViewController: UIViewController {
@IBOutlet weak var square: UIView!
var squareMidpoint:CGPoint = CGPointZero
@IBAction func handlePan(recognizer: UIPanGestureRecognizer) {
let hitInRect = CGRectContainsPoint(square.frame, recognizer.locationInView(self.view))
let isMoving = (squareMidpoint != CGPointZero)
@ivan3bx
ivan3bx / randomTimer.swift
Created May 2, 2015 19:34
Some throwaway code that invalidates an NSTimer, and create a new one for a pseudo-random timeout.
self.timer.invalidate()
let value = ((Float((random()&1000)) / 100) % 5) + 0.4
let rndTimeout = NSTimeInterval(value)
self.timer = NSTimer.scheduledTimerWithTimeInterval(rndTimeout, target: self, selector: Selector("showActivity:"), userInfo: nil, repeats: true)
@ivan3bx
ivan3bx / ansi_color_output.swift
Created December 7, 2014 23:01
ANSI color sequence example in Swift (Unicode)
let escape = "\u{001B}["
let none = escape + "0m"
let red = escape + "0;31m"
let green = escape + "0;32m"
let yellow = escape + "0;33m"
println("\(red)Everything \(green)or \(yellow)nothing\(none).")
@ivan3bx
ivan3bx / url_handler.swift
Created November 24, 2014 06:45
NXOAuth2 handler using AppleScript to open Safari
func createAuthHandler() -> NXOAuth2PreparedAuthorizationURLHandler {
return { (url: NSURL!) -> Void in
let sourceScript = "tell application \"Safari\"\n"
+ " open location \"\(url.absoluteString!)\"\n"
+ " activate\n"
+ "end tell\n"
var err : NSDictionary?
let script = NSAppleScript(source: sourceScript)!
if script.compileAndReturnError(&err) {
@ivan3bx
ivan3bx / detect_quote_header.rb
Created June 10, 2014 03:59
grabbing the last 200 characters preceeding a 'blockquote'
#
# Brute force method of grabbing 200 characters preceeding first <blockquote> element
#
p = Post.last
parts = p.split_as_html
root = Nokogiri::HTML.fragment(parts.first.raw_content)
bq = root.search('blockquote')[1]
test_string = root.inner_html[(root.inner_html.index(bq.to_html) - 200)...(root.inner_html.index(bq.to_html))]
# ">\n<br><br><div class=\"gmail_quote\">On Tue, Jun 29, 2014 at 11:19 AM, John Smith <span dir=\"ltr\">&lt;
@ivan3bx
ivan3bx / NSTextViewDelegate_textchange.m
Created April 15, 2014 05:36
Just a snippet of code for detecting a possible match. This seems inefficient for just single-character matches..
- (void)textDidChange:(NSNotification *)aNotification
{
NSTextView *textView = self.detailTextView;
NSString *contents = textView.textStorage.string;
NSRange selectionRange = textView.selectedRange;
if (selectionRange.length == 0 && contents.length > 0) {
NSString *possibleMatch = [contents substringWithRange:NSMakeRange(selectionRange.location - 1, 1)];
if ([possibleMatch isEqualToString:@"{"]) {
[textView complete:nil];
@ivan3bx
ivan3bx / filter_by_artwork.m
Created January 27, 2014 07:09
Forced to use @autoreleasepool in order to avoid memory leak in MPMediaItemArtwork.
NSMutableArray *albumIDs = [[NSMutableArray alloc] initWithCapacity:1000];
for (MPMediaItemCollection *collection in items) {
@autoreleasepool {
MPMediaItem *props = collection.representativeItem;
NSString *albumTitle = [props valueForProperty:MPMediaItemPropertyAlbumTitle];
NSString *artistName = [props valueForProperty:MPMediaItemPropertyAlbumArtist];
MPMediaItemArtwork *artwork = [props valueForProperty:MPMediaItemPropertyArtwork];
@ivan3bx
ivan3bx / read_schema.m
Last active December 30, 2015 16:19
Get the current schema out of a SQLite database..
- (NSString *)schemaFor:(NSString *)dbFile
{
sqlite3 *db;
NSString *query = @"select name, sql from sqlite_master where type = 'table'";
NSString *result;
if (sqlite3_open([dbFile UTF8String], &db) == SQLITE_OK) {
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(db, [query UTF8String], -1, &stmt, NULL) == SQLITE_OK) {
if (sqlite3_step(stmt) == SQLITE_ROW) {
@ivan3bx
ivan3bx / ITLibraryExample.m
Created September 7, 2013 18:40
Example of the ITLibrary API
NSError *error = nil;
ITLibrary *library = [ITLibrary libraryWithAPIVersion:@"1.0" error:&error];
if (library)
{
NSArray *playlists = library.allPlaylists;
NSArray *tracks = library.allMediaItems;
NSLog(@"Playlists count: %lu", [playlists count]);
NSLog(@"Tracks count: %lu", [tracks count]);