Skip to content

Instantly share code, notes, and snippets.

@adrianratnapala
adrianratnapala / assert.js
Created October 4, 2011 05:25
Javascript assert() implementation in 6 lines.
function AssertFailed(message) {
this.toString = function() { return 'AssertFailed: ' + message; }
}
function assert(exp, message) {
if (!exp) throw new AssertFailed(message);
}
@adrianratnapala
adrianratnapala / hello.S
Created October 28, 2011 06:59
Hello World on amd64 under Linux.
# Hello World on amd64 under Linux.
#
# One way to build this is with:
#
# gcc hello.S -s -nostartfiles -nostdlib -o hello
#
# for syscall numbers look in /usr/include/asm/unistd_64.h
# for examples look at http://99-bottles-of-beer.net/language-assembler-(amd64)-933.html
# for insipration look at http://www.muppetlabs.com/~breadbox/software/tiny/teensy.html
@adrianratnapala
adrianratnapala / automon.sh
Created October 28, 2011 20:56
Automatic multi-monitor config via xrandr.
#!/bin/sh
# automon.sh -- automatically configures multiple monitors.
#
# I use this script on Arch Linux. This handles my personal monitor setup more
# correctly and robustly than either Ubuntu or Windows 7, however it is only
# designed to work on my laptop.
#
# On that laptop it always calls "xrandr" using the exactly the same arguments.
# The --auto option in xrandr is clever enough to do the rest. One day this
# script might become more flexible.
@adrianratnapala
adrianratnapala / git-empty-gitignore.sh
Created October 29, 2011 05:18
Shell one-liner to add empty .hgignore to all empty directories in a git repository.
find -type d -empty | sed '/^\.\/\.git/d' | xargs -I{} touch '{}/.gitignore'
@adrianratnapala
adrianratnapala / SSH-X11-Forwarding.md
Created October 29, 2011 17:43
A note on X11-Forwarding in SSh.

I used to think that

ssh -X me@some.box

"just bloody worked". However this might not work - ssh must play ball on both sides of the link. On the remote (ssh server, X client) sshd must sit behind some port, tell Xlib to send X11 requests to it and then forward them back to you the X server (where the ssh client is). If the remote box is locked down to prevent this, you will get:

X11 forwarding request failed on channel 0

as part of an otherwise working login. As it happens, I am the admin of the remote box in question, so I followed the ArchWiki and went to /etc/ssh/sshd_config and uncommented

@adrianratnapala
adrianratnapala / strangeloop.c
Created November 15, 2011 22:50
I didn't notice this till turned up in a merge conflict.
CHK(chk_machine_code(gen, sizeof(4), identity_fun, ...etc...
xsetkbmap gb # make your keyboard British.
# also try "de", "us" etc.
@adrianratnapala
adrianratnapala / instaweb.py
Created December 29, 2011 19:29
Tiny web servers in python
#!/usr/bin/python3
# small web server that instruments "GET" but then serves up files
# to serve files using zero lines of code, do
#
# python -m http.server 8080 # python 3
#
# or
#
# python -m SimpleHTTPServer 8080 # python 2
#
static App()
{
var lang = XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag);
FrameworkElement.LanguageProperty.OverrideMetadata(
typeof(FrameworkElement), new FrameworkPropertyMetadata(lang)
);
var ceTypes = new Type[] {
typeof(System.Windows.Controls.DefinitionBase),
// A table for finding the properties with common names in two C# types.
// the result can reduce boilerplate when you do copies, compares.
// etc between pairs of structurally similar types. E.g. a model -- controller
// transition.
//
// hat tip to http://stackoverflow.com/questions/3057178/linq-join-two-dictionaries-using-a-common-key
static Tuple<PropertyInfo, PropertyInfo>[] piPairs = (
from mPi in typeof(Model.Component).GetProperties()
join pPi in typeof(ComponentRecord).GetProperties()
on mPi.Name equals pPi.Name