irb 1.14.1 → 1.15.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.
- checksums.yaml +4 -4
- data/Gemfile +3 -1
- data/README.md +13 -299
- data/Rakefile +4 -6
- data/irb.gemspec +2 -2
- data/lib/irb/color.rb +1 -0
- data/lib/irb/color_printer.rb +10 -9
- data/lib/irb/command/base.rb +3 -7
- data/lib/irb/command/copy.rb +73 -0
- data/lib/irb/command/history.rb +1 -1
- data/lib/irb/command/internal_helpers.rb +1 -1
- data/lib/irb/command/ls.rb +23 -11
- data/lib/irb/completion.rb +12 -5
- data/lib/irb/context.rb +105 -56
- data/lib/irb/debug/ui.rb +2 -4
- data/lib/irb/debug.rb +1 -0
- data/lib/irb/default_commands.rb +4 -1
- data/lib/irb/easter-egg.rb +2 -1
- data/lib/irb/history.rb +84 -55
- data/lib/irb/init.rb +4 -2
- data/lib/irb/input-method.rb +9 -1
- data/lib/irb/inspector.rb +11 -6
- data/lib/irb/lc/ja/help-message +4 -4
- data/lib/irb/nesting_parser.rb +1 -1
- data/lib/irb/pager.rb +125 -7
- data/lib/irb/source_finder.rb +2 -3
- data/lib/irb/statement.rb +21 -0
- data/lib/irb/version.rb +2 -2
- data/lib/irb/workspace.rb +6 -26
- data/lib/irb.rb +46 -901
- data/man/irb.1 +37 -2
- metadata +19 -8
- data/.document +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ce1957978a2c18545492323b39bd054ce881cbd54aae4b2056cee739c41b17c9
|
4
|
+
data.tar.gz: 52d7820dbc752ec441cf7f255ef3e5c94bd9ed524c186f51c52003eb10627db2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 35eb9995bb8dd7289360bff316d94af1df148ae36dc8bb0fc9dcfde1e78d940147a1021f93b2875c1f73f528ad3cb2198a292707d180bf4a7d510efb972e7d65
|
7
|
+
data.tar.gz: 6ab14019f16bb0b62ac075291067dd48c184e89e1e185ba9e57c1ac376e2115ac4da93d03aca45b21088531664da67e3deba983f43052e8bbd0511102aadff2d
|
data/Gemfile
CHANGED
@@ -7,7 +7,7 @@ is_truffleruby = RUBY_DESCRIPTION =~ /truffleruby/
|
|
7
7
|
|
8
8
|
if is_unix && ENV['WITH_VTERM']
|
9
9
|
gem "vterm", github: "ruby/vterm-gem"
|
10
|
-
gem "yamatanooroti", github: "ruby/yamatanooroti"
|
10
|
+
gem "yamatanooroti", github: "ruby/yamatanooroti"
|
11
11
|
end
|
12
12
|
|
13
13
|
gem "stackprof" if is_unix && !is_truffleruby
|
@@ -22,6 +22,8 @@ gem "rubocop"
|
|
22
22
|
gem "tracer" if !is_truffleruby
|
23
23
|
gem "debug", github: "ruby/debug", platforms: [:mri, :mswin]
|
24
24
|
|
25
|
+
gem "rdoc", ">= 6.11.0"
|
26
|
+
|
25
27
|
if RUBY_VERSION >= "3.0.0" && !is_truffleruby
|
26
28
|
gem "repl_type_completor"
|
27
29
|
end
|
data/README.md
CHANGED
@@ -1,33 +1,14 @@
|
|
1
1
|
# IRB
|
2
2
|
|
3
3
|
[](https://badge.fury.io/rb/irb)
|
4
|
+
[](https://ruby.github.io/irb/)
|
4
5
|
[](https://github.com/ruby/irb/actions/workflows/test.yml)
|
5
6
|
|
7
|
+
|
6
8
|
IRB stands for "interactive Ruby" and is a tool to interactively execute Ruby expressions read from the standard input.
|
7
9
|
|
8
10
|
The `irb` command from your shell will start the interpreter.
|
9
11
|
|
10
|
-
- [Installation](#installation)
|
11
|
-
- [Usage](#usage)
|
12
|
-
- [The `irb` Executable](#the-irb-executable)
|
13
|
-
- [The `binding.irb` Breakpoint](#the-bindingirb-breakpoint)
|
14
|
-
- [Commands](#commands)
|
15
|
-
- [Debugging with IRB](#debugging-with-irb)
|
16
|
-
- [More about `debug.gem`](#more-about-debuggem)
|
17
|
-
- [Advantages Over `debug.gem`'s Console](#advantages-over-debuggems-console)
|
18
|
-
- [Type Based Completion](#type-based-completion)
|
19
|
-
- [How to Enable IRB::TypeCompletor](#how-to-enable-irbtypecompletor)
|
20
|
-
- [Advantage over Default IRB::RegexpCompletor](#advantage-over-default-irbregexpcompletor)
|
21
|
-
- [Difference between Steep's Completion](#difference-between-steeps-completion)
|
22
|
-
- [Configuration](#configuration)
|
23
|
-
- [Environment Variables](#environment-variables)
|
24
|
-
- [Documentation](#documentation)
|
25
|
-
- [Extending IRB](#extending-irb)
|
26
|
-
- [Development](#development)
|
27
|
-
- [Contributing](#contributing)
|
28
|
-
- [Releasing](#releasing)
|
29
|
-
- [License](#license)
|
30
|
-
|
31
12
|
## Installation
|
32
13
|
|
33
14
|
> [!Note]
|
@@ -58,7 +39,7 @@ $ gem install irb
|
|
58
39
|
|
59
40
|
> [!Note]
|
60
41
|
>
|
61
|
-
> We're working hard to match Pry's variety of powerful features in IRB, and you can track our progress or find contribution ideas in [this document](https://github.
|
42
|
+
> We're working hard to match Pry's variety of powerful features in IRB, and you can track our progress or find contribution ideas in [this document](https://ruby.github.io/irb/COMPARED_WITH_PRY_md.html).
|
62
43
|
|
63
44
|
### The `irb` Executable
|
64
45
|
|
@@ -105,299 +86,32 @@ irb(main):002:0> exit
|
|
105
86
|
Hello World
|
106
87
|
```
|
107
88
|
|
108
|
-
|
109
|
-
|
110
|
-
The following commands are available on IRB. You can get the same output from the `help` command.
|
111
|
-
|
112
|
-
```txt
|
113
|
-
Help
|
114
|
-
help List all available commands. Use `help <command>` to get information about a specific command.
|
115
|
-
|
116
|
-
IRB
|
117
|
-
context Displays current configuration.
|
118
|
-
exit Exit the current irb session.
|
119
|
-
exit! Exit the current process.
|
120
|
-
irb_load Load a Ruby file.
|
121
|
-
irb_require Require a Ruby file.
|
122
|
-
source Loads a given file in the current session.
|
123
|
-
irb_info Show information about IRB.
|
124
|
-
history Shows the input history. `-g [query]` or `-G [query]` allows you to filter the output.
|
125
|
-
disable_irb Disable binding.irb.
|
126
|
-
|
127
|
-
Workspace
|
128
|
-
cwws Show the current workspace.
|
129
|
-
chws Change the current workspace to an object.
|
130
|
-
workspaces Show workspaces.
|
131
|
-
pushws Push an object to the workspace stack.
|
132
|
-
popws Pop a workspace from the workspace stack.
|
133
|
-
cd Move into the given object or leave the current context.
|
134
|
-
|
135
|
-
Multi-irb (DEPRECATED)
|
136
|
-
irb Start a child IRB.
|
137
|
-
jobs List of current sessions.
|
138
|
-
fg Switches to the session of the given number.
|
139
|
-
kill Kills the session with the given number.
|
140
|
-
|
141
|
-
Debugging
|
142
|
-
debug Start the debugger of debug.gem.
|
143
|
-
break Start the debugger of debug.gem and run its `break` command.
|
144
|
-
catch Start the debugger of debug.gem and run its `catch` command.
|
145
|
-
next Start the debugger of debug.gem and run its `next` command.
|
146
|
-
delete Start the debugger of debug.gem and run its `delete` command.
|
147
|
-
step Start the debugger of debug.gem and run its `step` command.
|
148
|
-
continue Start the debugger of debug.gem and run its `continue` command.
|
149
|
-
finish Start the debugger of debug.gem and run its `finish` command.
|
150
|
-
backtrace Start the debugger of debug.gem and run its `backtrace` command.
|
151
|
-
info Start the debugger of debug.gem and run its `info` command.
|
152
|
-
|
153
|
-
Misc
|
154
|
-
edit Open a file or source location.
|
155
|
-
measure `measure` enables the mode to measure processing time. `measure :off` disables it.
|
156
|
-
|
157
|
-
Context
|
158
|
-
show_doc Look up documentation with RI.
|
159
|
-
ls Show methods, constants, and variables.
|
160
|
-
show_source Show the source code of a given method, class/module, or constant.
|
161
|
-
whereami Show the source code around binding.irb again.
|
162
|
-
|
163
|
-
Helper methods
|
164
|
-
conf Returns the current IRB context.
|
165
|
-
|
166
|
-
Aliases
|
167
|
-
$ Alias for `show_source`
|
168
|
-
@ Alias for `whereami`
|
169
|
-
```
|
170
|
-
|
171
|
-
## Debugging with IRB
|
172
|
-
|
173
|
-
Starting from version 1.8.0, IRB boasts a powerful integration with `debug.gem`, providing a debugging experience akin to `pry-byebug`.
|
174
|
-
|
175
|
-
After hitting a `binding.irb` breakpoint, you can activate the debugger with the `debug` command. Alternatively, if the `debug` method happens to already be defined in the current scope, you can call `irb_debug`.
|
176
|
-
|
177
|
-
```shell
|
178
|
-
From: test.rb @ line 3 :
|
179
|
-
|
180
|
-
1:
|
181
|
-
2: def greet(word)
|
182
|
-
=> 3: binding.irb
|
183
|
-
4: puts "Hello #{word}"
|
184
|
-
5: end
|
185
|
-
6:
|
186
|
-
7: greet("World")
|
187
|
-
|
188
|
-
irb(main):001> debug
|
189
|
-
irb:rdbg(main):002>
|
190
|
-
```
|
191
|
-
|
192
|
-
Once activated, the prompt's header changes from `irb` to `irb:rdbg`, enabling you to use any of `debug.gem`'s [commands](https://github.com/ruby/debug#debug-command-on-the-debug-console):
|
193
|
-
|
194
|
-
```shell
|
195
|
-
irb:rdbg(main):002> info # use info command to see available variables
|
196
|
-
%self = main
|
197
|
-
_ = nil
|
198
|
-
word = "World"
|
199
|
-
irb:rdbg(main):003> next # use next command to move to the next line
|
200
|
-
[1, 7] in test.rb
|
201
|
-
1|
|
202
|
-
2| def greet(word)
|
203
|
-
3| binding.irb
|
204
|
-
=> 4| puts "Hello #{word}"
|
205
|
-
5| end
|
206
|
-
6|
|
207
|
-
7| greet("World")
|
208
|
-
=>#0 Object#greet(word="World") at test.rb:4
|
209
|
-
#1 <main> at test.rb:7
|
210
|
-
irb:rdbg(main):004>
|
211
|
-
```
|
212
|
-
|
213
|
-
Simultaneously, you maintain access to IRB's commands, such as `show_source`:
|
214
|
-
|
215
|
-
```shell
|
216
|
-
irb:rdbg(main):004> show_source greet
|
217
|
-
|
218
|
-
From: test.rb:2
|
219
|
-
|
220
|
-
def greet(word)
|
221
|
-
binding.irb
|
222
|
-
puts "Hello #{word}"
|
223
|
-
end
|
224
|
-
```
|
225
|
-
|
226
|
-
### More about `debug.gem`
|
227
|
-
|
228
|
-
`debug.gem` offers many advanced debugging features that simple REPLs can't provide, including:
|
229
|
-
|
230
|
-
- Step-debugging
|
231
|
-
- Frame navigation
|
232
|
-
- Setting breakpoints with commands
|
233
|
-
- Thread control
|
234
|
-
- ...and many more
|
235
|
-
|
236
|
-
To learn about these features, please refer to `debug.gem`'s [commands list](https://github.com/ruby/debug#debug-command-on-the-debug-console).
|
237
|
-
|
238
|
-
In the `irb:rdbg` session, the `help` command will also display all commands from `debug.gem`.
|
239
|
-
|
240
|
-
### Advantages Over `debug.gem`'s Console
|
241
|
-
|
242
|
-
This integration offers several benefits over `debug.gem`'s native console:
|
243
|
-
|
244
|
-
1. Access to handy IRB commands like `show_source` or `show_doc`.
|
245
|
-
2. Support for multi-line input.
|
246
|
-
3. Symbol shortcuts such as `@` (`whereami`) and `$` (`show_source`).
|
247
|
-
4. Autocompletion.
|
248
|
-
5. Customizable prompt.
|
249
|
-
|
250
|
-
However, there are also some limitations to be aware of:
|
251
|
-
|
252
|
-
1. `binding.irb` doesn't support `pre` and `do` arguments like [`binding.break`](https://github.com/ruby/debug#bindingbreak-method).
|
253
|
-
2. As IRB [doesn't currently support remote-connection](https://github.com/ruby/irb/issues/672), it can't be used with `debug.gem`'s remote debugging feature.
|
254
|
-
3. Access to the previous return value via the underscore `_` is not supported.
|
255
|
-
|
256
|
-
## Type Based Completion
|
257
|
-
|
258
|
-
IRB's default completion `IRB::RegexpCompletor` uses Regexp. IRB has another experimental completion `IRB::TypeCompletor` that uses type analysis.
|
259
|
-
|
260
|
-
### How to Enable IRB::TypeCompletor
|
261
|
-
|
262
|
-
Install [ruby/repl_type_completor](https://github.com/ruby/repl_type_completor/) with:
|
263
|
-
```
|
264
|
-
$ gem install repl_type_completor
|
265
|
-
```
|
266
|
-
Or add these lines to your project's Gemfile.
|
267
|
-
```ruby
|
268
|
-
gem 'irb'
|
269
|
-
gem 'repl_type_completor', group: [:development, :test]
|
270
|
-
```
|
271
|
-
|
272
|
-
Now you can use type based completion by:
|
273
|
-
|
274
|
-
Running IRB with the `--type-completor` option
|
275
|
-
```
|
276
|
-
$ irb --type-completor
|
277
|
-
```
|
278
|
-
|
279
|
-
Or writing this line to IRB's rc-file (e.g. `~/.irbrc`)
|
280
|
-
```ruby
|
281
|
-
IRB.conf[:COMPLETOR] = :type # default is :regexp
|
282
|
-
```
|
283
|
-
|
284
|
-
Or setting the environment variable `IRB_COMPLETOR`
|
285
|
-
```ruby
|
286
|
-
ENV['IRB_COMPLETOR'] = 'type'
|
287
|
-
IRB.start
|
288
|
-
```
|
289
|
-
|
290
|
-
To check if it's enabled, type `irb_info` into IRB and see the `Completion` section.
|
291
|
-
```
|
292
|
-
irb(main):001> irb_info
|
293
|
-
...
|
294
|
-
# Enabled
|
295
|
-
Completion: Autocomplete, ReplTypeCompletor: 0.1.0, Prism: 0.18.0, RBS: 3.3.0
|
296
|
-
# Not enabled
|
297
|
-
Completion: Autocomplete, RegexpCompletor
|
298
|
-
...
|
299
|
-
```
|
300
|
-
If you have `sig/` directory or `rbs_collection.lock.yaml` in current directory, IRB will load it.
|
301
|
-
|
302
|
-
### Advantage over Default IRB::RegexpCompletor
|
303
|
-
|
304
|
-
IRB::TypeCompletor can autocomplete chained methods, block parameters and more if type information is available.
|
305
|
-
These are some examples IRB::RegexpCompletor cannot complete.
|
306
|
-
|
307
|
-
```ruby
|
308
|
-
irb(main):001> 'Ruby'.upcase.chars.s # Array methods (sample, select, shift, size)
|
309
|
-
```
|
89
|
+
### Debugging
|
310
90
|
|
311
|
-
|
312
|
-
irb(main):001> 10.times.map(&:to_s).each do |s|
|
313
|
-
irb(main):002> s.up # String methods (upcase, upcase!, upto)
|
314
|
-
```
|
91
|
+
You can use IRB as a debugging console with `debug.gem` with these options:
|
315
92
|
|
316
|
-
|
317
|
-
|
318
|
-
irb(main):002> def foo
|
319
|
-
irb(main):003> sa # save, save!
|
320
|
-
```
|
93
|
+
- In `binding.irb`, use the `debug` command to start an `irb:rdbg` session with access to all `debug.gem` commands.
|
94
|
+
- Use the `RUBY_DEBUG_IRB_CONSOLE=1` environment variable to make `debug.gem` use IRB as the debugging console.
|
321
95
|
|
322
|
-
|
96
|
+
To learn more about debugging with IRB, see [Debugging with IRB](https://ruby.github.io/irb/#label-Debugging+with+IRB).
|
323
97
|
|
324
|
-
|
325
|
-
|
326
|
-
Compared with Steep, IRB::TypeCompletor has some difference and limitations.
|
327
|
-
```ruby
|
328
|
-
[0, 'a'].sample.
|
329
|
-
# Steep completes intersection of Integer methods and String methods
|
330
|
-
# IRB::TypeCompletor completes both Integer and String methods
|
331
|
-
```
|
332
|
-
|
333
|
-
Some features like type narrowing is not implemented.
|
334
|
-
```ruby
|
335
|
-
def f(arg = [0, 'a'].sample)
|
336
|
-
if arg.is_a?(String)
|
337
|
-
arg. # Completes both Integer and String methods
|
338
|
-
```
|
98
|
+
## Documentation
|
339
99
|
|
340
|
-
|
341
|
-
```ruby
|
342
|
-
irb(main):001> a = [1]
|
343
|
-
=> [1]
|
344
|
-
irb(main):002> a.first. # Completes Integer methods
|
345
|
-
```
|
100
|
+
https://ruby.github.io/irb/ provides a comprehensive guide to IRB's features and usage.
|
346
101
|
|
347
102
|
## Configuration
|
348
103
|
|
349
|
-
|
350
|
-
|
351
|
-
- `NO_COLOR`: Assigning a value to it disables IRB's colorization.
|
352
|
-
- `IRB_USE_AUTOCOMPLETE`: Setting it to `false` disables IRB's autocompletion.
|
353
|
-
- `IRB_COMPLETOR`: Configures IRB's auto-completion behavior, allowing settings for either `regexp` or `type`.
|
354
|
-
- `VISUAL`: Its value would be used to open files by the `edit` command.
|
355
|
-
- `EDITOR`: Its value would be used to open files by the `edit` command if `VISUAL` is unset.
|
356
|
-
- `IRBRC`: The file specified would be evaluated as IRB's rc-file.
|
357
|
-
|
358
|
-
## Documentation
|
359
|
-
|
360
|
-
https://ruby.github.io/irb/
|
104
|
+
See the [Configuration page](https://ruby.github.io/irb/Configurations_md.html) in the documentation.
|
361
105
|
|
362
106
|
## Extending IRB
|
363
107
|
|
364
108
|
IRB `v1.13.0` and later versions allows users/libraries to extend its functionality through official APIs.
|
365
109
|
|
366
|
-
For more information, please visit [
|
367
|
-
|
368
|
-
## Development
|
369
|
-
|
370
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
371
|
-
|
372
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
110
|
+
For more information, please visit the [IRB Extension Guide](https://ruby.github.io/irb/EXTEND_IRB_md.html).
|
373
111
|
|
374
112
|
## Contributing
|
375
113
|
|
376
|
-
|
377
|
-
|
378
|
-
### Set up the environment
|
379
|
-
|
380
|
-
1. Fork the project to your GithHub account
|
381
|
-
2. Clone the fork with `git clone git@github.com:[your_username]/irb.git`
|
382
|
-
3. Run `bundle install`
|
383
|
-
4. Run `bundle exec rake` to make sure tests pass locally
|
384
|
-
|
385
|
-
### Run integration tests
|
386
|
-
|
387
|
-
If your changes affect component rendering, such as the autocompletion's dialog/dropdown, you may need to run IRB's integration tests, known as `yamatanooroti`.
|
388
|
-
|
389
|
-
Before running these tests, ensure that you have `libvterm` installed. If you're using Homebrew, you can install it by running:
|
390
|
-
|
391
|
-
```bash
|
392
|
-
brew install libvterm
|
393
|
-
```
|
394
|
-
|
395
|
-
After installing `libvterm`, you can run the integration tests using the following commands:
|
396
|
-
|
397
|
-
```bash
|
398
|
-
WITH_VTERM=1 bundle install
|
399
|
-
WITH_VTERM=1 bundle exec rake test test_yamatanooroti
|
400
|
-
```
|
114
|
+
See [CONTRIBUTING.md](./CONTRIBUTING.md) for more information.
|
401
115
|
|
402
116
|
## Releasing
|
403
117
|
|
data/Rakefile
CHANGED
@@ -45,10 +45,8 @@ end
|
|
45
45
|
task :default => :test
|
46
46
|
|
47
47
|
RDoc::Task.new do |rdoc|
|
48
|
-
rdoc.title = "IRB"
|
49
|
-
rdoc.
|
50
|
-
rdoc.
|
51
|
-
rdoc.
|
52
|
-
rdoc.main = "README.md"
|
53
|
-
rdoc.options.push("--copy-files", "LICENSE.txt")
|
48
|
+
rdoc.title = "IRB Documentation"
|
49
|
+
rdoc.main = "Index.md"
|
50
|
+
rdoc.rdoc_dir = "_site"
|
51
|
+
rdoc.options.push("lib")
|
54
52
|
end
|
data/irb.gemspec
CHANGED
@@ -18,11 +18,10 @@ Gem::Specification.new do |spec|
|
|
18
18
|
|
19
19
|
spec.metadata["homepage_uri"] = spec.homepage
|
20
20
|
spec.metadata["source_code_uri"] = spec.homepage
|
21
|
-
spec.metadata["documentation_uri"] =
|
21
|
+
spec.metadata["documentation_uri"] = "https://ruby.github.io/irb/"
|
22
22
|
spec.metadata["changelog_uri"] = "#{spec.homepage}/releases"
|
23
23
|
|
24
24
|
spec.files = [
|
25
|
-
".document",
|
26
25
|
"Gemfile",
|
27
26
|
"LICENSE.txt",
|
28
27
|
"README.md",
|
@@ -43,4 +42,5 @@ Gem::Specification.new do |spec|
|
|
43
42
|
|
44
43
|
spec.add_dependency "reline", ">= 0.4.2"
|
45
44
|
spec.add_dependency "rdoc", ">= 4.0.0"
|
45
|
+
spec.add_dependency "pp", ">= 0.6.0"
|
46
46
|
end
|
data/lib/irb/color.rb
CHANGED
@@ -41,6 +41,7 @@ module IRB # :nodoc:
|
|
41
41
|
on_embvar: [[RED], ALL],
|
42
42
|
on_float: [[MAGENTA, BOLD], ALL],
|
43
43
|
on_gvar: [[GREEN, BOLD], ALL],
|
44
|
+
on_backref: [[GREEN, BOLD], ALL],
|
44
45
|
on_heredoc_beg: [[RED], ALL],
|
45
46
|
on_heredoc_end: [[RED], ALL],
|
46
47
|
on_ident: [[BLUE, BOLD], Ripper::EXPR_ENDFN],
|
data/lib/irb/color_printer.rb
CHANGED
@@ -4,12 +4,9 @@ require_relative 'color'
|
|
4
4
|
|
5
5
|
module IRB
|
6
6
|
class ColorPrinter < ::PP
|
7
|
-
METHOD_RESPOND_TO = Object.instance_method(:respond_to?)
|
8
|
-
METHOD_INSPECT = Object.instance_method(:inspect)
|
9
|
-
|
10
7
|
class << self
|
11
|
-
def pp(obj, out = $>, width = screen_width)
|
12
|
-
q = ColorPrinter.new(out, width)
|
8
|
+
def pp(obj, out = $>, width = screen_width, colorize: true)
|
9
|
+
q = ColorPrinter.new(out, width, colorize: colorize)
|
13
10
|
q.guard_inspect_key {q.pp obj}
|
14
11
|
q.flush
|
15
12
|
out << "\n"
|
@@ -24,12 +21,16 @@ module IRB
|
|
24
21
|
end
|
25
22
|
end
|
26
23
|
|
24
|
+
def initialize(out, width, colorize: true)
|
25
|
+
@colorize = colorize
|
26
|
+
|
27
|
+
super(out, width)
|
28
|
+
end
|
29
|
+
|
27
30
|
def pp(obj)
|
28
31
|
if String === obj
|
29
32
|
# Avoid calling Ruby 2.4+ String#pretty_print that splits a string by "\n"
|
30
33
|
text(obj.inspect)
|
31
|
-
elsif !METHOD_RESPOND_TO.bind(obj).call(:inspect)
|
32
|
-
text(METHOD_INSPECT.bind(obj).call)
|
33
34
|
else
|
34
35
|
super
|
35
36
|
end
|
@@ -46,9 +47,9 @@ module IRB
|
|
46
47
|
when ',', '=>', '[', ']', '{', '}', '..', '...', /\A@\w+\z/
|
47
48
|
super(str, width)
|
48
49
|
when /\A#</, '=', '>'
|
49
|
-
super(Color.colorize(str, [:GREEN]), width)
|
50
|
+
super(@colorize ? Color.colorize(str, [:GREEN]) : str, width)
|
50
51
|
else
|
51
|
-
super(Color.colorize_code(str, ignore_error: true), width)
|
52
|
+
super(@colorize ? Color.colorize_code(str, ignore_error: true) : str, width)
|
52
53
|
end
|
53
54
|
end
|
54
55
|
end
|
data/lib/irb/command/base.rb
CHANGED
@@ -5,13 +5,11 @@
|
|
5
5
|
#
|
6
6
|
|
7
7
|
module IRB
|
8
|
-
# :stopdoc:
|
9
|
-
|
10
8
|
module Command
|
11
|
-
class CommandArgumentError < StandardError; end
|
9
|
+
class CommandArgumentError < StandardError; end # :nodoc:
|
12
10
|
|
13
11
|
class << self
|
14
|
-
def extract_ruby_args(*args, **kwargs)
|
12
|
+
def extract_ruby_args(*args, **kwargs) # :nodoc:
|
15
13
|
throw :EXTRACT_RUBY_ARGS, [args, kwargs]
|
16
14
|
end
|
17
15
|
end
|
@@ -57,8 +55,6 @@ module IRB
|
|
57
55
|
end
|
58
56
|
end
|
59
57
|
|
60
|
-
Nop = Base
|
58
|
+
Nop = Base # :nodoc:
|
61
59
|
end
|
62
|
-
|
63
|
-
# :startdoc:
|
64
60
|
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module IRB
|
4
|
+
module Command
|
5
|
+
class Copy < Base
|
6
|
+
category "Misc"
|
7
|
+
description "Copy expression output to clipboard"
|
8
|
+
|
9
|
+
help_message(<<~HELP)
|
10
|
+
Usage: copy ([expression])
|
11
|
+
|
12
|
+
When given:
|
13
|
+
- an expression, copy the inspect result of the expression to the clipboard.
|
14
|
+
- no arguments, copy the last evaluated result (`_`) to the clipboard.
|
15
|
+
|
16
|
+
Examples:
|
17
|
+
|
18
|
+
copy Foo.new
|
19
|
+
copy User.all.to_a
|
20
|
+
copy
|
21
|
+
HELP
|
22
|
+
|
23
|
+
def execute(arg)
|
24
|
+
# Copy last value if no expression was supplied
|
25
|
+
arg = '_' if arg.to_s.strip.empty?
|
26
|
+
|
27
|
+
value = irb_context.workspace.binding.eval(arg)
|
28
|
+
output = irb_context.inspect_method.inspect_value(value, +'', colorize: false).chomp
|
29
|
+
|
30
|
+
if clipboard_available?
|
31
|
+
copy_to_clipboard(output)
|
32
|
+
else
|
33
|
+
warn "System clipboard not found"
|
34
|
+
end
|
35
|
+
rescue StandardError => e
|
36
|
+
warn "Error: #{e}"
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def copy_to_clipboard(text)
|
42
|
+
IO.popen(clipboard_program, 'w') do |io|
|
43
|
+
io.write(text)
|
44
|
+
end
|
45
|
+
|
46
|
+
raise IOError.new("Copying to clipboard failed") unless $? == 0
|
47
|
+
|
48
|
+
puts "Copied to system clipboard"
|
49
|
+
rescue Errno::ENOENT => e
|
50
|
+
warn e.message
|
51
|
+
warn "Is IRB.conf[:COPY_COMMAND] set to a bad value?"
|
52
|
+
end
|
53
|
+
|
54
|
+
def clipboard_program
|
55
|
+
@clipboard_program ||= if IRB.conf[:COPY_COMMAND]
|
56
|
+
IRB.conf[:COPY_COMMAND]
|
57
|
+
elsif executable?("pbcopy")
|
58
|
+
"pbcopy"
|
59
|
+
elsif executable?("xclip")
|
60
|
+
"xclip -selection clipboard"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def executable?(command)
|
65
|
+
system("which #{command} > /dev/null 2>&1")
|
66
|
+
end
|
67
|
+
|
68
|
+
def clipboard_available?
|
69
|
+
!!clipboard_program
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
data/lib/irb/command/history.rb
CHANGED
@@ -19,7 +19,7 @@ module IRB
|
|
19
19
|
# Use throw and catch to handle arg that includes `;`
|
20
20
|
# For example: "1, kw: (2; 3); 4" will be parsed to [[1], { kw: 3 }]
|
21
21
|
catch(:EXTRACT_RUBY_ARGS) do
|
22
|
-
@irb_context.workspace.binding.eval "IRB::Command.extract_ruby_args #{arg}"
|
22
|
+
@irb_context.workspace.binding.eval "::IRB::Command.extract_ruby_args #{arg}"
|
23
23
|
end || [[], {}]
|
24
24
|
end
|
25
25
|
end
|
data/lib/irb/command/ls.rb
CHANGED
@@ -11,7 +11,7 @@ module IRB
|
|
11
11
|
|
12
12
|
module Command
|
13
13
|
class Ls < Base
|
14
|
-
|
14
|
+
class EvaluationError < StandardError; end
|
15
15
|
|
16
16
|
category "Context"
|
17
17
|
description "Show methods, constants, and variables."
|
@@ -22,24 +22,35 @@ module IRB
|
|
22
22
|
-g [query] Filter the output with a query.
|
23
23
|
HELP_MESSAGE
|
24
24
|
|
25
|
+
def evaluate(code)
|
26
|
+
@irb_context.workspace.binding.eval(code)
|
27
|
+
rescue Exception => e
|
28
|
+
puts "#{e.class}: #{e.message}"
|
29
|
+
raise EvaluationError
|
30
|
+
end
|
31
|
+
|
25
32
|
def execute(arg)
|
26
33
|
if match = arg.match(/\A(?<target>.+\s|)(-g|-G)\s+(?<grep>.+)$/)
|
27
|
-
|
28
|
-
use_main = true
|
29
|
-
else
|
30
|
-
obj = @irb_context.workspace.binding.eval(match[:target])
|
31
|
-
end
|
34
|
+
target = match[:target]
|
32
35
|
grep = Regexp.new(match[:grep])
|
36
|
+
elsif match = arg.match(/\A((?<target>.+),|)\s*grep:(?<grep>.+)/)
|
37
|
+
# Legacy style `ls obj, grep: /regexp/`
|
38
|
+
# Evaluation order should be eval(target) then eval(grep)
|
39
|
+
target = match[:target] || ''
|
40
|
+
grep_regexp_code = match[:grep]
|
33
41
|
else
|
34
|
-
|
35
|
-
use_main = args.empty?
|
36
|
-
obj = args.first
|
37
|
-
grep = kwargs[:grep]
|
42
|
+
target = arg.strip
|
38
43
|
end
|
39
44
|
|
40
|
-
if
|
45
|
+
if target.empty?
|
41
46
|
obj = irb_context.workspace.main
|
42
47
|
locals = irb_context.workspace.binding.local_variables
|
48
|
+
else
|
49
|
+
obj = evaluate(target)
|
50
|
+
end
|
51
|
+
|
52
|
+
if grep_regexp_code
|
53
|
+
grep = evaluate(grep_regexp_code)
|
43
54
|
end
|
44
55
|
|
45
56
|
o = Output.new(grep: grep)
|
@@ -52,6 +63,7 @@ module IRB
|
|
52
63
|
o.dump("class variables", klass.class_variables)
|
53
64
|
o.dump("locals", locals) if locals
|
54
65
|
o.print_result
|
66
|
+
rescue EvaluationError
|
55
67
|
end
|
56
68
|
|
57
69
|
def dump_methods(o, klass, obj)
|