<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 
 <title>Tom Preston-Werner</title>
 <link href="http://tom.preston-werner.com/atom.xml" rel="self"/>
 <link href="http://tom.preston-werner.com/"/>
 <updated>2012-05-23T14:03:49-07:00</updated>
 <id>http://tom.preston-werner.com/</id>
 <author>
   <name>Tom Preston-Werner</name>
   <email>tom@mojombo.com</email>
 </author>

 
 <entry>
   <title>Handling Multiple tmux Configurations with jtmux</title>
   <link href="http://tom.preston-werner.com/handling-multiple-tmux-configurations-with-jtmux.html"/>
   <updated>2012-05-23T00:00:00-07:00</updated>
   <id>http://tom.preston-werner.com/handling-multiple-tmux-configurations-with-jtmux</id>
   <content type="html">&lt;p&gt;For a couple of weeks now my basic workflow consists mostly of terminal programs. Thus I now use iTerm2, tmux, zsh and vim for (almost) everything. However I found myself doing much of the same things, and today I realized what i needed.&lt;/p&gt;

&lt;p&gt;Usually I open several tmux windows on a per project basis. And usually I will have several sessions and just change between them, when I change the project I am working on. But having a lot of startup scripts for tmux somehow did not fit how I wanted things.&lt;/p&gt;

&lt;p&gt;Now I have a json-file in each project folder and can start the tmux session from there. The json format is very simple and easy to understand.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/2777792.js&quot;&gt; &lt;/script&gt;


&lt;p&gt;Now I will just call jtmux and it will either attach to the session (if it already exists) or create the session, and attach to it.&lt;/p&gt;

&lt;p&gt;Installation is also very easy:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;npm install -g jtmux
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Last but not least the &lt;a href=&quot;https://github.com/clauswitt/jtmux&quot;&gt;jtmux project is hosted at github&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>I May Just Change to Vim</title>
   <link href="http://tom.preston-werner.com/i-may-just-change-to-vim.html"/>
   <updated>2012-04-20T00:00:00-07:00</updated>
   <id>http://tom.preston-werner.com/i-may-just-change-to-vim</id>
   <content type="html">&lt;p&gt;In the (recent) past I have written about &lt;a href=&quot;http://clauswitt.com/Tools-I-use---at-the-moment.html&quot;&gt;the tools&lt;/a&gt; that I use. And on that very same day, that I wrote I used TextMate for almost everything, &lt;a href=&quot;http://clauswitt.com/sorry%2C-i-changed-back-to-sublime-text-2.html&quot;&gt;Sublime Text 2&lt;/a&gt; got under my skin. Now it has happened again.&lt;/p&gt;

&lt;p&gt;I have known for many years, that using either vim or emacs would make me a more productive writer and developer.&lt;/p&gt;

&lt;p&gt;Both of these approx. 40 year old editors have absolute powers. They have different philosophies, and very passionate followers, and that has off course caused a lot of conflict in the past. I have have never participated in these - not knowingly anyway - yet for some reason I have always known that vim was more for me, than emacs. (Having tried them both briefly before).&lt;/p&gt;

&lt;p&gt;I am writing this in my newly configured &lt;a href=&quot;http://code.google.com/p/macvim/&quot;&gt;MacVim&lt;/a&gt;. I have read (most of) the newly published &lt;a href=&quot;http://pragprog.com/book/dnvim/practical-vim&quot;&gt;Practical Vim&lt;/a&gt; and (re-)watched &lt;a href=&quot;https://peepcode.com/products/smash-into-vim-i&quot;&gt;both&lt;/a&gt; &lt;a href=&quot;https://peepcode.com/products/smash-into-vim-ii&quot;&gt;vim&lt;/a&gt; screencasts on &lt;a href=&quot;https://peepcode.com/&quot;&gt;Peepcode&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I am trying hard to use the &quot;right&quot; tools. And this time - much to my surprise - I am learning much faster than before. And best of all - if this ends with that I will not be using vim for the rest of my life - Sublime Texts command mode, allows for much of the stuff I am learning now, to be used there no mather what.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>A BitArray implementation in CoffeeScript</title>
   <link href="http://tom.preston-werner.com/a-bitarray-implementation-in-coffeescript.html"/>
   <updated>2012-03-25T00:00:00-07:00</updated>
   <id>http://tom.preston-werner.com/a-bitarray-implementation-in-coffeescript</id>
   <content type="html">&lt;p&gt;UPDATE: Added the each method (a callback for each bit), and changed the api to match Thomas Fuchs implementation in javascript.&lt;/p&gt;

&lt;p&gt;Thomas Fuchs &lt;a href=&quot;https://twitter.com/#!/thomasfuchs/status/183607567371616256&quot;&gt;asked yesterday&lt;/a&gt; if anybody knew a small JS lib for manipulating bit fields/arrays. He also posted a link to a &lt;a href=&quot;https://github.com/peterc/bitarray&quot;&gt;reference implementation in Ruby&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I used an hour trying to implement the same code - except the each part - in CoffeeScript today (gist below). And just before writing this blogpost, I realized that Thomas Fuchs - of course - had &lt;a href=&quot;https://github.com/madrobby/bitarray.js&quot;&gt;implemented it in javascript&lt;/a&gt; in the meantime. Looks like there are some issue with an element width above 24 - I guess I have to look into that.&lt;/p&gt;

&lt;p&gt;Maybe I will try to test my code against Thomas' tests - and then implement the tests from the Ruby implementation.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/2192677.js&quot;&gt;&lt;/script&gt;


&lt;p&gt;This was just a quick test of my CoffeeScript skills, and I liked the challenge.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Zsh Aliases and Functions for my Git Workflow</title>
   <link href="http://tom.preston-werner.com/zsh-aliases-and-functions-for-my-git-workflow.html"/>
   <updated>2012-03-16T00:00:00-07:00</updated>
   <id>http://tom.preston-werner.com/zsh-aliases-and-functions-for-my-git-workflow</id>
   <content type="html">&lt;p&gt;My workflow when using git has some things I do a lot. Most times I hack away on some code, and then at some point want to create separate commits of what I have been doing.&lt;/p&gt;

&lt;p&gt;For this I have some minor aliases and functions that I use, to make my life a little easier.&lt;/p&gt;

&lt;h2&gt;gsts&lt;/h2&gt;

&lt;p&gt;alias for&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;git status --short
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I use this to get a quick overview of what has been changed.&lt;/p&gt;

&lt;h2&gt;gdl&lt;/h2&gt;

&lt;p&gt;alias for&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;git diff 
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;this is used to ensure that I can have separete autocompletion of this, from the normal git diff (which I have aliased to gd)&lt;/p&gt;

&lt;p&gt;autocompletions uses the short git status format, to allow me to quickly autocomplete to get a git diff of a locally changed file.&lt;/p&gt;

&lt;h2&gt;gcl&lt;/h2&gt;

&lt;p&gt;a function to take the last parameter of last command and do a git checkout of that filename. (Used after a diff to quickly checkout a change, that is not needed).&lt;/p&gt;

&lt;h2&gt;gal&lt;/h2&gt;

&lt;p&gt;a function to take the last parameter of last command and add it to a commit via git add. (Also used after a diff).&lt;/p&gt;

&lt;p&gt;Usually a quick diff in these circumstances either results in a add, and subsequently a commit, or in a checkout.&lt;/p&gt;

&lt;h2&gt;c&lt;/h2&gt;

&lt;p&gt;A simple alias for&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;git commit
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;My git related aliases and functions can be seen in this gist:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/2049889.js&quot;&gt;&lt;/script&gt;

</content>
 </entry>
 
 <entry>
   <title>Sublime Text Snippets for Backbone, CoffeeScript and AMD</title>
   <link href="http://tom.preston-werner.com/sublime-text-snippets-for-backbone-coffeescript-and-amd.html"/>
   <updated>2012-03-15T00:00:00-07:00</updated>
   <id>http://tom.preston-werner.com/sublime-text-snippets-for-backbone-coffeescript-and-amd</id>
   <content type="html">&lt;p&gt;I have been working a lot with Sublime Text, Backbone, Require.js (amd modules) and written coffeescript code almost exclusively for a couple of weeks now. This of course have had an impact on how I use my tool, and on which shortcuts I try to create for myself.&lt;/p&gt;

&lt;p&gt;I have decided to share my snippets on github.&lt;/p&gt;

&lt;p&gt;The snippets shared have some assumptions, which match how we do our application at the moment.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;All code is written in CoffeeScript&lt;/li&gt;
&lt;li&gt;All Backbone models and views are AMD modules.&lt;/li&gt;
&lt;li&gt;jQuery, Underscore, Handlebars and Backbone are globally available&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;The snippets are as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Define backbone model (mod [tab]).&lt;/li&gt;
&lt;li&gt;Define relational model (rmod [tab])&lt;/li&gt;
&lt;li&gt;Define backbone view (view [tab])&lt;/li&gt;
&lt;li&gt;Define backbone subclass - of a model or view (sub [tab])&lt;/li&gt;
&lt;li&gt;Define jasmine test - (jas [tab])&lt;/li&gt;
&lt;li&gt;A raw amd define - (def [tab])&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;The snippets are only available in the source.coffeescript context, and is available at &lt;a href=&quot;https://github.com/clauswitt/Amd-Mvc-Snippets&quot;&gt;my github repository&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>An AppleScript for Blogging from Launchbar</title>
   <link href="http://tom.preston-werner.com/an-applescript-for-blogging-from-launchbar.html"/>
   <updated>2012-03-13T00:00:00-07:00</updated>
   <id>http://tom.preston-werner.com/an-applescript-for-blogging-from-launchbar</id>
   <content type="html">&lt;p&gt;I recently quit using Quicksilver and began using Launchbar instead. QuickSilver has been a trusty companion for a long time, but it seemed to crash more and more often, and I really need my launcher to be dependable. (Especially since I have disabled spotlight).&lt;/p&gt;

&lt;p&gt;I have written a lot about my Jekyll blog engine lately. And today I will tell you how I add a post to my blog from a quick key combination.&lt;/p&gt;

&lt;p&gt;First I have changed the rake task from Jekyll Bootstrap a bit to print out the relative path to the post file when creating a new post. The final line in the rake task is&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;puts &quot;#{filename}&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Secondly I have created an AppleScript to act as an action in Lauchbar, send the post title to the rake task, and finally open the blog post in Sublime Text.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/2029236.js&quot;&gt; &lt;/script&gt;


&lt;p&gt;I stole the &lt;a href=&quot;http://applescript.bratis-lover.net/library/string/#replaceString&quot;&gt;replaceString method from here&lt;/a&gt;. The rest of the file is pretty much straight forward.&lt;/p&gt;

&lt;p&gt;This means I press cmd+space, press &quot;b&quot;, press space, and then enter the title for the blog post, and press enter.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/blog-this.png&quot; /&gt;&lt;br /&gt;
&lt;img src=&quot;/images/blog-this-title.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Now the newly generated blogpost opens in Sublime Text. From here I write the post, and press ctrl+cmd+a to add the post to the git repo, ctrl+cmd+c to commit it (commit message opens in a new tab in sublime text). Finally (after committing) I press ctrl+cmd+p to push the changes remotely, and my blogpost gets pushed live.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Yet Another Layout Change</title>
   <link href="http://tom.preston-werner.com/yet-another-layout-change.html"/>
   <updated>2012-03-12T00:00:00-07:00</updated>
   <id>http://tom.preston-werner.com/yet-another-layout-change</id>
   <content type="html">&lt;p&gt;I have been writing a lot about Jekyll lately. I really love this way of working with my blog. Today, I changed my former layout - made by myself, and thus lacking in every way a design can, when a coder makes it - to this &lt;a href=&quot;http://jekyllbootstrap.com/&quot;&gt;jekyllbootstrap&lt;/a&gt; theme called &quot;the-program&quot;.&lt;/p&gt;

&lt;p&gt;I know that this design is lacking a bit as well, it is however better than before. I will fix the layout of gists very soon, and hopefully this will be the last - major - layout change of the blog for some time.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Sorry, I changed back to Sublime Text 2</title>
   <link href="http://tom.preston-werner.com/sorry%2C-i-changed-back-to-sublime-text-2.html"/>
   <updated>2012-03-02T00:00:00-08:00</updated>
   <id>http://tom.preston-werner.com/sorry,-i-changed-back-to-sublime-text-2</id>
   <content type="html">&lt;p&gt;Earlier today I wrote a &lt;a href=&quot;http://clauswitt.com/Tools-I-use---at-the-moment.html&quot;&gt;blog post about my favorite tools&lt;/a&gt;. A bit later I wrote a tweet about how switching (for a single task) to Sublime Text from Textmate 2 for a couple of minutes felt like a speed boost like no other.&lt;/p&gt;

&lt;p&gt;Now, however, I just used Sublime Text all night, instead of just using tm2, like I normally would. And I guess I am a convert. At least until tm2's speed rival that of Sublime Text. It is just crazy fast.&lt;/p&gt;

&lt;p&gt;The biggest problem at the moment is that the change means some keyboard shortcuts have changed. Oh, and for some reason I cannot get the coffeescript bundle to show me the generated js code - a feature I use a lot atm.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Tools I use - at the moment</title>
   <link href="http://tom.preston-werner.com/Tools-I-use---at-the-moment.html"/>
   <updated>2012-03-02T00:00:00-08:00</updated>
   <id>http://tom.preston-werner.com/Tools-I-use---at-the-moment</id>
   <content type="html">&lt;p&gt;This is an attempt to list what I use, how and why, for my work as a developer.&lt;/p&gt;

&lt;h2&gt;Editors / IDE's&lt;/h2&gt;

&lt;p&gt;I have always had two battles within me. One is the &quot;Text Editor or IDE&quot;-battle, the other is the classic which editor should I use battle.&lt;/p&gt;

&lt;p&gt;First of all, I usually use an IDE for php. I really love &lt;a href=&quot;http://www.jetbrains.com/phpstorm/&quot;&gt;PhpStorm&lt;/a&gt;. It is an awesome IDE, based on IntelliJ Idea (which in itself is awesome). All of Jetbrains' IDE's are in fact awesome, and I love how I can tweak them, and still have great support for autocompleting (which, I guess, is the biggest reason for having an IDE - their support for helping you find methods on classes easily is my biggest reason for using them).&lt;/p&gt;

&lt;p&gt;Since the alpha of Textmate 2 came out last december however, most of my programming has been in TextMate. Using Textmate for almost everything (even writing blog posts) makes me remember more and more features, and even made me &lt;a href=&quot;http://clauswitt.com/Using-TextMate-2-to-Deploy-my-Blog-Posts.html&quot;&gt;write my own bundles&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However when seeing people like &lt;a href=&quot;http://zedshaw.com/&quot;&gt;Zed Shaw&lt;/a&gt; or &lt;a href=&quot;http://tenderlovemaking.com/&quot;&gt;Aaron Patterson&lt;/a&gt; use &lt;a href=&quot;http://code.google.com/p/macvim/&quot;&gt;VIM&lt;/a&gt; I really feel I am missing out on something. But I have decided that for at least a couple of months I will not stop using textmate for most of my writing tasks, since changing my main texteditor constantly is one thing that will make absolutely sure that I will always miss out on something. I may take up Vim (or Emacs) at some later point though.&lt;/p&gt;

&lt;h2&gt;Programming languages&lt;/h2&gt;

&lt;h3&gt;Php&lt;/h3&gt;

&lt;p&gt;I  have been a &lt;a href=&quot;http://php.net/&quot;&gt;Php&lt;/a&gt; developer since 2004 when our company at the time (9iA) started using Typo3 as a Cms. Until then I was a &lt;a href=&quot;http://www.microsoft.com/net&quot;&gt;.net&lt;/a&gt; developer using C# to write my own Cms called iUpdate. Since 2004 I have worked on a couple of minor .net based projects, but nothing major. Php is still my main language, mostly because all our (backend) codebase at &lt;a href=&quot;http://www.arnsbomedia.com&quot;&gt;Arnsbo Media&lt;/a&gt; is PHP.&lt;/p&gt;

&lt;p&gt;When I started working at Arnsbo Media the MVC framework landscape for Php was not as great as it is now. I started by writing my own framework, after having looked at Symfony and CakePhp both of which was not quite what we needed. My own framework was first based on Typo3, later everything from Typo3 was thrown out, and parts of &lt;a href=&quot;http://zendframework.com/&quot;&gt;Zend Framework&lt;/a&gt; and Ez Components - since changed name to &lt;a href=&quot;http://incubator.apache.org/zetacomponents/&quot;&gt;Zeta Components&lt;/a&gt; - were used instead.&lt;/p&gt;

&lt;p&gt;The framework is still used internally for two big projects, one of which is being rewritten as I write this.&lt;/p&gt;

&lt;p&gt;Lately we have adopted the &lt;a href=&quot;http://flow3.typo3.org&quot;&gt;mighty FLOW3 framework&lt;/a&gt;, which uses &lt;a href=&quot;http://www.doctrine-project.org/&quot;&gt;Doctrine&lt;/a&gt; for &lt;a href=&quot;http://en.wikipedia.org/wiki/Domain_model&quot;&gt;Domain Models&lt;/a&gt;. Having followed the development of FLOW3 closely for years, it was an easy move. (However, I do love &lt;a href=&quot;http://symfony.com/&quot;&gt;Symfony 2&lt;/a&gt; as will, especially since it has better documentation). FLOW3 is being used for almost all new (server side) code written here.&lt;/p&gt;

&lt;h3&gt;Javascript&lt;/h3&gt;

&lt;p&gt;I have been loving JavaScript ever since 2001 - so much so, that I have started a &lt;a href=&quot;http://www.aarhusjs.dk&quot;&gt;meetup group here in Aarhus only about JavaScript&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I have been following the rise of JavaScript closely. I have been using Dojo since version 0.4, and every version of prototype and jQuery since their release. I have seen javascript go from the thing you used to do minor things, like show a random image from a list of images every 2 seconds (an actual use case from a 2003 website we did) to being the only language on a large scale webapplication, where both front- and backend are written in js.&lt;/p&gt;

&lt;p&gt;Node.js has been a great tool for me as well, whenever I find myself writing a shell script and run into a barrier for writing some functionality in bash/zsh script, I almost always start a simple javascript file, and run it through node instead. Whenever I do string manipulations on a set of files - and grep/sed/awk is either overkill, or beyond my skillset, I find it easier to write a quick js function to do what I want.&lt;/p&gt;

&lt;h3&gt;Ruby&lt;/h3&gt;

&lt;p&gt;I am a bit in love with Ruby. Or maybe just the idea of &lt;a href=&quot;http://www.ruby-lang.org/en/&quot;&gt;Ruby&lt;/a&gt;. The thing is, I actually love writing Ruby code, but I use it very seldom, so I usually end up in the same dead-ends, having to look up how to do a certain thing. I use Ruby - &lt;a href=&quot;http://clauswitt.com/the-how-and-why-of-my-move-to-jekyll.html&quot;&gt;Jekyll&lt;/a&gt; - for rendering my blog, and I have written some - still unused - plugins for my setup, which I am testing at the moment.&lt;/p&gt;

&lt;h2&gt;Pseudo Languages&lt;/h2&gt;

&lt;h3&gt;CoffeeScript&lt;/h3&gt;

&lt;p&gt;At the moment we are doing a complete rewrite of a big project. It is mostly a frontend webapp, and we are doing it with CoffeeScript. Actually we are using &lt;a href=&quot;http://coffeescript.org/&quot;&gt;CoffeeScript&lt;/a&gt;, &lt;a href=&quot;http://documentcloud.github.com/backbone/&quot;&gt;Backbone&lt;/a&gt;, &lt;a href=&quot;http://jquery.com/&quot;&gt;jQuery&lt;/a&gt;, &lt;a href=&quot;http://handlebarsjs.com/&quot;&gt;Handlebars&lt;/a&gt; and &lt;a href=&quot;http://requirejs.org/&quot;&gt;require.js&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This means that all application code is written in AMD modules in CoffeeScript. In development everything is running async, and loaded on demand. For production we run compiled js code through r.js and compile all javascript code and handlebars templates to one single file, which is then loaded asyncronously with require.js on load.&lt;/p&gt;

&lt;h3&gt;Scss&lt;/h3&gt;

&lt;p&gt;For that same project we are using Scss to write stylesheets per module, and combining them using the &lt;a href=&quot;http://sass-lang.com/&quot;&gt;sass command&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Apps&lt;/h2&gt;

&lt;h3&gt;Applications&lt;/h3&gt;

&lt;p&gt;These applications are used every day&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://www.omnigroup.com/products/omnifocus/&quot;&gt;Omnifocus&lt;/a&gt; - GTD app, very customizable. I use it every time I context-switch.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.dropbox.com/&quot;&gt;Dropbox&lt;/a&gt; - For storing files, that I need access to on the go (from iPhone/iPad)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.evernote.com/&quot;&gt;Evernote&lt;/a&gt; - Storing notes&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://agilebits.com/onepassword&quot;&gt;1Password&lt;/a&gt; - Storing passwords&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.noodlesoft.com/hazel.php&quot;&gt;Hazel&lt;/a&gt; - Automatically clean up directories, and sort my files.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://cordlessdog.com/stay/&quot;&gt;Stay&lt;/a&gt; - Automatically move/resize my applications windows to where I want them, depending on which screen (if any) is attached to my laptop.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://reederapp.com/&quot;&gt;Reeder&lt;/a&gt; - Rss reader using Google Reader as backend. I wrote &lt;a href=&quot;http://clauswitt.com/how-to-screen-more-than-sixty-rss-feeds-every-day.html&quot;&gt;a blogpost about my rss workflow while back&lt;/a&gt; - however back then I used Newsrob and Gruml, now I use Reeder exclusively&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;What do you use every day? And what have I missed?&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Quick js Snippet to Create Sequentially Numbered Directories in Javascript - using NodeJs</title>
   <link href="http://tom.preston-werner.com/Quick-js-Snippet-to-Create-Sequentially-Numbered-Directories-in-Javascript---using-NodeJs.html"/>
   <updated>2012-02-27T00:00:00-08:00</updated>
   <id>http://tom.preston-werner.com/Quick-js-Snippet-to-Create-Sequentially-Numbered-Directories-in-Javascript---using-NodeJs</id>
   <content type="html">&lt;p&gt;Lately I have become more and more into ALL the habits of &lt;a href=&quot;http://www.davidco.com/about-gtd&quot;&gt;GTD&lt;/a&gt;. And because of this, I have just created a virtual tickler file.&lt;/p&gt;

&lt;p&gt;However, lazy as I am, I would rather create a quick script (and post about it) than create 43 folders my self.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1923085.js&quot;&gt; &lt;/script&gt;


&lt;p&gt;This scripts can be called with a number (how many sequential directories you want) and another number (how many digits in each number - used for prefixing zeros).&lt;/p&gt;

&lt;p&gt;The result can be seen here, when called for days (1-31)&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/terminal-sequential-directories.png&quot; /&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Using TextMate 2 to Deploy my Blog Posts</title>
   <link href="http://tom.preston-werner.com/Using-TextMate-2-to-Deploy-my-Blog-Posts.html"/>
   <updated>2012-02-23T00:00:00-08:00</updated>
   <id>http://tom.preston-werner.com/Using-TextMate-2-to-Deploy-my-Blog-Posts</id>
   <content type="html">&lt;p&gt;I have posted about &lt;a href=&quot;http://clauswitt.com/the-how-and-why-of-my-move-to-jekyll.html&quot;&gt;my new platform for blogging earlier&lt;/a&gt;. And I have even written about how I &lt;a href=&quot;http://clauswitt.com/A-Simple-Create-New-Blogpost-Script.html&quot;&gt;create new posts from the command line&lt;/a&gt;, and how I deploy using a simple zsh function (that calls a deploy.sh script).&lt;/p&gt;

&lt;p&gt;Today Chriztian Steinmeier &lt;a href=&quot;https://twitter.com/#!/greystate/status/172625912951619584&quot;&gt;asked me a simple question&lt;/a&gt; that made me go back to TextMate 2 - seriously - again. I have been using &lt;a href=&quot;http://www.jetbrains.com/phpstorm/&quot;&gt;PhpStorm&lt;/a&gt; for almost all my coding lately, that I have forgotten all the niceness in tm2.&lt;/p&gt;

&lt;p&gt;Now, though, I have extended my own bundle to include some niceness for using &lt;a href=&quot;http://documentcloud.github.com/backbone/&quot;&gt;backbone&lt;/a&gt; in &lt;a href=&quot;http://coffeescript.org/&quot;&gt;CoffeeScript&lt;/a&gt; as Amd modules.&lt;/p&gt;

&lt;p&gt;And finally I have created two quick commands for my blogging purposes. One that commits, and pushes, the changes in the _posts directory, and one that deploys my blog live, for all to see. Both with a simple keystroke in Textmate (which I already used for writing the posts anyway). On top of that I changed my ruby script for creating the posts file, just a bit, to ensure that textmate opens the new file as a markdown document (instead of as a diff file, which it apparently auto detected it to be.)&lt;/p&gt;

&lt;p&gt;The commit script has these settings in the bundle editor:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;images/commit-and-push.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;and this source for the command:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1895372.js&quot;&gt; &lt;/script&gt;


&lt;p&gt;The deploy script has these settings in the bundle editor:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;images/deploy.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;and this source for the command.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1895379.js&quot;&gt; &lt;/script&gt;


&lt;p&gt;And now my workflow from idea to published is as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the console write: blog &quot;Using TextMate 2 to Deploy my Blog Posts&quot;&lt;/li&gt;
&lt;li&gt;Write the post in TextMate&lt;/li&gt;
&lt;li&gt;Press command+option+c for commiting and pushing to BitBucket&lt;/li&gt;
&lt;li&gt;Press command+d to deploy the post on the webserver.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Only downside is that all commits have the same commit message - I will have to solve that soon.&lt;/p&gt;

&lt;p&gt;A post about the coffee-script snippets is comming soon.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Quick Local Branch Removal Script</title>
   <link href="http://tom.preston-werner.com/Quick-Local-Branch-Removal-Script.html"/>
   <updated>2012-02-20T00:00:00-08:00</updated>
   <id>http://tom.preston-werner.com/Quick-Local-Branch-Removal-Script</id>
   <content type="html">&lt;p&gt;A couple of weeks ago I wrote a post about a bash script I used to clean up remote branches. I have just created its brother (and have plans to combine them to one script at one point).&lt;/p&gt;

&lt;p&gt;In both scripts I check if the branch about to be removed is one i expect I want to keep. (In my cases master and live branches are off limits to deleting).&lt;/p&gt;

&lt;p&gt;As always the script is located as a gist on github. (Embedded here, to make it seem I have worked on this post for more than a couple of minutes.)&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1866409.js&quot;&gt; &lt;/script&gt;


&lt;p&gt;If you have a better way to check for the branches, I would love to hear from you, since I find my &quot;solution&quot; too much of a hack.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>How to do a Regular Expression Search Replace with Case Shifting In TextMate</title>
   <link href="http://tom.preston-werner.com/How-to-do-a-Regular-Expression-Search-Replace-with-Case-Shifting-In-TextMate.html"/>
   <updated>2012-02-07T00:00:00-08:00</updated>
   <id>http://tom.preston-werner.com/How-to-do-a-Regular-Expression-Search-Replace-with-Case-Shifting-In-TextMate</id>
   <content type="html">&lt;p&gt;Recently I had to clone a lot of repositories from BitBucket into a FLOW3 installation. But since FLOW3 expects that packages are named with camelcase, and bitbucket insists on saving all names as lowercase, I need to set the name of the cloned repository manually (instead of just using the last part of the repo url).&lt;/p&gt;

&lt;p&gt;This is off course as easy as:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;git clone git@bitbucket.org:username/packagename.git PackageName
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;But since I am lazy, and like to have my software do most of my work, this is what I did.&lt;/p&gt;

&lt;p&gt;Locally I created a list of all packages in the FLOW3/Packages/Application directory (which should all be cloned on the remote server).&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;ls files.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then opened the file list in TextMate, and ran the follow search/replace (as a regular expression)&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#Search
(.*)\n
#Replace
git clone git@bitbucket.org:USERNAME/\L$1\E.git $1\n
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;\L lowercases everything until \E, between those I just pass the directoryname, which is also the name of the repo.&lt;/p&gt;

&lt;p&gt;Finally I pasted the lines to the console on the remote server, and presto! All repos were cloned with the correct naming.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Removing Unused git branches Automatically</title>
   <link href="http://tom.preston-werner.com/Removing-Unused-git-branches-Automatically.html"/>
   <updated>2012-01-23T00:00:00-08:00</updated>
   <id>http://tom.preston-werner.com/Removing-Unused-git-branches-Automatically</id>
   <content type="html">&lt;p&gt;At work, our git workflow forces every issue in jira to be created in a new tracking branch. The naming convention then makes sure that the release manager can identify issue and code, and their relation.&lt;/p&gt;

&lt;p&gt;However this means that either the release manager needs to remove unused tracking branches, or the branche count on our remote repo goes through the roof. Until now, it has been a chore that has been postponed, until today.&lt;/p&gt;

&lt;p&gt;My colleague &lt;a href=&quot;http://jesperrasmussen.com/&quot;&gt;Jesper Rasmussen&lt;/a&gt; created an issue in Jira today about creating an automatizeable process to remove unused tracking branches. I jumped on the idea as soon as I saw it, since it has been bothering me for some time.&lt;/p&gt;

&lt;p&gt;Google, git and bash to the rescue.&lt;/p&gt;

&lt;p&gt;My idea was to create a bash script, make it list all tracking branches which is already merged to master, and then go through that list, and remove all the tracking branches.&lt;/p&gt;

&lt;p&gt;The resulting script looks like this&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1662029.js&quot;&gt; &lt;/script&gt;


&lt;p&gt;I have a alias for the script and can call it from any git repo I have with cleanupBranches&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Simple node.js Module to Get Distance Between two coordinates</title>
   <link href="http://tom.preston-werner.com/Simple-node.js-Module-to-Get-Distance-Between-two-coordinates.html"/>
   <updated>2012-01-13T00:00:00-08:00</updated>
   <id>http://tom.preston-werner.com/Simple-node.js-Module-to-Get-Distance-Between-two-coordinates</id>
   <content type="html">&lt;p&gt;In one of our latest projects at work, we need to calculate two world coordinates. Instead of going into details about how the calculations is made, I will just refer to &lt;a href=&quot;http://www.movable-type.co.uk/scripts/latlong.html&quot;&gt;this page&lt;/a&gt;. This explains all details of math involved in getting distances and/or bearings from lat/long pairs.&lt;/p&gt;

&lt;p&gt;I have implemented this calculation in several languages in the last couple of months - both for work, but also for learning details about a language through implementing a known algorithm in it.&lt;/p&gt;

&lt;p&gt;Here is a simple nodejs module extracted from the javascript code on that page. The module has one public method called getDistance.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1604972.js&quot;&gt; &lt;/script&gt;


&lt;p&gt;My changes are released licensed under the &lt;a href=&quot;http://en.wikipedia.org/wiki/WTFPL&quot;&gt;WTFPL&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>A zsh function to Add Last Parameter of Last Command to git</title>
   <link href="http://tom.preston-werner.com/A-zsh-function-to-Add-Last-Parameter-of-Last-Command-to-git.html"/>
   <updated>2012-01-09T00:00:00-08:00</updated>
   <id>http://tom.preston-werner.com/A-zsh-function-to-Add-Last-Parameter-of-Last-Command-to-git</id>
   <content type="html">&lt;p&gt;My git worflow often has me changing several files, and testing the changes before I commit anything to any of the files. I find the easiest way to do a commit per file is to ensure that I review the code of each file just before I commit. That way I can also ensure that the commit message is just a bit accurate, as I make no assumptions to what the change was before commiting. (Admit it, it has happened to you too!).&lt;/p&gt;

&lt;p&gt;Lazy as I am however, I was tired of writing pretty much the same command twice, or going back in history and changing diff to add before my commit.&lt;/p&gt;

&lt;pre&gt;
git diff Some/Path/To/File.ext
git add Some/Path/To/File.ext
git commit -m &quot;Commit message here&quot;
&lt;/pre&gt;


&lt;p&gt;(Yes, I know I can specify the file in the commit directly, for some reason - I never have). I created a minor zsh function to take the last parameter of the last command, and adding that parameter to the git add command.&lt;/p&gt;

&lt;pre&gt;
gd Some/Path/To/File.ext
gal
c -m &quot;Commit message here&quot;
&lt;/pre&gt;




&lt;script src=&quot;https://gist.github.com/1582827.js&quot;&gt; &lt;/script&gt;



</content>
 </entry>
 
 <entry>
   <title>A Simple Create New Blogpost Script</title>
   <link href="http://tom.preston-werner.com/A-Simple-Create-New-Blogpost-Script.html"/>
   <updated>2012-01-07T00:00:00-08:00</updated>
   <id>http://tom.preston-werner.com/A-Simple-Create-New-Blogpost-Script</id>
   <content type="html">&lt;p&gt;As I wrote yesterday I just &lt;a href=&quot;/the-how-and-why-of-my-move-to-jekyll.html&quot;&gt;changed my blog platform from Wordpress to Jekyll&lt;/a&gt;. Today I have created some minor tweaks in my setup to allow me to easier create a post, and deploy the blog live. Now I call a simple command in the terminal, with the blog post name as a parameter, and the file is created in the correct place, and opened in TextMate.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/terminal-new-blog-post.png?1&quot; /&gt;&lt;/p&gt;

&lt;p&gt;And this is how that looks in textmate:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/textmate-new-blog-post.png?1&quot; /&gt;&lt;/p&gt;

&lt;p&gt;I did a quick search on google - figuring someone else had solved this problem before me - and found &lt;a href=&quot;http://minhajuddin.com/2010/10/01/helper-script-to-create-new-posts-using-jekyll/&quot;&gt;this post&lt;/a&gt; which was (mostly) what I needed. I have done two minor tweaks to my forked version of the gist he links to.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Changed the template file to _template.markdown&lt;/li&gt;
&lt;li&gt;Instead of outputting the filename, open it in textmat&lt;/li&gt;
&lt;/ul&gt;


&lt;script src=&quot;https://gist.github.com/1574298.js&quot;&gt; &lt;/script&gt;


&lt;p&gt;Then I created two small zsh functions to ensure that I can start a blogpost (and deploy my blog) from any directory on my machine.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1574360.js&quot;&gt; &lt;/script&gt;


&lt;p&gt;And finally I fixed a minor thing, where my ruby scripts and, the deploy.sh file was deployed to the website. The following line was added to _config.yml&lt;/p&gt;

&lt;pre&gt;
exclude: deploy.sh new.rb _template.markdown
&lt;/pre&gt;


&lt;p&gt;The last entry in that file is actually redundant. The reason I changed template.markdown to _template.markdown was actually that it rendered a file on the website called template.html which was empty - the fix to change the name could not be used for the script files (well, it could, but I did not want to do that).&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>The How and Why of my Move to Jekyll</title>
   <link href="http://tom.preston-werner.com/the-how-and-why-of-my-move-to-jekyll.html"/>
   <updated>2012-01-06T11:45:00-08:00</updated>
   <id>http://tom.preston-werner.com/the-how-and-why-of-my-move-to-jekyll</id>
   <content type="html">&lt;p&gt;Today I decided to move my blog from &lt;a href=&quot;http://www.wordpress.org&quot;&gt;Wordpress&lt;/a&gt; to static html files. These files are generated via &lt;a href=&quot;https://github.com/mojombo/jekyll&quot;&gt;Jekyll&lt;/a&gt;. The first version of this new blog, contains (most) blog posts from the wordpress installation. The included &lt;a href=&quot;https://github.com/mojombo/jekyll/wiki/blog-migrations&quot;&gt;migration script&lt;/a&gt; in the jekyll package was used to move all posts to markdown format. All absolute urls to images was changed to relative urls, and all [gist]-shortcodes was changed (via a simple regular expression search and replace in Textmate2) to the standard script-include.&lt;/p&gt;

&lt;pre&gt;
Search: \[gist id=([0-9]*)\]
Replace: script tag with the src: https://gist.github.com/$1.js
&lt;/pre&gt;


&lt;p&gt;I have had this move planned for some time actually. I was tired of the constant surveilance and updating of wordpress (and plugins). And I was tired of the tedious creation and editing of posts. Now, whenever I want to create a new post, I just create a new &lt;a href=&quot;http://daringfireball.net/projects/markdown/&quot;&gt;markdown-file&lt;/a&gt; in the _posts folder - all content are also stored in a git-repo instead of in databases. All unnecessary software has been removed from my blog - and only a simple &lt;a href=&quot;http://nginx.org/&quot;&gt;nginx&lt;/a&gt; server is used for serving the content. The downsides are simply outweighed by the upsides.&lt;/p&gt;

&lt;p&gt;I use a simple two (three if you count the &lt;a href=&quot;http://en.wikipedia.org/wiki/Shebang_(Unix)&quot;&gt;shebang&lt;/a&gt; line shell script to upload the changes to my blog.&lt;/p&gt;

&lt;pre&gt;
#! /bin/bash
jekyll
rsync -avz --delete _site/ user@domain.tld:/var/www/rootDir/
&lt;/pre&gt;


&lt;p&gt;The complete installation of Jekyll, move from wordpress and first deploy took me less than one hour. And I love it already. I may need to do some design work at some point though.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Per Package Unit Testing in FLOW3 using PhpStorm</title>
   <link href="http://tom.preston-werner.com/per-package-unit-testing-in-flow3-using-phpstorm.html"/>
   <updated>2011-11-23T23:37:09-08:00</updated>
   <id>http://tom.preston-werner.com/per-package-unit-testing-in-flow3-using-phpstorm</id>
   <content type="html">&lt;h2&gt;UPDATE&lt;/h2&gt;


&lt;p&gt;Do not do this! &lt;a href=&quot;http://clauswitt.com/per-package-unit-testing-in-flow3-using-phpstorm.html#comment-425500463&quot;&gt;Check Karstens comment why&lt;/a&gt; - and go to &lt;a href=&quot;http://blog.k-fish.de/2011/02/unit-testing-flow3-with-phpstorm.html&quot;&gt;his blog post about setting this up correctly&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Everything below this, line is not useful for anything (except perhaps learning that you should not try to create a fix for a problem that does not exist.)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; you may add you pear directory as a directory in the project, to ensure that you get them indexed (and get autocomplete on for instance ). On my mac, the pear package is located in /usr/local/share/pear - find where yours is by running:&lt;/p&gt;

&lt;pre&gt;
pear config-show |grep &quot;PEAR directory&quot;
&lt;/pre&gt;


&lt;p&gt;At &lt;a href=&quot;http://www.arnsbomedia.com/&quot;&gt;Arnsbo Media&lt;/a&gt; we don't do unit testing on everything. Whereas for some projects (mostly containing models, views and controllers) we feel unit testing [and tdd in generel] is overkill - other projects (or some modules/packages) are prime candidates for tdd. Typically when we are creating service integration we do it test driven. My own definition is that when all users of a given package/module are other packages/modules - and not databases, webservers, end users and so forth - unit testing is a viable way to ensure longevity of the APIs, and solidity of the implementation.&lt;/p&gt;

&lt;p&gt;At the moment we are doing several parts of a very big system in &lt;a href=&quot;http://flow3.typo3.org/&quot;&gt;FLOW3&lt;/a&gt;. Each part is an independent package, and most of them are service layers, and/or abstractions of external systems. Each of these packages should be testable without depending on other packages (apart from the ones required by flow3). Thus we use FLOW3s built-in test framework (based on phpunit), but we have placed our own configuration and bootstrap for testing a package as part of the package.&lt;/p&gt;

&lt;p&gt;It makes several assumptions however:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The package is placed in the directory FLOW3/Packages/Application&lt;/li&gt;
&lt;li&gt;The FLOW3 packages are placed in FLOW3/Packages/Framework&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/sebastianbergmann/phpunit/&quot;&gt;Phpunit&lt;/a&gt; is installed&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/mikey179/vfsStream/wiki/Install&quot;&gt;VfsStream&lt;/a&gt; is installed&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;The layout of the package is pretty standard.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/ScreenShot.png?1&quot; alt=&quot;Package contents&quot; title=&quot;ScreenShot.png&quot; border=&quot;0&quot; width=&quot;410&quot; height=&quot;348&quot; /&gt;&lt;/p&gt;

&lt;p&gt;We have created a Build/PhpUnit directory in which we place the configuration file, and the bootstrap for the package. These files a copy-paste-able to any package. The content of both are copied from the framework supplied files in FLOW3/Build/Common/PhpUnit - and tailored to be used from the packages directory. For both files the only thing changed is how to resolve the directories from the current file. Otherwise everything is left as is.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1390828.js&quot;&gt; &lt;/script&gt;




&lt;script src=&quot;https://gist.github.com/1390830.js&quot;&gt; &lt;/script&gt;


&lt;p&gt;After this all you have to do is setup a Run Configuration for PhpStorm.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/Screen-Shot-2011-11-24-at-08.28.54-.png?1&quot; alt=&quot;Screen Shot 2011 11 24 at 08 28 54&quot; title=&quot;Screen Shot 2011-11-24 at 08.28.54 .png&quot; border=&quot;0&quot; width=&quot;198&quot; height=&quot;288&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/Screen-Shot-2011-11-24-at-08.29.48-.png?1&quot; alt=&quot;Screen Shot 2011 11 24 at 08 29 48&quot; title=&quot;Screen Shot 2011-11-24 at 08.29.48 .png&quot; border=&quot;0&quot; width=&quot;380&quot; height=&quot;101&quot; /&gt;&lt;/p&gt;

&lt;p&gt;All tests are placed in the Tests/Unit directory, and as a rule of thumb I place them in a matching subdirectory to where the class being tested is under the Classes directory, and name it by appending Test to the class- (and subsequently the file-) name. In this case we have a single GoogleHandler class, and a test for it.&lt;/p&gt;

&lt;p&gt;Finally all you have to do is run the app using the phpunit run configuration, and you test results are ready for you.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>A Simple ViewHelper For FLOW3</title>
   <link href="http://tom.preston-werner.com/a-simple-viewhelper-for-flow3.html"/>
   <updated>2011-11-15T05:56:22-08:00</updated>
   <id>http://tom.preston-werner.com/a-simple-viewhelper-for-flow3</id>
   <content type="html">&lt;p&gt;For some time now I have wanted to use FLOW3 for work-projects. However it was not until it recently got out of the alpha/beta cycle that we decided to use it for a critical project.&lt;/p&gt;

&lt;p&gt;Mostly for fun, I just now finished a simple viewhelper which is a very simple version of the &lt;a href=&quot;http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-distance_of_time_in_words&quot;&gt;rails method distance_of_time_in_words&lt;/a&gt;. My viewhelper is much simpler than this, and were created in less than twenty minutes. I might however implement the Rails conventions since I quite like them.&lt;/p&gt;

&lt;p&gt;As always I have created a gist where you can find the code for it:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1367106.js&quot;&gt; &lt;/script&gt;


&lt;p&gt;The usage is as simple as all other FLOW3 view helpers. First include the namespace, and then call the viewhelper by name. Call it with either a DateTime object, or a string which it's constructor accepts, otherwise an exception will be thrown.&lt;/p&gt;

&lt;pre&gt;
{namespace c=ClausWitt\ViewHelpers}
&amp;lt;c:ago date=&quot;2011-11-15T18:15:44+01:00&quot;&amp;gt;&amp;lt;/c:ago&amp;gt;
&lt;/pre&gt;


&lt;p&gt;At the time of writing, the above outputtet: &quot;3 hours from now&quot;.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>A Simple Promises AMD Module</title>
   <link href="http://tom.preston-werner.com/a-simple-promises-amd-module.html"/>
   <updated>2011-11-13T23:56:31-08:00</updated>
   <id>http://tom.preston-werner.com/a-simple-promises-amd-module</id>
   <content type="html">&lt;p&gt;A week or so ago I wrote a post about &lt;a href=&quot;http://www.clauswitt.com/unique-filename-generator-amd-module/&quot;&gt;a simple AMD module&lt;/a&gt; I had created. Now I have created one more, this time a little more complex. While the latter was for a secret project, this one is (apart from also being for that project) also a part of my upcoming talk on Scaling Javascript at the &lt;a href=&quot;https://www.facebook.com/event.php?eid=171521292931689&quot;&gt;Aarhus Frontend Meetup&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This code is a very simple implementation of a Promise. It is in no way a full implementation of how futures/promises should work in javascript, there are many great implementations of that out there. This is however a simple working module, that I use for doing simple promise like calls when I need a callback when some long-running function has ended and returned.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1363471.js&quot;&gt; &lt;/script&gt;


&lt;p&gt;I have also written a simple test of the functionality in the module. Both the module and the test has been tested with RequireJs only. This test should first show you an alert with the text &quot;Test began&quot;, followed by a pause of one second, and then show an alert with the text &quot;test complete&quot;.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1363479.js&quot;&gt;&lt;/script&gt;


&lt;p&gt; &lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Unique Filename Generator AMD Module</title>
   <link href="http://tom.preston-werner.com/unique-filename-generator-amd-module.html"/>
   <updated>2011-11-01T13:38:21-07:00</updated>
   <id>http://tom.preston-werner.com/unique-filename-generator-amd-module</id>
   <content type="html">&lt;p&gt;I have recently begun to work a lot with AMD modules. I usually use RequireJS to load the modules - since this works both in the browser and in node. I am currently working - privately - on some exciting minor projects, in which it is possible to share modules. The most general of these will be published here, and the first one is allready here.&lt;/p&gt;

&lt;p&gt;It is a very simple module, which has one purpose. To generate filenames which will be unique in an otherwise empty (temporary) directory. It does this by using the date objects getTime method - to get the unix epoch (in milliseconds), and then if it is called two times within the same millisecond, it will append &lt;em&gt;1, &lt;/em&gt;2 .. _n to the name. It has an optional parameter to set the file extension.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1331802.js&quot;&gt; &lt;/script&gt;


&lt;p&gt;The usage is very simple.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1331826.js&quot;&gt; &lt;/script&gt;

</content>
 </entry>
 
 <entry>
   <title>Even Simpler Stats in Javascript</title>
   <link href="http://tom.preston-werner.com/even-simpler-stats-in-javascript.html"/>
   <updated>2011-10-19T01:01:52-07:00</updated>
   <id>http://tom.preston-werner.com/even-simpler-stats-in-javascript</id>
   <content type="html">&lt;p&gt;Last week I wrote about &lt;a href=&quot;http://www.clauswitt.com/simple-statistics-in-javascript/&quot;&gt;simple statistics in javascript&lt;/a&gt;. However it turned out I needed something else. First of all, the only values I needed was the mean (arithmetic mean) and the standard deviation - but I needed these values from a large set of large numbers. However this turned out to be a problem, because the sum of all these numbers could possibly be greater than the largest number representable by javascript.&lt;/p&gt;

&lt;p&gt;The solution was to make these values available as running calculations. Each time you add an element, the mean and standard deviation is calculated from the existing values, set size and the new value. This means for large sets, this code will use much less memory.&lt;/p&gt;

&lt;p&gt;Like last time, the complete source is embedded from a gist at github.com&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1297704.js&quot;&gt; &lt;/script&gt;


&lt;p&gt;To check the values - I have tested with the same values as last time:&lt;/p&gt;

&lt;pre&gt;
var stat = SimpleStats([12, 13, 15, 18, 19, 4, 59, 100, 6, 129, 83, 5]);
console.log(stat.getArithmeticMean()); //38.583333333333336
console.log(stat.getStandardDeviation()); //41.27844137346058
&lt;/pre&gt;


&lt;p&gt;However now you should also be able to add a number to the set, and have the new values instantly available to you:&lt;/p&gt;

&lt;pre&gt;
stat.addValues([1,2,3,4,5,6,7]);
console.log(stat.getArithmeticMean()); //25.842105263157894
console.log(stat.getStandardDeviation()); //36.82285211214811
&lt;/pre&gt;

</content>
 </entry>
 
 <entry>
   <title>Fixing Slow DNS Queries in OS X Lion</title>
   <link href="http://tom.preston-werner.com/fixing-slow-dns-queries-in-os-x-lion.html"/>
   <updated>2011-10-14T12:54:59-07:00</updated>
   <id>http://tom.preston-werner.com/fixing-slow-dns-queries-in-os-x-lion</id>
   <content type="html">&lt;p&gt;This post is a bit embarrasing. For a long time (from Lion got released, and until a moment ago) I thought that my hosts-file problem in Lion was unsolvable and that it was the fault of Apple.&lt;/p&gt;

&lt;p&gt;The issue I had was that when developing on my local machine (PHP 5.3 in Apache), all requests to the webserver were painfully slow. The reason was that all dns queries for my development domains (all ending with .local) went to a remote dns server, before failing, and falling back to the entry from the /etc/hosts file.&lt;/p&gt;

&lt;p&gt;Apparently it is because OS X Lion is running IPv6, and thus since my hosts file only contained IPv4 addresses, Lion decided to look up if a IPv6 entry was at the dns server, before falling back to checking the IPv4 in the hosts file.&lt;/p&gt;

&lt;h2&gt;First fix your hosts file&lt;/h2&gt;


&lt;p&gt;In your hosts file, you need to provide both IPv4 and IPv6 address for each domain you need to develop on.&lt;/p&gt;

&lt;pre&gt;
127.0.0.1 flow3.local #This was the old line
fe80::1%lo0 flow3.local #This needs to be added as well. 
&lt;/pre&gt;




&lt;h2&gt;Second fix your apache configuration&lt;/h2&gt;


&lt;p&gt;If you previously had your apache virtual hosts ip based, you need to have them only port based.&lt;/p&gt;

&lt;pre&gt;
#Before
NameVirtualHost 127.0.0.1:80

#After
NameVirtualHost *:80

#Before
&amp;lt;VirtualHost 127.0.0.1:80&amp;gt;

#After
&amp;lt;VirtualHost *:80&amp;gt;
&lt;/pre&gt;


&lt;p&gt;Page load times went from 2-3 seconds, to almost instantly.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Simple Statistics in JavaScript</title>
   <link href="http://tom.preston-werner.com/simple-statistics-in-javascript.html"/>
   <updated>2011-10-13T14:05:32-07:00</updated>
   <id>http://tom.preston-werner.com/simple-statistics-in-javascript</id>
   <content type="html">&lt;p&gt;I love tinkering with javascript, and working out solutions to simple little problems. Recently I had a simple statistical program in mind, that should be able to run in the browser. However while thinking about the idea, I wondered if there were any statistical functions in js allready available. Most like for instance &lt;a href=&quot;http://www.jstat.org/&quot;&gt;jStat&lt;/a&gt; were completely overkill. I just needed mean, harmonic mean, geometric mean, standard deviation and median for a simple array of values.&lt;/p&gt;

&lt;p&gt;I decided to write up a simple little &quot;class&quot; for that myself.&lt;/p&gt;

&lt;p&gt;The code should pretty much speak for itself, and all functions have a comment with a link to the description of what it is on Wikipedia.&lt;/p&gt;

&lt;p&gt;The complete source of the Stats.js file is embedded here. (Or go to &lt;a href=&quot;https://gist.github.com/1285478&quot;&gt;gist.github.com&lt;/a&gt; to fork it).&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1285478.js&quot;&gt; &lt;/script&gt;


&lt;p&gt;The package can be used like so:&lt;/p&gt;

&lt;pre&gt;
var stat = Stats([12, 13, 15, 18, 19, 4, 59, 100, 6, 129, 83, 5]);
console.log(stat.getArithmeticMean()); //38.583333333333336
console.log(stat.getGeometricMean()); //20.66546833393584
console.log(stat.getHarmonicMean()); //12.017712168189883
console.log(stat.getStandardDeviation()); //41.27844137346058
console.log(stat.getMedian()); //18
&lt;/pre&gt;

</content>
 </entry>
 
 <entry>
   <title>Moving a Subdirectory to a Submodule in Git</title>
   <link href="http://tom.preston-werner.com/moving-a-subdirectory-to-a-submodule-in-git.html"/>
   <updated>2011-09-20T01:57:18-07:00</updated>
   <id>http://tom.preston-werner.com/moving-a-subdirectory-to-a-submodule-in-git</id>
   <content type="html">&lt;p&gt;Sometimes when you work on a project long enough, you find that for one reason or the other, parts of the project should be in a separate repository. Either the subdir contains reusable code, to be used on another project, or - as it was in our case today - some part of the project, was needed on many servers, and the rest only on one. We decided to create a separate module to ensure that we did not clone the entire projects for these servers.&lt;/p&gt;

&lt;p&gt;There are two steps to this procedure. 1. Get the subdir - with the complete git history into a new repo, and 2. get the new repo in to the old project as a submodule.&lt;/p&gt;

&lt;p&gt;This is how you move &quot;Application/scripts/shell&quot; from OldProject into its own repo called NewProject. And then link it as a submodule at the same location it were before.&lt;/p&gt;

&lt;h2&gt;Subdir to another repo&lt;/h2&gt;


&lt;pre&gt;
git clone --no-hardlinks OldProject NewProject
cd NewProject
git filter-branch --subdirectory-filter Application/scripts/shell HEAD
git reset --hard
rm -rf .git/refs/original/
git reflog expire --expire=now --all
git gc --aggressive --prune=now
&lt;/pre&gt;


&lt;p&gt;Next you need to set the remote (in our case origin) to the new repo's remote.&lt;/p&gt;

&lt;h2&gt;Create submodule&lt;/h2&gt;


&lt;p&gt;Back in the old project, remove the part you have extracted, and commit. Then run the following commands.&lt;/p&gt;

&lt;pre&gt;
git submodule add git@your.git.host:NewProject.git Application/scripts/shell
-- commit -- 
git submodule init
git submodule update 
-- commit again -- 
&lt;/pre&gt;


&lt;p&gt;You should now have you former subdirectory as a separate repository, at the same location.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Project Euler Problem Nine</title>
   <link href="http://tom.preston-werner.com/project-euler-problem-nine.html"/>
   <updated>2011-07-12T22:00:14-07:00</updated>
   <id>http://tom.preston-werner.com/project-euler-problem-nine</id>
   <content type="html">&lt;p&gt;The ninth Euler Problem is currently the one I have solved with the fewest lines of code. The problem is to find the product of the (only) pythagorean triplet for which the sum of the three numbers is 1000. Look at the &lt;a href=&quot;http://projecteuler.net/index.php?section=problems&amp;id=9&quot;&gt;problem description&lt;/a&gt; to learn what a pythagorean triplet is.&lt;/p&gt;

&lt;p&gt;The code to solve this is again a simple iteration (within an iteration). The solution is written in thirteen lines of code - five of which are closing braces.&lt;/p&gt;

&lt;p&gt;For every a and b combination c is calculated. If c is an integer, it is checked wether a+b+c equals 1000. If it does, the routine returns the product.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1063783.js&quot;&gt; &lt;/script&gt;


&lt;p&gt;run with nodejs.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Click to split text in the browser</title>
   <link href="http://tom.preston-werner.com/click-to-split-text-in-the-browser.html"/>
   <updated>2011-07-10T22:00:10-07:00</updated>
   <id>http://tom.preston-werner.com/click-to-split-text-in-the-browser</id>
   <content type="html">&lt;p&gt;Recently I had to implement a feature - running in chrome, safari and firefox - that enabled users to click in a text to split the text into two paragraphs. At first the solution eluded me, however a morning while entertaining my son before going on road trip to my wires parents house, a possible solution popped into my head. Fortunately my son let me try it - and it worked!&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1063805.js&quot;&gt; &lt;/script&gt;


&lt;p&gt;The whole trick is to let the click event call a method that uses document.caretRangeFromPoint() (for WebKit browsers) and document.createRange() for Firefox to create a range from the clicked point, and then using the start of that range - range.startOffset as the point in the text to split. All text before the point is kept in the current paragraph, all text after the point is used for the newly created paragraph after the clicked one.&lt;/p&gt;

&lt;p&gt;I have created a &lt;a href=&quot;http://demo.clauswitt.com/testClickToSplit.html&quot;&gt;quick and dirty demo&lt;/a&gt; - only working (tested) in (the latest versions of) Safari, Chrome and Firefox.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Project Euler Problem Eight</title>
   <link href="http://tom.preston-werner.com/project-euler-problem-eight.html"/>
   <updated>2011-07-08T22:00:45-07:00</updated>
   <id>http://tom.preston-werner.com/project-euler-problem-eight</id>
   <content type="html">&lt;p&gt;The eighth problem of Project Euler is very simple. &lt;a href=&quot;http://projecteuler.net/index.php?section=problems&amp;id=8&quot;&gt;Find the greatest product of five consecutive digits in the 1000-digit number&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The problem at first however has a bit of a twist. You cannot represent that large a number as an integer in javascript. However, that is not needed at all. And this is an important lesson to take away from this problem, that you will definitely need later on.&lt;/p&gt;

&lt;p&gt;I decided to place the 1000 digit number in a string, then split the string into a 1000-element array. Then checking a product of 4 consecutive digits is very simple.&lt;/p&gt;

&lt;p&gt;As always the code is javascript, and can be run with nodejs.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1063774.js&quot;&gt; &lt;/script&gt;

</content>
 </entry>
 
 <entry>
   <title>Project Euler Problem Seven</title>
   <link href="http://tom.preston-werner.com/project-euler-problem-seven.html"/>
   <updated>2011-07-06T22:00:46-07:00</updated>
   <id>http://tom.preston-werner.com/project-euler-problem-seven</id>
   <content type="html">&lt;p&gt;&lt;a href=&quot;http://projecteuler.net/index.php?section=problems&amp;id=7&quot;&gt;The seventh problem&lt;/a&gt; of Project Euler is again a simple problem, which can be solved by iteration. The challenge is how to establish when a number is a prime. However, reading the definition of a prime number, you can get some help. &quot;A natural number is called a prime number (or a prime) if it is bigger than one and has no divisors other than 1 and itself.&quot; - &lt;a href=&quot;http://en.wikipedia.org/wiki/Prime_number&quot;&gt;Wikipedia&lt;/a&gt;. By knowing this we have a simple routine that exits early for all even numbers above 2 (which is the only even number that is a prime). After that all numbers up to the square root is checked as a divisor, if one is found, the number is not a prime.&lt;/p&gt;

&lt;p&gt;The reason for checking only up to the square root of number, is that if any divisor exists above the square root, one must exist below the square root as well, meaning that we already have established that the number is not a prime.&lt;/p&gt;

&lt;p&gt;The code for the problem is run with nodes… Again..&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1063766.js&quot;&gt; &lt;/script&gt;

</content>
 </entry>
 
 <entry>
   <title>Project Euler Problem Six</title>
   <link href="http://tom.preston-werner.com/project-euler-problem-six.html"/>
   <updated>2011-07-04T22:00:33-07:00</updated>
   <id>http://tom.preston-werner.com/project-euler-problem-six</id>
   <content type="html">&lt;p&gt;&lt;a href=&quot;http://projecteuler.net/index.php?section=problems&amp;id=6&quot;&gt;The sixth problem&lt;/a&gt; from &lt;a href=&quot;http://projecteuler.net/&quot;&gt;Project Euler&lt;/a&gt; is about finding the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.&lt;/p&gt;

&lt;p&gt;This problem is quite simple. Both numbers (sum of squares, and square of sum) can be found through simple iterations. First we find the square of sums.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1063229.js&quot;&gt; &lt;/script&gt;


&lt;p&gt;Then we find the sum of squares.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1063232.js&quot;&gt; &lt;/script&gt;


&lt;p&gt;Simple enough. Here the total solution is wrapped in a js function called through node.js&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1063238.js&quot;&gt; &lt;/script&gt;

</content>
 </entry>
 
 <entry>
   <title>A Simple NodeJs-based Chat Demo</title>
   <link href="http://tom.preston-werner.com/a-simple-nodejs-based-chat-demo.html"/>
   <updated>2011-06-05T10:26:29-07:00</updated>
   <id>http://tom.preston-werner.com/a-simple-nodejs-based-chat-demo</id>
   <content type="html">&lt;p&gt;I have written a few posts about &lt;a href=&quot;http://www.clauswitt.com/tag/nodejs/&quot;&gt;NodeJs&lt;/a&gt;. (Albeit only as a way to demo some Project Euler solutions - until now).&lt;/p&gt;

&lt;p&gt;Requirements for getting my code to run on your own machine is that you &lt;a href=&quot;http://howtonode.org/how-to-install-nodejs&quot; target=&quot;_blank&quot;&gt;install nodejs&lt;/a&gt; and &lt;a href=&quot;https://github.com/isaacs/npm&quot; target=&quot;_blank&quot;&gt;npm&lt;/a&gt;. With npm you should install express and socket.io.&lt;/p&gt;

&lt;p&gt;This simple demo is split into four parts, in three files. A static index.html page, a client side js file, and then a server.js file which includes setup for express and socket.io. Express is a great framework for http servers, and socket.io is a cross browser WebSockets implementation with fallbacks for browsers that do not implement websockets.&lt;/p&gt;

&lt;p&gt;The main part is server.js:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1009177.js&quot;&gt; &lt;/script&gt;


&lt;p&gt;The first part is the setup of express. The line:&lt;/p&gt;

&lt;pre&gt;
app.use(express.static(__dirname + '/../Public'));
&lt;/pre&gt;


&lt;p&gt;serves static files from the public directory, where we place our index.html file.&lt;/p&gt;

&lt;p&gt;Express uses a simple way to setup simple routes&lt;/p&gt;

&lt;pre&gt;
//A simple action to set the counter
app.get('/sentCounter/set/:count', function(req, res){
    sentCounter = req.params.count;
    res.send('ok');
});
&lt;/pre&gt;


&lt;p&gt;This route for example is used to set the sentCounter value to a value sent in as a url part. /sentCounter/set/200 would set the value to 200.&lt;/p&gt;

&lt;p&gt;To setup socket.io, simply call its listen method with the express-server as an argument. Socket io then takes over all calls to urls starting with /socket.io/&lt;/p&gt;

&lt;p&gt;The socket.io server gets an event everytime a client connects, disconnects and sends a message. The server can send messages to specific clients or broadcast to all clients (with an option to filter specific clients away).&lt;/p&gt;

&lt;p&gt;My demo implementation also has a timed event, that sends a message to all connected clients every 20 seconds.&lt;/p&gt;

&lt;p&gt;The html is all vanilla, with a script included: /socket.io/socket.io.js - this is the client side api of the socket.io library.&lt;/p&gt;

&lt;p&gt;The last part is my client side js file, which sets some event handlers for the buttons in the html, and handles connect, disconnect, incomming messages, and sending messages from the textarea.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1009189.js&quot;&gt; &lt;/script&gt;


&lt;p&gt;It pretty much straight forward. The interesting parts are&lt;/p&gt;

&lt;pre&gt;
socket.on('message', function(data){});
&lt;/pre&gt;


&lt;p&gt;Which is called every time the client receives a message.&lt;/p&gt;

&lt;p&gt;and&lt;/p&gt;

&lt;pre&gt;
socket.send(message);
&lt;/pre&gt;


&lt;p&gt;Which sends a message (you can send json, if you need to send objects).&lt;/p&gt;

&lt;p&gt;That's it - you can grab the complete demo at &lt;a href=&quot;https://github.com/clauswitt/NodeJs-Test-Chat&quot; target=&quot;_blank&quot;&gt;Github&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>A Simple Dojo Twitter Widget</title>
   <link href="http://tom.preston-werner.com/a-simple-dojo-twitter-widget.html"/>
   <updated>2011-02-17T09:22:58-08:00</updated>
   <id>http://tom.preston-werner.com/a-simple-dojo-twitter-widget</id>
   <content type="html">&lt;p&gt;For a simple walk through of how you can write widgets I decided to write a Twitter Widget, which shows a list of tweets for one or more people. The widget is simple to insert into a page.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/832116.js&quot;&gt;&lt;/script&gt;


&lt;p&gt;&lt;/p&gt;

&lt;p&gt;This widget of course needs Dojo on the page, and parseOnLoad (or a manual call to parse the widgets) as well as the source of the widget of course. On top of that a lot of css is needed for it to look ok.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/832126.js&quot;&gt;&lt;/script&gt;


&lt;p&gt;&lt;/p&gt;

&lt;p&gt;The javascript file for the widget has three parts. Some required (boilerplate) code for it to be a Dojo Widget, some public methods needed for it to interact with the page, and last but not least the actual implementation of the widget.&lt;/p&gt;

&lt;p&gt;The first part is covered many times. The second consists of using the method preamble() in which you can manipulate the parameters of the widget, before the widget is actually instantiated. In the example this is used to ensure that the class twitterTickerWidget is prefixed to any custom class the user adds to the widget, as well as ensuring that a header is set, if it is empty when before instantiating.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/832112.js&quot;&gt; &lt;/script&gt;


&lt;p&gt;There are also some default values set - the widget will e.g. only show tweets from my twitter account, if no twitterusernames is included when inserting into a page.&lt;/p&gt;

&lt;p&gt;The main part of the widget is a jsonp call to &lt;a href=&quot;http://search.twitter.com/search.json&quot;&gt;twitters public api&lt;/a&gt;. The result of this api call is a json string representing an array of objects. One object per tweet. This array is then parsed and converted into a dom representation which is placed into the root dom node of the widget.&lt;/p&gt;

&lt;p&gt;There is used some &lt;a href=&quot;http://www.arstdesign.com/articles/autolink.html&quot; target=&quot;_blank&quot;&gt;regex magic to auto link urls in tweets&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You can see a quick demo (with some css styling) of the &lt;a href=&quot;http://clauswitt.github.com/testTickerWidget.html&quot;&gt;Dojo Twitter Widget&lt;/a&gt;. &lt;strong&gt;Note:&lt;/strong&gt; it seams the demo does not work in Safari at the moment. The error is that it cannot find the js file. It works in Firefox and Chrome though.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;UPDATE:&lt;/strong&gt; the error that made it fail in Safari (and IE -  though this last claim is untested, since I'm on a Mac without a virtual machine for testing in ie atm) was of course an error in &lt;strong&gt;my&lt;/strong&gt; code. Since class is a reserved word in javascript, you cannot reference object.class instead you should use object[&quot;class&quot;].&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/833462.js&quot;&gt; &lt;/script&gt;



</content>
 </entry>
 
 <entry>
   <title>Average CLI Script Run Time with grep, sed and awk</title>
   <link href="http://tom.preston-werner.com/average-cli-script-run-time-with-grep-sed-and-awk.html"/>
   <updated>2011-02-13T21:00:28-08:00</updated>
   <id>http://tom.preston-werner.com/average-cli-script-run-time-with-grep-sed-and-awk</id>
   <content type="html">&lt;p&gt;Our order processing system at &lt;a href=&quot;http://www.voicearchive.com&quot; title=&quot;voice overs&quot; target=&quot;_blank&quot;&gt;voicearchive&lt;/a&gt; has several background tasks that need to run often. For this we have a very nice cli script, written in php, from which these tasks are called. The cli runs every two minutes - first checking if the last call has finished, if not, it exists early. To make sure that this script keeps running, we have made several measures. The way we know if the cli script is already running, is by a pid-file, whose age we monitor every minute - and if it is above a certain threshhold for more than a few samples, we call some error correction actions, and delete the pid file.&lt;/p&gt;

&lt;p&gt;But since this error correction and pid deletion is potentially harmful - for instance it could stop an action from running, but mark is already run, or it could run the same action on the same object twice, both of which could have potential harmful consequences for our customers and suppliers. we have decided to also monitor the health of the cli script in another way.&lt;/p&gt;

&lt;p&gt;The script creates a log file and every (successful) run of the script logs a line with the CLI RUN TIME followed by the run time in seconds as a float. We decided to parse the average runtime of the last hours script runs from the log, and alert the appropriate people, if this value exceeds a certain threshold.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/823650.js&quot;&gt;&lt;/script&gt;


&lt;p&gt;&lt;/p&gt;

&lt;p&gt;We use tail -630 because we have learned that this number of lines roughly equals to 30 runs which (when running normally) would give is the last hour.&lt;/p&gt;

&lt;p&gt;The grep part filters the file, so that we only get lines containing &quot;CLI RUN TIME&quot;.&lt;/p&gt;

&lt;p&gt;The sed part filters out all parts that are not a valid float value leaving only the seconds part.&lt;/p&gt;

&lt;p&gt;The awk part - which I pretty much just &lt;a target=&quot;_blank&quot; href=&quot;http://snippets.aktagon.com/snippets/15-Log-file-analysis-with-AWK-Calculating-the-sum-and-average&quot; title=&quot;Log file analysis with AWK&quot;&gt;stole from here&lt;/a&gt; -  sums all the values, and prints out the average value (the sum divided by the number of samples).&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Roman Numerals in JavaScript</title>
   <link href="http://tom.preston-werner.com/roman-numerals-in-javascript.html"/>
   <updated>2011-02-09T21:30:49-08:00</updated>
   <id>http://tom.preston-werner.com/roman-numerals-in-javascript</id>
   <content type="html">&lt;p&gt;I know, I know. I have written enough about Roman Numerals.&lt;/p&gt;

&lt;p&gt;I have created a version of the &lt;a href=&quot;http://www.clauswitt.com/roman-numerals-a-code-kata/&quot;&gt;Roman Numerals code kata&lt;/a&gt; I did in php and &lt;a href=&quot;http://www.clauswitt.com/how-i-learned-python-in-an-hour/&quot;&gt;python&lt;/a&gt; in JavaScript. It has two public methods getRomanNumeral(number) which gets a valid roman numeral from a number and getNumber(romanNumeral) which will convert a valid roman numeral to its numeric value.&lt;/p&gt;

&lt;p&gt;I will not go into details of the implementation, since it is mostly the same as the php and python version. But I have created a small demo which is located at &lt;a href=&quot;http://clauswitt.github.com/testRomanNumerals.html&quot;&gt;http://clauswitt.github.com/testRomanNumerals.html&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You can get the source of the implementation at &lt;a href=&quot;https://github.com/clauswitt/Roman-Numerals-Kata/tree/master/javascript&quot;&gt;Github&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>How I Learned Python in an Hour</title>
   <link href="http://tom.preston-werner.com/how-i-learned-python-in-an-hour.html"/>
   <updated>2011-02-08T01:24:41-08:00</updated>
   <id>http://tom.preston-werner.com/how-i-learned-python-in-an-hour</id>
   <content type="html">&lt;p&gt;A while back I wrote about a code kata that I wrote, &lt;a href=&quot;http://www.clauswitt.com/roman-numerals-a-code-kata/&quot;&gt;Roman Numerals in PHP&lt;/a&gt;. I decided that I would try to re-implement the same algorithm in different programming languages - both some I knew, and some i did not. My first choice was python. &lt;/p&gt;




&lt;p&gt;I have written a bit of python earlier, but have never used it for anything important. I pretty much started from scratch. &lt;/p&gt;


&lt;p&gt;&lt;strong&gt;Python is very sensitive to indentation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;One of my biggest frustrations - until I found a tip that I needed to set soft-tabs as default for my python source files in Textmate. Go to Bundles-&gt;Bundle Editor-&gt;Show Bundle Editor. Find Python, and then Miscellaneous. Enter the following: &lt;/p&gt;


&lt;script src=&quot;https://gist.github.com/816152.js&quot;&gt; &lt;/script&gt;


&lt;p&gt;All python files should now use softwrap, instead of tabs. That worked for me. &lt;/p&gt;


&lt;p&gt;&lt;strong&gt;All class methods must have a self parameter&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;At first I thought the python interpreter was taunting me. When calling a method it constantly told me that I send exactly one more parameter to the method than were expected. In other languages the reference to the current object are sent transparently. In Python it is also sent transparently, but you need to define it in the method signature in your class. &lt;/p&gt;


&lt;p&gt;&lt;strong&gt;Files and classes does not match&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;My first instinct was to include my python class as a file and then instantiate it with &lt;/p&gt;


&lt;script src=&quot;https://gist.github.com/816159.js&quot;&gt; &lt;/script&gt;


&lt;p&gt;however this is wrong. The python file in which the class is placed is called a module in python, which is what I imported. This imported module can then contain functions and/or classes. From php I am used to - since I use auto loading extensively - that classnames and filenames match. This is not necessarily the case here. The correct way to instantiate the class is thus (the first three lines for a cli program to test the RomanNumerals class): &lt;/p&gt;


&lt;script src=&quot;https://gist.github.com/816161.js&quot;&gt; &lt;/script&gt;


&lt;p&gt;&lt;strong&gt;Class variables are not accessed with this or self&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;One thing I struggled with for a long time, was how to access my &quot;constants&quot;. Since python does not support class constants, I used variables instead. And they were rather hard to reference. The correct way seems to be self.__class__.VARNAME &lt;/p&gt;




&lt;p&gt;Take a look at the source for the &lt;a href=&quot;https://github.com/clauswitt/Roman-Numerals-Kata/tree/master/python&quot;&gt;python implementation of Roman Numerals on github&lt;/a&gt;. &lt;/p&gt;



</content>
 </entry>
 
 <entry>
   <title>A phing plugin for Oh My Zsh</title>
   <link href="http://tom.preston-werner.com/a-phing-plugin-for-oh-my-zsh.html"/>
   <updated>2010-11-25T12:29:20-08:00</updated>
   <id>http://tom.preston-werner.com/a-phing-plugin-for-oh-my-zsh</id>
   <content type="html">&lt;p&gt;A long time ago (a year and a half, I just checked) my good friend &lt;a href=&quot;http://www.jesperrasmussen.com/&quot;&gt;Jesper Rasmussen&lt;/a&gt; &lt;a href=&quot;http://jesperrasmussen.com/switching-bash-with-zsh&quot;&gt;blogged about zsh&lt;/a&gt;, a shell that he had been ranting about for at least a year. About a year later I shifted to it as well, and soon thereafter I began using the setup from &lt;a href=&quot;https://github.com/robbyrussell/oh-my-zsh&quot;&gt;Oh My Zsh&lt;/a&gt;. It has some great plugins, of which I use the git support every day.&lt;/p&gt;

&lt;p&gt;A couple of weeks ago I decided to create a &lt;a href=&quot;https://github.com/clauswitt/oh-my-zsh/blob/master/plugins/phing/phing.plugin.zsh&quot;&gt;plugin of my own&lt;/a&gt;, since there was no autocomplete support for &lt;a href=&quot;http://phing.info/trac/&quot;&gt;phing&lt;/a&gt;, which I also use every day. (And I love saving a couple of keystrokes whenever I build or deploy).&lt;/p&gt;

&lt;p&gt;Creating these auto complete plugins for oh my zsh is rather simple. In my case I have assumed that the build file is allways called build.xml. This way I can get all the targets of the default file with just the command &quot;phing -l&quot;. The output of this command is then saved in a temp-file which is used to autocomplete the phing command.&lt;/p&gt;

&lt;p&gt;The only problem with this approach is that the &quot;phing -l&quot; command lists some data we cannot use. This is filtered out with grep, before the output is sent to the file. The complete command is:&lt;/p&gt;

&lt;pre&gt;phing -l |grep -v &quot;:&quot; |grep -v &quot;^$&quot;|grep -v &quot;\-&quot; &gt; .phing_targets&lt;/pre&gt;


&lt;p&gt;Please contact me if you have feedback, bugfixes, ideas, feature requests or anything else.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Roman Numerals, a Code Kata</title>
   <link href="http://tom.preston-werner.com/roman-numerals-a-code-kata.html"/>
   <updated>2010-09-20T02:33:57-07:00</updated>
   <id>http://tom.preston-werner.com/roman-numerals-a-code-kata</id>
   <content type="html">&lt;p&gt;Doing code katas is something I have done for about a year now. Deliberately practicing my skills as a programmer is something that I find very pleasing, and I constantly see an improvement in the way I solve problems.&lt;/p&gt;

&lt;p&gt;Initially I did these katas in the privacy of my own home, and never published anything about it. A couple of months however I published a couple of my Project Euler solutions in Javascript. And I got some great feedback from others, which helped me become a better programmer of js. Thus I decided to try to post katas every couple of weeks - but alas, the holiday disrupted my usual habit of doing these exercises. (Apparently summer holidays do that - the amount of physical exercise I get, has also plummeted).&lt;/p&gt;

&lt;p&gt;Well, back to the Kata at hand.&lt;/p&gt;

&lt;p&gt;The idea to this Kata came from the Coding Dojos &lt;a href=&quot;http://www.codingdojo.org/cgi-bin/wiki.pl?KataRomanNumerals&quot;&gt;page about Roman Numerals Kata&lt;/a&gt;. I have created a simple solution for converting numbers to roman numerals, and back again.&lt;/p&gt;

&lt;p&gt;I started by writing the tests. So I created a PhpUnit Testcase with the following methods:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/587647.js&quot;&gt; &lt;/script&gt;


&lt;p&gt;A method to check that the roman numerals I get from my new (unimplemented) class would in fact match values I knew were correct. (Some simple values and the year I was born).&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/587650.js&quot;&gt; &lt;/script&gt;


&lt;p&gt;A method to check that the values of roman numerals I knew would also match what I knew they should.&lt;/p&gt;

&lt;p&gt;From there on it was just happy hacking. The first implementation was quite crude, but passed the tests. (&lt;a href=&quot;http://github.com/wittnezz/Roman-Numerals-Php-Kata/commit/f752d3a49c83a47a7d8592b3f1de2bce82eb6f35&quot;&gt;Take a look at the history at github&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;From then it was a mather of adding a few tests for Exception handling, and refactoring to a more elegant solution. No doubt it could have been done even better - but this works, and was done in a mather of an hour or so.&lt;/p&gt;

&lt;p&gt;Take a look at &lt;a href=&quot;http://github.com/wittnezz/Roman-Numerals-Php-Kata&quot;&gt;the source at git hub&lt;/a&gt;. And please leave a comment about how you would go about solving the problem.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Project Euler Problem Five</title>
   <link href="http://tom.preston-werner.com/project-euler-problem-five.html"/>
   <updated>2010-06-06T22:30:37-07:00</updated>
   <id>http://tom.preston-werner.com/project-euler-problem-five</id>
   <content type="html">&lt;p&gt;Continuing our combined &lt;a href=&quot;http://nodejs.org&quot;&gt;node.js&lt;/a&gt; and &lt;a href=&quot;http://www.clauswitt.com/tag/project-euler/&quot;&gt;project euler posts&lt;/a&gt;. We got to &lt;a href=&quot;http://projecteuler.net/index.php?section=problems&amp;id=5&quot;&gt;problem five&lt;/a&gt;, which asks us &quot;What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?&quot;.&lt;/p&gt;

&lt;p&gt;First of all we only check numbers below or equal to 20! because we know for sure that this number will satisfy the criterium of the question, we just don't know if there is a result below this number, we also know that the number must be at least 20. For this reason we check all numbers between 20 and 20! if all numbers between 1 and 20 is a divisor - if one of the numbers fail, we move on to the next candidate. As soon as a candidate proves to be correct, we exit and return the result.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/420267.js&quot;&gt; &lt;/script&gt;



</content>
 </entry>
 
 <entry>
   <title>Project Euler Problem Four</title>
   <link href="http://tom.preston-werner.com/project-euler-problem-four.html"/>
   <updated>2010-06-03T22:00:34-07:00</updated>
   <id>http://tom.preston-werner.com/project-euler-problem-four</id>
   <content type="html">&lt;p&gt;Find the largest palindrome made from the product of two 3-digit numbers. This is &lt;a href=&quot;http://projecteuler.net/index.php?section=problems&amp;id=4&quot;&gt;problem four of project euler&lt;/a&gt;, and this one actually caused me some problems. The reason was that I wrongly assumed that a number with the largest divisor would also would be the largest number. But of course this is not necessarily the case.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/418472.js&quot;&gt; &lt;/script&gt;


&lt;p&gt;As the &lt;a href=&quot;http://www.clauswitt.com/project-euler-problem-one/&quot;&gt;previous&lt;/a&gt; &lt;a href=&quot;http://www.clauswitt.com/project-euler-problem-two/&quot;&gt;three&lt;/a&gt; &lt;a href=&quot;http://www.clauswitt.com/project-euler-problem-three/&quot;&gt;problems&lt;/a&gt; I have posted about, this is written to run on &lt;a href=&quot;http://nodejs.org&quot;&gt;node.js&lt;/a&gt;.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Project Euler Problem Three</title>
   <link href="http://tom.preston-werner.com/project-euler-problem-three.html"/>
   <updated>2010-05-31T22:00:54-07:00</updated>
   <id>http://tom.preston-werner.com/project-euler-problem-three</id>
   <content type="html">&lt;p&gt;Continuing my posts about &lt;a href=&quot;http://projecteuler.net/index.php?section=problems&quot;&gt;Project Euler Problems&lt;/a&gt;, this time with &lt;a href=&quot;http://projecteuler.net/index.php?section=problems&amp;id=3&quot;&gt;problem three&lt;/a&gt;, which asks us to find the &quot;the largest prime factor of the number 600851475143&quot;.&lt;/p&gt;

&lt;p&gt;This time I have decided to explain the code a bit more, through comments - since this problem is a bit more tricky than the previous two problems.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/418464.js&quot;&gt;&lt;/script&gt;


&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Once again, you will need node.js to run the code.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Project Euler Problem Two</title>
   <link href="http://tom.preston-werner.com/project-euler-problem-two.html"/>
   <updated>2010-05-30T01:00:51-07:00</updated>
   <id>http://tom.preston-werner.com/project-euler-problem-two</id>
   <content type="html">&lt;p&gt;Yesterday I posted &lt;a href=&quot;http://www.clauswitt.com/project-euler-problem-one/&quot;&gt;my first euler-code&lt;/a&gt;. This time we will take a look at &lt;a href=&quot;http://projecteuler.net/index.php?section=problems&amp;amp;id=2&quot;&gt;problem two&lt;/a&gt;, again with the help of some javascript running on node.js.&lt;/p&gt;

&lt;p&gt;The problem is &quot;Find the sum of all the even-valued terms in the [fibonacci] sequence which do not exceed four million.&quot;&lt;/p&gt;

&lt;p&gt;My take on the solution was to run through all fibonacci numbers below 4.000.000 and adding them to the total, if they were equal. The next fibonacci number is calculated by always saving the last two numbers found.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/418453.js&quot;&gt; &lt;/script&gt;


&lt;p&gt;You will need node.js to run this code.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Project Euler Problem One</title>
   <link href="http://tom.preston-werner.com/project-euler-problem-one.html"/>
   <updated>2010-05-29T10:55:17-07:00</updated>
   <id>http://tom.preston-werner.com/project-euler-problem-one</id>
   <content type="html">&lt;p&gt;A long time ago I decided to do a couple of posts about &lt;a href=&quot;http://projecteuler.net/&quot;&gt;project euler&lt;/a&gt;. I find &lt;a href=&quot;http://projecteuler.net/index.php?section=problems&quot;&gt;the problems&lt;/a&gt; to be great excersize. It was not before I began to look into &lt;a href=&quot;http://nodejs.org/&quot;&gt;node.js&lt;/a&gt; though, that I began writing code for the problems for the purpose of publishing it.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://projecteuler.net/index.php?section=problems&amp;id=1&quot;&gt;The first problem&lt;/a&gt; on the list is very simple. &quot;Find the sum of all the multiples of 3 or 5 below 1000&quot;. The solution, then, must be to find all numbers that are multiples of 3 and 5, and then adding those...&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/418446.js&quot;&gt; &lt;/script&gt;


&lt;p&gt;This solution is pretty simple. You (probably) need nodejs to run the code.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Using Quicksilver to edit hosts file</title>
   <link href="http://tom.preston-werner.com/using-quicksilver-to-edit-hosts-file.html"/>
   <updated>2009-11-04T06:38:41-08:00</updated>
   <id>http://tom.preston-werner.com/using-quicksilver-to-edit-hosts-file</id>
   <content type="html">&lt;p&gt;This is mostly just a follow up for my previous post on &lt;a href=&quot;http://www.clauswitt.com/2009/11/04/319/&quot;&gt;using a shell script to add and remove test domains in the hosts file&lt;/a&gt;. I have created two small applescripts for use in Quicksilver to execute that script. If you need to use these you place them in ~/Library/Application Support/Quicksilver/Actions and set your password. (Beware this could pose a threat, since you will have your password in cleartext on your harddrive).&lt;/p&gt;

&lt;p&gt;The two scripts are almost identical. Actually the only difference is the action the shell script should call (add or remove). The rest of the scripts are completely the same.&lt;/p&gt;

&lt;p&gt;I call this file addhost.&lt;/p&gt;

&lt;pre name=&quot;code&quot; class=&quot;applescript&quot;&gt;
using terms from application &quot;Quicksilver&quot;
    on process text txt
        set the_password to &quot;YOURPASSWORD&quot;
        repeat with delimiter_position from 1 to (length of txt)
            if character delimiter_position of txt = &quot; &quot; then exit repeat
        end repeat
        if delimiter_position = (length of txt) then
            set hostname to txt as string
            set ipaddress to &quot;&quot;
        else
            set hostname to characters 1 thru (delimiter_position - 1) of txt as string
            set ipaddress to characters (delimiter_position + 1) thru (length of txt) of txt as string
            
        end if
        
        do shell script &quot;sudo hosts.sh add &quot; &amp; hostname &amp; &quot; &quot; &amp; ipaddress password the_password with administrator privileges
        
        return nothing
    end process text
end using terms from
&lt;/pre&gt;


&lt;p&gt;I call this file removehost.&lt;/p&gt;

&lt;pre name=&quot;code&quot; class=&quot;applescript&quot;&gt;
using terms from application &quot;Quicksilver&quot;
    on process text txt
        set the_password to &quot;YOURPASSWORD&quot;
        repeat with delimiter_position from 1 to (length of txt)
            if character delimiter_position of txt = &quot; &quot; then exit repeat
        end repeat
        if delimiter_position = (length of txt) then
            set hostname to txt as string
            set ipaddress to &quot;&quot;
        else
            set hostname to characters 1 thru (delimiter_position - 1) of txt as string
            set ipaddress to characters (delimiter_position + 1) thru (length of txt) of txt as string
            
        end if
        
        do shell script &quot;sudo hosts.sh remove &quot; &amp; hostname &amp; &quot; &quot; &amp; ipaddress password the_password with administrator privileges
        
        return nothing
    end process text
end using terms from
&lt;/pre&gt;


&lt;p&gt;Now I am able to create a local domain by opening Quicksilver (double-control in my case) press period, write the domain name, press tab, and select the addhost action (ad is enough in my case). Removing is just as easy - the last step is just to select the removehost action (re is enough in my case).&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Bash script for editing the hosts file</title>
   <link href="http://tom.preston-werner.com/319.html"/>
   <updated>2009-11-04T01:02:46-08:00</updated>
   <id>http://tom.preston-werner.com/319</id>
   <content type="html">&lt;p&gt;I just read a &lt;a href=&quot;http://jesperrasmussen.com/coding-on-the-go-setting-up-local-apache&quot;&gt;post about local development in apache&lt;/a&gt; by &lt;a href=&quot;http://jesperrasmussen.com/&quot;&gt;Jesper Rasmussen&lt;/a&gt; and thought one thing was missing. I usually test my sites and applications locally with the real domain - to ensure that functionality based on the url works as expected. However this means editing the hosts file several times a day. Now I have made a small shellscript that will add and remove lines from the hosts file.&lt;/p&gt;

&lt;pre name=&quot;code&quot; class=&quot;bash&quot;&gt;
#! /bin/bash
DEFAULT_IP=127.0.0.1
IP=${3:-$DEFAULT_IP}

case &quot;$1&quot; in
  add)
        echo &quot;$IP $2&quot;  &gt;&gt; /etc/hosts
        ;;
  remove)
        sed -ie &quot;\|^$IP $2\$|d&quot; /etc/hosts
        ;;

  *)
        echo &quot;Usage: &quot;
        echo &quot;hosts.sh [add|remove] [hostname] [ip]&quot;
        echo 
        echo &quot;Ip defaults to 127.0.0.1&quot;
        echo &quot;Examples:&quot;
        echo &quot;hosts.sh add testing.com&quot;
        echo &quot;hosts.sh remove testing.com 192.168.1.1&quot;
        exit 1
        ;;
esac

exit 0
&lt;/pre&gt;


&lt;p&gt;As you can see the script defaults the ip to 127.0.0.1 for easy creation of local domains. Next step is to create a quicksilver (and a gnome do) plugin for easy creation without ever touching the terminal. (Even though we all love the terminal, right?)&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Converting Filenames to Lowercase with Bash</title>
   <link href="http://tom.preston-werner.com/converting-filenames-to-lowercase-with-bash.html"/>
   <updated>2009-06-02T23:24:36-07:00</updated>
   <id>http://tom.preston-werner.com/converting-filenames-to-lowercase-with-bash</id>
   <content type="html">&lt;p&gt;Once again I have a bash-oneliner. For some reason it is all I blog about at the moment.&lt;/p&gt;

&lt;p&gt;I had an import script for importing demo files (in mp3 format) to a database used at &lt;a href=&quot;http://www.voicearchive.com/&quot;&gt;Voicearchive&lt;/a&gt;. Only problem the import script depended on the files being named precisely according to a specific rule. But for some reason some times it was named with uppercase letters, and sometimes with lowercase letters. I decided to rename all files to lowercase, and change the importscript accordingly.&lt;/p&gt;

&lt;pre name=&quot;code&quot; class=&quot;bash&quot;&gt;
find . -maxdepth 1 -type f -execdir rename 'y/A-Z/a-z/' '{}' \;
&lt;/pre&gt;



</content>
 </entry>
 
 <entry>
   <title>Bash Oneliner for Counting Files</title>
   <link href="http://tom.preston-werner.com/bash-oneliner-for-counting-files.html"/>
   <updated>2009-05-14T01:56:51-07:00</updated>
   <id>http://tom.preston-werner.com/bash-oneliner-for-counting-files</id>
   <content type="html">&lt;p&gt;I needed to find a way to count the number of files in a directory and only output that number. Bash is still my best friend for this sort of thing - especially with a little help from google.&lt;/p&gt;

&lt;pre name=&quot;code&quot; class=&quot;bash&quot;&gt;
ls -1 | wc -l
&lt;/pre&gt;


&lt;p&gt;by the way - the answer was 2062 files.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Renaming file extensions with bash script</title>
   <link href="http://tom.preston-werner.com/renaming-file-extensions-with-bash-script.html"/>
   <updated>2009-04-29T22:58:04-07:00</updated>
   <id>http://tom.preston-werner.com/renaming-file-extensions-with-bash-script</id>
   <content type="html">&lt;p&gt;A couple of weeks ago I had to rename the file extension of several view scripts from html (from my own mvc framework) to phtml (zend framework). I found, as always, bash to be my friend.&lt;/p&gt;

&lt;pre name=&quot;code&quot; class=&quot;bash&quot;&gt;
for i in *
do
  ( file $i | grep html ) &amp;&amp; mv $i $(echo $i | sed s/html$/phtml/)
done
&lt;/pre&gt;

</content>
 </entry>
 
 <entry>
   <title>OpenID and OAuth for SSO</title>
   <link href="http://tom.preston-werner.com/openid-and-oauth-for-sso.html"/>
   <updated>2009-03-24T02:07:32-07:00</updated>
   <id>http://tom.preston-werner.com/openid-and-oauth-for-sso</id>
   <content type="html">&lt;p&gt;In my previous post about &lt;a href=&quot;http://www.clauswitt.com/2009/03/09/sso-for-webapplications/&quot;&gt;SSO for webapplications&lt;/a&gt; I have missed the obvious - why not use OpenId and OAuth.&lt;/p&gt;

&lt;p&gt;It is important however to know the difference between the two. OpenId is used for authentication - is the user who he says he is - and OAuth is used for authorization is this specific user allowed to do this specific action. Well, in the case of OAuth i guess it is more, is this application allowed to do this action on behalf of this user. A use case described in the post &lt;a href=&quot;http://portalzone.blogspot.com/2007/12/openid-oauth-complimentary-or-competing.html&quot;&gt;OpenID  &amp;amp; OAuth &amp;#8211; complimentary or competing?&lt;/a&gt; is:&lt;/p&gt;

&lt;blockquote cite=&quot;http://portalzone.blogspot.com/2007/12/openid-oauth-complimentary-or-competing.html&quot;&gt;
Let’s say you are registering as a delegate on a conference website. With OAuth it is possible for the conference website to automatically add the event to your google calendar or yahoo calendar with your consent (assuming google and yahoo support OAuth). How does it work ? Well, once you decide to let the conference website add an event to your google calendar, you get redirected to google. On google , you explicitly authorize the conference website to modify your calendar. After this authorization, the conference website will have permission to modify your calendar data.
&lt;/blockquote&gt;


&lt;p&gt;The problem with OAuth is that you have to login to every site that you wish to give an application access to. For this reason &lt;a href=&quot;http://googledataapis.blogspot.com/2009/01/bringing-openid-and-oauth-together.html&quot;&gt;Google has published a hybrid protocol&lt;/a&gt;, and tries to make this a new standard. They &lt;a href=&quot;http://googlecodesamples.com/hybrid/&quot;&gt;created a demo&lt;/a&gt;, and &lt;a href=&quot;http://code.google.com/p/gdata-samples/source/browse/#svn/trunk/hybrid&quot;&gt;released the source for that&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This really looks like a technique that could be used for my applications. I am looking into this, and hopefully will have a quick tutorial on the subject in the near future.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Quick Textmate Productivity Tip</title>
   <link href="http://tom.preston-werner.com/quick-textmate-productivity-tip.html"/>
   <updated>2009-03-03T02:15:53-08:00</updated>
   <id>http://tom.preston-werner.com/quick-textmate-productivity-tip</id>
   <content type="html">&lt;p&gt;Today I found myself needing a bit of Textmate magic. I had a sql-file containing a lot of tables, from some of these I needed all fieldnames in an array. I quickly found that Textmate would be my friend. This is mostly a reminder to myself. (I tend to forget these things somehow).&lt;/p&gt;

&lt;p&gt;I Pasted all the lines with fieldnames from the sql-file to a new text-document in Textmate. Then i pressed option while using the mouse to mark the beginning of each line and then inserting a quote. (Resulting in a quote on the beginning of each line.) I did the same on the other side of the textfields and added a comma as well. The only problem now is that because not all fieldnames are the same length I now have some unwanted whitespace between the end of the fieldname and the closing quote. Enter regexp, and probably the simplest one I ever wrote.&lt;/p&gt;

&lt;pre name=&quot;code&quot; class=&quot;regexp&quot;&gt;
[ ]
&lt;/pre&gt;


&lt;p&gt;It simply matches all whitespace in the file. Thus making it easy to use find/replace, and replace the whitespace with nothing. Downside is, all the time I saved using this technique has been used to write this blog post. :-)&lt;/p&gt;
</content>
 </entry>
 
 
</feed>
