aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiquel Sabaté <mikisabate@gmail.com>2011-11-02 14:20:36 +0100
committerMiquel Sabaté <mikisabate@gmail.com>2011-11-02 14:20:36 +0100
commit67cbcb0e0c8eec26313fbaf70010b2458f36d8f8 (patch)
tree9f5e3e541864acc197dbe8d15008afe3ff56c108
parent3bd20f9d8a0945564e8bd275f0378778ef75c8de (diff)
downloaddotfiles-67cbcb0e0c8eec26313fbaf70010b2458f36d8f8.tar.gz
dotfiles-67cbcb0e0c8eec26313fbaf70010b2458f36d8f8.zip
Added a rake completion file
-rw-r--r--bashrc5
-rw-r--r--install.rb3
-rwxr-xr-xrake_completion69
3 files changed, 75 insertions, 2 deletions
diff --git a/bashrc b/bashrc
index ab8dd22..b274426 100644
--- a/bashrc
+++ b/bashrc
@@ -14,6 +14,9 @@ source $HOME/.gitcompletion.sh
# prompt with git completion
PS1='\u:\w$(__git_ps1 "\[\033[0;32m\]@\[\033[1;32m\]%s\[\033[0m\]\]") $ '
+# Rake completion
+complete -C $HOME/.rake_completion -o default rake
+
# Paths
export KDEDIR=/home/mssola/kde
export KDEDIRS=$KDEDIR
@@ -34,7 +37,7 @@ GO_SHORTCUTS=(
$HOME/Projects/kde-unstable/kdevelop/kdev-ruby
)
-# This is the function that takes advantage of the defined shortcuts.
+
function go {
local target=$1
local len=${#GO_SHORTCUTS[@]}
diff --git a/install.rb b/install.rb
index c83173a..e705a57 100644
--- a/install.rb
+++ b/install.rb
@@ -24,4 +24,5 @@ home ||= '$HOME'
`cp global.gitignore #{home}/.gitignore`
`cp vimrc #{home}/.vimrc`
`cp hgrc #{home}/.hgrc`
-
+`cp rake_completion #{home}/.rake_completion`
+`chmod +x #{home}/.rake_completion`
diff --git a/rake_completion b/rake_completion
new file mode 100755
index 0000000..172c158
--- /dev/null
+++ b/rake_completion
@@ -0,0 +1,69 @@
+#!/usr/bin/env ruby
+
+# to install, add the following line to your .bash_profile or .bashrc
+# complete -C path/to/rake_completion -o default rake
+
+# 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 RakeCompletion
+ CACHE_FILE_NAME = '.rake_tasks~'
+
+ def initialize(command)
+ @command = command
+ end
+
+ def matches
+ exit 0 if rakefile.nil?
+ matching_tasks.map do |task|
+ task.sub(typed_before_colon, '')
+ end
+ end
+
+ private
+
+ def typed
+ @command[/\s(.+?)$/, 1] || ''
+ end
+
+ def typed_before_colon
+ typed[/.+\:/] || ''
+ end
+
+ def matching_tasks
+ all_tasks.select do |task|
+ task[0, typed.length] == typed
+ end
+ end
+
+ def all_tasks
+ cache_current? ? tasks_from_cache : generate_tasks
+ end
+
+ def cache_current?
+ File.exist?(cache_file) && File.mtime(cache_file) >= File.mtime(rakefile)
+ end
+
+ def rakefile
+ ['rakefile', 'Rakefile', 'rakefile.rb', 'Rakefile.rb'].detect do |file|
+ File.file? File.join(Dir.pwd, file)
+ end
+ end
+
+ def cache_file
+ File.join(Dir.pwd, CACHE_FILE_NAME)
+ end
+
+ def tasks_from_cache
+ IO.read(cache_file).split
+ end
+
+ def generate_tasks
+ tasks = `rake --tasks`.split("\n")[1..-1].collect {|line| line.split[1]}
+ File.open(cache_file, 'w') { |f| f.write tasks.join("\n") }
+ tasks
+ end
+end
+
+puts RakeCompletion.new(ENV["COMP_LINE"]).matches
+exit 0