pry 0.8.3 → 0.8.4pre1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,38 @@
1
+ class Pry
2
+ module DefaultCommands
3
+
4
+ Input = Pry::CommandSet.new :input do
5
+
6
+ command "!", "Clear the input buffer. Useful if the parsing process goes wrong and you get stuck in the read loop." do
7
+ output.puts "Input buffer cleared!"
8
+ opts[:eval_string].clear
9
+ end
10
+
11
+ command "hist", "Show and replay Readline history. When given no args history is displayed.\nType `hist --help` for more info." do |*args|
12
+ hist_array = Readline::HISTORY.to_a
13
+
14
+ if args.empty?
15
+ text = add_line_numbers(hist_array.join("\n"), 0)
16
+ stagger_output(text)
17
+ next
18
+ end
19
+
20
+ opts = Slop.parse(args) do |opt|
21
+ opt.banner "Usage: hist [--replay START..END]\nView and replay history\nWhen given no args, history is displayed.\ne.g hist --replay 2..8"
22
+ opt.on :r, :replay, 'The line (or range of lines) to replay.', true, :as => Range
23
+ opt.on :h, :help, 'Show this message.', :tail => true do
24
+ output.puts opt.help
25
+ end
26
+ end
27
+
28
+ next if opts.h?
29
+
30
+ actions = Array(hist_array[opts[:replay]]).join("\n") + "\n"
31
+ Pry.active_instance.input = StringIO.new(actions)
32
+ end
33
+
34
+
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,190 @@
1
+ class Pry
2
+ module DefaultCommands
3
+
4
+ Introspection = Pry::CommandSet.new :introspection do
5
+
6
+ command "show-method", "Show the source for METH. Type `show-method --help` for more info. Aliases: $, show-source" do |*args|
7
+ target = target()
8
+
9
+ opts = Slop.parse!(args) do |opts|
10
+ opts.banner %{Usage: show-method [OPTIONS] [METH]
11
+ Show the source for method METH. Tries instance methods first and then methods by default.
12
+ e.g: show-method hello_method
13
+ --
14
+ }
15
+ opts.on :l, "line-numbers", "Show line numbers."
16
+ opts.on :M, "instance-methods", "Operate on instance methods."
17
+ opts.on :m, :methods, "Operate on methods."
18
+ opts.on :f, :flood, "Do not use a pager to view text longer than one screen."
19
+ opts.on :c, :context, "Select object context to run under.", true do |context|
20
+ target = Pry.binding_for(target.eval(context))
21
+ end
22
+ opts.on :h, :help, "This message." do
23
+ output.puts opts
24
+ end
25
+ end
26
+
27
+ next if opts.help?
28
+
29
+ meth_name = args.shift
30
+ if (meth = get_method_object(meth_name, target, opts.to_hash(true))).nil?
31
+ output.puts "Invalid method name: #{meth_name}. Type `show-method --help` for help"
32
+ next
33
+ end
34
+
35
+ code, code_type = code_and_code_type_for(meth)
36
+ next if !code
37
+
38
+ output.puts make_header(meth, code_type, code)
39
+ if Pry.color
40
+ code = CodeRay.scan(code, code_type).term
41
+ end
42
+
43
+ start_line = false
44
+ if opts.l?
45
+ start_line = meth.source_location ? meth.source_location.last : 1
46
+ end
47
+
48
+ render_output(opts.flood?, start_line, code)
49
+ code
50
+ end
51
+
52
+ alias_command "show-source", "show-method", ""
53
+ alias_command "$", "show-method", ""
54
+
55
+ command "show-command", "Show the source for CMD. Type `show-command --help` for more info." do |*args|
56
+ options = {}
57
+ target = target()
58
+ command_name = nil
59
+
60
+ OptionParser.new do |opts|
61
+ opts.banner = %{Usage: show-command [OPTIONS] [CMD]
62
+ Show the source for command CMD.
63
+ e.g: show-command show-method
64
+ --
65
+ }
66
+ opts.on("-l", "--line-numbers", "Show line numbers.") do |line|
67
+ options[:l] = true
68
+ end
69
+
70
+ opts.on("-f", "--flood", "Do not use a pager to view text longer than one screen.") do
71
+ options[:f] = true
72
+ end
73
+
74
+ opts.on_tail("-h", "--help", "This message.") do
75
+ output.puts opts
76
+ options[:h] = true
77
+ end
78
+ end.order(args) do |v|
79
+ command_name = v
80
+ end
81
+
82
+ next if options[:h]
83
+
84
+ if !command_name
85
+ output.puts "You must provide a command name."
86
+ next
87
+ end
88
+
89
+ if commands[command_name]
90
+ meth = commands[command_name].block
91
+
92
+ code = strip_leading_whitespace(meth.source)
93
+ file, line = meth.source_location
94
+ set_file_and_dir_locals(file)
95
+ check_for_dynamically_defined_method(meth)
96
+
97
+ output.puts make_header(meth, :ruby, code)
98
+
99
+ if Pry.color
100
+ code = CodeRay.scan(code, :ruby).term
101
+ end
102
+
103
+ render_output(options[:f], options[:l] ? meth.source_location.last : false, code)
104
+ code
105
+ else
106
+ output.puts "No such command: #{command_name}."
107
+ end
108
+ end
109
+
110
+ command "edit-method", "Edit a method. Type `edit-method --help` for more info." do |*args|
111
+ target = target()
112
+
113
+ opts = Slop.parse!(args) do |opts|
114
+ opts.banner %{Usage: edit-method [OPTIONS] [METH]
115
+ Edit the method METH in an editor.
116
+ Ensure #{bold("Pry.editor")} is set to your editor of choice.
117
+ e.g: edit-method hello_method
118
+ --
119
+ }
120
+ opts.on :M, "instance-methods", "Operate on instance methods."
121
+ opts.on :m, :methods, "Operate on methods."
122
+ opts.on "no-reload", "Do not automatically reload the method's file after editting."
123
+ opts.on :n, "no-jump", "Do not fast forward editor to first line of method."
124
+ opts.on :c, :context, "Select object context to run under.", true do |context|
125
+ target = Pry.binding_for(target.eval(context))
126
+ end
127
+ opts.on :h, :help, "This message." do
128
+ output.puts opts
129
+ end
130
+ end
131
+
132
+ next if opts.help?
133
+
134
+ meth_name = args.shift
135
+ if (meth = get_method_object(meth_name, target, opts.to_hash(true))).nil?
136
+ output.puts "Invalid method name: #{meth_name}."
137
+ next
138
+ end
139
+
140
+ next output.puts "Error: No editor set!\nEnsure that #{bold("Pry.editor")} is set to your editor of choice." if !Pry.editor
141
+
142
+ if is_a_c_method?(meth)
143
+ output.puts "Error: Can't edit a C method."
144
+ elsif is_a_dynamically_defined_method?(meth)
145
+ output.puts "Error: Can't edit an eval method."
146
+
147
+ # editor is invoked here
148
+ else
149
+ file, line = meth.source_location
150
+ set_file_and_dir_locals(file)
151
+
152
+ if Pry.editor.respond_to?(:call)
153
+ editor_invocation = Pry.editor.call(file, line)
154
+ else
155
+ # only use start line if -n option is not used
156
+ start_line_syntax = opts.n? ? "" : start_line_for_editor(line)
157
+ editor_invocation = "#{Pry.editor} #{start_line_syntax} #{file}"
158
+ end
159
+
160
+ run ".#{editor_invocation}"
161
+ silence_warnings do
162
+ load file if !opts["no-reload"]
163
+ end
164
+ end
165
+ end
166
+
167
+
168
+ helpers do
169
+
170
+ def start_line_for_editor(line_number)
171
+ case Pry.editor
172
+ when /^[gm]?vi/, /^emacs/, /^nano/, /^pico/, /^gedit/, /^kate/
173
+ "+#{line_number}"
174
+ when /^mate/, /^geany/
175
+ "-l #{line_number}"
176
+ else
177
+ if RUBY_PLATFORM =~ /mswin|mingw/
178
+ ""
179
+ else
180
+ "+#{line_number}"
181
+ end
182
+ end
183
+ end
184
+
185
+ end
186
+
187
+ end
188
+ end
189
+ end
190
+
@@ -0,0 +1,199 @@
1
+ class Pry
2
+ module DefaultCommands
3
+
4
+ Ls = Pry::CommandSet.new :ls do
5
+
6
+ command "ls", "Show the list of vars and methods in the current scope. Type `ls --help` for more info." do |*args|
7
+ options = {}
8
+ # Set target local to the default -- note that we can set a different target for
9
+ # ls if we like: e.g ls my_var
10
+ target = target()
11
+
12
+ OptionParser.new do |opts|
13
+ opts.banner = %{Usage: ls [OPTIONS] [VAR]\n\
14
+ List information about VAR (the current context by default).
15
+ Shows local and instance variables by default.
16
+ --
17
+ }
18
+ opts.on("-g", "--globals", "Display global variables.") do
19
+ options[:g] = true
20
+ end
21
+
22
+ opts.on("-c", "--constants", "Display constants.") do
23
+ options[:c] = true
24
+ end
25
+
26
+ opts.on("-l", "--locals", "Display locals.") do
27
+ options[:l] = true
28
+ end
29
+
30
+ opts.on("-i", "--ivars", "Display instance variables.") do
31
+ options[:i] = true
32
+ end
33
+
34
+ opts.on("-k", "--class-vars", "Display class variables.") do
35
+ options[:k] = true
36
+ end
37
+
38
+ opts.on("-m", "--methods", "Display methods (public methods by default).") do
39
+ options[:m] = true
40
+ end
41
+
42
+ opts.on("-M", "--instance-methods", "Display instance methods (only relevant to classes and modules).") do
43
+ options[:M] = true
44
+ end
45
+
46
+ opts.on("-P", "--public", "Display public methods (with -m).") do
47
+ options[:P] = true
48
+ end
49
+
50
+ opts.on("-r", "--protected", "Display protected methods (with -m).") do
51
+ options[:r] = true
52
+ end
53
+
54
+ opts.on("-p", "--private", "Display private methods (with -m).") do
55
+ options[:p] = true
56
+ end
57
+
58
+ opts.on("-j", "--just-singletons", "Display just the singleton methods (with -m).") do
59
+ options[:j] = true
60
+ end
61
+
62
+ opts.on("-s", "--super", "Include superclass entries (relevant to constant and methods options).") do
63
+ options[:s] = true
64
+ end
65
+
66
+ opts.on("-a", "--all", "Display all types of entries.") do
67
+ options[:a] = true
68
+ end
69
+
70
+ opts.on("-v", "--verbose", "Verbose ouput.") do
71
+ options[:v] = true
72
+ end
73
+
74
+ opts.on("-f", "--flood", "Do not use a pager to view text longer than one screen.") do
75
+ options[:f] = true
76
+ end
77
+
78
+ opts.on("--grep REG", "Regular expression to be used.") do |reg|
79
+ options[:grep] = Regexp.new(reg)
80
+ end
81
+
82
+ opts.on_tail("-h", "--help", "Show this message.") do
83
+ output.puts opts
84
+ options[:h] = true
85
+ end
86
+ end.order(args) do |new_target|
87
+ target = Pry.binding_for(target.eval("#{new_target}")) if !options[:h]
88
+ end
89
+
90
+ # exit if we've displayed help
91
+ next if options[:h]
92
+
93
+ # default is locals/ivars/class vars.
94
+ # Only occurs when no options or when only option is verbose
95
+ options.merge!({
96
+ :l => true,
97
+ :i => true,
98
+ :k => true
99
+ }) if options.empty? || (options.size == 1 && options[:v]) || (options.size == 1 && options[:grep])
100
+
101
+ options[:grep] = // if !options[:grep]
102
+
103
+
104
+ # Display public methods by default if -m or -M switch is used.
105
+ options[:P] = true if (options[:m] || options[:M]) && !(options[:p] || options[:r] || options[:j])
106
+
107
+ info = {}
108
+ target_self = target.eval('self')
109
+
110
+ # ensure we have a real boolean and not a `nil` (important when
111
+ # interpolating in the string)
112
+ options[:s] = !!options[:s]
113
+
114
+ # Numbers (e.g 0, 1, 2) are for ordering the hash values in Ruby 1.8
115
+ i = -1
116
+
117
+ # Start collecting the entries selected by the user
118
+ info["local variables"] = [Array(target.eval("local_variables")).sort, i += 1] if options[:l] || options[:a]
119
+ info["instance variables"] = [Array(target.eval("instance_variables")).sort, i += 1] if options[:i] || options[:a]
120
+
121
+ info["class variables"] = [if target_self.is_a?(Module)
122
+ Array(target.eval("class_variables")).sort
123
+ else
124
+ Array(target.eval("self.class.class_variables")).sort
125
+ end, i += 1] if options[:k] || options[:a]
126
+
127
+ info["global variables"] = [Array(target.eval("global_variables")).sort, i += 1] if options[:g] || options[:a]
128
+
129
+ info["public methods"] = [Array(target.eval("public_methods(#{options[:s]})")).uniq.sort, i += 1] if (options[:m] && options[:P]) || options[:a]
130
+
131
+ info["protected methods"] = [Array(target.eval("protected_methods(#{options[:s]})")).sort, i += 1] if (options[:m] && options[:r]) || options[:a]
132
+
133
+ info["private methods"] = [Array(target.eval("private_methods(#{options[:s]})")).sort, i += 1] if (options[:m] && options[:p]) || options[:a]
134
+
135
+ info["just singleton methods"] = [Array(target.eval("methods(#{options[:s]})")).sort, i += 1] if (options[:m] && options[:j]) || options[:a]
136
+
137
+ info["public instance methods"] = [Array(target.eval("public_instance_methods(#{options[:s]})")).uniq.sort, i += 1] if target_self.is_a?(Module) && ((options[:M] && options[:P]) || options[:a])
138
+
139
+ info["protected instance methods"] = [Array(target.eval("protected_instance_methods(#{options[:s]})")).uniq.sort, i += 1] if target_self.is_a?(Module) && ((options[:M] && options[:r]) || options[:a])
140
+
141
+ info["private instance methods"] = [Array(target.eval("private_instance_methods(#{options[:s]})")).uniq.sort, i += 1] if target_self.is_a?(Module) && ((options[:M] && options[:p]) || options[:a])
142
+
143
+ # dealing with 1.8/1.9 compatibility issues :/
144
+ csuper = options[:s]
145
+ if Module.method(:constants).arity == 0
146
+ csuper = nil
147
+ end
148
+
149
+ info["constants"] = [Array(target_self.is_a?(Module) ? target.eval("constants(#{csuper})") :
150
+ target.eval("self.class.constants(#{csuper})")).uniq.sort, i += 1] if options[:c] || options[:a]
151
+
152
+ text = ""
153
+
154
+ # verbose output?
155
+ if options[:v]
156
+ # verbose
157
+
158
+ info.sort_by { |k, v| v.last }.each do |k, v|
159
+ if !v.first.empty?
160
+ text << "#{k}:\n--\n"
161
+ filtered_list = v.first.grep options[:grep]
162
+ if Pry.color
163
+ text << CodeRay.scan(Pry.view(filtered_list), :ruby).term + "\n"
164
+ else
165
+ text << Pry.view(filtered_list) + "\n"
166
+ end
167
+ text << "\n\n"
168
+ end
169
+ end
170
+
171
+ if !options[:f]
172
+ stagger_output(text)
173
+ else
174
+ output.puts text
175
+ end
176
+
177
+ # plain
178
+ else
179
+ list = info.values.sort_by(&:last).map(&:first).inject(&:+)
180
+ list = list.grep(options[:grep]) if list
181
+ list.uniq! if list
182
+ if Pry.color
183
+ text << CodeRay.scan(Pry.view(list), :ruby).term + "\n"
184
+ else
185
+ text << Pry.view(list) + "\n"
186
+ end
187
+ if !options[:f]
188
+ stagger_output(text)
189
+ else
190
+ output.puts text
191
+ end
192
+ list
193
+ end
194
+ end
195
+
196
+
197
+ end
198
+ end
199
+ end
@@ -0,0 +1,90 @@
1
+ class Pry
2
+ module DefaultCommands
3
+
4
+ Shell = Pry::CommandSet.new :shell do
5
+
6
+ # this cannot be accessed, it's just for help purposes.
7
+ command ".<shell command>", "All text following a '.' is forwarded to the shell." do
8
+ end
9
+
10
+ command "shell-mode", "Toggle shell mode. Bring in pwd prompt and file completion." do
11
+ case Pry.active_instance.prompt
12
+ when Pry::SHELL_PROMPT
13
+ Pry.active_instance.pop_prompt
14
+ Pry.active_instance.custom_completions = Pry::DEFAULT_CUSTOM_COMPLETIONS
15
+ else
16
+ Pry.active_instance.push_prompt Pry::SHELL_PROMPT
17
+ Pry.active_instance.custom_completions = Pry::FILE_COMPLETIONS
18
+ Readline.completion_proc = Pry::InputCompleter.build_completion_proc target,
19
+ Pry.active_instance.instance_eval(&Pry::FILE_COMPLETIONS)
20
+ end
21
+ end
22
+
23
+ alias_command "file-mode", "shell-mode", ""
24
+
25
+
26
+ command "cat", "Show output of file FILE. Type `cat --help` for more information." do |*args|
27
+ options= {}
28
+ file_name = nil
29
+ start_line = 0
30
+ end_line = -1
31
+ file_type = nil
32
+
33
+ OptionParser.new do |opts|
34
+ opts.banner = %{Usage: cat [OPTIONS] FILE
35
+ Cat a file. Defaults to displaying whole file. Syntax highlights file if type is recognized.
36
+ e.g: cat hello.rb
37
+ --
38
+ }
39
+ opts.on("-l", "--line-numbers", "Show line numbers.") do |line|
40
+ options[:l] = true
41
+ end
42
+
43
+ opts.on("-s", "--start LINE", "Start line (defaults to start of file). Line 1 is the first line.") do |line|
44
+ start_line = line.to_i - 1
45
+ end
46
+
47
+ opts.on("-e", "--end LINE", "End line (defaults to end of file). Line -1 is the last line.") do |line|
48
+ end_line = line.to_i - 1
49
+ end
50
+
51
+ opts.on("-t", "--type TYPE", "The specific file type for syntax higlighting (e.g ruby, python, cpp, java)") do |type|
52
+ file_type = type.to_sym
53
+ end
54
+
55
+ opts.on("-f", "--flood", "Do not use a pager to view text longer than one screen.") do
56
+ options[:f] = true
57
+ end
58
+
59
+ opts.on_tail("-h", "--help", "This message.") do
60
+ output.puts opts
61
+ options[:h] = true
62
+ end
63
+ end.order(args) do |v|
64
+ file_name = v
65
+ end
66
+
67
+ next if options[:h]
68
+
69
+ if !file_name
70
+ output.puts "Must provide a file name."
71
+ next
72
+ end
73
+
74
+ contents, normalized_start_line, _ = read_between_the_lines(file_name, start_line, end_line)
75
+
76
+ if Pry.color
77
+ contents = syntax_highlight_by_file_type_or_specified(contents, file_name, file_type)
78
+ end
79
+
80
+ set_file_and_dir_locals(file_name)
81
+ render_output(options[:f], options[:l] ? normalized_start_line + 1 : false, contents)
82
+ contents
83
+ end
84
+
85
+
86
+ end
87
+
88
+ end
89
+ end
90
+