May 2008 Archives

Sat May 31 22:21:17 CDT 2008

Todo.txt part 2: php interface.

Last week I talked about using a text file to manage tasks. To make it as easy as possible for my wife to use it, I modified a php script to use as an interface. Now, instead of opening a terminal we leave the page open in Firefox.

The page uses a form to take input, specialized links to mark items as done, displays my list, and can even send the grocery list to the printer. You can view the complete source code here: todo.php.

The page is a heavily modified version of code posted to the todo.txt Yahoo! group. The original is nice and compact, but lacks several features that I like. The real work of the script is interaction with todo.sh. This is handled by php exec. (See line 110 for an example) Everything else is child's play. The php page takes data from either _POST or _GET, depending on the action.

Most of the data is posted with the form, and usually for the grocery list. I added the default value "add @buy " to speed item entry. In Firefox, the page automatically starts the cursor just after this text so all you need to do is type. Although others tested browsers (Opera, Konqueror, and IE6) all place the cursor at the beginning of the text box, we generally use Firefox so it shouldn't be a problem. If you want to use the script for yourself but don't like the default text, change it on line 77.

Posted by Nesman | Permanent Link

Sat May 24 22:36:50 CDT 2008

Todo.txt - Using a text file to manage tasks.

As long as I can remember, I've used a text file for my todo list. I keep one at home that collects short-term goals like grocery shopping. Sometimes I use it for a scratchpad for URLs and random thoughts. I keep another one at work to remind me of hostnames, ip addresses, names, extensions, and anything else I need for a service call. Since switching to Linux, I've gotten away from having to keep my personal todo.txt on my thumbdrive. In fact, I've gotten away from using a text editor on it almost entirely.

Gina Trapani of Lifehacker maintains a small script to manage your todo list. Todotxt.org has the main bash script (todo.sh) along with several tools to integrate with it, including progress trackers, tab completion, and bots for Jabber and AIM. If bash isn't your language of choice, there is also a Python version. You can see a video demo of todo.sh on Youtube.

More than anything, I find myself using my todo.sh to manage a grocery list. Moriah and I leave it open on the computer so that we can add eggs or bread easily when we run low. To keep grocery items separate from "rake leaves" and "build workbench" we mark them with "@buy", which has the bonus of sorting them near the top of the list, all together. Then, we can view the list of groceries with this command:
$ todo.sh list @buy
23 @buy bread
22 @buy contact solution
19 @buy juice
20 @buy mayo


The cut[0] command, built into the GNU coreutils, makes it easy to grab just the important parts of that output.
$ todo.sh list @buy | cut -f3- -d" "
bread
contact solution
juice
mayo


Finally, I can pipe the output into lpr and print it.
$ todo.sh list @buy | cut -f3- -d\ | lpr

Next time, I'll go over the php script that I use as an interface to the shell script.

[0] Here, I use cut with two flags. -f3- means "from third field to end of line" and -d specifies the delimiter. Here I delimit with spaces, which you can quote or escape with a backslash.

Posted by Nesman | Permanent Link

Tue May 13 15:18:39 CDT 2008

Tried to Count in Base3 got 3-Sided Fractal

Recently, I got a wild hair in an uncomfortable place that suggested I look into trinary counting. Binary is great, but it doesn't seen very efficient. Each number holder is seen as either 0% or 100%. It would be nice to use a more precise system. Instead of splitting each digit in half, give 3 possibilities.

To test this, I tried to visualize how I might build a counting machine that worked in binary, but represented a base 3 system. I'm pretty sure that I got it wrong. My plan was to have a line of toggle switches that would flip (or not) based on the value of the previous switch. Each increment would toggle the switch on the right, so the next switch would only be flipped every other time. I wasn't sure, but I figured this might be a good place to start. My experiment in testing this had interesting results.

After working on it for a while, I began to picture it as a bucket brigade. The digit on the far right (Number 0) was the bucket brigadier in charge of the spigot. (We call him Number 0 because he doesn't know how to catch. This is why he's in charge of the spigot.) Each second, Number 0 fills his bucket and throws it to the next person (Number 1). If the next person has an empty bucket, he catches the water and turns to face the next person in line. The next second, everybody with water in their bucket throws some to the next person. They only throw half of the water, to make sure they never run out. (Don't worry about this part too much. Just run with it) They continue doing this until they are hit in the back of the head with water from the previous person, which causes them to turn around and prepare to catch the next bucket.

In this system, Number 0 will flip Number 1 each second. Number 2 (the next person) will catch a bucket of water (either in her own bucket or to the back of her head) every other second. This will cause Number 3 to flip every 3 seconds and so forth. In this way, the first bucket of water takes 1 second per person to make it to the end of the line. Following buckets are more and more likely to end up being wasted, but it makes for an interesting counting experiment.

To help me visualize this, I would need a script to calculate the state of each person in line, each second. The main mechanic is that each digit is flipped only if the previous is in the "on" position (throwing water). Eventually I figured that I could represent each state as 1 or -1. To determine what happens to any particular digit, just multiply it by the one on it's right. Here, we consider 1 to be "off" because multiplying causes no change, while -1 is "on" because it will toggle negative status. Unfortunately, 1 and -1 are not very pretty in large groups; they just look like vertical and horizontal dashes.

Before I could go much further, I had to figure out how to do my calculations with 1 and -1 while displaying something that looked decent. I ended up with two arrays: all of my heavy lifting was done in the first, then the pattern was copied to the second as a prettier version. My first thought was to use an if/then selection to swap the value: if $val == -1 { then $binval = 0 }; That just felt lazy. I never tested it, but I'm sure that's two slow. Finally I found a method to convert either value to 1 or 0 from 1 or -1: $binvar = (($var +1) / 2); This will use -1 to toggle, but display 0 for better contrast.

Finally, it's time to run it and watch my numbers run. An early test looked like this: (each second is displayed below the previous, and counting starts from the right)
	1 1 1 1 1 1 0
	1 1 1 1 1 0 1
	1 1 1 1 0 0 0
	1 1 1 0 1 1 1
	1 1 0 0 1 1 0
	1 0 1 0 1 0 1
	0 0 0 0 0 0 0
	1 1 1 1 1 1 1
Remember that here, 1 is off, 0 is on for the ability to toggle the number next to you. Also, the number on the right is toggled for each new row. This is OK to do manually for small numbers, but to really test the concept, I wanted to run it with hundreds of columns and rows at once. Since 1's and 0's take up more room than needed, I loaded the result into opera and zoomed out. Here is a screen shot of my result:
Sierpinski Triangle in ones and zeros

My result was a kind of tilted Sierpinski Triangle. Although I completely failed to count in base 3, I succeeded in building a 3-sided fractal.

The final version of my code can be found here: negcount.php You should be able to run this on any web server with php4 if you would like to play with it.

Update:After a little research, I see others with code for the same thing. Jason Underdown wrote Sierpinski's Triangle in C which is 3 times as long as mine, if you count comments and twice as long without comments. (Found via Wikipedia article for Sierpinski Triangle).

Before I brag too much, I should point out this example (done in Ruby) of a one-liner Sierpinski's Triangle:
ruby -le '64.times{|y|print" "*(63-y),(0..y).map{|x|~y&x>0?" .":" A"}}'

Posted by Nesman | Permanent Link