Christian Mayer's Weblog

Retimr

After I saw the simplicity of moro I curtly decided to rewrite Timr. It is time to release a useful Timr version. It has not reached v1.0.0 anyway.

The first attempt to build Timr was using TermKit and Curses. Read more in the first blog post about Timr.

So I rebuild Timr. It does not use TermKit anymore. Means, there is no interactive User Interface. You run a single command and after that it terminates. Like you execute /bin/date.

Command-line Arguments

To handle command-line arguments I most use OptionParser. You can register arguments you want to accept by your script and OptionParser handles everything for you. It even can print you a help page. I’m not using OptionParser for Timr. Timr has sub-commands. The structure is like on Git. For example:

timr [<common_options>] <command> [<command_options>] [<sub_commands...>] [<sub_options>]

OptionParser is too simple for this purpose. It does not support subcommands. I even tried to build an own arguments parser. Named SimpleOptParser. But it does not went so simple in the end. So I’m not using SimpleOptParser. Parsing arguments should be easy. However. I ended up having a parsing loop for each command. Some commands don’t have subcommands rather than dynamic arguments. For example, look at Git Log:

$ git log -h
usage: git log [<options>] [<revision-range>] [[--] <path>...]

Most Git Commands have this double-dash -- argument to separate options from files. The point is it’s optional. Git needs to understand what you want even if you don’t use --. It even recognizes the <options> at the end of the line instead on the 3rd position. Assumed when not using --.

In Timrs case not all commands have subcommands. Putting this in one big formular is difficult. You need to differ between subcommands and dynamic arguments. I did not want to spend too much time on this single feature. This is maybe an idea for a separate project. I continued building relevant Timr features.

Terminal Tables

Using the Terminal-Table Gem is an easy way to present a table on the terminal. You need to handle the column size of the terminal by yourself. Also too long lines. Otherwise everything looks shit. Terminal-Table does not handle this for you. I created an own Table version for Timr. I don’t want to invent the wheel for everything. Some things just don’t fit my needs.

Progressbar

Also I wanted to use ruby-progressbar to show the Estimated Remaining Time for Tasks. It looks promising. But it does not fit my requirements. It says Ruby/ProgressBar is an extremely flexible text progress bar library. I cannot get it just as one single String? Why does it even have a to_s function when I cannot use it as stand-alone? See issue #131. How complicated can it be to get a progressbar as a single String?

I also tried progress_bar. That is a flop too. This drives me crazy. And again, I built an own version of a Progressbar. Without bullshit.

Ruby Hashes

You maybe already saw this to assign default value to a Hash key:

def my_test(options = Hash.new)
	options[:format] ||= '%x'
	
	# Do something with options[:format] here.
end

This means options[:format] will be set to %x when it’s unset. This approach is good for an initialize method, when no further

def initialize(options = Hash.new)
	@options = options
	@options[:format] ||= '%x'
end

On normal class methods I prefer Hash#fetch:

def my_test(options = Hash.new)
	format = options.fetch(:format, '%x')
	
	# Do something with format here.
end

Man pages

Creating man pages is pretty simple using Ronn. I’m documenting as much as possible.

See man pages for Timr here: https://timr.fox21.at/man/

Posted on .
Categories: Project. Tags: Time, Timesheet, Tracking, Command-line, CLI, Shell, Terminal, Ruby.

Imprint | Usage | Archive | Categories | Feed
Copyright © 2006 by