Cogs and Levers A blog full of technical stuff

OverloadedStrings Language Pragma

The OverloadedStrings language pragma can be enabled either by passing the -XOverloadedStrings switch to GHC or you can just add the following to the top of your Haskell source:

{-# LANGUAGE OverloadedStrings #-}

The OverloadedStrings language pragma changes the way that literal strings identify themselves, in a way that favours performance. [Char] is a rather cumbersome type to be used when dealing with something as primitive as a string.

Prelude> :t "Hello, world"
"Hello, world" :: [Char]
Prelude> :set -XOverloadedStrings
Prelude> :t "Hello, world"
"Hello, world" :: Data.String.IsString t => t

The literal string "Hello, world" now identifies as a call to the fromString function out of the IstString type class. You can define instances like so:

import GHC.Exts ( IsString(..) )

data Colour = Red | Green | Blue | Other String deriving Show

instance IsString Colour where
  fromString "Red" = Red
  fromString "Green" = Green
  fromString "Blue" = Blue 
  fromString xs = Other xs

Now we just cast our strings to our type, and the fromString functions are invoked for us:

Prelude GHC.Exts> "Red" :: Colour
Red
Prelude GHC.Exts> "Yellow" :: Colour
Other "Yellow"

Animation in Java

The abstract window toolkit provide the programmer with a great level of flexibility when creating user interfaces. Today’s blog post is going to go through the basic setup of a double-buffered animation loop implemented in Java, using AWT.

Settings

First off, we start by making some system settings; setting up to use OpenGL, etc:

static {
  System.setProperty("sun.java2d.trace", "timestamp,log,count");
  
  System.setProperty("sun.java2d.transaccel", "True");
  System.setProperty("sun.java2d.opengl", "True");
  
  System.setProperty("sun.java2d.d3d", "false"); //default on windows
  System.setProperty("sun.java2d.ddforcevram", "true");
}

The particulars of these flags can be found in the documentation. These flags,

  • Setup trace logging
  • Use hardware acceleration for translucency
  • Use OpenGL
  • Turn off Direct3D
  • Put images into vram

Canvas

We’ll draw to a Canvas and flip that onto our Frame. We need to configure the Canvas so that it’ll behave in a render-loop fashion, rather than responding to paint messages as it does normally.

We ignore these repaints using setIgnoreRepaint.

Now comes the double-buffer part. We create a BufferStrategy using createBufferStrategy. The strategy is what holds our graphics objects that we’ll render to.

this.createBufferStrategy(2);
strategy = this.getBufferStrategy();

Rendering

The pump for the application is the renderer. It’s pretty simple:

public void render() {
  // get the graphics object
  Graphics2D bkG = (Graphics2D) strategy.getDrawGraphics();

  // start with a black canvas
  bkG.setPaint(backgroundGradient);
  bkG.fillRect(0, 0, getWidth(), getHeight());

  // TODO: Here's where the render code goes

  // release the resources held by the background image
  bkG.dispose();

  // flip the back buffer now
  strategy.show();
  Toolkit.getDefaultToolkit().sync();
}

Get running

Here is a class that you can use to get running immediately.

Commit squashing

Git provides some excellent features when you need to amend your commit history before pushing your changes into a repository. In today’s article, I’ll run through a relativly simple use-case of the rebase command and how you can squash commits down.

This blog

I’ve recently changed text editors from Sublime back to vim. In the repository of this blog, I have a script that bootstraps a blog post with the title and date; and then opens my text editor ready for me to write.

Today I also patched a small bug where, when the file name is calculated from the title of the blog I specify - consecutive letters were being de-duplicated. Made it a real pain for a lot of words . . so, this changed. These two changes occured on the same file, new-post.sh; but I didn’t think about changing my text editor until after.

The first change

So, the first change went into the file. I staged the change and then committed it:

➜  tuttlem.github.io git:(master) ✗ git add .
➜  tuttlem.github.io git:(master) ✗ git commit -m "Patched create script"
[master 5b7b3b4] Patched create script
 1 file changed, 1 insertion(+), 1 deletion(-)

The second change

Then, thinking about it, I changed my text editor after.

➜  tuttlem.github.io git:(master) ✗ git add .
➜  tuttlem.github.io git:(master) ✗ git commit -m "Changed editor"
[master 7408fdf] Changed editor
 1 file changed, 1 insertion(+), 1 deletion(-)

This sucks. Really only wanted this in the one commit. So, I kicked off rebase in interactive mode (with -i).

➜  tuttlem.github.io git:(master) git rebase -i origin/master

This opened up a text editor for me, allowing me to make changes.

Changing history!

You’re presented with a list of commits that haven’t yet been pushed to the server. You’re also given the opportunity to change what goes on with these before you push:

pick 50412a2 New article
pick 3029ddc New article
pick e8df37a New article
pick ef5028c New article
pick e151644 New article
pick 378fc34 New article
pick eaccbd8 New article
pick 05c2d82 New article
pick d60be6a New articles
pick 5b7b3b4 Patched create script
pick 7408fdf Changed editor

# Rebase 5c2c93d..7408fdf onto 5c2c93d (11 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

I just want 7408fdf squashed into 5b7b3b4. I also want to change the commit message of 5b7b3b4. Afterwards, I’d learned that git will give me the opportunity to re-word this to combine commit messages.

pick 50412a2 New article
pick 3029ddc New article
pick e8df37a New article
pick ef5028c New article
pick e151644 New article
pick 378fc34 New article
pick eaccbd8 New article
pick 05c2d82 New article
pick d60be6a New articles
reword 5b7b3b4 Patched create script and changed editor
squash 7408fdf Changed editor

Saving this file off, we’re now presented with another text editor that allows us to update the commit message accordingly. After a bit of processing, we’re done:

➜  tuttlem.github.io git:(master) git rebase -i origin/master
[detached HEAD f0b825a] Patched create script
 Date: Wed Mar 8 11:54:00 2017 +1000
 1 file changed, 1 insertion(+), 1 deletion(-)
[detached HEAD 7bc6902] Patched create script
 Date: Wed Mar 8 11:54:00 2017 +1000
 1 file changed, 2 insertions(+), 2 deletions(-)
Successfully rebased and updated refs/heads/master.

Your git log should now confirm that everything has gone to plan:

commit 7bc6902d2543eb87dad14b088edc9a5506295809
Author: Michael Tuttle <tuttlem@gmail.com>
Date:   Wed Mar 8 11:54:00 2017 +1000

  Patched create script

  Changed editor

  Patched create script and changed editor

commit d60be6ae1acc508669193a5bfead887913284aef
Author: Michael Tuttle <tuttlem@gmail.com>
Date:   Fri Mar 3 20:04:06 2017 +1000

  New articles

Push to the server.

dotfiles

GitHub hosts a dotfiles page that lists a lot of popular repositories hosted by other users.

Docker tips

Today’s post is a collection of helpful tips to manage Docker.

Kill all running containers

Kills all of the containers currently running on your machine.

$ docker kill $(docker ps -q)

Remove all containers

Remove all containers (running or stopped) from your machine.

$ docker rm $(docker ps -q -a)

Remove dangling images

Any image builds that have failed mid-build will end up in a dangling state. You can remove any of these easily.

$ docker rmi $(docker images -q -f "dangling=true")

Remove all images from your machine

If you need to turn over ALL of the images in your local repository, you can purge out anything with the following.

$ docker rmi $(docker images -q)

Inspect the history of an image

$ docker history --no-trunc image_id

Add changes to existing images

If you’ve got a minor change to make to an already existing image, you can use commit to prevent a full build process.

$  docker commit --change "ENV DEBUG true" image_id

Tune ulimit

Make sure you have enough file descriptors to work with.

$ docker run --ulimit nofile=1024:1024 rest_of_run_arguments