irb 1.14.3 → 1.15.0
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 +2 -0
- data/README.md +13 -310
- data/Rakefile +4 -4
- data/irb.gemspec +2 -2
- data/lib/irb/color.rb +1 -0
- data/lib/irb/color_printer.rb +10 -9
- data/lib/irb/command/copy.rb +73 -0
- data/lib/irb/command/history.rb +1 -1
- data/lib/irb/context.rb +95 -60
- data/lib/irb/default_commands.rb +4 -1
- data/lib/irb/history.rb +4 -0
- data/lib/irb/init.rb +3 -1
- data/lib/irb/inspector.rb +11 -6
- data/lib/irb/pager.rb +116 -6
- data/lib/irb/statement.rb +21 -0
- data/lib/irb/version.rb +2 -2
- data/lib/irb.rb +37 -894
- data/man/irb.1 +2 -0
- metadata +18 -4
- data/.document +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5167476c1b7e39f80080c2970d4974dac3a84a6721c027f0429ae9c793dfba92
|
4
|
+
data.tar.gz: 357002e006b01bbfd46dc36bae924c9b79adefc9b213b341fcc0f80134d0579c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: baeace22dd0cc116af8ddc4126fa6fee4b314008ffa56632547047501aefd6d8806e3d9a3518cd576422b8751ec285cb837c93b924a1f12c6230cd701c72f802
|
7
|
+
data.tar.gz: b9bce4b85a77cada09e1d4f06394aee7540cd307e9ce321a2ce5d609a3f6ee2f80dbcb1fce04374bddffb9f2a5370792a4b1498aa9479f8fd11d5340dd24da6a
|
data/Gemfile
CHANGED
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,310 +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
|
-
### Getting Started
|
174
|
-
|
175
|
-
- In `binding.irb`, use the `debug` command to start a `irb:rdbg` session with access to all `debug.gem` commands.
|
176
|
-
- Use `RUBY_DEBUG_IRB_CONSOLE=1` environment variable to make `debug.gem` use IRB as the debugging console.
|
177
|
-
|
178
|
-
### Details
|
179
|
-
|
180
|
-
Starting from version 1.8.0, IRB boasts a powerful integration with `debug.gem`, providing a debugging experience akin to `pry-byebug`.
|
181
|
-
|
182
|
-
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`.
|
183
|
-
|
184
|
-
```shell
|
185
|
-
From: test.rb @ line 3 :
|
186
|
-
|
187
|
-
1:
|
188
|
-
2: def greet(word)
|
189
|
-
=> 3: binding.irb
|
190
|
-
4: puts "Hello #{word}"
|
191
|
-
5: end
|
192
|
-
6:
|
193
|
-
7: greet("World")
|
194
|
-
|
195
|
-
irb(main):001> debug
|
196
|
-
irb:rdbg(main):002>
|
197
|
-
```
|
198
|
-
|
199
|
-
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):
|
200
|
-
|
201
|
-
```shell
|
202
|
-
irb:rdbg(main):002> info # use info command to see available variables
|
203
|
-
%self = main
|
204
|
-
_ = nil
|
205
|
-
word = "World"
|
206
|
-
irb:rdbg(main):003> next # use next command to move to the next line
|
207
|
-
[1, 7] in test.rb
|
208
|
-
1|
|
209
|
-
2| def greet(word)
|
210
|
-
3| binding.irb
|
211
|
-
=> 4| puts "Hello #{word}"
|
212
|
-
5| end
|
213
|
-
6|
|
214
|
-
7| greet("World")
|
215
|
-
=>#0 Object#greet(word="World") at test.rb:4
|
216
|
-
#1 <main> at test.rb:7
|
217
|
-
irb:rdbg(main):004>
|
218
|
-
```
|
219
|
-
|
220
|
-
Simultaneously, you maintain access to IRB's commands, such as `show_source`:
|
221
|
-
|
222
|
-
```shell
|
223
|
-
irb:rdbg(main):004> show_source greet
|
224
|
-
|
225
|
-
From: test.rb:2
|
226
|
-
|
227
|
-
def greet(word)
|
228
|
-
binding.irb
|
229
|
-
puts "Hello #{word}"
|
230
|
-
end
|
231
|
-
```
|
232
|
-
|
233
|
-
### More about `debug.gem`
|
89
|
+
### Debugging
|
234
90
|
|
235
|
-
|
91
|
+
You can use IRB as a debugging console with `debug.gem` with these options:
|
236
92
|
|
237
|
-
-
|
238
|
-
-
|
239
|
-
- Setting breakpoints with commands
|
240
|
-
- Thread control
|
241
|
-
- ...and many more
|
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.
|
242
95
|
|
243
|
-
To learn about
|
244
|
-
|
245
|
-
In the `irb:rdbg` session, the `help` command will also display all commands from `debug.gem`.
|
246
|
-
|
247
|
-
### Advantages Over `debug.gem`'s Console
|
248
|
-
|
249
|
-
This integration offers several benefits over `debug.gem`'s native console:
|
250
|
-
|
251
|
-
1. Access to handy IRB commands like `show_source` or `show_doc`.
|
252
|
-
2. Support for multi-line input.
|
253
|
-
3. Symbol shortcuts such as `@` (`whereami`) and `$` (`show_source`).
|
254
|
-
4. Autocompletion.
|
255
|
-
5. Customizable prompt.
|
256
|
-
|
257
|
-
However, there are also some limitations to be aware of:
|
258
|
-
|
259
|
-
1. `binding.irb` doesn't support `pre` and `do` arguments like [`binding.break`](https://github.com/ruby/debug#bindingbreak-method).
|
260
|
-
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.
|
261
|
-
3. Access to the previous return value via the underscore `_` is not supported.
|
262
|
-
|
263
|
-
## Type Based Completion
|
264
|
-
|
265
|
-
IRB's default completion `IRB::RegexpCompletor` uses Regexp. IRB has another experimental completion `IRB::TypeCompletor` that uses type analysis.
|
266
|
-
|
267
|
-
### How to Enable IRB::TypeCompletor
|
268
|
-
|
269
|
-
Install [ruby/repl_type_completor](https://github.com/ruby/repl_type_completor/) with:
|
270
|
-
```
|
271
|
-
$ gem install repl_type_completor
|
272
|
-
```
|
273
|
-
Or add these lines to your project's Gemfile.
|
274
|
-
```ruby
|
275
|
-
gem 'irb'
|
276
|
-
gem 'repl_type_completor', group: [:development, :test]
|
277
|
-
```
|
278
|
-
|
279
|
-
Now you can use type based completion by:
|
280
|
-
|
281
|
-
Running IRB with the `--type-completor` option
|
282
|
-
```
|
283
|
-
$ irb --type-completor
|
284
|
-
```
|
285
|
-
|
286
|
-
Or writing this line to IRB's rc-file (e.g. `~/.irbrc`)
|
287
|
-
```ruby
|
288
|
-
IRB.conf[:COMPLETOR] = :type # default is :regexp
|
289
|
-
```
|
96
|
+
To learn more about debugging with IRB, see [Debugging with IRB](https://ruby.github.io/irb/#label-Debugging+with+IRB).
|
290
97
|
|
291
|
-
|
292
|
-
```ruby
|
293
|
-
ENV['IRB_COMPLETOR'] = 'type'
|
294
|
-
IRB.start
|
295
|
-
```
|
296
|
-
|
297
|
-
To check if it's enabled, type `irb_info` into IRB and see the `Completion` section.
|
298
|
-
```
|
299
|
-
irb(main):001> irb_info
|
300
|
-
...
|
301
|
-
# Enabled
|
302
|
-
Completion: Autocomplete, ReplTypeCompletor: 0.1.0, Prism: 0.18.0, RBS: 3.3.0
|
303
|
-
# Not enabled
|
304
|
-
Completion: Autocomplete, RegexpCompletor
|
305
|
-
...
|
306
|
-
```
|
307
|
-
If you have `sig/` directory or `rbs_collection.lock.yaml` in current directory, IRB will load it.
|
308
|
-
|
309
|
-
### Advantage over Default IRB::RegexpCompletor
|
310
|
-
|
311
|
-
IRB::TypeCompletor can autocomplete chained methods, block parameters and more if type information is available.
|
312
|
-
These are some examples IRB::RegexpCompletor cannot complete.
|
313
|
-
|
314
|
-
```ruby
|
315
|
-
irb(main):001> 'Ruby'.upcase.chars.s # Array methods (sample, select, shift, size)
|
316
|
-
```
|
317
|
-
|
318
|
-
```ruby
|
319
|
-
irb(main):001> 10.times.map(&:to_s).each do |s|
|
320
|
-
irb(main):002> s.up # String methods (upcase, upcase!, upto)
|
321
|
-
```
|
322
|
-
|
323
|
-
```ruby
|
324
|
-
irb(main):001> class User < ApplicationRecord
|
325
|
-
irb(main):002> def foo
|
326
|
-
irb(main):003> sa # save, save!
|
327
|
-
```
|
328
|
-
|
329
|
-
As a trade-off, completion calculation takes more time than IRB::RegexpCompletor.
|
330
|
-
|
331
|
-
### Difference between Steep's Completion
|
332
|
-
|
333
|
-
Compared with Steep, IRB::TypeCompletor has some difference and limitations.
|
334
|
-
```ruby
|
335
|
-
[0, 'a'].sample.
|
336
|
-
# Steep completes intersection of Integer methods and String methods
|
337
|
-
# IRB::TypeCompletor completes both Integer and String methods
|
338
|
-
```
|
339
|
-
|
340
|
-
Some features like type narrowing is not implemented.
|
341
|
-
```ruby
|
342
|
-
def f(arg = [0, 'a'].sample)
|
343
|
-
if arg.is_a?(String)
|
344
|
-
arg. # Completes both Integer and String methods
|
345
|
-
```
|
98
|
+
## Documentation
|
346
99
|
|
347
|
-
|
348
|
-
```ruby
|
349
|
-
irb(main):001> a = [1]
|
350
|
-
=> [1]
|
351
|
-
irb(main):002> a.first. # Completes Integer methods
|
352
|
-
```
|
100
|
+
https://ruby.github.io/irb/ provides a comprehensive guide to IRB's features and usage.
|
353
101
|
|
354
102
|
## Configuration
|
355
103
|
|
356
|
-
|
357
|
-
|
358
|
-
- `NO_COLOR`: Assigning a value to it disables IRB's colorization.
|
359
|
-
- `IRB_USE_AUTOCOMPLETE`: Setting it to `false` disables IRB's autocompletion.
|
360
|
-
- `IRB_COMPLETOR`: Configures IRB's auto-completion behavior, allowing settings for either `regexp` or `type`.
|
361
|
-
- `VISUAL`: Its value would be used to open files by the `edit` command.
|
362
|
-
- `EDITOR`: Its value would be used to open files by the `edit` command if `VISUAL` is unset.
|
363
|
-
- `IRBRC`: The file specified would be evaluated as IRB's rc-file.
|
364
|
-
- `XDG_CONFIG_HOME`: If it is set and `IRBRC` is unset, the file `$XDG_CONFIG_HOME/irb/irbrc` would be evaluated as IRB's rc-file.
|
365
|
-
- `RI_PAGER`: The command specified would be used as a pager.
|
366
|
-
- `PAGER`: The command specified would be used as a pager if `RI_PAGER` is unset.
|
367
|
-
- `IRB_LANG`, `LC_MESSAGES`, `LC_ALL`, `LANG`: The first of these that is set would be used as the locale value.
|
368
|
-
|
369
|
-
## Documentation
|
370
|
-
|
371
|
-
https://ruby.github.io/irb/
|
104
|
+
See the [Configuration page](https://ruby.github.io/irb/Configurations_md.html) in the documentation.
|
372
105
|
|
373
106
|
## Extending IRB
|
374
107
|
|
375
108
|
IRB `v1.13.0` and later versions allows users/libraries to extend its functionality through official APIs.
|
376
109
|
|
377
|
-
For more information, please visit [
|
378
|
-
|
379
|
-
## Development
|
380
|
-
|
381
|
-
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.
|
382
|
-
|
383
|
-
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).
|
384
111
|
|
385
112
|
## Contributing
|
386
113
|
|
387
|
-
|
388
|
-
|
389
|
-
### Set up the environment
|
390
|
-
|
391
|
-
1. Fork the project to your GithHub account
|
392
|
-
2. Clone the fork with `git clone git@github.com:[your_username]/irb.git`
|
393
|
-
3. Run `bundle install`
|
394
|
-
4. Run `bundle exec rake` to make sure tests pass locally
|
395
|
-
|
396
|
-
### Run integration tests
|
397
|
-
|
398
|
-
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`.
|
399
|
-
|
400
|
-
Before running these tests, ensure that you have `libvterm` installed. If you're using Homebrew, you can install it by running:
|
401
|
-
|
402
|
-
```bash
|
403
|
-
brew install libvterm
|
404
|
-
```
|
405
|
-
|
406
|
-
After installing `libvterm`, you can run the integration tests using the following commands:
|
407
|
-
|
408
|
-
```bash
|
409
|
-
WITH_VTERM=1 bundle install
|
410
|
-
WITH_VTERM=1 bundle exec rake test test_yamatanooroti
|
411
|
-
```
|
114
|
+
See [CONTRIBUTING.md](./CONTRIBUTING.md) for more information.
|
412
115
|
|
413
116
|
## Releasing
|
414
117
|
|
data/Rakefile
CHANGED
@@ -45,8 +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.options.push("
|
48
|
+
rdoc.title = "IRB Documentation"
|
49
|
+
rdoc.main = "Index.md"
|
50
|
+
rdoc.rdoc_dir = "_site"
|
51
|
+
rdoc.options.push("lib")
|
52
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
|
@@ -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
|