cldwalker-lightning 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE.txt +22 -0
- data/README.markdown +127 -0
- data/bin/lightning-complete +7 -0
- data/bin/lightning-full_path +15 -0
- data/bin/lightning-install +5 -0
- data/lib/lightning/completion.rb +30 -0
- data/lib/lightning/config.rb +60 -0
- data/lib/lightning/core_extensions.rb +9 -0
- data/lib/lightning/generator.rb +47 -0
- data/lib/lightning/path_map.rb +57 -0
- data/lib/lightning.rb +26 -0
- data/lightning.yml.example +85 -0
- data/lightning_completions.example +147 -0
- data/test/lightning.yml +39 -0
- data/test/lightning_completion_test.rb +23 -0
- data/test/lightning_config_test.rb +41 -0
- data/test/lightning_path_map_test.rb +44 -0
- data/test/lightning_test.rb +35 -0
- data/test/test_helper.rb +20 -0
- metadata +75 -0
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT LICENSE
|
2
|
+
|
3
|
+
Copyright (c) 2008 Gabriel Horner
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
Description
|
2
|
+
===========
|
3
|
+
|
4
|
+
Autocomplete a group of files and directories simply by listing their globbable paths
|
5
|
+
in a config file. Generate completions from your config, source them into your shell
|
6
|
+
and you're ready to rock.
|
7
|
+
|
8
|
+
So instead of carpal-typing
|
9
|
+
|
10
|
+
bash> vim /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/irb.rb
|
11
|
+
|
12
|
+
you type or complete
|
13
|
+
|
14
|
+
bash> rvim irb.rb
|
15
|
+
|
16
|
+
Uneasy about what lightning will execute? Test it out with a -test flag
|
17
|
+
|
18
|
+
bash> rvim -test irb.rb
|
19
|
+
rvim '/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/irb.rb'
|
20
|
+
|
21
|
+
As you can see, you only need to autocomplete the basenames of paths and lightning will resolve their
|
22
|
+
full paths. rvim is a lightning command configured to autocomplete a certain group of paths for vim.
|
23
|
+
In this case, rvim is configured to complete my ruby core and standard library files.
|
24
|
+
|
25
|
+
|
26
|
+
Install
|
27
|
+
=======
|
28
|
+
|
29
|
+
To make your own commands, you'll need to:
|
30
|
+
|
31
|
+
1. Create ~/.lightning.yml or a lightning.yml in the current directory.
|
32
|
+
Use the Configuration section below and the provided lightning.yml.example as guides.
|
33
|
+
|
34
|
+
2. Execute `lightning-install` to generate ~/.lightning\_completions from your config.
|
35
|
+
There is a config option for changing the location of the generated file. See Configuration
|
36
|
+
below. See lightning\_completions.example for what would be generated for the enclosed example
|
37
|
+
config.
|
38
|
+
|
39
|
+
3. Source the generated file in your bashrc ie `source ~/.lightning\_completions`.
|
40
|
+
|
41
|
+
|
42
|
+
Globbable paths
|
43
|
+
===============
|
44
|
+
|
45
|
+
Since the globbable paths are interpreted by ruby's Dir.glob(), you can:
|
46
|
+
|
47
|
+
1. Autocomplete files and directories that don't start with specific letters.
|
48
|
+
|
49
|
+
Dir.glob("[^ls]*") -> Matches anything not starting with l or s
|
50
|
+
|
51
|
+
2. Autocomplete files ending with specific file extensions for a given directory.
|
52
|
+
|
53
|
+
Dir.glob("/painfully/long/path/*.{rb,erb}") -> Matches files ending with .rb or .erb
|
54
|
+
|
55
|
+
3. Autocomplete all directories however many levels deep under the current directory.
|
56
|
+
|
57
|
+
Dir.glob("**/")
|
58
|
+
|
59
|
+
`ri Dir.glob` for more examples.
|
60
|
+
|
61
|
+
Configuration
|
62
|
+
=====================
|
63
|
+
|
64
|
+
It helps to look at lightning.yml.example while reading this.
|
65
|
+
|
66
|
+
Configuration options are:
|
67
|
+
|
68
|
+
* generated\_file: Location of shell script file generated from config. Defaults to
|
69
|
+
~/.lightning\_completions.
|
70
|
+
* ignore\_paths: List of paths to globally ignore when listing completions.
|
71
|
+
* shell: Specifies shell script generator used for generating completions. Defaults to bash.
|
72
|
+
* commands: A list of lightning commands. A lightning command is just a shell function
|
73
|
+
which executes a specified shell function with a defined set of paths to autocomplete on.
|
74
|
+
A command consists of the following options/keys:
|
75
|
+
|
76
|
+
* name (required): Name of the lightning command you'll be typing.
|
77
|
+
* map\_to (required): Shell command which the lightning command passes arguments to.
|
78
|
+
* description: Description which is placed as a comment in the generated shell script.
|
79
|
+
* paths (required): A list of globbable paths, whose basenames are autocompleted. You can also
|
80
|
+
pass this a path name that has been defined in the paths option.
|
81
|
+
|
82
|
+
* paths: This takes a hash (pairs) of path names and globbable paths. This should be used when
|
83
|
+
you have a group of paths that are used in multiple commands. When doing that, you would specify
|
84
|
+
the path name defined here in the command's paths key.
|
85
|
+
Note: path names should only be alphanumeric
|
86
|
+
|
87
|
+
* post\_path: Text to add immediately after a resolved path. lightning.yml.example contains
|
88
|
+
an example used for opening gem documentation in your browser.
|
89
|
+
|
90
|
+
Duplicate Basenames
|
91
|
+
===================
|
92
|
+
|
93
|
+
So what happens when their are multiple matches for the same basename?
|
94
|
+
Lightning appends a '/' + the full directory to each of the basenames.
|
95
|
+
|
96
|
+
For example, if I autocomplete button.rb for my ruby standard libraries I get:
|
97
|
+
|
98
|
+
bash> rvim button.rb
|
99
|
+
button.rb//System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/tk
|
100
|
+
button.rb//System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/tkextlib/bwidget
|
101
|
+
button.rb//System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/tkextlib/blt/tile
|
102
|
+
|
103
|
+
This isn't the prettiest completion but it resolves duplicates, displays the path differences
|
104
|
+
between each and easily autocompletes. I'm open to suggestions on this syntax.
|
105
|
+
|
106
|
+
Limitations
|
107
|
+
===========
|
108
|
+
|
109
|
+
* The generated shell scripts default to bash but could easily be extended for other shell languages. Patches welcome.
|
110
|
+
* All arguments passed to a lightning command are considered part of the basename to resolve. So
|
111
|
+
it's not yet possible to resolve some arguments and not others.
|
112
|
+
|
113
|
+
Motivation
|
114
|
+
==========
|
115
|
+
|
116
|
+
I've seen dotfiles on github and on blogs which accomplish this kind of autocompletion for gem
|
117
|
+
documentation. There's even a gem just for gem editing: http://gemedit.rubyforge.org/.
|
118
|
+
But once I saw how easy it was to manipulate completion through ruby,
|
119
|
+
http://github.com/ryanb/dotfiles/blob/master/bash/completion\_scripts/project\_completion,
|
120
|
+
I had to do something.
|
121
|
+
|
122
|
+
Todo
|
123
|
+
====
|
124
|
+
|
125
|
+
* Clean up code to better test.
|
126
|
+
* Aliases for common autocompletions.
|
127
|
+
* Allow lightning commands to only path-resolve one of multiple arguments given.
|
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# == Description
|
4
|
+
# Used by path completion functions to return first possible full path which matches the given basename for the given key.
|
5
|
+
# Warning: Basenames that occur in multiple directories will return the first directory found.
|
6
|
+
|
7
|
+
require File.expand_path(File.dirname(__FILE__) + "/../lib/lightning")
|
8
|
+
|
9
|
+
key = ARGV.shift or raise "No key given"
|
10
|
+
if ARGV.empty?
|
11
|
+
puts "No arguments given"
|
12
|
+
exit 1
|
13
|
+
end
|
14
|
+
puts Lightning.full_path_for_completion(ARGV, key) || ''
|
15
|
+
exit 0
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# derived from http://github.com/ryanb/dotfiles/tree/master/bash/completion_scripts/project_completion
|
2
|
+
#This class handles completions given a path key and the text already typed.
|
3
|
+
class Lightning
|
4
|
+
class Completion
|
5
|
+
def self.complete(text_to_complete, path_key)
|
6
|
+
new(text_to_complete, path_key).matches
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize(text_typed, path_key)
|
10
|
+
@text_typed = text_typed
|
11
|
+
@path_key = path_key
|
12
|
+
end
|
13
|
+
|
14
|
+
def matches
|
15
|
+
possible_completions.select do |e|
|
16
|
+
e[0, typed.length] == typed
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def typed
|
21
|
+
# @text_typed[/\s(.+?)$/, 1] || ''
|
22
|
+
text = @text_typed[/^(\S+)\s+(#{Lightning::TEST_FLAG})?\s*(.+?)$/, 3] || ''
|
23
|
+
text.strip
|
24
|
+
end
|
25
|
+
|
26
|
+
def possible_completions
|
27
|
+
Lightning.path_map.completions(@path_key)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module Lightning::Config
|
2
|
+
def config
|
3
|
+
unless @config
|
4
|
+
@config = read_config
|
5
|
+
add_command_paths(@config)
|
6
|
+
end
|
7
|
+
@config
|
8
|
+
end
|
9
|
+
|
10
|
+
def read_config(config_file=nil)
|
11
|
+
default_config = {:shell=>'bash', :generated_file=>File.expand_path(File.join('~', '.lightning_completions'))}
|
12
|
+
config_file ||= File.exists?('lightning.yml') ? 'lightning.yml' : File.expand_path(File.join("~",".lightning.yml"))
|
13
|
+
hash = YAML::load(File.new(config_file))
|
14
|
+
default_config.merge(hash.symbolize_keys)
|
15
|
+
end
|
16
|
+
|
17
|
+
#should return array of globbable paths
|
18
|
+
def globbable_paths_by_key(key)
|
19
|
+
config[:paths][key] || []
|
20
|
+
end
|
21
|
+
|
22
|
+
def config_command(name)
|
23
|
+
config[:commands].find {|e| e['name'] == name} || {}
|
24
|
+
end
|
25
|
+
|
26
|
+
def command_to_path_key(map_to_command, new_command)
|
27
|
+
"#{map_to_command}-#{new_command}"
|
28
|
+
end
|
29
|
+
|
30
|
+
def path_key_to_command(path_key)
|
31
|
+
path_key.split("-")[1]
|
32
|
+
end
|
33
|
+
|
34
|
+
def add_command_paths(config)
|
35
|
+
config[:paths] ||= {}
|
36
|
+
config[:commands].each do |e|
|
37
|
+
#mapping a referenced path
|
38
|
+
if e['paths'].is_a?(String)
|
39
|
+
e['path_key'] = e['paths'].dup
|
40
|
+
e['paths'] = config[:paths][e['paths'].strip] || []
|
41
|
+
end
|
42
|
+
#create a path entry + key if none exists
|
43
|
+
if e['path_key'].nil?
|
44
|
+
#extract command in case it has options after it
|
45
|
+
e['map_to'] =~ /\s*(\w+)/
|
46
|
+
path_key = command_to_path_key($1, e['name'])
|
47
|
+
e['path_key'] = path_key
|
48
|
+
config[:paths][path_key] = e['paths']
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def ignore_paths
|
54
|
+
unless @ignore_paths
|
55
|
+
@ignore_paths = []
|
56
|
+
@ignore_paths += config[:ignore_paths] if config[:ignore_paths] && !config[:ignore_paths].empty?
|
57
|
+
end
|
58
|
+
@ignore_paths
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
#This class generates shell scripts from a configuration.
|
2
|
+
class Lightning
|
3
|
+
class Generator
|
4
|
+
class<<self
|
5
|
+
def generate_completions
|
6
|
+
output = generate(Lightning.config[:shell], Lightning.config[:commands])
|
7
|
+
File.open(Lightning.config[:generated_file], 'w'){|f| f.write(output) }
|
8
|
+
output
|
9
|
+
end
|
10
|
+
|
11
|
+
def generate(*args)
|
12
|
+
shell = args.shift
|
13
|
+
send("#{shell}_generator", *args)
|
14
|
+
end
|
15
|
+
|
16
|
+
def bash_generator(commands)
|
17
|
+
body = <<-INIT
|
18
|
+
#### This file was generated by Lightning. ####
|
19
|
+
#LBIN_PATH="$PWD/bin/" #only use for development
|
20
|
+
LBIN_PATH=""
|
21
|
+
|
22
|
+
INIT
|
23
|
+
commands.each do |e|
|
24
|
+
body += <<-EOS
|
25
|
+
|
26
|
+
#{'#' + e['description'] if e['description']}
|
27
|
+
#{e['name']} () {
|
28
|
+
if [ -z "$1" ]; then
|
29
|
+
echo "No arguments given"
|
30
|
+
return
|
31
|
+
fi
|
32
|
+
FULL_PATH="`${LBIN_PATH}lightning-full_path #{e['path_key']} $@`#{e['post_path'] if e['post_path']}"
|
33
|
+
if [ $1 == '#{Lightning::TEST_FLAG}' ]; then
|
34
|
+
CMD="#{e['map_to']} '$FULL_PATH'#{' '+ e['add_to_command'] if e['add_to_command']}"
|
35
|
+
echo $CMD
|
36
|
+
else
|
37
|
+
#{e['map_to']} "$FULL_PATH"#{' '+ e['add_to_command'] if e['add_to_command']}
|
38
|
+
fi
|
39
|
+
}
|
40
|
+
complete -o default -C "${LBIN_PATH}lightning-complete #{e['path_key']}" #{e['name']}
|
41
|
+
EOS
|
42
|
+
end
|
43
|
+
body.gsub(/^\s{6,10}/, '')
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
#This class maps completions to their full paths for a given path key.
|
2
|
+
class Lightning
|
3
|
+
class PathMap
|
4
|
+
def initialize
|
5
|
+
@maps = {}
|
6
|
+
end
|
7
|
+
|
8
|
+
def [](key)
|
9
|
+
@maps[key] ||= create_map(key)
|
10
|
+
end
|
11
|
+
|
12
|
+
def completions(key)
|
13
|
+
self[key].keys
|
14
|
+
end
|
15
|
+
|
16
|
+
def create_map(key)
|
17
|
+
create_map_for_globs(Lightning.globbable_paths_by_key(key))
|
18
|
+
end
|
19
|
+
|
20
|
+
#should return hash
|
21
|
+
def create_map_for_globs(globs)
|
22
|
+
path_hash = {}
|
23
|
+
ignore_paths = ['.', '..'] + Lightning.ignore_paths
|
24
|
+
globs.each do |d|
|
25
|
+
Dir.glob(d, File::FNM_DOTMATCH).each do |e|
|
26
|
+
basename = File.basename(e)
|
27
|
+
unless ignore_paths.include?(basename)
|
28
|
+
#save paths of duplicate basenames to process later
|
29
|
+
if path_hash.has_key?(basename)
|
30
|
+
if path_hash[basename].is_a?(Array)
|
31
|
+
path_hash[basename] << e
|
32
|
+
else
|
33
|
+
path_hash[basename] = [path_hash[basename], e]
|
34
|
+
end
|
35
|
+
else
|
36
|
+
path_hash[basename] = e
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
map_duplicate_basenames(path_hash)
|
42
|
+
path_hash
|
43
|
+
end
|
44
|
+
|
45
|
+
#map saved duplicates
|
46
|
+
def map_duplicate_basenames(path_hash)
|
47
|
+
path_hash.select {|k,v| v.is_a?(Array)}.each do |key,paths|
|
48
|
+
paths.each do |e|
|
49
|
+
new_key = "#{key}/#{File.dirname(e)}"
|
50
|
+
path_hash[new_key] = e
|
51
|
+
end
|
52
|
+
path_hash.delete(key)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
data/lib/lightning.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__))
|
2
|
+
require 'yaml'
|
3
|
+
require 'lightning/completion'
|
4
|
+
require 'lightning/config'
|
5
|
+
require 'lightning/path_map'
|
6
|
+
require 'lightning/core_extensions'
|
7
|
+
require 'lightning/generator'
|
8
|
+
|
9
|
+
class Lightning
|
10
|
+
TEST_FLAG = '-test'
|
11
|
+
extend Config
|
12
|
+
class<<self
|
13
|
+
def path_map
|
14
|
+
@path_map ||= PathMap.new
|
15
|
+
end
|
16
|
+
|
17
|
+
def full_path_for_completion(basename, path_key)
|
18
|
+
basename = basename.join(" ") if basename.is_a?(Array)
|
19
|
+
basename.gsub!(/\s*#{TEST_FLAG}\s*/,'')
|
20
|
+
if (regex = config_command(path_key_to_command(path_key))['completion_regex'])
|
21
|
+
basename = basename[/#{regex}/]
|
22
|
+
end
|
23
|
+
path_map[path_key][basename]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
---
|
2
|
+
generated_file: lightning_completions
|
3
|
+
|
4
|
+
ignore_paths:
|
5
|
+
- .DS_Store
|
6
|
+
- .git
|
7
|
+
|
8
|
+
# You'll notice that most commands have the format of "#{original_command_alias}-#{path_alias}"
|
9
|
+
# I recommend this for most of these commands as you can then easily autocomplete them for recall.
|
10
|
+
commands:
|
11
|
+
- name : c
|
12
|
+
map_to : cd
|
13
|
+
description : my code directories
|
14
|
+
paths:
|
15
|
+
- /Users/bozo/code/gems/*
|
16
|
+
- /Users/bozo/code/repo/*
|
17
|
+
- /Users/bozo/code/tds/gems/*
|
18
|
+
|
19
|
+
- name : cd-g
|
20
|
+
map_to : cd
|
21
|
+
paths: gem
|
22
|
+
|
23
|
+
- name : m-g
|
24
|
+
map_to : mate
|
25
|
+
paths: gem
|
26
|
+
|
27
|
+
- name : o-g
|
28
|
+
map_to : open
|
29
|
+
description : open gem's doc in browser
|
30
|
+
post_path : /rdoc/index.html
|
31
|
+
paths: gem_rdoc
|
32
|
+
|
33
|
+
- name : v-r
|
34
|
+
map_to : vim
|
35
|
+
description : ruby core files
|
36
|
+
paths:
|
37
|
+
#picked from $LOAD_PATH
|
38
|
+
- /Library/Ruby/Site/1.8/**/*.rb
|
39
|
+
- /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/**/*.rb
|
40
|
+
|
41
|
+
- name : cd-r
|
42
|
+
map_to : cd
|
43
|
+
description : ruby core directories
|
44
|
+
paths:
|
45
|
+
- /Library/Ruby/Site/1.8/**/
|
46
|
+
- /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/**/
|
47
|
+
|
48
|
+
- name : oa
|
49
|
+
map_to : open -a
|
50
|
+
description : open mac applications
|
51
|
+
paths:
|
52
|
+
- /Applications/*.app
|
53
|
+
- /Applications/Utilities/*.app
|
54
|
+
|
55
|
+
- name : v-g
|
56
|
+
map_to : vim
|
57
|
+
description : open files when in the base directory of a ruby gem project
|
58
|
+
paths:
|
59
|
+
- lib/**/*.rb
|
60
|
+
- test/**/*.rb
|
61
|
+
- spec/**/*.rb
|
62
|
+
- bin/**
|
63
|
+
|
64
|
+
- name : v-rr
|
65
|
+
map_to : vim
|
66
|
+
description: open files when in the base directory of a rails project
|
67
|
+
paths:
|
68
|
+
- config/**/*.rb
|
69
|
+
- app/**/*.{rb,erb,rhtml}
|
70
|
+
- lib/**/*.rb
|
71
|
+
- test/**/*.rb
|
72
|
+
- spec/**/*.rb
|
73
|
+
|
74
|
+
paths:
|
75
|
+
# to obtain your own paths to your ruby gems:
|
76
|
+
# `gem environment path`.split(":").map {|e| e +"/gems/*" }
|
77
|
+
gem:
|
78
|
+
- /Users/bozo/.gem/ruby/1.8/gems/*
|
79
|
+
- /Library/Ruby/Gems/1.8/gems/*
|
80
|
+
- /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/*
|
81
|
+
|
82
|
+
gem_rdoc:
|
83
|
+
- /Users/bozo/.gem/ruby/1.8/doc/*
|
84
|
+
- /Library/Ruby/Gems/1.8/doc/*
|
85
|
+
- /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/doc/*
|
@@ -0,0 +1,147 @@
|
|
1
|
+
#### This file was generated by Lightning. ####
|
2
|
+
#LBIN_PATH="$PWD/bin/" #only use for development
|
3
|
+
LBIN_PATH=""
|
4
|
+
|
5
|
+
#my code directories
|
6
|
+
c () {
|
7
|
+
if [ -z "$1" ]; then
|
8
|
+
echo "No arguments given"
|
9
|
+
return
|
10
|
+
fi
|
11
|
+
FULL_PATH="`${LBIN_PATH}lightning-full_path cd-c $@`"
|
12
|
+
if [ $1 == '-test' ]; then
|
13
|
+
CMD="cd '$FULL_PATH'"
|
14
|
+
echo $CMD
|
15
|
+
else
|
16
|
+
cd "$FULL_PATH"
|
17
|
+
fi
|
18
|
+
}
|
19
|
+
complete -o default -C "${LBIN_PATH}lightning-complete cd-c" c
|
20
|
+
|
21
|
+
|
22
|
+
cd-g () {
|
23
|
+
if [ -z "$1" ]; then
|
24
|
+
echo "No arguments given"
|
25
|
+
return
|
26
|
+
fi
|
27
|
+
FULL_PATH="`${LBIN_PATH}lightning-full_path gem $@`"
|
28
|
+
if [ $1 == '-test' ]; then
|
29
|
+
CMD="cd '$FULL_PATH'"
|
30
|
+
echo $CMD
|
31
|
+
else
|
32
|
+
cd "$FULL_PATH"
|
33
|
+
fi
|
34
|
+
}
|
35
|
+
complete -o default -C "${LBIN_PATH}lightning-complete gem" cd-g
|
36
|
+
|
37
|
+
|
38
|
+
m-g () {
|
39
|
+
if [ -z "$1" ]; then
|
40
|
+
echo "No arguments given"
|
41
|
+
return
|
42
|
+
fi
|
43
|
+
FULL_PATH="`${LBIN_PATH}lightning-full_path gem $@`"
|
44
|
+
if [ $1 == '-test' ]; then
|
45
|
+
CMD="mate '$FULL_PATH'"
|
46
|
+
echo $CMD
|
47
|
+
else
|
48
|
+
mate "$FULL_PATH"
|
49
|
+
fi
|
50
|
+
}
|
51
|
+
complete -o default -C "${LBIN_PATH}lightning-complete gem" m-g
|
52
|
+
|
53
|
+
#open gem's doc in browser
|
54
|
+
o-g () {
|
55
|
+
if [ -z "$1" ]; then
|
56
|
+
echo "No arguments given"
|
57
|
+
return
|
58
|
+
fi
|
59
|
+
FULL_PATH="`${LBIN_PATH}lightning-full_path gem_rdoc $@`/rdoc/index.html"
|
60
|
+
if [ $1 == '-test' ]; then
|
61
|
+
CMD="open '$FULL_PATH'"
|
62
|
+
echo $CMD
|
63
|
+
else
|
64
|
+
open "$FULL_PATH"
|
65
|
+
fi
|
66
|
+
}
|
67
|
+
complete -o default -C "${LBIN_PATH}lightning-complete gem_rdoc" o-g
|
68
|
+
|
69
|
+
#ruby core files
|
70
|
+
v-r () {
|
71
|
+
if [ -z "$1" ]; then
|
72
|
+
echo "No arguments given"
|
73
|
+
return
|
74
|
+
fi
|
75
|
+
FULL_PATH="`${LBIN_PATH}lightning-full_path vim-v-r $@`"
|
76
|
+
if [ $1 == '-test' ]; then
|
77
|
+
CMD="vim '$FULL_PATH'"
|
78
|
+
echo $CMD
|
79
|
+
else
|
80
|
+
vim "$FULL_PATH"
|
81
|
+
fi
|
82
|
+
}
|
83
|
+
complete -o default -C "${LBIN_PATH}lightning-complete vim-v-r" v-r
|
84
|
+
|
85
|
+
#ruby core directories
|
86
|
+
cd-r () {
|
87
|
+
if [ -z "$1" ]; then
|
88
|
+
echo "No arguments given"
|
89
|
+
return
|
90
|
+
fi
|
91
|
+
FULL_PATH="`${LBIN_PATH}lightning-full_path cd-cd-r $@`"
|
92
|
+
if [ $1 == '-test' ]; then
|
93
|
+
CMD="cd '$FULL_PATH'"
|
94
|
+
echo $CMD
|
95
|
+
else
|
96
|
+
cd "$FULL_PATH"
|
97
|
+
fi
|
98
|
+
}
|
99
|
+
complete -o default -C "${LBIN_PATH}lightning-complete cd-cd-r" cd-r
|
100
|
+
|
101
|
+
#open mac applications
|
102
|
+
oa () {
|
103
|
+
if [ -z "$1" ]; then
|
104
|
+
echo "No arguments given"
|
105
|
+
return
|
106
|
+
fi
|
107
|
+
FULL_PATH="`${LBIN_PATH}lightning-full_path open-oa $@`"
|
108
|
+
if [ $1 == '-test' ]; then
|
109
|
+
CMD="open -a '$FULL_PATH'"
|
110
|
+
echo $CMD
|
111
|
+
else
|
112
|
+
open -a "$FULL_PATH"
|
113
|
+
fi
|
114
|
+
}
|
115
|
+
complete -o default -C "${LBIN_PATH}lightning-complete open-oa" oa
|
116
|
+
|
117
|
+
#open files when in the base directory of a ruby gem project
|
118
|
+
v-g () {
|
119
|
+
if [ -z "$1" ]; then
|
120
|
+
echo "No arguments given"
|
121
|
+
return
|
122
|
+
fi
|
123
|
+
FULL_PATH="`${LBIN_PATH}lightning-full_path vim-v-g $@`"
|
124
|
+
if [ $1 == '-test' ]; then
|
125
|
+
CMD="vim '$FULL_PATH'"
|
126
|
+
echo $CMD
|
127
|
+
else
|
128
|
+
vim "$FULL_PATH"
|
129
|
+
fi
|
130
|
+
}
|
131
|
+
complete -o default -C "${LBIN_PATH}lightning-complete vim-v-g" v-g
|
132
|
+
|
133
|
+
#open files when in the base directory of a rails project
|
134
|
+
v-rr () {
|
135
|
+
if [ -z "$1" ]; then
|
136
|
+
echo "No arguments given"
|
137
|
+
return
|
138
|
+
fi
|
139
|
+
FULL_PATH="`${LBIN_PATH}lightning-full_path vim-v-rr $@`"
|
140
|
+
if [ $1 == '-test' ]; then
|
141
|
+
CMD="vim '$FULL_PATH'"
|
142
|
+
echo $CMD
|
143
|
+
else
|
144
|
+
vim "$FULL_PATH"
|
145
|
+
fi
|
146
|
+
}
|
147
|
+
complete -o default -C "${LBIN_PATH}lightning-complete vim-v-rr" v-rr
|
data/test/lightning.yml
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
---
|
2
|
+
generated_file: lightning_completions
|
3
|
+
|
4
|
+
ignore_paths:
|
5
|
+
- .DS_Store
|
6
|
+
- .git
|
7
|
+
|
8
|
+
#only oa command is currently used
|
9
|
+
commands:
|
10
|
+
- name : oa
|
11
|
+
map_to : open -a
|
12
|
+
description : open mac applications
|
13
|
+
paths:
|
14
|
+
- /Applications/*.app
|
15
|
+
- /Applications/Utilities/*.app
|
16
|
+
|
17
|
+
- name : gcd
|
18
|
+
map_to : cd
|
19
|
+
paths: gem
|
20
|
+
|
21
|
+
- name : rcd
|
22
|
+
map_to : cd
|
23
|
+
description : ruby core directories
|
24
|
+
paths:
|
25
|
+
- /Library/Ruby/Site/1.8/**/
|
26
|
+
- /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/**/
|
27
|
+
|
28
|
+
paths:
|
29
|
+
# to obtain your own paths to your ruby gems:
|
30
|
+
# `gem environment path`.split(":").map {|e| e +"/gems/*" }
|
31
|
+
gem:
|
32
|
+
- /Users/bozo/.gem/ruby/1.8/gems/*
|
33
|
+
- /Library/Ruby/Gems/1.8/gems/*
|
34
|
+
- /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/*
|
35
|
+
|
36
|
+
gem_rdoc:
|
37
|
+
- /Users/bozo/.gem/ruby/1.8/doc/*
|
38
|
+
- /Library/Ruby/Gems/1.8/doc/*
|
39
|
+
- /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/doc/*
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class LightningCompletionTest < Test::Unit::TestCase
|
4
|
+
context "Completion" do
|
5
|
+
|
6
|
+
test "from script matches correctly" do
|
7
|
+
Lightning.path_map.stub!(:completions, :return=>%w{at ap blah})
|
8
|
+
assert_arrays_equal Lightning::Completion.complete('cd-test a', 'blah'), %w{at ap}
|
9
|
+
end
|
10
|
+
|
11
|
+
test "for basic case matches correctly" do
|
12
|
+
Lightning.path_map.stub!(:completions, :return=>%w{at ap blah})
|
13
|
+
@completion = Lightning::Completion.new('cd-test a', 'blah')
|
14
|
+
assert_arrays_equal @completion.matches, %w{at ap}
|
15
|
+
end
|
16
|
+
|
17
|
+
test "with test flag matches correctly" do
|
18
|
+
Lightning.path_map.stub!(:completions, :return=>%w{at ap blah})
|
19
|
+
@completion = Lightning::Completion.new('cd-test -test a', 'blah')
|
20
|
+
assert_arrays_equal @completion.matches, %w{at ap}
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class LightningConfigTest < Test::Unit::TestCase
|
4
|
+
context "A config" do
|
5
|
+
config = Lightning.read_config(File.dirname(__FILE__) + '/lightning.yml')
|
6
|
+
|
7
|
+
should "be a hash" do
|
8
|
+
assert config.is_a?(Hash)
|
9
|
+
end
|
10
|
+
|
11
|
+
should "have keys that are symbols" do
|
12
|
+
assert config.keys.all? {|e| e.is_a?(Symbol)}
|
13
|
+
end
|
14
|
+
|
15
|
+
should "have read supported keys" do
|
16
|
+
supported_keys = [:generated_file, :commands, :ignore_paths, :paths, :shell]
|
17
|
+
assert_arrays_equal supported_keys, config.keys
|
18
|
+
end
|
19
|
+
|
20
|
+
should "have a generated_file key which is a string" do
|
21
|
+
assert config[:generated_file].is_a?(String)
|
22
|
+
end
|
23
|
+
|
24
|
+
should "have a commands key which is an array" do
|
25
|
+
assert config[:commands].is_a?(Array)
|
26
|
+
end
|
27
|
+
|
28
|
+
should "have a command with valid keys" do
|
29
|
+
assert config[:commands][0].slice('name', 'map_to', 'description').values.all? {|e| e.is_a?(String)}
|
30
|
+
assert config[:commands][0]['paths'].is_a?(Array)
|
31
|
+
end
|
32
|
+
|
33
|
+
should "have a paths key which is a hash" do
|
34
|
+
assert config[:paths].is_a?(Hash)
|
35
|
+
end
|
36
|
+
|
37
|
+
should "have an ignore_paths key which is an array" do
|
38
|
+
assert config[:ignore_paths].is_a?(Array)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class LightningPathMapTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context "PathMap" do
|
6
|
+
before(:each) { @map = Lightning::PathMap.new }
|
7
|
+
|
8
|
+
def stub_dir_glob(path_hash)
|
9
|
+
Dir.stub!(:glob) { path_hash.values }
|
10
|
+
end
|
11
|
+
|
12
|
+
test "creates map only once when accessing same key multiple times" do
|
13
|
+
# @map.expects(:create_map).once.returns({})
|
14
|
+
called = 0
|
15
|
+
@map.stub!(:create_map) {|e| called += 1; {}}
|
16
|
+
@map['blah']
|
17
|
+
@map['blah']
|
18
|
+
assert called == 1
|
19
|
+
end
|
20
|
+
|
21
|
+
test "creates basic map" do
|
22
|
+
expected_map = {"path1"=>"/dir1/path1", "path2"=>"/dir1/path2"}
|
23
|
+
stub_dir_glob(expected_map)
|
24
|
+
assert_equal expected_map, @map.create_map_for_globs(['blah'])
|
25
|
+
end
|
26
|
+
|
27
|
+
test "ignores paths from Lightning.ignore_paths" do
|
28
|
+
Lightning.stub!(:ignore_paths, :return=>['path1'])
|
29
|
+
expected_map = {"path1"=>"/dir1/path1", "path2"=>"/dir1/path2"}
|
30
|
+
stub_dir_glob(expected_map)
|
31
|
+
assert_equal expected_map.slice('path2'), @map.create_map_for_globs(['blah'])
|
32
|
+
end
|
33
|
+
|
34
|
+
test "creates map with duplicates" do
|
35
|
+
expected_map = {"path1//dir3"=>"/dir3/path1", "path2"=>"/dir1/path2", "path1//dir1"=>"/dir1/path1", "path1//dir2"=>"/dir2/path1"}
|
36
|
+
stub_dir_glob(expected_map)
|
37
|
+
assert_equal expected_map, @map.create_map_for_globs(['blah'])
|
38
|
+
end
|
39
|
+
|
40
|
+
test "creates hash even for invalid key" do
|
41
|
+
@map.create_map('blah').is_a?(Hash)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class LightningTest < Test::Unit::TestCase
|
4
|
+
context "Generator" do
|
5
|
+
before(:all) do
|
6
|
+
@config_file = File.dirname(__FILE__) + '/lightning_completions'
|
7
|
+
Lightning.config[:generated_file] = @config_file
|
8
|
+
Lightning::Generator.generate_completions
|
9
|
+
end
|
10
|
+
after(:all) { FileUtils.rm_f(Lightning.config[:generated_file]) }
|
11
|
+
|
12
|
+
test "generates file in expected location" do
|
13
|
+
assert File.exists?(@config_file)
|
14
|
+
end
|
15
|
+
|
16
|
+
#this depends on oa
|
17
|
+
test "generates expected output for a command" do
|
18
|
+
generated_command = <<-EOS.gsub(/^\s{6}/,'')
|
19
|
+
#open mac applications
|
20
|
+
oa () {
|
21
|
+
FULL_PATH="`${LBIN_PATH}lightning-full_path open-oa $@`"
|
22
|
+
if [ $1 == '-test' ]; then
|
23
|
+
CMD="open -a '$FULL_PATH'"
|
24
|
+
echo $CMD
|
25
|
+
else
|
26
|
+
open -a "$FULL_PATH"
|
27
|
+
fi
|
28
|
+
}
|
29
|
+
complete -o default -C "${LBIN_PATH}lightning-complete open-oa" oa
|
30
|
+
EOS
|
31
|
+
output = File.read(@config_file)
|
32
|
+
assert output.include?(generated_command)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'context' #gem
|
4
|
+
require 'stump' #gem
|
5
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
|
6
|
+
require 'lightning'
|
7
|
+
|
8
|
+
class Test::Unit::TestCase
|
9
|
+
def assert_arrays_equal(a1, a2)
|
10
|
+
assert_equal a1.map {|e| e.to_s}.sort, a2.map{|e| e.to_s}.sort
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
#from ActiveSupport
|
16
|
+
class Hash
|
17
|
+
def slice(*keys)
|
18
|
+
reject { |key,| !keys.include?(key) }
|
19
|
+
end
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cldwalker-lightning
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gabriel Horner
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-01-15 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Path completions for your shell that will let you navigate like lightning.
|
17
|
+
email: gabriel.horner@gmail.com
|
18
|
+
executables:
|
19
|
+
- lightning-complete
|
20
|
+
- lightning-full_path
|
21
|
+
- lightning-install
|
22
|
+
extensions: []
|
23
|
+
|
24
|
+
extra_rdoc_files:
|
25
|
+
- README.markdown
|
26
|
+
- LICENSE.txt
|
27
|
+
files:
|
28
|
+
- lightning_completions.example
|
29
|
+
- lightning.yml.example
|
30
|
+
- README.markdown
|
31
|
+
- LICENSE.txt
|
32
|
+
- bin/lightning-complete
|
33
|
+
- bin/lightning-full_path
|
34
|
+
- bin/lightning-install
|
35
|
+
- lib/lightning
|
36
|
+
- lib/lightning/completion.rb
|
37
|
+
- lib/lightning/config.rb
|
38
|
+
- lib/lightning/core_extensions.rb
|
39
|
+
- lib/lightning/generator.rb
|
40
|
+
- lib/lightning/path_map.rb
|
41
|
+
- lib/lightning.rb
|
42
|
+
- test/lightning.yml
|
43
|
+
- test/lightning_completion_test.rb
|
44
|
+
- test/lightning_config_test.rb
|
45
|
+
- test/lightning_path_map_test.rb
|
46
|
+
- test/lightning_test.rb
|
47
|
+
- test/test_helper.rb
|
48
|
+
has_rdoc: true
|
49
|
+
homepage: http://github.com/cldwalker/lightning
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: "0"
|
60
|
+
version:
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: "0"
|
66
|
+
version:
|
67
|
+
requirements: []
|
68
|
+
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 1.2.0
|
71
|
+
signing_key:
|
72
|
+
specification_version: 2
|
73
|
+
summary: Path completions for your shell that will let you navigate like lightning.
|
74
|
+
test_files: []
|
75
|
+
|