rake and truncated task descriptions
February 24th, 2008
Has anyone noticed that the newer rake (as of 8.0) truncates task descriptions a lot shorter than the old rake (7.3)? Here’s what I mean:
jason@old-box:~/dev/some_app$ rake --version
rake, version 0.7.3
jason@old-box:~/dev/some_app$ rake -T rails
(in /home/jason/dev/some_app)
rake doc:clobber_rails # Remove rdoc products
rake doc:rails # Build the rails HTML Files
rake doc:rerails # Force a rebuild of the RDOC files
rake rails:freeze:edge # Lock to latest Edge Rails or a specific revision with REVISION=X (ex: REVISION=4021) or a tag with TAG=Y (ex: TAG=rel_1-1-0)
(etc...)
jason@new-box:~/dev/some_app$ rake --version
rake, version 0.8.1
jason@new-box:~/dev/some_app$ rake -T rails
(in /home/jason/dev/some_app)
rake doc:clobber_rails # Remove rdoc products
rake doc:rails # Build the rails HTML Files
rake doc:rerails # Force a rebuild of the RDOC files
rake rails:freeze:edge # Lock to latest Edge Rails or a specific re...
(etc...)
If you dig into the rake 8.0/8.1 gems, you might notice that the task list output is being truncated to 80 columns. This change was the result of a suggestion to make the task list more condensed, while at the same time adding a -D option for full descriptions (Rake 0.8.0 Released [rubyforge.org]):
jason@jason-4200:~/dev/phoenixmedia$ rake -D rails
(in /home/jason/dev/phoenixmedia)
rake doc:clobber_rails
Remove rdoc products
rake doc:rails
Build the rails HTML Files
rake doc:rerails
Force a rebuild of the RDOC files
rake rails:freeze:edge
Lock to latest Edge Rails or a specific revision with REVISION=X (ex: REVISION=4021) or a tag with TAG=Y (ex: TAG=rel_1-1-0)
(etc...)
...but as you can see, the full-description version puts a bunch of line breaks in the output.
I liked it better the old way! I don’t doubt the change was made for the best (line-wrapped descriptions in a small window are not very nice to look at), but I’d really like the option to set my own column width. For now, I’ve resorted to dropping this rake task into my Rails apps (you can save this as something.rake under my_rails_app_root/lib/tasks):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
NUMBER_OF_COLUMNS_TO_DISPLAY = 180 task :t, :p do |t, args| pattern = args.p || '.' displayable_tasks = Rake.application.tasks.select { |t| t.comment && t.name =~ /#{pattern}/ } width = displayable_tasks.collect { |t| t.name_with_args.length }.max || 10 cols = ENV['RAKE_DESC_COLS'] ? ENV['RAKE_DESC_COLS'].to_i : NUMBER_OF_COLUMNS_TO_DISPLAY max_column = cols - Rake.application.name.size - width - 7 displayable_tasks.each do |t| printf "#{Rake.application.name} %-#{width}s # %s\n", t.name_with_args, truncate(t.comment, max_column) end end def truncate(string, width) return '' if string.nil? if string.length <= width string else string[0, width-3] + "..." end end |
Note that this uses the new rake 8.x feature of allowing for command-line arguments to get passed into a task, so we can pass in a ‘pattern’ to search on without using an environment variable. Now I can do this:
jason@new-box:~/dev/some_app$ rake t[rails]
(in /home/jason/dev/some_app)
rake doc:clobber_rails # Remove rdoc products
rake doc:rails # Build the rails HTML Files
rake doc:rerails # Force a rebuild of the RDOC files
rake rails:freeze:edge # Lock to latest Edge Rails or a specific revision with REVISION=X (ex: REVISION=4021) or a tag with TAG=Y (ex: TAG=rel_1-1-0)
(etc...)
I set the default number of columns at 180 because that’s just right for a maximized terminal window with the Monospace font (default in Ubuntu) at 9pt on my 1280×1024 display. Of course, I left an option to easily change the number of columns via an environment variable. For example, on my EeePC, 110 is about right:
jason@eeePC:~/dev/some_app$ export RAKE_DESC_COLS=110
jason@eeePC:~/dev/some_app$ rake t[rails]
(in /home/jason/dev/some_app)
(in /home/jason/dev/phoenixmedia)
rake doc:clobber_rails # Remove rdoc products
rake doc:rails # Build the rails HTML Files
rake doc:rerails # Force a rebuild of the RDOC files
rake rails:freeze:edge # Lock to latest Edge Rails or a specific revision with REVISION=X (ex: RE...
(etc...)
Hey, I can actually read the descriptions again!


April 6th, 2008 at 12:10 PM
Here’s a different way to do it that looks at the number of columns wide your window actually is. (Only works on unix systems, but is coded defensively so it should not crash on others (knock on wood).)
Index: lib/rake.rb
lib/rake.rb (revision 639) + lib/rake.rb (working copy)
@ -2010,14 +2010,24@ end else width = displayable_tasks.collect { |t| t.name_with_args.length }.max || 10 - max_column = 80 – name.size – width – 7 + max_column = cols – name.size – width – 7 displayable_tasks.each do |t| printf ”#{name} %-#{width}s # %s\n”, t.name_with_args, truncate(t.comment, max_column) end end end + + def cols + unix? ? (%x{stty size 2>/dev/null}.split.collect { |x| x.to_i }[1]) : 80 + rescue + 80 + end+ def unix? + RUBY_PLATFORM =~ /(aix|darwin|linux|(net|free|open)bsd|cygwin|solaris|irix|hpux|)/i + end + def truncate(string, width) if string.length <= width string
April 6th, 2008 at 12:16 PM
Here’s a different way to do it that looks at the number of columns wide your window actually is. (Only works on unix systems, but is coded defensively so it should not crash on others (knock on wood).)
Index: lib/rake.rb
lib/rake.rb (revision 639) + lib/rake.rb (working copy)
@ -2010,14 +2010,24@ end else width = displayable_tasks.collect { |t| t.name_with_args.length }.max || 10 - max_column = 80 – name.size – width – 7 + max_column = cols – name.size – width – 7 displayable_tasks.each do |t| printf ”#{name} %-#{width}s # %s\n”, t.name_with_args, truncate(t.comment, max_column) end end end + + def cols + unix? ? (%x{stty size 2>/dev/null}.split.collect { |x| x.to_i }[1]) : 80 + rescue + 80 + endApril 6th, 2008 at 12:18 PM
Here’s a different way to do it that looks at the number of columns wide your window actually is. (Only works on unix systems, but is coded defensively so it should not crash on others (knock on wood).)
See http://rubyforge.org/pipermail/rake-devel/2008-April/000423.html