Class: Lightning::Function
- Inherits:
-
Object
- Object
- Lightning::Function
- Defined in:
- lib/lightning/function.rb
Overview
A Function object represents a shell function which wraps around a shell command and a Bolt. This shell function autocompletes bolt paths by their basenames and translates arguments that are these basenames to their full paths.
Argument Translation
Before executing its shell command, a function checks each argument to see if it can translate it. Translation is done if the argument matches the basename of one its bolt’s paths.
$ echo-ruby irb.rb
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/irb.rb.
For translation to occur, the full basename must match. The only exception to this is when using lightning’s own filename expansion syntax: a ‘..’ at the end of an argument expands the argument with all completions that matched up to ‘..’. For example:
$ echo-ruby ad[TAB]
address.rb addressbook.rb
$ echo-ruby ad..
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/osx/addressbook.rb
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/wsdl/soap/address.rb
This expansion of any bolt paths combined with regex completion makes for a powerfully quick way of typing paths.
Constant Summary collapse
- ATTRIBUTES =
:name, :post_path, :shell_command, :bolt, :desc
Instance Method Summary collapse
-
#aliases ⇒ Hash
User-defined aliases for any path.
-
#completion_map ⇒ CompletionMap
Map of basenames to full paths used in completion.
-
#completions ⇒ Array
All possible completions.
-
#globs ⇒ Array
Globs used to create completion_map.
-
#initialize(hash) ⇒ Function
constructor
A new instance of Function.
-
#translate(args) ⇒ Array
Translates function’s arguments.
Constructor Details
#initialize(hash) ⇒ Function
Returns a new instance of Function.
26 27 28 29 30 31 |
# File 'lib/lightning/function.rb', line 26 def initialize(hash) raise ArgumentError, "Function must have a name and bolt" unless hash['name'] && hash['bolt'] hash.each do |k,v| instance_variable_set("@#{k}", v) end end |
Instance Method Details
#aliases ⇒ Hash
User-defined aliases for any path. Defaults to its bolt’s aliases.
45 46 47 |
# File 'lib/lightning/function.rb', line 45 def aliases @aliases ||= Hash[@bolt.aliases.map {|k,v| [k, gsub_shell_vars(v)] }] end |
#completion_map ⇒ CompletionMap
Returns Map of basenames to full paths used in completion.
50 51 52 |
# File 'lib/lightning/function.rb', line 50 def completion_map @completion_map ||= CompletionMap.new(globs, :aliases=>aliases) end |
#completions ⇒ Array
Returns All possible completions.
34 35 36 |
# File 'lib/lightning/function.rb', line 34 def completions completion_map.keys end |
#globs ⇒ Array
Returns Globs used to create completion_map.
39 40 41 |
# File 'lib/lightning/function.rb', line 39 def globs @globs ||= @bolt.globs.map {|g| gsub_shell_vars(g) } end |
#translate(args) ⇒ Array
Returns Translates function’s arguments.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/lightning/function.rb', line 55 def translate(args) translated = Array(args).map {|arg| !completion_map[arg] && (new_arg = arg[/^(.*)\.\.$/,1]) ? Completion.complete(new_arg, self, false) : arg }.flatten.map {|arg| new_arg = completion_map[arg] || arg.dup new_arg << @post_path if @post_path && new_arg != arg if new_arg == arg && (dir = new_arg[/^([^\/]+)\//,1]) && (full_dir = completion_map[dir]) new_arg.sub!(dir, full_dir) new_arg = File.(new_arg) end new_arg } end |