Posts Tagged ‘Javascript’


Sunday, June 24th, 2012

Quite a while since I wrote a post now, I’ve not been sick or anything, but there has been a lot of work abound, and outside work I prioritized sleeping over writing. But now I’m back for the moment, so let’s get down to business :)

Since last time I’ve come up with new ways of abusing awk, such as having it find the highest value from a command outputting in the following syntax:

\t<characters, integers, fullstop>: <integer>\n

To make it a little more different, the command also spits out a header, as well as an additional newline after the end of output.

I just now, while writing this, came up with a different solution, which doesn’t use awk:

theCommand | grep -v '^[^ \t]\+' | tr -d ' ' | cut -d':' -f2 | sort -r | head -n 1

but what I ended up using was:

theCommand | awk 'BEGIN { highest = 0 } $0 ~ /^[ \t]/ { if ( $2 > highest ) { highest = $2 } } END { print highest }'

In this case, from what I can gather, awk is the more efficient solution. One process versus five.

Update: As Werner points out, the if statement isn’t really necessary (which also makes it possible to cut out the BEGIN statement as well):

theCommand | awk '/^[^ \t]/ && $2 > highest { highest = $2 } END { printf "%d\n", highest }'


  • ditaa (a.k.a DIagrams Through Ascii Art) makes it easy to generate nice-looking diagram images from… rather nice-looking ASCII diagrams
  • docopt, a command-line interface description language, which also seems to support generating the parser for the CLI being described
  • Peity for generating different types of charts using jQuery and <canvas>
  • interacting with web pages, programmatically

As of late I have been thinking a great deal about backups and the project which seems the most interesting to me is Duplicity.

Random tech stuff

Other random not-so-techy stuff

What I pass for humour



Sunday, August 21st, 2011


The adventures in LaTeX-land continues. Rikard didn’t want any page number displayed on the table of contents page, and after having tried a couple of different variants of \pagestyle{empty} and the likes we realized that for some reason that won’t work at all in the book-class. \pagenumbering{gobble}, however, seems to work in every class, but NOT inside the actual document (so I guess this means after having issued the first \chapter or \section command).

For the interested, I found the answer by searching and finding this post, which in turn lead me here.

RSS for logging purposes

I am toying with the idea of writing a small daemon which would create an RSS feed (or Atom or whatever is popular today, I don’t care) which I could then plug into feed2imap which I have on the server.

The idea then would be that I could write small monitoring scripts for whatever I wanted, check the temperature, check space on the disks, whatever, and have fcron execute these scripts every now and then, and the result of these scripts would be fed into the RSS daemon.

I haven’t thought this through yet at any rate, but I quite like the idea. We’ll see what comes of it :)

Revisiting my old friend Django

Grégoire (or as we like to call him, greg) began working on a Django implementation of the myConf concept, and I am helping out as best I can with it.

There were some template bugs which made the template overly complex which I am currently trying to iron out, and mostly the problem seems to boil down to that the input data to the template is stored in a way which makes access in the template harder than necessary.

So I’m attempting splitting the data up further, and using dictionaries as the overall structure, which of course meant that I needed to find how to iterate over a dictionary in Django.

I admit that it was quite some time since I read the Django docs, and had I done so I would eventually have found the solution, but google, as usual, beat me to finding the solution somewhere else.

The relevant parts are:

{% for key, value in dictionary.items %}{{ value }}{% endfor %}


A non-intrusive (but javascript-required) approach to comment spam filtering. It’s probably a good solution, but I don’t like forcing users to activate javascript in their browser.

This is just plain frakking disgusting.

Interesting, and well-documented, approach to combating email spam.


Sunday, June 19th, 2011


I have begun to use {c,d}f<character> to change or delete from the cursor up until (including) <character>.

It has made me a little bit faster in some few edge cases of text-editing, but today (Monday) I found myself in need of doing a couple of manipulations on text up until (NOT including) <character>. helped me find {c,d}t<character>.

Very nice :)

MediaWiki sortable tables

In preparation for FSCONS one of my duties as team leader for the Hardware team is to keep track of all the hardware available to FSCONS (i.e. hardware owned by FFKP) and Jonas felt it would be a good idea to have that list available on a wiki.

When I was done adding the laptops and cameras it kindof bugged me that I’d added the laptops in the “wrong” order (i.e. the serial number column wasn’t ordered).

Luckily this was an easy fix, as one can add class="wikitable sortable" to the table header in the wiki-syntax and it will automagically add the necessary javascript and buttons to make each column sortable.


The “Bump” Challenge aimed at creating a simple(r) way of exchanging public keys and establishing trust, possibly (probably?) using smartphones which are able to sync with the FreedomBox, seems like a rather nice idea.

Personal Wikis

A wiki can be a great tool, and for a while I was maintaining a personal mediawiki installation, just adding stuff I needed from time to time, but never often enough that I could learn it.

Then I stumbled over Zim, which became collateral damage as I got into my “replace as much as possible with a command-line alternative”-phase (this phase hasn’t abated yet ;)) which left me migrating to VimWiki.

I am not likely to replace VimWiki, it works well for me, but sometimes it might not be the right tool for the job. Which is why I am always on the lookout for new stuff.

I already knew about ikiWiki, but at the time I discovered it I didn’t have the time to look further into it. I guess I should change that.

And today I found TiddlyWiki, a wiki self-contained within an html file. Which people have extended for other uses.

SQLite Triggers

SQLite has support for triggers, how cool is that?! And these triggers can be triggered by other triggers! :D


Vim Casts is a Vim screencast resource for learning / improving your knowledge in Vim (thank you for the tip hook).

Tahoe-LAFS (Least Authority File System) is a decentralized fault-tolerant peer-to-peer file system. I can’t really speak about its security, but it looks pretty good, at least on paper.

ZRTP seems to be a pretty cool VoIP encryption protocol, and there seems to be an implementation for Android devices as well.

“Towards a Lifelong Content Management System” is a rather nice, well thought-through blog post on how we might want to change the way we think about content management systems (thanks @mlinksva).



Sunday, May 8th, 2011

META section
I thought I’d try something new, like batching up things I discover along the week, and append it to a post to be published at the end of the week.

I am pretty certain that it will prove to be more diverse than previous posts, and with summarized points, it might actually be shorter than my “regular” posts.

If you like my longer, but more irregularly scheduled posts, fear not, those will continue, with about the same irregularity as usual ;P

Content section


Modernizr is a javascript library designed to detect what html5 capabilities a visiting browser has. This enables a kind of “progressive enhancement” which I find very appealing.

Using this one could first design a site which works with most browsers (I consider MSIE6.0 a lost cause) and then extend the capabilities of the site for those browsers that can handle it.


timetrack and timesummer

I recently started working on a small project aimed to help me keep track of the hours I put into various (other) projects, and the result is two scripts, timetrack and timesummer (I am desperately trying to find a better name for the last one, suggestions welcome). I promise to have it in a public repository soonish. timetrack can now be found at bitbucket

timetrack stores current date and time in a “timetrack file” whenever it is called, and at the same time determines if the current invocation will close an ongoing session, or start a new one.

If it is determined that the script is closing the session, it will also ask that I briefly describe what I have been working on.  The script then calculates how long the session was and writes this to the file as well along with the brief session summary.

timesummer simply reads the same timetrack file, and sums up the hours from all the sessions, and prints it to STDOUT.

It is multi-user capable-ish, since each file is created and stored in the format “.timetrack.$USER”. All in all it serves me pretty well.

Another project of mine is, a script created to live in /etc/wicd/scripts/postconnect/ and copy /etc/hosts-home or /etc/hosts-not-home into /etc/hosts depending on my location (inside or outside of my home network).

Why I do this is a long-ish kind of story, but if you have ever cloned a mercurial repository from inside a private network and then tried to access it from outside the network, you should be able to figure it out.

The script stopped working. That’s twice now this has happened, but sufficiently far apart that I couldn’t remember why it happened without investigating it.

It all boiled down to me using GET (found in perl-libwww package) to fetch my external IP-address so that I could determine if I am inside my network, or outside it.

GET (and POST and HEAD) doesn’t live in /usr/bin or /usr/local/bin or some place nice like that. No, GET lives in /usr/bin/vendor_perl (or at least it does now, Before a system upgrade it lived somewhere else…

I don’t know why someone (package maintainer, the perl community, whoever…) felt it was necessary to move it (twice now), but I guess they had their reasons, and since I used absolute paths in so that I wouldn’t need to worry about what environment variables had been set when the script was executed, renaming the directory GET lived in meant breakage…

This isn’t me passive aggressively blaming anyone, but it did kind of irk me that the same thing has happened twice now.

Plz to be can haz makingz up of mindz nao, plz, kthxbai.

I love GET and HEAD, and will continue using them, manually. For the script, the obvious solution was to switch to something which by default lives in /usr/bin and doesn’t move, something like… curl.



I have found myself working with PHP again. To my great surprise it is also rather pleasant. I have however found myself in need of a templating system, and I am not in control of the server the project is going to be deployed on, and so cannot move outside the document root.

From what I gather, that disqualifies Smarty, which was my first thought. Then I found Savant, and although I am sure that Savant doesn’t sport nearly all the bells and whistles that Smarty does, for the time being, it seems to be just enough for me.

I am going to enjoy taking it for a spin and see how it will fare.



I do not enjoy bashing well-meaning projects, especially not projects I know I could benefit from myself, but after reading the material on the unhosted site, I remain sceptically unconvinced.

The idea is great, have your data encrypted and stored in a trusted silo controlled by you or someone you trust enough to host it, henceforth called “the storage host”.

Then an “application host” provides javascripts which in turn requests access to your data, which you either grant, and then the application code does something for you, and you see that it is good, and all is well, or you don’t grant access and you go on your merry way.

The idea is that since everything is executed on the client side, the user can verify that the code isn’t doing anything naughty with your data. Like storing it unencrypted somewhere else to sell to advertisers or the like.

For me, this premise is sound, because I am a developer, a code monkey. I can (with time) decipher what most javascripts do.

Problem: the majority of people aren’t developers (well that is not a problem, they shouldn’t have to be), but what I’m saying is that of all people only a subset knows that there exist a language called javascript, and it is only a subset of that subset which can actually read javascript (i.e. in perspective VERY FEW).

For me personally, this concept rocks! I could use this and feel confident in it. But requiring the end user to the first, last and only line of defense against malicious application providers… (well, of course, the situation right now is at least as bad) isn’t going to fly.

One could experiment with code-signing, and perhaps a browser add-on, and make a “fool-proof” user interface, hiding away the underlying public key cryptography that would be needed, but somewhere along the line the user would still need to know someone who could read the code, could sign it, and then act as a trusted verifier.

My thoughts on what would be easier to teach the end user; public key cryptography or javascript? Neither… :(



Finally, a random assortment of links I found in various places during the week:

The Bun Protocol
Laptop Bubbles
Hybrid Core, A WordPress Theme Framework
201 ways to arouse your creativity


Revelation of the week: Thanks to the “Laptop Bubbles” post I realized that I now consider bash (shell scripting) my primary language, thus displacing Python to a second place.

Adventures in Javascript land

Tuesday, May 5th, 2009

Last night an old classmate of mine approached me with a programming problem, this time in Javascript. This is not a language I have spent much time with for various reasons and my previous attempts at grokking it have all been unsuccessful.

But times seem to have changed. Either I am a better programmer now, or Javascript has matured (I haven’t been keeping tabs on what is going on in the Javascript camp so I am still a bit fuzzy on the whole DOM thingy and if you can, nowadays, write a Javascript which both Internet Explorer and all the other browsers will understand without jumping through at least a couple of hoops), but in any case I was able to help him.

The problem he was having oriented around string manipulation. Given a pipe-separated string of “key=value” pairs, extract the values and store them in a variable of their own.

I am guessing he was going to extract all the values, but the example code he sent to me just included the extraction of a single value. His attempt was not bad, combining indexOf() and substring(), and I would like to think he had a pretty smart idea going, but Javascript was not intelligent enough for his idea.

The “mistake” he made was thinking that indexOf() would continue and find the next occurrence once it had been called once thus saving its “state” from the previous search, much like how a file/array iterator would behave.

He also had some rather “funny” constraints, or rather the reasoning behind them. There could be no magic numbers in the code (which is of course a good thing in and of its own) because the given string could be subject to change in the future.

I have no objection to this, except for the fact that he used hard-coded strings as argument to indexOf(). But sure, having to update that one place instead of two is better because otherwise you’ll just miss the other place when you update the first. (The magic numbers constraint surfaced when I suggested adding an offset to the index returned by indexOf() so that he would get the appropriate parts of the string.)

The initial code (obscured for his benefit) looked something like this:

var arg = "key1=value1|key2=value2|key3=value3|key4=value4";
var index = arg.indexOf("key2");
var key2 = arg.substring(index, indexOf("|"));

There were some initial mistakes in the code (second indexOf() called on nothing) as well as the assumption described earlier, that indexOf() kept a state. This code, after modification, would of course have tried to return a negative length substring. Now I haven’t checked, but somehow I doubt that Javascript would actually return a reversed substring of the indicated length and position.

My suggestion was on par with:

var arg = "key1=value1|key2=value2|key3=value3|key4=value4";
var index = arg.indexOf("key2") + 4;
var key2 = arg.substring(index, arg.indexOf("|", index));

This brought up the magic numbers constraint, so I then suggested replacing line 2 with:

var index = arg.indexOf("key2") + "key2".length;

But that was not satisfactory either. I believe he ended up with a solution of his own, calculating a second index finding the first “=” after “key2″ (using index as offset) and then from there in the substring incrementing the second index by one:

var arg = "key1=value1|key2=value2|key3=value3|key4=value4";
var index = arg.indexOf("key2");
var index2 = arg.indexOf("=", index);
var key2 = arg.substring(index2 + 1, arg.indexOf("|", index));

He was happy with that solution, but I was less than enthusiastic with any of that code so I started digging on my own. One solution could be regular expressions (I don’t know if it is a good or bad thing that I almost always nowadays think “regexp” when I have to find and/or break out data from a string…) and I found a pretty neat solution due to the fact that the Javascript string type has a built-in function match() which takes a regexp pattern as argument.

var arg = "key1=value1|key2=value2|key3=value3|key4=value4";
var key2_pattern = /key2=(\d+)/;
var key2 = arg.match(key2_pattern)[1];
// [0] == entire matched string, [1] == matched group (i.e. \d+)

Still I was not entirely satisfied, regular expressions can be great, if kept uncomplicated, but really how often do things stay uncomplicated? I was also wondering whether it would be possible to just store it all, dynamically, in a dictionary type of structure. That’s when I realized that Javascript has associative arrays. :D

var arg = "key1=value1|key2=value2|key3=value3|key4=value4";
var tmp_array = arg.split("|"); // array with "key=value" elements
var dictionary = new Array();
while (e = tmp_array.pop())
    tmp = e.split("=");
    dictionary[tmp[0]] = tmp[1]; // dictionary["key"] = "value";

I haven’t fully tried out this last suggestion (it works, but I haven’t yet tried how Javascript deals with “2” as opposed to 2, and how this might screw things up. I am thinking that it probably will screw things up. On the other hand so should substring (since it per definition also returns a string).

None of this really matters, my friend came to a working solution and I got to play with Javascript. Win-win :)