Assembling a new home

I recently moved to New York, leaving most of my belongings back in Australia, since many people keep asking me about where I bought things, I thought I should make a list of the products that I’d buy again…

From Amazon (including gratuitous use of affiliate links)

Shipping from amazon is fast and usually well packed; this is not true for furniture though, and I strongly advise purchasing anything over 20kg elsewhere, the dining table had to be delivered three times before I got one that hadn’t been damaged in shipping.

From Totally Furniture

Arrived very quickly for such a massive item, cannot fault the freight company they used.

From Walmart (Online)

Quite a hefty list; it takes a lot to fill an apartment…..
Many items didn’t make the list due to falling apart after only days, or arriving damaged.

Hopefully someone might find this post useful.
Made myself a nice new bash prompt using lua. Code can be found in this gist.
I started off trying to set PS1, but bash will incorrectly calcuate the prompt length. This leaves you with strange line wrapping behaviour. Thanks to Rob Hoelz I found out about PROMPT_COMMAND, which allows you to run a command before PS1 is printed.
I used ljsyscall to avoid as many new processes as possible, they could be replaced by commands if you want.
To get the git and mercurial (hg) status, I had to call out to see if the current directory is inside a repository: this can sometimes be slow, as git and hg have to traverse the whole way up the directory tree stat-ing for .git and .hg folders.
EDIT: Using PROMPT_COMMAND caused issues when scrolling through history, or pressing backspace at the beginning of a line. From the #bash IRC channel I found out that you can inform bash of non-printing characters by wrapping the bytes in \1 and \2. The gist has been updated.

Made myself a nice new bash prompt using lua. Code can be found in this gist.

I started off trying to set PS1, but bash will incorrectly calcuate the prompt length. This leaves you with strange line wrapping behaviour. Thanks to Rob Hoelz I found out about PROMPT_COMMAND, which allows you to run a command before PS1 is printed.

I used ljsyscall to avoid as many new processes as possible, they could be replaced by commands if you want.

To get the git and mercurial (hg) status, I had to call out to see if the current directory is inside a repository: this can sometimes be slow, as git and hg have to traverse the whole way up the directory tree stat-ing for .git and .hg folders.

EDIT: Using PROMPT_COMMAND caused issues when scrolling through history, or pressing backspace at the beginning of a line. From the #bash IRC channel I found out that you can inform bash of non-printing characters by wrapping the bytes in \1 and \2. The gist has been updated.

Lua Workshop 2012

I’m excited to be going to the lua workshop this year.

If you’re going, make sure we have a chat!

Document your ABIs!

Most of the time when programming in C, your API IS your ABI; but there are various cases where use of a pre-processor will break this symmetry.

I should be able to interface with your library without running a pre-processor over the headers first.

Getting FFmpeg on windows

All the guides to getting ffmpeg working on windows seem to be extremely outdated; so here is a quick howto:

  1. Download mingw
  2. Install mingw with MSYS option; it will download and setup in C:\MinGW
  3. Get the mingw coreutils extra package (can be found from here); current version
  4. Install the coreutils extras package by un-7zipping and un-tarring it to C:\MinGW\msys\1.0\bin
  5. Download yasm; save it as C:\MinGW\msys\1.0\bin\yasm.exe
  6. Download and extract ffmpeg source (I used revision 647ec6f)
  7. Edit your C:\MinGW\msys\1.0\etc\fstab file to add the location of your ffmpeg source
  8. Open MinGW shell (it should be in your start menu)
  9. change to the directory you extract ffmpeg (going via the alias you added in fstab)
  10. ./configure
  11. make

HTML generation with lua

In my adventures on the web; I discovered an aid for writing HTML called zen-coding. Playing a bit with it, I found it lacking in some departments, and began to think that macros aren’t whats needed, but complete generation.

I remembered seeing an example of some html generating lua code a while back, and for a bit of fun decided to do my own take on it…

local function renderattributes ( attr )
    local t , i = { } , 1
    for k , v in pairs ( attr ) do
        t [ i ] = ’ ‘
        t [ i + 1 ] = k
        t [ i + 2 ] = ‘=”’
        t [ i + 3 ] = v
        t [ i + 4 ] = ‘”’
        i = i + 5
    end
    return table.concat ( t )
end

local function tostringtable ( t )
    local r = { }
    for i , v in ipairs ( t ) do
        r [ i ] = tostring ( v )
    end
    return r
end

local elementmeta = {
    __tostring = function ( t )
        if #t == 0 then — Self closing tag
            return “<” .. t.tag .. renderattributes ( t.attr ) .. “/>”
        else
            return “<” .. t.tag .. renderattributes ( t.attr ) .. “>” .. table.concat ( tostringtable ( t ) ) .. “</” .. t.tag .. “>”
        end
    end ;
}

local escapecodes = setmetatable (
    {
        [“<”] = “&lt;” ;
        [“>”] = “&gt;” ;
        [“&”] = “&amp;” ;
        [‘”’] = “&quot;” ;
    } ,
    { __index = function ( t , k ) return string.format ( “&#%03d;” , string.byte ( k ) ) end }
)

local function escapehtml ( str )
    return ( str:gsub ( [=[[<>&”]]=] , escapecodes ) )
end

local stringmeta = {
    __tostring = function ( t )
        return escapehtml ( table.concat ( t ) )
    end
}

local function construct ( tag , contents )
    local attr = { }
    local r = { tag = tag , attr = attr }

    for k , v in pairs ( contents ) do
        if type ( k ) == “string” then — attribute
            assert ( type ( v ) == “string” , “Attributes must be strings” )
            attr [ k ] = v
        elseif type ( k ) == “number” then — Contents
            if type ( v ) == “string” then
                r [ k ] = setmetatable ( { v } , stringmeta )
            elseif type ( v ) == “number” then
                r [ k ] = setmetatable ( { tostring ( v ) } , stringmeta )
            else
                r [ k ] = v
            end
        else
            error ( “Bad type” )
        end
    end

    return setmetatable ( r , elementmeta )
end

local CDATA = function ( p ) return setmetatable ( p , { __tostring = function ( t ) return “<![CDATA[” .. table.concat ( tostringtable ( p ) ) .. “]]>” end } ) end
local javascriptblock = function ( p ) return construct ( “script” , { type=”text/javascript” ; “//” ; CDATA { ‘\n’ ; p ; ‘\n//’ } } ) end
tags = setmetatable ( {
        CDATA = CDATA ;
        javascriptblock = javascriptblock ;
    } ,
    { __index = function ( t , k ) return function ( p ) return construct ( k , p ) end end }
)

To be used like so:

setmetatable ( _G , { __index = tags } )

print ( html {
        head {
            title { “Text” } ;
            javascriptblock [[alert(“XHTML compatible!” );]] ;
        } ;
        body {
            div { id=”main” ;
                img { src = “blah.jpg” };
            }
        }
    } )

Output:

<html><head><title>Text</title><script type=”text/javascript”>//<![CDATA[
alert(“XHTML compatible!” );
//]]></script></head><body><div id=”main”><img src=”blah.jpg”/></div></body></html>

The code is quite extensible, to create a new tag/block, all you need is a table with a __tostring metamethod.

Contrary to first appearances, it doesn’t just create a document from a table, but creates an internal structure of a document; which can then be generated on demand (via the recursive __tostring metamethods). This means that dynamic content can be used with the same structure:

userloggedin = function () return true  end
function loggedinonly ( p ) if userloggedin() then return setmetatable ( p , { __tostring = function ( t ) return table.concat ( tostringtable ( p ) ) end } ) else return “” end end
print ( body {
    loggedinonly {
        div {
            “Logged in!”
        }
    }
} )

Obviously, to facilitate writing conditional code easier, some simple helper functions can be created:

setpassthrough = function ( t ) return setmetatable ( t , { __tostring = function ( o ) return table.concat ( tostringtable ( o ) ) end } ) end

function ifcond ( cond , t , f )
    if cond then
        return setpassthrough ( t )
    else
        if f then
            return setpassthrough ( f )
        else
            return “”
        end
    end
end

Maybe when I need to write some html I can put this into practice!

If you find this useful, please give me a yell!

Tags: lua coding html

Motorbike annoyances

So I finally got my GSXR-400 back together on friday, after taking it apart three months earlier for a service… And I hear a valve that isn’t correctly adjusted. Now I have to take the tank, fan and engine cover off again to readjust a valve clearance :(

Tags: motorbike