Skip to content

Instantly share code, notes, and snippets.

@vrld
vrld / iutf8.lua
Last active December 30, 2015 08:29
Simple, non-standard compliant UTF8 iterator for vanilla Lua
local function utf8_iter(s, i)
if i >= #s then return end
local b, nbytes = s:byte(i+1,i+1), 1
-- determine width of the codepoint by counting the number if set bits
-- from top to bottom. not 100% to standard, but it works well enough
if b/4 >= 63 then nbytes = 6
elseif b/8 >= 31 then nbytes = 5
elseif b/16 >= 15 then nbytes = 4
elseif b/32 >= 7 then nbytes = 3
@vrld
vrld / module-pipeline-example.py
Last active December 24, 2015 12:09
Simple Module Pipeline Thingy
class Constant(Module):
def __init__(self, val = 0):
self.out = val
def __repr__(self):
return 'Constant({})'.format(self.out)
class Mul(Module):
def __repr__(self):
return 'Mul()'
@vrld
vrld / Table.py
Created September 30, 2013 14:24
Lua-Table like dictionary for Python
class Table(dict):
def __getattr__(self, name):
return self[name] if name in self else None
def __setattr__(self, name, value):
if value is not None:
self[name] = value
elif name in self:
del self[name]
def __delattr__(self, name):
self.__setattr__(name, None)
@vrld
vrld / aix
Created May 29, 2013 16:53
Quick hack to get a more comfortable search tool on APT-based distros. Inspired by gentoo's wonderful `eix'.
#!/usr/bin/python
class AnsiColor:
default = '\033[00;30m'
bold = '\033[01;30m'
green = '\033[00;32m'
boldgreen = '\033[01;32m'
boldred = '\033[01;31m'
pass
@vrld
vrld / gist:5166463
Created March 15, 2013 00:14
make love
love=$(/usr/bin/env love)
zip=$(/usr/bin/env zip)
luac=$(/usr/bin/env luac)
# path to win and osx distributions
windir=~/Stuff/love-win-x86
osxapp=~/Stuff/love.0.8.app
game=TITLE-OF-THE-GAME.love
@vrld
vrld / grains.lua
Created December 13, 2012 11:35
Grain synthesis with LHC.
local lhc = require 'lhc'
local SAMPLERATE = 44100
local generator = {
sine = function(x) return math.sin(x * 2 * math.pi) end,
tri = function(x) x = (x-.5)%1 return math.min(2*x-1, 1-2*x) end,
saw = function(x) return (2*x-1)%2 - 1 end,
rect = function(x) return 2 * math.floor((2*x)%2) -1 end,
wn = function(x) return math.random() end,
@vrld
vrld / example.cpp
Created November 27, 2012 13:27
Less annoying luaL_Buffer handling in C++
int l_imwrite(lua_State *L)
{
// see https://gist.github.com/4154182
cv::Mat *img = LuaProxy<cv::Mat>::get(L, 1);
const char *path = luaL_checkstring(L, 2);
if (cv::imwrite(path, *img))
{
lua_pushboolean(L, true);
return 1;
@vrld
vrld / example.cpp
Created November 27, 2012 13:16
Helpers for exposing (parts of) C++ structs to Lua, e.g. to be passed around in "glue code"
#include <lua_proxy.hpp>
#include <opencv.hpp>
// Exposing cv::Mat to Lua
template<> struct LuaXTraits<cv::Mat>
{
constexpr static const char *name()
{
return "Type.cv::Mat";
@vrld
vrld / gist:755487
Created December 26, 2010 15:53
Opera Speed Dial like thing for luakit
local chrome = require "chrome"
local capi = { luakit = luakit }
local page = "chrome://favs/"
local pattern = page.."?"
local cutycapt_bin = "/home/matthias/.bin/CutyCapt"
local html_template = [====[
<html>
// ==UserScript==
// @include *love2d.org/forums/viewtopic.php*
// ==/UserScript==
(function() {
var users = []; // the users to hide automatically
function getElementsByTagAndClass(tag, cls) {
var tags = document.getElementsByTagName(tag);
var ret = [];