Skip to content

Instantly share code, notes, and snippets.

@awidegreen
Last active December 16, 2015 09:58

Revisions

  1. awidegreen revised this gist Apr 18, 2013. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions csh_complete_wrapper.rb
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@
    #
    # A call returns a space-seperated string
    # of the possible completions, e.g.:
    # ParameterCompletion.getCompletionList("cmd1 diff")
    # ParameterCompletion.getCompletionString("cmd1 diff")
    # returns: "-1 -2 -s -r"
    #
    # The definition of possible completions is done in a hash, see below.
    @@ -58,11 +58,11 @@ def self.getCompletion(comp_hash, tokens, position)
    end
    end

    def self.getCompletionList(command)
    def self.getCompletionString(command)
    tokens = command.split(" ")
    getCompletion(@@complete_hash, tokens, 0)
    end
    end

    ### example call
    puts ParameterCompletion.getCompletionList(ARGV.join(" "))
    puts ParameterCompletion.getCompletionString(ARGV.join(" "))
  2. awidegreen created this gist Apr 18, 2013.
    68 changes: 68 additions & 0 deletions csh_complete_wrapper.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    #!/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.getCompletionList("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.getCompletionList(command)
    tokens = command.split(" ")
    getCompletion(@@complete_hash, tokens, 0)
    end
    end

    ### example call
    puts ParameterCompletion.getCompletionList(ARGV.join(" "))