Class: Lightning::Function

Inherits:
Object
  • Object
show all
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

ATTRIBUTES =
:name, :post_path, :shell_command, :bolt, :desc

Instance Method Summary

Constructor Details

- (Function) initialize(hash)

A new instance of Function

Raises:

  • (ArgumentError)

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

- (Hash) aliases

User-defined aliases for any path. Defaults to its bolt’s aliases.

Returns:

  • (Hash) — Maps aliases to full paths

45
46
47
# File 'lib/lightning/function.rb', line 45

def aliases
  @aliases ||= @bolt.aliases
end

- (CompletionMap) completion_map

Map of basenames to full paths used in completion

Returns:

  • (CompletionMap) — 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

- (Array) completions

All possible completions

Returns:

  • (Array) — All possible completions

34
35
36
# File 'lib/lightning/function.rb', line 34

def completions
  completion_map.keys
end

- (Array) globs

Globs used to create completion_map

Returns:


39
40
41
# File 'lib/lightning/function.rb', line 39

def globs
  @globs ||= @bolt.globs
end

- (Array) translate(args)

Translates function’s arguments

Returns:

  • (Array) — 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.expand_path(new_arg)
    end
    new_arg
  }
end