lightning 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,43 @@
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, bolt_key)
6
+ new(text_to_complete, bolt_key).matches
7
+ end
8
+
9
+ def initialize(text_typed, bolt_key)
10
+ @text_typed = text_typed
11
+ @bolt_key = bolt_key
12
+ end
13
+
14
+ def matches
15
+ if Lightning.config[:complete_regex]
16
+ begin
17
+ possible_completions.grep(/#{blob_to_regex(typed)}/)
18
+ rescue RegexpError
19
+ ['#Error: Invalid regular expression']
20
+ end
21
+ else
22
+ possible_completions.select do |e|
23
+ e[0, typed.length] == typed
24
+ end
25
+ end
26
+ end
27
+
28
+ #just converts * to .* to make a glob-like regex
29
+ def blob_to_regex(string)
30
+ string.gsub(/^\*|([^\.])\*/) {|e| $1 ? $1 + ".*" : ".*" }
31
+ end
32
+
33
+ def typed
34
+ # @text_typed[/\s(.+?)$/, 1] || ''
35
+ text = @text_typed[/^(\S+)\s+(#{Lightning::TEST_FLAG})?\s*(.+?)$/, 3] || ''
36
+ text.strip
37
+ end
38
+
39
+ def possible_completions
40
+ Lightning.bolts[@bolt_key].completions
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,59 @@
1
+ #This class maps completions to their full paths for the given blobs
2
+ class Lightning
3
+ class CompletionMap
4
+ attr_accessor :map
5
+ attr_reader :alias_map
6
+
7
+ def initialize(*globs)
8
+ options = globs[-1].is_a?(Hash) ? globs.pop : {}
9
+ globs.flatten!
10
+ @map = create_map_for_globs(globs)
11
+ @alias_map = (options[:global_aliases] || {}).merge(options[:aliases] || {})
12
+ end
13
+
14
+ def [](completion)
15
+ @map[completion] || @alias_map[completion]
16
+ end
17
+
18
+ def keys
19
+ (@map.keys + @alias_map.keys).uniq
20
+ end
21
+
22
+ #should return hash
23
+ def create_map_for_globs(globs)
24
+ path_hash = {}
25
+ ignore_paths = ['.', '..'] + Lightning.ignore_paths
26
+ globs.each do |d|
27
+ Dir.glob(d, File::FNM_DOTMATCH).each do |e|
28
+ basename = File.basename(e)
29
+ unless ignore_paths.include?(basename)
30
+ #save paths of duplicate basenames to process later
31
+ if path_hash.has_key?(basename)
32
+ if path_hash[basename].is_a?(Array)
33
+ path_hash[basename] << e
34
+ else
35
+ path_hash[basename] = [path_hash[basename], e]
36
+ end
37
+ else
38
+ path_hash[basename] = e
39
+ end
40
+ end
41
+ end
42
+ end
43
+ map_duplicate_basenames(path_hash)
44
+ path_hash
45
+ end
46
+
47
+ #map saved duplicates
48
+ def map_duplicate_basenames(path_hash)
49
+ path_hash.select {|k,v| v.is_a?(Array)}.each do |key,paths|
50
+ paths.each do |e|
51
+ new_key = "#{key}/#{File.dirname(e)}"
52
+ path_hash[new_key] = e
53
+ end
54
+ path_hash.delete(key)
55
+ end
56
+ end
57
+
58
+ end
59
+ end
@@ -0,0 +1,72 @@
1
+ class Lightning
2
+ class Config < ::Hash
3
+ class <<self
4
+ attr_accessor :config_file
5
+ def config_file
6
+ @config_file ||= (File.exists?('lightning.yml') ? 'lightning.yml' : File.expand_path(File.join("~",".lightning.yml")))
7
+ end
8
+
9
+ def create(options={})
10
+ hash = read_config_file
11
+ obj = new(hash)
12
+ configure_commands_and_paths(obj) if options[:read_only]
13
+ obj
14
+ end
15
+
16
+ def read_config_file(file=nil)
17
+ default_config = {'shell'=>'bash', 'generated_file'=>File.expand_path(File.join('~', '.lightning_completions')),
18
+ 'complete_regex'=>true}
19
+ @config_file = file if file
20
+ hash = YAML::load_file(config_file)
21
+ default_config.merge(hash)
22
+ end
23
+
24
+ def commands_to_bolt_key(map_to_command, new_command)
25
+ "#{map_to_command}-#{new_command}"
26
+ end
27
+
28
+ def configure_commands_and_paths(hash)
29
+ hash[:paths] ||= {}
30
+ hash[:commands].each do |e|
31
+ #mapping a referenced path
32
+ if e['paths'].is_a?(String)
33
+ e['bolt_key'] = e['paths'].dup
34
+ end
35
+ #create a path entry + key if none exists
36
+ if e['bolt_key'].nil?
37
+ #extract command in case it has options after it
38
+ e['map_to'] =~ /\s*(\w+)/
39
+ bolt_key = commands_to_bolt_key($1, e['name'])
40
+ e['bolt_key'] = bolt_key
41
+ hash[:paths][bolt_key] = e['paths'] || []
42
+ end
43
+ end
44
+ hash
45
+ end
46
+ end
47
+
48
+ def save
49
+ File.open(self.class.config_file, "w") { |f| f.puts self.to_hash.to_yaml }
50
+ end
51
+
52
+ def initialize(hash)
53
+ super
54
+ replace(hash)
55
+ self.replace(self.symbolize_keys)
56
+ end
57
+
58
+ def to_hash
59
+ hash = Hash.new
60
+ hash.replace(self)
61
+ end
62
+
63
+ #from Rails' ActiveSupport
64
+ def symbolize_keys
65
+ inject({}) do |options, (key, value)|
66
+ options[(key.to_sym rescue key) || key] = value
67
+ options
68
+ end
69
+ end
70
+
71
+ end
72
+ end
@@ -0,0 +1,48 @@
1
+ #This class generates shell scripts from a configuration.
2
+ class Lightning
3
+ class Generator
4
+ class<<self
5
+ def generate_completions(generated_file=nil)
6
+ generated_file ||= Lightning.config[:generated_file]
7
+ output = generate(Lightning.config[:shell], Lightning.config[:commands])
8
+ File.open(generated_file, 'w'){|f| f.write(output) }
9
+ output
10
+ end
11
+
12
+ def generate(*args)
13
+ shell = args.shift
14
+ send("#{shell}_generator", *args)
15
+ end
16
+
17
+ def bash_generator(commands)
18
+ body = <<-INIT
19
+ #### This file was generated by Lightning. ####
20
+ #LBIN_PATH="$PWD/bin/" #only use for development
21
+ LBIN_PATH=""
22
+
23
+ INIT
24
+ commands.each do |e|
25
+ body += <<-EOS
26
+
27
+ #{'#' + e['description'] if e['description']}
28
+ #{e['name']} () {
29
+ if [ -z "$1" ]; then
30
+ echo "No arguments given"
31
+ return
32
+ fi
33
+ FULL_PATH="`${LBIN_PATH}lightning-full_path #{e['name']} $@`#{e['post_path'] if e['post_path']}"
34
+ if [ $1 == '#{Lightning::TEST_FLAG}' ]; then
35
+ CMD="#{e['map_to']} '$FULL_PATH'#{' '+ e['add_to_command'] if e['add_to_command']}"
36
+ echo $CMD
37
+ else
38
+ #{e['map_to']} "$FULL_PATH"#{' '+ e['add_to_command'] if e['add_to_command']}
39
+ fi
40
+ }
41
+ complete -o default -C "${LBIN_PATH}lightning-complete #{e['name']}" #{e['name']}
42
+ EOS
43
+ end
44
+ body.gsub(/^\s{6,10}/, '')
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,87 @@
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
+ aliases:
48
+ r1: /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8
49
+
50
+ - name : oa
51
+ map_to : open -a
52
+ description : open mac applications
53
+ paths:
54
+ - /Applications/*.app
55
+ - /Applications/Utilities/*.app
56
+
57
+ - name : v-g
58
+ map_to : vim
59
+ description : open files when in the base directory of a ruby gem project
60
+ paths:
61
+ - lib/**/*.rb
62
+ - test/**/*.rb
63
+ - spec/**/*.rb
64
+ - bin/**
65
+
66
+ - name : v-rr
67
+ map_to : vim
68
+ description: open files when in the base directory of a rails project
69
+ paths:
70
+ - config/**/*.rb
71
+ - app/**/*.{rb,erb,rhtml}
72
+ - lib/**/*.rb
73
+ - test/**/*.rb
74
+ - spec/**/*.rb
75
+
76
+ paths:
77
+ # to obtain your own paths to your ruby gems:
78
+ # `gem environment path`.split(":").map {|e| e +"/gems/*" }
79
+ gem:
80
+ - /Users/bozo/.gem/ruby/1.8/gems/*
81
+ - /Library/Ruby/Gems/1.8/gems/*
82
+ - /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/*
83
+
84
+ gem_rdoc:
85
+ - /Users/bozo/.gem/ruby/1.8/doc/*
86
+ - /Library/Ruby/Gems/1.8/doc/*
87
+ - /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