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.
data/lib/irb.rb CHANGED
@@ -14,6 +14,7 @@ require_relative "irb/default_commands"
14
14
 
15
15
  require_relative "irb/ruby-lex"
16
16
  require_relative "irb/statement"
17
+ require_relative "irb/history"
17
18
  require_relative "irb/input-method"
18
19
  require_relative "irb/locale"
19
20
  require_relative "irb/color"
@@ -23,862 +24,10 @@ require_relative "irb/easter-egg"
23
24
  require_relative "irb/debug"
24
25
  require_relative "irb/pager"
25
26
 
26
- # ## IRB
27
- #
28
- # Module IRB ("Interactive Ruby") provides a shell-like interface that supports
29
- # user interaction with the Ruby interpreter.
30
- #
31
- # It operates as a *read-eval-print loop*
32
- # ([REPL](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop))
33
- # that:
34
- #
35
- # * ***Reads*** each character as you type. You can modify the IRB context to
36
- # change the way input works. See [Input](rdoc-ref:IRB@Input).
37
- # * ***Evaluates*** the code each time it has read a syntactically complete
38
- # passage.
39
- # * ***Prints*** after evaluating. You can modify the IRB context to change
40
- # the way output works. See [Output](rdoc-ref:IRB@Output).
41
- #
42
- #
43
- # Example:
44
- #
45
- # $ irb
46
- # irb(main):001> File.basename(Dir.pwd)
47
- # => "irb"
48
- # irb(main):002> Dir.entries('.').size
49
- # => 25
50
- # irb(main):003* Dir.entries('.').select do |entry|
51
- # irb(main):004* entry.start_with?('R')
52
- # irb(main):005> end
53
- # => ["README.md", "Rakefile"]
54
- #
55
- # The typed input may also include [\IRB-specific
56
- # commands](rdoc-ref:IRB@IRB-Specific+Commands).
57
- #
58
- # As seen above, you can start IRB by using the shell command `irb`.
59
- #
60
- # You can stop an IRB session by typing command `exit`:
61
- #
62
- # irb(main):006> exit
63
- # $
64
- #
65
- # At that point, IRB calls any hooks found in array `IRB.conf[:AT_EXIT]`, then
66
- # exits.
67
- #
68
- # ## Startup
69
- #
70
- # At startup, IRB:
71
- #
72
- # 1. Interprets (as Ruby code) the content of the [configuration
73
- # file](rdoc-ref:IRB@Configuration+File) (if given).
74
- # 2. Constructs the initial session context from [hash
75
- # IRB.conf](rdoc-ref:IRB@Hash+IRB.conf) and from default values; the hash
76
- # content may have been affected by [command-line
77
- # options](rdoc-ref:IB@Command-Line+Options), and by direct assignments in
78
- # the configuration file.
79
- # 3. Assigns the context to variable `conf`.
80
- # 4. Assigns command-line arguments to variable `ARGV`.
81
- # 5. Prints the [prompt](rdoc-ref:IRB@Prompt+and+Return+Formats).
82
- # 6. Puts the content of the [initialization
83
- # script](rdoc-ref:IRB@Initialization+Script) onto the IRB shell, just as if
84
- # it were user-typed commands.
85
- #
86
- #
87
- # ### The Command Line
88
- #
89
- # On the command line, all options precede all arguments; the first item that is
90
- # not recognized as an option is treated as an argument, as are all items that
91
- # follow.
92
- #
93
- # #### Command-Line Options
94
- #
95
- # Many command-line options affect entries in hash `IRB.conf`, which in turn
96
- # affect the initial configuration of the IRB session.
97
- #
98
- # Details of the options are described in the relevant subsections below.
99
- #
100
- # A cursory list of the IRB command-line options may be seen in the [help
101
- # message](https://raw.githubusercontent.com/ruby/irb/master/lib/irb/lc/help-message),
102
- # which is also displayed if you use command-line option `--help`.
103
- #
104
- # If you are interested in a specific option, consult the
105
- # [index](rdoc-ref:doc/irb/indexes.md@Index+of+Command-Line+Options).
106
- #
107
- # #### Command-Line Arguments
108
- #
109
- # Command-line arguments are passed to IRB in array `ARGV`:
110
- #
111
- # $ irb --noscript Foo Bar Baz
112
- # irb(main):001> ARGV
113
- # => ["Foo", "Bar", "Baz"]
114
- # irb(main):002> exit
115
- # $
116
- #
117
- # Command-line option `--` causes everything that follows to be treated as
118
- # arguments, even those that look like options:
119
- #
120
- # $ irb --noscript -- --noscript -- Foo Bar Baz
121
- # irb(main):001> ARGV
122
- # => ["--noscript", "--", "Foo", "Bar", "Baz"]
123
- # irb(main):002> exit
124
- # $
125
- #
126
- # ### Configuration File
127
- #
128
- # You can initialize IRB via a *configuration file*.
129
- #
130
- # If command-line option `-f` is given, no configuration file is looked for.
131
- #
132
- # Otherwise, IRB reads and interprets a configuration file if one is available.
133
- #
134
- # The configuration file can contain any Ruby code, and can usefully include
135
- # user code that:
136
- #
137
- # * Can then be debugged in IRB.
138
- # * Configures IRB itself.
139
- # * Requires or loads files.
140
- #
141
- #
142
- # The path to the configuration file is the first found among:
143
- #
144
- # * The value of variable `$IRBRC`, if defined.
145
- # * The value of variable `$XDG_CONFIG_HOME/irb/irbrc`, if defined.
146
- # * File `$HOME/.irbrc`, if it exists.
147
- # * File `$HOME/.config/irb/irbrc`, if it exists.
148
- # * File `.irbrc` in the current directory, if it exists.
149
- # * File `irb.rc` in the current directory, if it exists.
150
- # * File `_irbrc` in the current directory, if it exists.
151
- # * File `$irbrc` in the current directory, if it exists.
152
- #
153
- #
154
- # If the search fails, there is no configuration file.
155
- #
156
- # If the search succeeds, the configuration file is read as Ruby code, and so
157
- # can contain any Ruby programming you like.
158
- #
159
- # Method `conf.rc?` returns `true` if a configuration file was read, `false`
160
- # otherwise. Hash entry `IRB.conf[:RC]` also contains that value.
161
- #
162
- # ### Hash `IRB.conf`
163
- #
164
- # The initial entries in hash `IRB.conf` are determined by:
165
- #
166
- # * Default values.
167
- # * Command-line options, which may override defaults.
168
- # * Direct assignments in the configuration file.
169
- #
170
- #
171
- # You can see the hash by typing `IRB.conf`.
172
- #
173
- # Details of the entries' meanings are described in the relevant subsections
174
- # below.
175
- #
176
- # If you are interested in a specific entry, consult the
177
- # [index](rdoc-ref:doc/irb/indexes.md@Index+of+IRB.conf+Entries).
178
- #
179
- # ### Notes on Initialization Precedence
180
- #
181
- # * Any conflict between an entry in hash `IRB.conf` and a command-line option
182
- # is resolved in favor of the hash entry.
183
- # * Hash `IRB.conf` affects the context only once, when the configuration file
184
- # is interpreted; any subsequent changes to it do not affect the context and
185
- # are therefore essentially meaningless.
186
- #
187
- #
188
- # ### Initialization Script
189
- #
190
- # By default, the first command-line argument (after any options) is the path to
191
- # a Ruby initialization script.
192
- #
193
- # IRB reads the initialization script and puts its content onto the IRB shell,
194
- # just as if it were user-typed commands.
195
- #
196
- # Command-line option `--noscript` causes the first command-line argument to be
197
- # treated as an ordinary argument (instead of an initialization script);
198
- # `--script` is the default.
199
- #
200
- # ## Input
201
- #
202
- # This section describes the features that allow you to change the way IRB input
203
- # works; see also [Input and Output](rdoc-ref:IRB@Input+and+Output).
204
- #
205
- # ### Input Command History
206
- #
207
- # By default, IRB stores a history of up to 1000 input commands in a file named
208
- # `.irb_history`. The history file will be in the same directory as the
209
- # [configuration file](rdoc-ref:IRB@Configuration+File) if one is found, or in
210
- # `~/` otherwise.
211
- #
212
- # A new IRB session creates the history file if it does not exist, and appends
213
- # to the file if it does exist.
214
- #
215
- # You can change the filepath by adding to your configuration file:
216
- # `IRB.conf[:HISTORY_FILE] = *filepath*`, where *filepath* is a string filepath.
217
- #
218
- # During the session, method `conf.history_file` returns the filepath, and
219
- # method `conf.history_file = *new_filepath*` copies the history to the file at
220
- # *new_filepath*, which becomes the history file for the session.
221
- #
222
- # You can change the number of commands saved by adding to your configuration
223
- # file: `IRB.conf[:SAVE_HISTORY] = *n*`, wheHISTORY_FILEre *n* is one of:
224
- #
225
- # * Positive integer: the number of commands to be saved,
226
- # * Zero: all commands are to be saved.
227
- # * `nil`: no commands are to be saved,.
228
- #
229
- #
230
- # During the session, you can use methods `conf.save_history` or
231
- # `conf.save_history=` to retrieve or change the count.
232
- #
233
- # ### Command Aliases
234
- #
235
- # By default, IRB defines several command aliases:
236
- #
237
- # irb(main):001> conf.command_aliases
238
- # => {:"$"=>:show_source, :"@"=>:whereami}
239
- #
240
- # You can change the initial aliases in the configuration file with:
241
- #
242
- # IRB.conf[:COMMAND_ALIASES] = {foo: :show_source, bar: :whereami}
243
- #
244
- # You can replace the current aliases at any time with configuration method
245
- # `conf.command_aliases=`; Because `conf.command_aliases` is a hash, you can
246
- # modify it.
247
- #
248
- # ### End-of-File
249
- #
250
- # By default, `IRB.conf[:IGNORE_EOF]` is `false`, which means that typing the
251
- # end-of-file character `Ctrl-D` causes the session to exit.
252
- #
253
- # You can reverse that behavior by adding `IRB.conf[:IGNORE_EOF] = true` to the
254
- # configuration file.
255
- #
256
- # During the session, method `conf.ignore_eof?` returns the setting, and method
257
- # `conf.ignore_eof = *boolean*` sets it.
258
- #
259
- # ### SIGINT
260
- #
261
- # By default, `IRB.conf[:IGNORE_SIGINT]` is `true`, which means that typing the
262
- # interrupt character `Ctrl-C` causes the session to exit.
263
- #
264
- # You can reverse that behavior by adding `IRB.conf[:IGNORE_SIGING] = false` to
265
- # the configuration file.
266
- #
267
- # During the session, method `conf.ignore_siging?` returns the setting, and
268
- # method `conf.ignore_sigint = *boolean*` sets it.
269
- #
270
- # ### Automatic Completion
271
- #
272
- # By default, IRB enables [automatic
273
- # completion](https://en.wikipedia.org/wiki/Autocomplete#In_command-line_interpr
274
- # eters):
275
- #
276
- # You can disable it by either of these:
277
- #
278
- # * Adding `IRB.conf[:USE_AUTOCOMPLETE] = false` to the configuration file.
279
- # * Giving command-line option `--noautocomplete` (`--autocomplete` is the
280
- # default).
281
- #
282
- #
283
- # Method `conf.use_autocomplete?` returns `true` if automatic completion is
284
- # enabled, `false` otherwise.
285
- #
286
- # The setting may not be changed during the session.
287
- #
288
- # ### Automatic Indentation
289
- #
290
- # By default, IRB automatically indents lines of code to show structure (e.g.,
291
- # it indent the contents of a block).
292
- #
293
- # The current setting is returned by the configuration method
294
- # `conf.auto_indent_mode`.
295
- #
296
- # The default initial setting is `true`:
297
- #
298
- # irb(main):001> conf.auto_indent_mode
299
- # => true
300
- # irb(main):002* Dir.entries('.').select do |entry|
301
- # irb(main):003* entry.start_with?('R')
302
- # irb(main):004> end
303
- # => ["README.md", "Rakefile"]
304
- #
305
- # You can change the initial setting in the configuration file with:
306
- #
307
- # IRB.conf[:AUTO_INDENT] = false
308
- #
309
- # Note that the *current* setting *may not* be changed in the IRB session.
310
- #
311
- # ### Input Method
312
- #
313
- # The IRB input method determines how command input is to be read; by default,
314
- # the input method for a session is IRB::RelineInputMethod. Unless the
315
- # value of the TERM environment variable is 'dumb', in which case the
316
- # most simplistic input method is used.
317
- #
318
- # You can set the input method by:
319
- #
320
- # * Adding to the configuration file:
321
- #
322
- # * `IRB.conf[:USE_SINGLELINE] = true` or `IRB.conf[:USE_MULTILINE]=
323
- # false` sets the input method to IRB::ReadlineInputMethod.
324
- # * `IRB.conf[:USE_SINGLELINE] = false` or `IRB.conf[:USE_MULTILINE] =
325
- # true` sets the input method to IRB::RelineInputMethod.
326
- #
327
- #
328
- # * Giving command-line options:
329
- #
330
- # * `--singleline` or `--nomultiline` sets the input method to
331
- # IRB::ReadlineInputMethod.
332
- # * `--nosingleline` or `--multiline` sets the input method to
333
- # IRB::RelineInputMethod.
334
- # * `--nosingleline` together with `--nomultiline` sets the
335
- # input to IRB::StdioInputMethod.
336
- #
337
- #
338
- # Method `conf.use_multiline?` and its synonym `conf.use_reline` return:
339
- #
340
- # * `true` if option `--multiline` was given.
341
- # * `false` if option `--nomultiline` was given.
342
- # * `nil` if neither was given.
343
- #
344
- #
345
- # Method `conf.use_singleline?` and its synonym `conf.use_readline` return:
346
- #
347
- # * `true` if option `--singleline` was given.
348
- # * `false` if option `--nosingleline` was given.
349
- # * `nil` if neither was given.
350
- #
351
- #
352
- # ## Output
353
- #
354
- # This section describes the features that allow you to change the way IRB
355
- # output works; see also [Input and Output](rdoc-ref:IRB@Input+and+Output).
356
- #
357
- # ### Return-Value Printing (Echoing)
358
- #
359
- # By default, IRB prints (echoes) the values returned by all input commands.
360
- #
361
- # You can change the initial behavior and suppress all echoing by:
362
- #
363
- # * Adding to the configuration file: `IRB.conf[:ECHO] = false`. (The default
364
- # value for this entry is `nil`, which means the same as `true`.)
365
- # * Giving command-line option `--noecho`. (The default is `--echo`.)
366
- #
367
- #
368
- # During the session, you can change the current setting with configuration
369
- # method `conf.echo=` (set to `true` or `false`).
370
- #
371
- # As stated above, by default IRB prints the values returned by all input
372
- # commands; but IRB offers special treatment for values returned by assignment
373
- # statements, which may be:
374
- #
375
- # * Printed with truncation (to fit on a single line of output), which is the
376
- # default; an ellipsis (`...` is suffixed, to indicate the truncation):
377
- #
378
- # irb(main):001> x = 'abc' * 100
379
- #
380
- #
381
- # > "abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc...
382
- #
383
- # * Printed in full (regardless of the length).
384
- # * Suppressed (not printed at all)
385
- #
386
- #
387
- # You can change the initial behavior by:
388
- #
389
- # * Adding to the configuration file: `IRB.conf[:ECHO_ON_ASSIGNMENT] = false`.
390
- # (The default value for this entry is `niL`, which means the same as
391
- # `:truncate`.)
392
- # * Giving command-line option `--noecho-on-assignment` or
393
- # `--echo-on-assignment`. (The default is `--truncate-echo-on-assignment`.)
394
- #
395
- #
396
- # During the session, you can change the current setting with configuration
397
- # method `conf.echo_on_assignment=` (set to `true`, `false`, or `:truncate`).
398
- #
399
- # By default, IRB formats returned values by calling method `inspect`.
400
- #
401
- # You can change the initial behavior by:
402
- #
403
- # * Adding to the configuration file: `IRB.conf[:INSPECT_MODE] = false`. (The
404
- # default value for this entry is `true`.)
405
- # * Giving command-line option `--noinspect`. (The default is `--inspect`.)
406
- #
407
- #
408
- # During the session, you can change the setting using method
409
- # `conf.inspect_mode=`.
410
- #
411
- # ### Multiline Output
412
- #
413
- # By default, IRB prefixes a newline to a multiline response.
414
- #
415
- # You can change the initial default value by adding to the configuration file:
416
- #
417
- # IRB.conf[:NEWLINE_BEFORE_MULTILINE_OUTPUT] = false
418
- #
419
- # During a session, you can retrieve or set the value using methods
420
- # `conf.newline_before_multiline_output?` and
421
- # `conf.newline_before_multiline_output=`.
422
- #
423
- # Examples:
424
- #
425
- # irb(main):001> conf.inspect_mode = false
426
- # => false
427
- # irb(main):002> "foo\nbar"
428
- # =>
429
- # foo
430
- # bar
431
- # irb(main):003> conf.newline_before_multiline_output = false
432
- # => false
433
- # irb(main):004> "foo\nbar"
434
- # => foo
435
- # bar
436
- #
437
- # ### Evaluation History
438
- #
439
- # By default, IRB saves no history of evaluations (returned values), and the
440
- # related methods `conf.eval_history`, `_`, and `__` are undefined.
441
- #
442
- # You can turn on that history, and set the maximum number of evaluations to be
443
- # stored:
444
- #
445
- # * In the configuration file: add `IRB.conf[:EVAL_HISTORY] = *n*`. (Examples
446
- # below assume that we've added `IRB.conf[:EVAL_HISTORY] = 5`.)
447
- # * In the session (at any time): `conf.eval_history = *n*`.
448
- #
449
- #
450
- # If `n` is zero, all evaluation history is stored.
451
- #
452
- # Doing either of the above:
453
- #
454
- # * Sets the maximum size of the evaluation history; defines method
455
- # `conf.eval_history`, which returns the maximum size `n` of the evaluation
456
- # history:
457
- #
458
- # irb(main):001> conf.eval_history = 5
459
- # => 5
460
- # irb(main):002> conf.eval_history
461
- # => 5
462
- #
463
- # * Defines variable `_`, which contains the most recent evaluation, or `nil`
464
- # if none; same as method `conf.last_value`:
465
- #
466
- # irb(main):003> _
467
- # => 5
468
- # irb(main):004> :foo
469
- # => :foo
470
- # irb(main):005> :bar
471
- # => :bar
472
- # irb(main):006> _
473
- # => :bar
474
- # irb(main):007> _
475
- # => :bar
476
- #
477
- # * Defines variable `__`:
478
- #
479
- # * `__` unadorned: contains all evaluation history:
480
- #
481
- # irb(main):008> :foo
482
- # => :foo
483
- # irb(main):009> :bar
484
- # => :bar
485
- # irb(main):010> :baz
486
- # => :baz
487
- # irb(main):011> :bat
488
- # => :bat
489
- # irb(main):012> :bam
490
- # => :bam
491
- # irb(main):013> __
492
- # =>
493
- # 9 :bar
494
- # 10 :baz
495
- # 11 :bat
496
- # 12 :bam
497
- # irb(main):014> __
498
- # =>
499
- # 10 :baz
500
- # 11 :bat
501
- # 12 :bam
502
- # 13 ...self-history...
503
- #
504
- # Note that when the evaluation is multiline, it is displayed
505
- # differently.
506
- #
507
- # * `__[`*m*`]`:
508
- #
509
- # * Positive *m*: contains the evaluation for the given line number,
510
- # or `nil` if that line number is not in the evaluation history:
511
- #
512
- # irb(main):015> __[12]
513
- # => :bam
514
- # irb(main):016> __[1]
515
- # => nil
516
- #
517
- # * Negative *m*: contains the `mth`-from-end evaluation, or `nil` if
518
- # that evaluation is not in the evaluation history:
519
- #
520
- # irb(main):017> __[-3]
521
- # => :bam
522
- # irb(main):018> __[-13]
523
- # => nil
524
- #
525
- # * Zero *m*: contains `nil`:
526
- #
527
- # irb(main):019> __[0]
528
- # => nil
529
- #
530
- #
531
- #
532
- #
533
- # ### Prompt and Return Formats
534
- #
535
- # By default, IRB uses the prompt and return value formats defined in its
536
- # `:DEFAULT` prompt mode.
537
- #
538
- # #### The Default Prompt and Return Format
539
- #
540
- # The default prompt and return values look like this:
541
- #
542
- # irb(main):001> 1 + 1
543
- # => 2
544
- # irb(main):002> 2 + 2
545
- # => 4
546
- #
547
- # The prompt includes:
548
- #
549
- # * The name of the running program (`irb`); see [IRB
550
- # Name](rdoc-ref:IRB@IRB+Name).
551
- # * The name of the current session (`main`); See [IRB
552
- # Sessions](rdoc-ref:IRB@IRB+Sessions).
553
- # * A 3-digit line number (1-based).
554
- #
555
- #
556
- # The default prompt actually defines three formats:
557
- #
558
- # * One for most situations (as above):
559
- #
560
- # irb(main):003> Dir
561
- # => Dir
562
- #
563
- # * One for when the typed command is a statement continuation (adds trailing
564
- # asterisk):
565
- #
566
- # irb(main):004* Dir.
567
- #
568
- # * One for when the typed command is a string continuation (adds trailing
569
- # single-quote):
570
- #
571
- # irb(main):005' Dir.entries('.
572
- #
573
- #
574
- # You can see the prompt change as you type the characters in the following:
575
- #
576
- # irb(main):001* Dir.entries('.').select do |entry|
577
- # irb(main):002* entry.start_with?('R')
578
- # irb(main):003> end
579
- # => ["README.md", "Rakefile"]
580
- #
581
- # #### Pre-Defined Prompts
582
- #
583
- # IRB has several pre-defined prompts, stored in hash `IRB.conf[:PROMPT]`:
584
- #
585
- # irb(main):001> IRB.conf[:PROMPT].keys
586
- # => [:NULL, :DEFAULT, :CLASSIC, :SIMPLE, :INF_RUBY, :XMP]
587
- #
588
- # To see the full data for these, type `IRB.conf[:PROMPT]`.
589
- #
590
- # Most of these prompt definitions include specifiers that represent values like
591
- # the IRB name, session name, and line number; see [Prompt
592
- # Specifiers](rdoc-ref:IRB@Prompt+Specifiers).
593
- #
594
- # You can change the initial prompt and return format by:
595
- #
596
- # * Adding to the configuration file: `IRB.conf[:PROMPT] = *mode*` where
597
- # *mode* is the symbol name of a prompt mode.
598
- # * Giving a command-line option:
599
- #
600
- # * `--prompt *mode*`: sets the prompt mode to *mode*. where *mode* is the
601
- # symbol name of a prompt mode.
602
- # * `--simple-prompt` or `--sample-book-mode`: sets the prompt mode to
603
- # `:SIMPLE`.
604
- # * `--inf-ruby-mode`: sets the prompt mode to `:INF_RUBY` and suppresses
605
- # both `--multiline` and `--singleline`.
606
- # * `--noprompt`: suppresses prompting; does not affect echoing.
607
- #
608
- #
609
- #
610
- # You can retrieve or set the current prompt mode with methods
611
- #
612
- # `conf.prompt_mode` and `conf.prompt_mode=`.
613
- #
614
- # If you're interested in prompts and return formats other than the defaults,
615
- # you might experiment by trying some of the others.
616
- #
617
- # #### Custom Prompts
618
- #
619
- # You can also define custom prompts and return formats, which may be done
620
- # either in an IRB session or in the configuration file.
621
- #
622
- # A prompt in IRB actually defines three prompts, as seen above. For simple
623
- # custom data, we'll make all three the same:
624
- #
625
- # irb(main):001* IRB.conf[:PROMPT][:MY_PROMPT] = {
626
- # irb(main):002* PROMPT_I: ': ',
627
- # irb(main):003* PROMPT_C: ': ',
628
- # irb(main):004* PROMPT_S: ': ',
629
- # irb(main):005* RETURN: '=> '
630
- # irb(main):006> }
631
- # => {:PROMPT_I=>": ", :PROMPT_C=>": ", :PROMPT_S=>": ", :RETURN=>"=> "}
632
- #
633
- # If you define the custom prompt in the configuration file, you can also make
634
- # it the current prompt by adding:
635
- #
636
- # IRB.conf[:PROMPT_MODE] = :MY_PROMPT
637
- #
638
- # Regardless of where it's defined, you can make it the current prompt in a
639
- # session:
640
- #
641
- # conf.prompt_mode = :MY_PROMPT
642
- #
643
- # You can view or modify the current prompt data with various configuration
644
- # methods:
645
- #
646
- # * `conf.prompt_mode`, `conf.prompt_mode=`.
647
- # * `conf.prompt_c`, `conf.c=`.
648
- # * `conf.prompt_i`, `conf.i=`.
649
- # * `conf.prompt_s`, `conf.s=`.
650
- # * `conf.return_format`, `return_format=`.
651
- #
652
- #
653
- # #### Prompt Specifiers
654
- #
655
- # A prompt's definition can include specifiers for which certain values are
656
- # substituted:
657
- #
658
- # * `%N`: the name of the running program.
659
- # * `%m`: the value of `self.to_s`.
660
- # * `%M`: the value of `self.inspect`.
661
- # * `%l`: an indication of the type of string; one of `"`, `'`, `/`, `]`.
662
- # * `%NNi`: Indentation level. NN is a 2-digit number that specifies the number
663
- # of digits of the indentation level (03 will result in 001).
664
- # * `%NNn`: Line number. NN is a 2-digit number that specifies the number
665
- # of digits of the line number (03 will result in 001).
666
- # * `%%`: Literal `%`.
667
- #
668
- #
669
- # ### Verbosity
670
- #
671
- # By default, IRB verbosity is disabled, which means that output is smaller
672
- # rather than larger.
673
- #
674
- # You can enable verbosity by:
675
- #
676
- # * Adding to the configuration file: `IRB.conf[:VERBOSE] = true` (the default
677
- # is `nil`).
678
- # * Giving command-line options `--verbose` (the default is `--noverbose`).
679
- #
680
- #
681
- # During a session, you can retrieve or set verbosity with methods
682
- # `conf.verbose` and `conf.verbose=`.
683
- #
684
- # ### Help
685
- #
686
- # Command-line option `--version` causes IRB to print its help text and exit.
687
- #
688
- # ### Version
689
- #
690
- # Command-line option `--version` causes IRB to print its version text and exit.
691
- #
692
- # ## Input and Output
693
- #
694
- # ### Color Highlighting
695
- #
696
- # By default, IRB color highlighting is enabled, and is used for both:
697
- #
698
- # * Input: As you type, IRB reads the typed characters and highlights elements
699
- # that it recognizes; it also highlights errors such as mismatched
700
- # parentheses.
701
- # * Output: IRB highlights syntactical elements.
702
- #
703
- #
704
- # You can disable color highlighting by:
705
- #
706
- # * Adding to the configuration file: `IRB.conf[:USE_COLORIZE] = false` (the
707
- # default value is `true`).
708
- # * Giving command-line option `--nocolorize`
709
- #
710
- #
711
- # ## Debugging
712
- #
713
- # Command-line option `-d` sets variables `$VERBOSE` and `$DEBUG` to `true`;
714
- # these have no effect on IRB output.
715
- #
716
- # ### Warnings
717
- #
718
- # Command-line option `-w` suppresses warnings.
719
- #
720
- # Command-line option `-W[*level*]` sets warning level;
721
- #
722
- # * 0=silence
723
- # * 1=medium
724
- # * 2=verbose
725
- #
726
- # ## Other Features
727
- #
728
- # ### Load Modules
729
- #
730
- # You can specify the names of modules that are to be required at startup.
731
- #
732
- # Array `conf.load_modules` determines the modules (if any) that are to be
733
- # required during session startup. The array is used only during session
734
- # startup, so the initial value is the only one that counts.
735
- #
736
- # The default initial value is `[]` (load no modules):
737
- #
738
- # irb(main):001> conf.load_modules
739
- # => []
740
- #
741
- # You can set the default initial value via:
742
- #
743
- # * Command-line option `-r`
744
- #
745
- # $ irb -r csv -r json
746
- # irb(main):001> conf.load_modules
747
- # => ["csv", "json"]
748
- #
749
- # * Hash entry `IRB.conf[:LOAD_MODULES] = *array*`:
750
- #
751
- # IRB.conf[:LOAD_MODULES] = %w[csv, json]
752
- #
753
- #
754
- # Note that the configuration file entry overrides the command-line options.
755
- #
756
- # ### RI Documentation Directories
757
- #
758
- # You can specify the paths to RI documentation directories that are to be
759
- # loaded (in addition to the default directories) at startup; see details about
760
- # RI by typing `ri --help`.
761
- #
762
- # Array `conf.extra_doc_dirs` determines the directories (if any) that are to be
763
- # loaded during session startup. The array is used only during session startup,
764
- # so the initial value is the only one that counts.
765
- #
766
- # The default initial value is `[]` (load no extra documentation):
767
- #
768
- # irb(main):001> conf.extra_doc_dirs
769
- # => []
770
- #
771
- # You can set the default initial value via:
772
- #
773
- # * Command-line option `--extra_doc_dir`
774
- #
775
- # $ irb --extra-doc-dir your_doc_dir --extra-doc-dir my_doc_dir
776
- # irb(main):001> conf.extra_doc_dirs
777
- # => ["your_doc_dir", "my_doc_dir"]
778
- #
779
- # * Hash entry `IRB.conf[:EXTRA_DOC_DIRS] = *array*`:
780
- #
781
- # IRB.conf[:EXTRA_DOC_DIRS] = %w[your_doc_dir my_doc_dir]
782
- #
783
- #
784
- # Note that the configuration file entry overrides the command-line options.
785
- #
786
- # ### IRB Name
787
- #
788
- # You can specify a name for IRB.
789
- #
790
- # The default initial value is `'irb'`:
791
- #
792
- # irb(main):001> conf.irb_name
793
- # => "irb"
794
- #
795
- # You can set the default initial value via hash entry `IRB.conf[:IRB_NAME] =
796
- # *string*`:
797
- #
798
- # IRB.conf[:IRB_NAME] = 'foo'
799
- #
800
- # ### Application Name
801
- #
802
- # You can specify an application name for the IRB session.
803
- #
804
- # The default initial value is `'irb'`:
805
- #
806
- # irb(main):001> conf.ap_name
807
- # => "irb"
808
- #
809
- # You can set the default initial value via hash entry `IRB.conf[:AP_NAME] =
810
- # *string*`:
811
- #
812
- # IRB.conf[:AP_NAME] = 'my_ap_name'
813
- #
814
- # ### Configuration Monitor
815
- #
816
- # You can monitor changes to the configuration by assigning a proc to
817
- # `IRB.conf[:IRB_RC]` in the configuration file:
818
- #
819
- # IRB.conf[:IRB_RC] = proc {|conf| puts conf.class }
820
- #
821
- # Each time the configuration is changed, that proc is called with argument
822
- # `conf`:
823
- #
824
- # ### Encodings
825
- #
826
- # Command-line option `-E *ex*[:*in*]` sets initial external (ex) and internal
827
- # (in) encodings.
828
- #
829
- # Command-line option `-U` sets both to UTF-8.
830
- #
831
- # ### Commands
832
- #
833
- # Please use the `help` command to see the list of available commands.
834
- #
835
- # ### IRB Sessions
836
- #
837
- # IRB has a special feature, that allows you to manage many sessions at once.
838
- #
839
- # You can create new sessions with Irb.irb, and get a list of current sessions
840
- # with the `jobs` command in the prompt.
841
- #
842
- # #### Configuration
843
- #
844
- # The command line options, or IRB.conf, specify the default behavior of
845
- # Irb.irb.
846
- #
847
- # On the other hand, each conf in IRB@Command-Line+Options is used to
848
- # individually configure IRB.irb.
849
- #
850
- # If a proc is set for `IRB.conf[:IRB_RC]`, its will be invoked after execution
851
- # of that proc with the context of the current session as its argument. Each
852
- # session can be configured using this mechanism.
853
- #
854
- # #### Session variables
855
- #
856
- # There are a few variables in every Irb session that can come in handy:
857
- #
858
- # `_`
859
- # : The value command executed, as a local variable
860
- # `__`
861
- # : The history of evaluated commands. Available only if
862
- # `IRB.conf[:EVAL_HISTORY]` is not `nil` (which is the default). See also
863
- # IRB::Context#eval_history= and IRB::History.
864
- # `__[line_no]`
865
- # : Returns the evaluation value at the given line number, `line_no`. If
866
- # `line_no` is a negative, the return value `line_no` many lines before the
867
- # most recent return value.
868
- #
869
- #
870
- # ## Restrictions
871
- #
872
- # Ruby code typed into IRB behaves the same as Ruby code in a file, except that:
873
- #
874
- # * Because IRB evaluates input immediately after it is syntactically
875
- # complete, some results may be slightly different.
876
- # * Forking may not be well behaved.
877
- #
878
27
  module IRB
879
28
 
880
29
  # An exception raised by IRB.irb_abort
881
- class Abort < Exception;end
30
+ class Abort < Exception;end # :nodoc:
882
31
 
883
32
  class << self
884
33
  # The current IRB::Context of the session, see IRB.conf
@@ -972,7 +121,7 @@ module IRB
972
121
  # debugger.
973
122
  input = nil
974
123
  forced_exit = catch(:IRB_EXIT) do
975
- if IRB.conf[:SAVE_HISTORY] && context.io.support_history_saving?
124
+ if History.save_history? && context.io.support_history_saving?
976
125
  # Previous IRB session's history has been saved when `Irb#run` is exited We need
977
126
  # to make sure the saved history is not saved again by resetting the counter
978
127
  context.io.reset_history_counter
@@ -1003,9 +152,10 @@ module IRB
1003
152
  prev_context = conf[:MAIN_CONTEXT]
1004
153
  conf[:MAIN_CONTEXT] = context
1005
154
 
1006
- save_history = !in_nested_session && conf[:SAVE_HISTORY] && context.io.support_history_saving?
155
+ load_history = !in_nested_session && context.io.support_history_saving?
156
+ save_history = load_history && History.save_history?
1007
157
 
1008
- if save_history
158
+ if load_history
1009
159
  context.io.load_history
1010
160
  end
1011
161
 
@@ -1119,29 +269,25 @@ module IRB
1119
269
  loop do
1120
270
  code = readmultiline
1121
271
  break unless code
1122
- yield build_statement(code), @line_no
272
+ yield parse_input(code), @line_no
1123
273
  @line_no += code.count("\n")
1124
274
  rescue RubyLex::TerminateLineInput
1125
275
  end
1126
276
  end
1127
277
 
1128
- def build_statement(code)
278
+ def parse_input(code)
1129
279
  if code.match?(/\A\n*\z/)
1130
280
  return Statement::EmptyInput.new
1131
281
  end
1132
282
 
1133
- code.force_encoding(@context.io.encoding)
1134
- if (command, arg = @context.parse_command(code))
1135
- command_class = Command.load_command(command)
1136
- Statement::Command.new(code, command_class, arg)
1137
- else
1138
- is_assignment_expression = @scanner.assignment_expression?(code, local_variables: @context.local_variables)
1139
- Statement::Expression.new(code, is_assignment_expression)
1140
- end
283
+ code = code.dup.force_encoding(@context.io.encoding)
284
+ is_assignment_expression = @scanner.assignment_expression?(code, local_variables: @context.local_variables)
285
+
286
+ @context.parse_input(code, is_assignment_expression)
1141
287
  end
1142
288
 
1143
289
  def command?(code)
1144
- !!@context.parse_command(code)
290
+ parse_input(code).is_a?(Statement::Command)
1145
291
  end
1146
292
 
1147
293
  def configure_io
@@ -1280,7 +426,10 @@ module IRB
1280
426
  # The "<top (required)>" in "(irb)" may be the top level of IRB so imitate the main object.
1281
427
  message = message.gsub(/\(irb\):(?<num>\d+):in (?<open_quote>[`'])<(?<frame>top \(required\))>'/) { "(irb):#{$~[:num]}:in #{$~[:open_quote]}<main>'" }
1282
428
  puts message
1283
- puts 'Maybe IRB bug!' if irb_bug
429
+
430
+ if irb_bug
431
+ puts "This may be an issue with IRB. If you believe this is an unexpected behavior, please report it to https://github.com/ruby/irb/issues"
432
+ end
1284
433
  rescue Exception => handler_exc
1285
434
  begin
1286
435
  puts exc.inspect
@@ -1367,40 +516,36 @@ module IRB
1367
516
  end
1368
517
 
1369
518
  def output_value(omit = false) # :nodoc:
1370
- str = @context.inspect_last_value
1371
- multiline_p = str.include?("\n")
1372
- if omit
1373
- winwidth = @context.io.winsize.last
1374
- if multiline_p
1375
- first_line = str.split("\n").first
1376
- result = @context.newline_before_multiline_output? ? (@context.return_format % first_line) : first_line
1377
- output_width = Reline::Unicode.calculate_width(result, true)
1378
- diff_size = output_width - Reline::Unicode.calculate_width(first_line, true)
1379
- if diff_size.positive? and output_width > winwidth
1380
- lines, _ = Reline::Unicode.split_by_width(first_line, winwidth - diff_size - 3)
1381
- str = "%s..." % lines.first
1382
- str += "\e[0m" if Color.colorable?
1383
- multiline_p = false
1384
- else
1385
- str = str.gsub(/(\A.*?\n).*/m, "\\1...")
1386
- str += "\e[0m" if Color.colorable?
1387
- end
1388
- else
1389
- output_width = Reline::Unicode.calculate_width(@context.return_format % str, true)
1390
- diff_size = output_width - Reline::Unicode.calculate_width(str, true)
1391
- if diff_size.positive? and output_width > winwidth
1392
- lines, _ = Reline::Unicode.split_by_width(str, winwidth - diff_size - 3)
1393
- str = "%s..." % lines.first
1394
- str += "\e[0m" if Color.colorable?
1395
- end
1396
- end
519
+ unless @context.return_format.include?('%')
520
+ puts @context.return_format
521
+ return
1397
522
  end
1398
523
 
1399
- if multiline_p && @context.newline_before_multiline_output?
1400
- str = "\n" + str
524
+ winheight, winwidth = @context.io.winsize
525
+ if omit
526
+ content, overflow = Pager.take_first_page(winwidth, 1) do |out|
527
+ @context.inspect_last_value(out)
528
+ end
529
+ if overflow
530
+ content = "\n#{content}" if @context.newline_before_multiline_output?
531
+ content = "#{content}..."
532
+ content = "#{content}\e[0m" if Color.colorable?
533
+ end
534
+ puts format(@context.return_format, content.chomp)
535
+ elsif Pager.should_page? && @context.inspector_support_stream_output?
536
+ formatter_proc = ->(content, multipage) do
537
+ content = content.chomp
538
+ content = "\n#{content}" if @context.newline_before_multiline_output? && (multipage || content.include?("\n"))
539
+ format(@context.return_format, content)
540
+ end
541
+ Pager.page_with_preview(winwidth, winheight, formatter_proc) do |out|
542
+ @context.inspect_last_value(out)
543
+ end
544
+ else
545
+ content = @context.inspect_last_value.chomp
546
+ content = "\n#{content}" if @context.newline_before_multiline_output? && content.include?("\n")
547
+ Pager.page_content(format(@context.return_format, content), retain_content: true)
1401
548
  end
1402
-
1403
- Pager.page_content(format(@context.return_format, str), retain_content: true)
1404
549
  end
1405
550
 
1406
551
  # Outputs the local variables to this current session, including #signal_status
@@ -1467,10 +612,10 @@ module IRB
1467
612
  when "N"
1468
613
  @context.irb_name
1469
614
  when "m"
1470
- main_str = @context.main.to_s rescue "!#{$!.class}"
615
+ main_str = @context.safe_method_call_on_main(:to_s) rescue "!#{$!.class}"
1471
616
  truncate_prompt_main(main_str)
1472
617
  when "M"
1473
- main_str = @context.main.inspect rescue "!#{$!.class}"
618
+ main_str = @context.safe_method_call_on_main(:inspect) rescue "!#{$!.class}"
1474
619
  truncate_prompt_main(main_str)
1475
620
  when "l"
1476
621
  ltype