rb-readline 0.4.2 → 0.5.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.
- data/CHANGES +25 -0
- data/{README → README.rdoc} +8 -1
- data/Rakefile +5 -5
- data/examples/tinyirb.rb +11 -11
- data/lib/rb-readline.rb +16 -0
- data/lib/rbreadline.rb +8863 -8703
- data/lib/readline.rb +524 -520
- data/test/test_completion.rb +0 -2
- data/test/test_filename_completion_proc.rb +19 -1
- data/test/test_history.rb +29 -0
- data/test/test_rbreadline.rb +19 -5
- data/test/test_readline.rb +193 -152
- metadata +41 -58
data/lib/readline.rb
CHANGED
@@ -1,520 +1,524 @@
|
|
1
|
-
#
|
2
|
-
#
|
3
|
-
#
|
4
|
-
#
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
end
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
end
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
1
|
+
# encoding: US-ASCII
|
2
|
+
#
|
3
|
+
# readline.rb -- GNU Readline module
|
4
|
+
# Copyright (C) 1997-2001 Shugo Maeda
|
5
|
+
#
|
6
|
+
# Ruby translation by Park Heesob phasis@gmail.com
|
7
|
+
|
8
|
+
module Readline
|
9
|
+
|
10
|
+
require 'rbreadline'
|
11
|
+
include RbReadline
|
12
|
+
|
13
|
+
@completion_proc = nil
|
14
|
+
@completion_case_fold = false
|
15
|
+
|
16
|
+
# Begins an interactive terminal process using +prompt+ as the command
|
17
|
+
# prompt that users see when they type commands. The method returns the
|
18
|
+
# line entered whenever a carriage return is encountered.
|
19
|
+
#
|
20
|
+
# If an +add_history+ argument is provided, commands entered by users are
|
21
|
+
# stored in a history buffer that can be recalled for later use.
|
22
|
+
#
|
23
|
+
# Note that this method depends on $stdin and $stdout both being open.
|
24
|
+
# Because this is meant as an interactive console interface, they should
|
25
|
+
# generally not be redirected.
|
26
|
+
#
|
27
|
+
# Example:
|
28
|
+
#
|
29
|
+
# loop{ Readline.readline('> ') }
|
30
|
+
#
|
31
|
+
def readline(prompt, add_history=nil)
|
32
|
+
if $stdin.closed?
|
33
|
+
raise IOError, "stdin closed"
|
34
|
+
end
|
35
|
+
|
36
|
+
RbReadline.rl_instream = $stdin
|
37
|
+
RbReadline.rl_outstream = $stdout
|
38
|
+
|
39
|
+
begin
|
40
|
+
buff = RbReadline.readline(prompt)
|
41
|
+
rescue Exception => e
|
42
|
+
buff = nil
|
43
|
+
RbReadline.rl_cleanup_after_signal()
|
44
|
+
RbReadline.rl_deprep_terminal()
|
45
|
+
raise e
|
46
|
+
end
|
47
|
+
|
48
|
+
if add_history && buff
|
49
|
+
RbReadline.add_history(buff)
|
50
|
+
end
|
51
|
+
|
52
|
+
return buff ? buff.dup : nil
|
53
|
+
end
|
54
|
+
|
55
|
+
# Sets the input stream (an IO object) for readline interaction. The
|
56
|
+
# default is <tt>$stdin</tt>.
|
57
|
+
#
|
58
|
+
def self.input=(input)
|
59
|
+
RbReadline.rl_instream = input
|
60
|
+
end
|
61
|
+
|
62
|
+
# Sets the output stream (an IO object) for readline interaction. The
|
63
|
+
# default is <tt>$stdout</tt>.
|
64
|
+
#
|
65
|
+
def self.output=(output)
|
66
|
+
RbReadline.rl_outstream = output
|
67
|
+
end
|
68
|
+
|
69
|
+
# Returns current line buffer
|
70
|
+
#
|
71
|
+
def self.line_buffer
|
72
|
+
RbReadline.rl_line_buffer
|
73
|
+
end
|
74
|
+
|
75
|
+
# Sets the auto-completion procedure (i.e. tab auto-complete).
|
76
|
+
#
|
77
|
+
# The +proc+ argument is typically a Proc object. It must respond to
|
78
|
+
# <tt>.call</tt>, take a single String argument and return an Array of
|
79
|
+
# candidates for completion.
|
80
|
+
#
|
81
|
+
# Example:
|
82
|
+
#
|
83
|
+
# list = ['search', 'next', 'clear']
|
84
|
+
# Readline.completion_proc = proc{ |s| list.grep( /^#{Regexp.escape(s)}/) }
|
85
|
+
#
|
86
|
+
def self.completion_proc=(proc)
|
87
|
+
unless defined? proc.call
|
88
|
+
raise ArgumentError,"argument must respond to `call'"
|
89
|
+
end
|
90
|
+
@completion_proc = proc
|
91
|
+
end
|
92
|
+
|
93
|
+
# Returns the current auto-completion procedure.
|
94
|
+
#
|
95
|
+
def self.completion_proc()
|
96
|
+
@completion_proc
|
97
|
+
end
|
98
|
+
|
99
|
+
# Sets whether or not the completion proc should ignore case sensitivity.
|
100
|
+
# The default is false, i.e. completion procs are case sensitive.
|
101
|
+
#
|
102
|
+
def self.completion_case_fold=(bool)
|
103
|
+
@completion_case_fold = bool
|
104
|
+
end
|
105
|
+
|
106
|
+
# Returns whether or not the completion proc is case sensitive. The
|
107
|
+
# default is false, i.e. completion procs are case sensitive.
|
108
|
+
#
|
109
|
+
def self.completion_case_fold()
|
110
|
+
@completion_case_fold
|
111
|
+
end
|
112
|
+
|
113
|
+
# Returns nil if no matches are found or an array of strings:
|
114
|
+
#
|
115
|
+
# [0] is the replacement for text
|
116
|
+
# [1..n] the possible matches
|
117
|
+
# [n+1] nil
|
118
|
+
#
|
119
|
+
# The possible matches should not include [0].
|
120
|
+
#
|
121
|
+
# If this method sets RbReadline.rl_attempted_completion_over to true,
|
122
|
+
# then the default completion function will not be called when this
|
123
|
+
# function returns nil.
|
124
|
+
def self.readline_attempted_completion_function(text,start,_end)
|
125
|
+
proc = @completion_proc
|
126
|
+
return nil if proc.nil?
|
127
|
+
|
128
|
+
RbReadline.rl_attempted_completion_over = true
|
129
|
+
|
130
|
+
case_fold = @completion_case_fold
|
131
|
+
ary = proc.call(text)
|
132
|
+
if ary.class != Array
|
133
|
+
ary = Array(ary)
|
134
|
+
else
|
135
|
+
ary.compact!
|
136
|
+
end
|
137
|
+
|
138
|
+
matches = ary.length
|
139
|
+
return nil if (matches == 0)
|
140
|
+
result = Array.new(matches+2)
|
141
|
+
for i in 0 ... matches
|
142
|
+
result[i+1] = ary[i].dup
|
143
|
+
end
|
144
|
+
result[matches+1] = nil
|
145
|
+
|
146
|
+
if(matches==1)
|
147
|
+
result[0] = result[1].dup
|
148
|
+
result[1] = nil
|
149
|
+
else
|
150
|
+
i = 1
|
151
|
+
low = 100000
|
152
|
+
|
153
|
+
while (i < matches)
|
154
|
+
if (case_fold)
|
155
|
+
si = 0
|
156
|
+
while ((c1 = result[i][si,1].downcase) &&
|
157
|
+
(c2 = result[i + 1][si,1].downcase))
|
158
|
+
break if (c1 != c2)
|
159
|
+
si += 1
|
160
|
+
end
|
161
|
+
else
|
162
|
+
si = 0
|
163
|
+
while ((c1 = result[i][si,1]) &&
|
164
|
+
(c2 = result[i + 1][si,1]))
|
165
|
+
break if (c1 != c2)
|
166
|
+
si += 1
|
167
|
+
end
|
168
|
+
end
|
169
|
+
if (low > si)
|
170
|
+
low = si
|
171
|
+
end
|
172
|
+
i+=1
|
173
|
+
end
|
174
|
+
result[0] = result[1][0,low]
|
175
|
+
end
|
176
|
+
|
177
|
+
result
|
178
|
+
end
|
179
|
+
|
180
|
+
# Sets vi editing mode.
|
181
|
+
#
|
182
|
+
def self.vi_editing_mode()
|
183
|
+
RbReadline.rl_vi_editing_mode(1,0)
|
184
|
+
nil
|
185
|
+
end
|
186
|
+
|
187
|
+
# Sets emacs editing mode
|
188
|
+
#
|
189
|
+
def self.emacs_editing_mode()
|
190
|
+
RbReadline.rl_emacs_editing_mode(1,0)
|
191
|
+
nil
|
192
|
+
end
|
193
|
+
|
194
|
+
# Sets the character that is automatically appended after the
|
195
|
+
# Readline.completion_proc method is called.
|
196
|
+
#
|
197
|
+
# If +char+ is nil or empty, then a null character is used.
|
198
|
+
#
|
199
|
+
def self.completion_append_character=(char)
|
200
|
+
if char.nil?
|
201
|
+
RbReadline.rl_completion_append_character = ?\0
|
202
|
+
elsif char.length==0
|
203
|
+
RbReadline.rl_completion_append_character = ?\0
|
204
|
+
else
|
205
|
+
RbReadline.rl_completion_append_character = char[0].chr
|
206
|
+
end
|
207
|
+
end
|
208
|
+
|
209
|
+
# Returns the character that is automatically appended after the
|
210
|
+
# Readline.completion_proc method is called.
|
211
|
+
#
|
212
|
+
def self.completion_append_character()
|
213
|
+
if RbReadline.rl_completion_append_character == ?\0
|
214
|
+
return nil
|
215
|
+
end
|
216
|
+
return RbReadline.rl_completion_append_character
|
217
|
+
end
|
218
|
+
|
219
|
+
# Sets the character string that signal a break between words for the
|
220
|
+
# completion proc.
|
221
|
+
#
|
222
|
+
def self.basic_word_break_characters=(str)
|
223
|
+
RbReadline.rl_basic_word_break_characters = str.dup
|
224
|
+
end
|
225
|
+
|
226
|
+
# Returns the character string that signal a break between words for the
|
227
|
+
# completion proc. The default is " \t\n\"\\'`@$><=|&{(".
|
228
|
+
#
|
229
|
+
def self.basic_word_break_characters()
|
230
|
+
if RbReadline.rl_basic_word_break_characters.nil?
|
231
|
+
nil
|
232
|
+
else
|
233
|
+
RbReadline.rl_basic_word_break_characters.dup
|
234
|
+
end
|
235
|
+
end
|
236
|
+
|
237
|
+
# Sets the character string that signal the start or end of a word for
|
238
|
+
# the completion proc.
|
239
|
+
#
|
240
|
+
def self.completer_word_break_characters=(str)
|
241
|
+
RbReadline.rl_completer_word_break_characters = str.dup
|
242
|
+
end
|
243
|
+
|
244
|
+
# Returns the character string that signal the start or end of a word for
|
245
|
+
# the completion proc.
|
246
|
+
#
|
247
|
+
def self.completer_word_break_characters()
|
248
|
+
if RbReadline.rl_completer_word_break_characters.nil?
|
249
|
+
nil
|
250
|
+
else
|
251
|
+
RbReadline.rl_completer_word_break_characters.dup
|
252
|
+
end
|
253
|
+
end
|
254
|
+
|
255
|
+
# Sets the list of quote characters that can cause a word break.
|
256
|
+
#
|
257
|
+
def self.basic_quote_characters=(str)
|
258
|
+
RbReadline.rl_basic_quote_characters = str.dup
|
259
|
+
end
|
260
|
+
|
261
|
+
# Returns the list of quote characters that can cause a word break.
|
262
|
+
# The default is "'\"" (single and double quote characters).
|
263
|
+
#
|
264
|
+
def self.basic_quote_characters()
|
265
|
+
if RbReadline.rl_basic_quote_characters.nil?
|
266
|
+
nil
|
267
|
+
else
|
268
|
+
RbReadline.rl_basic_quote_characters.dup
|
269
|
+
end
|
270
|
+
end
|
271
|
+
|
272
|
+
# Sets the list of characters that can be used to quote a substring of
|
273
|
+
# the line, i.e. a group of characters within quotes.
|
274
|
+
#
|
275
|
+
def self.completer_quote_characters=(str)
|
276
|
+
RbReadline.rl_completer_quote_characters = str.dup
|
277
|
+
end
|
278
|
+
|
279
|
+
# Returns the list of characters that can be used to quote a substring
|
280
|
+
# of the line, i.e. a group of characters inside quotes.
|
281
|
+
#
|
282
|
+
def self.completer_quote_characters()
|
283
|
+
if RbReadline.rl_completer_quote_characters.nil?
|
284
|
+
nil
|
285
|
+
else
|
286
|
+
RbReadline.rl_completer_quote_characters.dup
|
287
|
+
end
|
288
|
+
end
|
289
|
+
|
290
|
+
# Sets the character string of one or more characters that indicate quotes
|
291
|
+
# for the filename completion of user input.
|
292
|
+
#
|
293
|
+
def self.filename_quote_characters=(str)
|
294
|
+
RbReadline.rl_filename_quote_characters = str.dup
|
295
|
+
end
|
296
|
+
|
297
|
+
# Returns the character string used to indicate quotes for the filename
|
298
|
+
# completion of user input.
|
299
|
+
#
|
300
|
+
def self.filename_quote_characters()
|
301
|
+
if RbReadline.rl_filename_quote_characters.nil?
|
302
|
+
nil
|
303
|
+
else
|
304
|
+
RbReadline.rl_filename_quote_characters.dup
|
305
|
+
end
|
306
|
+
end
|
307
|
+
|
308
|
+
# Returns the current offset in the current input line.
|
309
|
+
#
|
310
|
+
def self.point()
|
311
|
+
RbReadline.rl_point
|
312
|
+
end
|
313
|
+
|
314
|
+
# The History class encapsulates a history of all commands entered by
|
315
|
+
# users at the prompt, providing an interface for inspection and retrieval
|
316
|
+
# of all commands.
|
317
|
+
class History
|
318
|
+
extend Enumerable
|
319
|
+
|
320
|
+
# The History class, stringified in all caps.
|
321
|
+
#--
|
322
|
+
# Why?
|
323
|
+
#
|
324
|
+
def self.to_s
|
325
|
+
"HISTORY"
|
326
|
+
end
|
327
|
+
|
328
|
+
# Returns the command that was entered at the specified +index+
|
329
|
+
# in the history buffer.
|
330
|
+
#
|
331
|
+
# Raises an IndexError if the entry is nil.
|
332
|
+
#
|
333
|
+
def self.[](index)
|
334
|
+
if index < 0
|
335
|
+
index += RbReadline.history_length
|
336
|
+
end
|
337
|
+
entry = RbReadline.history_get(RbReadline.history_base+index)
|
338
|
+
if entry.nil?
|
339
|
+
raise IndexError,"invalid index"
|
340
|
+
end
|
341
|
+
entry.line.dup
|
342
|
+
end
|
343
|
+
|
344
|
+
# Sets the command +str+ at the given index in the history buffer.
|
345
|
+
#
|
346
|
+
# You can only replace an existing entry. Attempting to create a new
|
347
|
+
# entry will result in an IndexError.
|
348
|
+
#
|
349
|
+
def self.[]=(index,str)
|
350
|
+
if index<0
|
351
|
+
index += RbReadline.history_length
|
352
|
+
end
|
353
|
+
entry = RbReadline.replace_history_entry(index,str,nil)
|
354
|
+
if entry.nil?
|
355
|
+
raise IndexError,"invalid index"
|
356
|
+
end
|
357
|
+
str
|
358
|
+
end
|
359
|
+
|
360
|
+
# Synonym for Readline.add_history.
|
361
|
+
#
|
362
|
+
def self.<<(str)
|
363
|
+
RbReadline.add_history(str)
|
364
|
+
end
|
365
|
+
|
366
|
+
# Pushes a list of +args+ onto the history buffer.
|
367
|
+
#
|
368
|
+
def self.push(*args)
|
369
|
+
args.each do |str|
|
370
|
+
RbReadline.add_history(str)
|
371
|
+
end
|
372
|
+
end
|
373
|
+
|
374
|
+
# Internal function that removes the item at +index+ from the history
|
375
|
+
# buffer, performing necessary duplication in the process.
|
376
|
+
#--
|
377
|
+
# TODO: mark private?
|
378
|
+
#
|
379
|
+
def self.rb_remove_history(index)
|
380
|
+
entry = RbReadline.remove_history(index)
|
381
|
+
if (entry)
|
382
|
+
val = entry.line.dup
|
383
|
+
entry = nil
|
384
|
+
return val
|
385
|
+
end
|
386
|
+
nil
|
387
|
+
end
|
388
|
+
|
389
|
+
# Removes and returns the last element from the history buffer.
|
390
|
+
#
|
391
|
+
def self.pop()
|
392
|
+
if RbReadline.history_length>0
|
393
|
+
rb_remove_history(RbReadline.history_length-1)
|
394
|
+
else
|
395
|
+
nil
|
396
|
+
end
|
397
|
+
end
|
398
|
+
|
399
|
+
# Removes and returns the first element from the history buffer.
|
400
|
+
#
|
401
|
+
def self.shift()
|
402
|
+
if RbReadline.history_length>0
|
403
|
+
rb_remove_history(0)
|
404
|
+
else
|
405
|
+
nil
|
406
|
+
end
|
407
|
+
end
|
408
|
+
|
409
|
+
# Iterates over each entry in the history buffer.
|
410
|
+
#
|
411
|
+
def self.each()
|
412
|
+
for i in 0 ... RbReadline.history_length
|
413
|
+
entry = RbReadline.history_get(RbReadline.history_base + i)
|
414
|
+
break if entry.nil?
|
415
|
+
yield entry.line.dup
|
416
|
+
end
|
417
|
+
self
|
418
|
+
end
|
419
|
+
|
420
|
+
# Returns the length of the history buffer.
|
421
|
+
#
|
422
|
+
def self.length()
|
423
|
+
RbReadline.history_length
|
424
|
+
end
|
425
|
+
|
426
|
+
# Synonym for Readline.length.
|
427
|
+
#
|
428
|
+
def self.size()
|
429
|
+
RbReadline.history_length
|
430
|
+
end
|
431
|
+
|
432
|
+
# Returns a bolean value indicating whether or not the history buffer
|
433
|
+
# is empty.
|
434
|
+
#
|
435
|
+
def self.empty?()
|
436
|
+
RbReadline.history_length == 0
|
437
|
+
end
|
438
|
+
|
439
|
+
# Deletes an entry from the histoyr buffer at the specified +index+.
|
440
|
+
#
|
441
|
+
def self.delete_at(index)
|
442
|
+
if index < 0
|
443
|
+
i += RbReadline.history_length
|
444
|
+
end
|
445
|
+
if index < 0 || index > RbReadline.history_length - 1
|
446
|
+
raise IndexError, "invalid index"
|
447
|
+
end
|
448
|
+
rb_remove_history(index)
|
449
|
+
end
|
450
|
+
|
451
|
+
end
|
452
|
+
|
453
|
+
HISTORY = History
|
454
|
+
|
455
|
+
# The Fcomp class provided to encapsulate typical filename completion
|
456
|
+
# procedure. You will not typically use this directly, but will instead
|
457
|
+
# use the Readline::FILENAME_COMPLETION_PROC.
|
458
|
+
#
|
459
|
+
class Fcomp
|
460
|
+
def self.call(str)
|
461
|
+
matches = RbReadline.rl_completion_matches(str, :rl_filename_completion_function)
|
462
|
+
if (matches)
|
463
|
+
result = []
|
464
|
+
i = 0
|
465
|
+
while(matches[i])
|
466
|
+
result << matches[i].dup
|
467
|
+
matches[i] = nil
|
468
|
+
i += 1
|
469
|
+
end
|
470
|
+
matches = nil
|
471
|
+
if (result.length >= 2)
|
472
|
+
result.shift
|
473
|
+
end
|
474
|
+
else
|
475
|
+
result = nil
|
476
|
+
end
|
477
|
+
return result
|
478
|
+
end
|
479
|
+
end
|
480
|
+
|
481
|
+
FILENAME_COMPLETION_PROC = Fcomp
|
482
|
+
|
483
|
+
# The Ucomp class provided to encapsulate typical filename completion
|
484
|
+
# procedure. You will not typically use this directly, but will instead
|
485
|
+
# use the Readline::USERNAME_COMPLETION_PROC.
|
486
|
+
#
|
487
|
+
# Note that this feature currently only works on Unix systems since it
|
488
|
+
# ultimately uses the Etc module to iterate over a list of users.
|
489
|
+
#
|
490
|
+
class Ucomp
|
491
|
+
def self.call(str)
|
492
|
+
matches = RbReadline.rl_completion_matches(str, :rl_username_completion_function)
|
493
|
+
if (matches)
|
494
|
+
result = []
|
495
|
+
i = 0
|
496
|
+
while(matches[i])
|
497
|
+
result << matches[i].dup
|
498
|
+
matches[i] = nil
|
499
|
+
i += 1
|
500
|
+
end
|
501
|
+
matches = nil
|
502
|
+
if (result.length >= 2)
|
503
|
+
result.shift
|
504
|
+
end
|
505
|
+
else
|
506
|
+
result = nil
|
507
|
+
end
|
508
|
+
return result
|
509
|
+
end
|
510
|
+
end
|
511
|
+
|
512
|
+
USERNAME_COMPLETION_PROC = Ucomp
|
513
|
+
|
514
|
+
RbReadline.rl_readline_name = "Ruby"
|
515
|
+
|
516
|
+
RbReadline.using_history()
|
517
|
+
|
518
|
+
VERSION = RbReadline.rl_library_version
|
519
|
+
|
520
|
+
module_function :readline
|
521
|
+
|
522
|
+
RbReadline.rl_attempted_completion_function = :readline_attempted_completion_function
|
523
|
+
|
524
|
+
end
|