Skip to content

Instantly share code, notes, and snippets.

@ahknight
ahknight / start_socks_proxy.sh
Last active August 29, 2015 13:56
usage: start_socks_proxy.sh hostname [SSH port]
#!/bin/bash
# Using SSH, create a SOCKS4/5 proxy connection to a remote server and configure the system to use it.
_pgrep() {
P_CMD=$1
P_PID=$( ps -u $USER -o pid,command | grep "${P_CMD}" | grep -v grep | head -1 | awk '{ print $1 }' )
echo "$P_PID"
if [ -z $P_PID ]; then return 1; else return 0; fi
}
@ahknight
ahknight / symkvs.py
Created January 3, 2014 21:27
Using symlinks as a key-value store. Great for multiprocessing. Sample subclass to keep links to unique files, though there are other uses (considering values only go up to PATH_MAX (usually 1024)).
#!/usr/bin/env python
import os
from errno import *
class SymlinkKVS(object):
def __init__(self, storepath):
self.storepath = storepath
def __delitem__(self, key):
@ahknight
ahknight / searchmail.sh
Created December 30, 2013 18:05
Presuming you keep an archive of your email in a local maildir and presuming you're wise enough to have found mu (it's in homebrew), this script searches for messages and opens up mutt with the results (also in homebrew).
#!/bin/bash
MAILDIR="~/Maildir"
RESULTS="~/Maildir/.SearchResults"
mu find -o links --linksdir="${RESULTS}" --clearlinks $@ && mutt -f "${RESULTS}"
@ahknight
ahknight / dotfiles.sh
Last active May 5, 2017 18:09
Manages a dotfiles repo with global and host-specific settings.
#!/bin/bash
HOSTNAME=$(hostname -f)
GREEN=$( tput setaf 10 )
CYAN=$( tput setaf 14 )
RED=$( tput setaf 1 )
GRAY=$( tput setaf 8 )
RESET=$( tput setaf 7 )
NAME=$( basename "$0" )
@ahknight
ahknight / clang_diagnostic.c
Last active December 26, 2015 13:59
For when you really do know what you're doing...
#define _HELPER(x) #x
#define START_IGNORE_DIAGNOSTIC(diag) _Pragma("clang diagnostic push"); _Pragma(_HELPER(clang diagnostic ignored #diag));
#define STOP_IGNORE_DIAGNOSTIC _Pragma("clang diagnostic pop");
START_IGNORE_DIAGNOSTIC(-Wsomething-stupid)
somethingSeeminglyStupid();
STOP_IGNORE_DIAGNOSTIC
@ahknight
ahknight / NSString+AWESOME.m
Created October 22, 2013 14:24
Python-style accessors for NSString.
@interface NSString (IndexedAccessors)
- (id)objectAtIndexedSubscript:(NSUInteger)idx;
- (id)objectForKeyedSubscript:(id)key;
@end
@implementation NSString (IndexedAccessors)
- (id)objectAtIndexedSubscript:(NSUInteger)idx { return @([self characterAtIndex:idx]); }
- (id)objectForKeyedSubscript:(id)key {
@ahknight
ahknight / gist:6888789
Created October 8, 2013 18:00
Forward calls for a specific delegate protocol to another class for answering.
-(id)forwardingTargetForSelector:(SEL)aSelector
{
// Do we have a scroll view delegate?
if (_scrollViewDelegate && [_scrollViewDelegate conformsToProtocol:@protocol(UIScrollViewDelegate)]) {
// Is this a selector in that protocol?
struct objc_method_description methodDescription = protocol_getMethodDescription(@protocol(UIScrollViewDelegate), aSelector, NO, YES);
if (methodDescription.name != NULL) return _scrollViewDelegate;
}
return [super forwardingTargetForSelector:aSelector];
}
@ahknight
ahknight / make_ios_icons.sh
Last active December 23, 2015 10:58
Given an input image as the single argument, creates all the files needed for a Universal iOS app (in the same directory, overwriting). Uses pngcrush to compress the images, but that's easily removed/commented out.
#!/bin/bash
function make_icon {
sips -s format png -Z $3 "$1" --out "$2"
pngcrush -ow -q "$2"
}
echo "Using $1 as the base image."
echo "** App Store: iTunesArtwork **"
@ahknight
ahknight / KeyObject.m
Last active December 21, 2015 22:59
Public domain. Init with a dictionary, define some properties as dynamic, serve from -valueForUndefinedKey however you wish.
#import <Foundation/Foundation.h>
#import <objc/objc.h>
#import <objc/objc-class.h>
#import <malloc/malloc.h>
#pragma mark Keyed Object
@interface KeyedObject : NSObject
@end
@ahknight
ahknight / notification-center.py
Created April 25, 2013 15:18
A simple Python script that lets you post to the notification center from the command line without dealing with a full desktop app. Because I can.
#!/usr/bin/python
# Copyright (c) 2013 Adam Knight
#
# 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: