Skip to content

Instantly share code, notes, and snippets.

@awidegreen
Last active December 16, 2015 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awidegreen/5416537 to your computer and use it in GitHub Desktop.
Save awidegreen/5416537 to your computer and use it in GitHub Desktop.
A parameter completion wrapper for (t)csh. It can be called from the built-in csh complete command
#!/usr/bin/env ruby
#
# This is a wrapper to query the appropiate command/parameter
# completions for (t)csh.
#
# Armin Widegreen (c) 2013
#
# A call returns a space-seperated string
# of the possible completions, e.g.:
# ParameterCompletion.getCompletionString("cmd1 diff")
# returns: "-1 -2 -s -r"
#
# The definition of possible completions is done in a hash, see below.
# The implementation relies on the termination of the last hash-element entry
# with nil.
#
# More examples:
# in: cm > "cmd1 cmd2"
# in: cmd1 di > "diff history"
# in: cmd2 d > "" ; no return needed
class ParameterCompletion
@@complete_hash = {
"cmd1" =>
{
"diff" =>
{
"-1" => nil,
"-2" => nil,
"-s" => nil,
"-r" => nil
},
"history" =>
{
"-l" => nil,
"-a" => nil
}
},
"cmd2" =>
{
"s" => nil,
"d" => nil
}
}
def self.getCompletion(comp_hash, tokens, position)
token = tokens[position]
if comp_hash[token].is_a?(Hash)
# has sub-hash with sub-param
return getCompletion(comp_hash[token], tokens, position+1)
elsif position == tokens.length - 1 &&
comp_hash.has_key?(token)
# last element and has key, no completion!
return ""
else
# last element (no hash), but no match
return comp_hash.keys.join(" ")
end
end
def self.getCompletionString(command)
tokens = command.split(" ")
getCompletion(@@complete_hash, tokens, 0)
end
end
### example call
puts ParameterCompletion.getCompletionString(ARGV.join(" "))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment