Permalink
Browse files

Added auto completion.

  • Loading branch information...
1 parent 0944f54 commit 1168707be09e9016edb35e2d66e6c03e27e7be22 @DAddYE committed Aug 31, 2011
Showing with 40 additions and 2 deletions.
  1. +36 −0 extras/completion.rb
  2. +4 −2 lib/do/common.rb
View
@@ -0,0 +1,36 @@
+#!/usr/bin/env ruby
+
+# to install, add the following line to your .bash_profile or .bashrc
+# complete -C ~/.do/completion -o default doit
+
+# Rake completion will return matching rake tasks given typed text. This way
+# you can auto-complete tasks as you are typing them by hitting [tab] or [tab][tab]
+# This also caches the rake tasks for optimium speed
+class DoCompletion
+ BIN = File.expand_path('../../bin/doit', __FILE__)
+
+ def initialize(command)
+ @command = command
+ end
+
+ def matches
+ do_tasks.select { |task| task =~ %r[^#{typed}] }.
+ map { |task| task.sub(typed_before_colon, '')}
+ end
+
+ private
+ def typed
+ @command[/\s(.+?)$/, 1] || ''
+ end
+
+ def typed_before_colon
+ typed[/.+\:/] || ''
+ end
+
+ def do_tasks
+ `#{BIN} list`.split("\n")[1..-1].map { |line| line.split[1] }
+ end
+end
+
+puts DoCompletion.new(ENV["COMP_LINE"]).matches
+exit 0
View
@@ -33,7 +33,7 @@
hrc = File.expand_path("~/.dorc")
orc = File.join(DO_PATH, 'dorc')
if File.exist?(orc)
- log "Config already exist in your %s path" % DO_PATH unless options[:dependency]
+ log "Config already exists in your \e[1m%s\e[0m path." % DO_PATH unless options[:dependency]
else
template = <<-RUBY.gsub(/^ {6}/, '')
##
@@ -52,6 +52,8 @@
log "\e[36mGenerated template, now you can add your config to: '%s'\e[0m" % orc
end
sh 'ln -s %s %s' % [orc, hrc] unless File.exist?(hrc)
+ log "To enable autocompletion add to your \e[1m.bash_profile\e[0m:"
+ log " complete -C %s -o default doit" % File.expand_path('../../../extras/completion.rb', __FILE__)
end
desc "show version number"
@@ -61,7 +63,7 @@
desc "show task list"
task :list do
- formatted = tasks.map { |t| ["\e[1mdoit\e[0m\e[34m %s:%s\e[0m" % [t[:namespace], t[:name]], t[:desc]] }
+ formatted = tasks.map { |t| ["\e[1mdoit\e[0m\e[34m %s:%s \e[0m" % [t[:namespace], t[:name]], t[:desc]] }
formatted.each { |f| f[0].gsub!(/\s:/, ' ') }
formatted.reject! { |t, desc| desc == '' }
max = formatted.max { |a,b| a[0].size <=> b[0].size }[0].size

0 comments on commit 1168707

Please sign in to comment.