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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
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
|
GNU Readline ã«ããã³ãã³ãã©ã¤ã³å
¥åã¤ã³ã¿ãã§ã¼ã¹ãæä¾ããã¢ã¸ã¥ã¼ã«
ã§ããGNU Readline ã®äºæã©ã¤ãã©ãªã®ã²ã¨ã¤ã§ãã Edit Line(libedit) ã
ãµãã¼ããã¦ãã¾ãã
GNU Readline:: http://www.gnu.org/directory/readline.html
libedit:: http://www.thrysoee.dk/editline/
Readline.readline ã使ç¨ãã¦ã¦ã¼ã¶ããã®å
¥åãåå¾ã§ãã¾ãããã®ã¨ãã
GNU Readline ã®ããã«å
¥åã®è£å®ãEmacs ã®ãããªãã¼æä½ãªã©ãã§ãã¾ãã
require "readline"
while buf = Readline.readline("> ", true)
p buf
end
ã¦ã¼ã¶ãå
¥åããå
容ãå±¥æ´(以ä¸ããã¹ããª)ã¨ãã¦è¨é²ãããã¨ãã§ãã¾ãã
å®æ° Readline::HISTORY ã使ç¨ãã¦ãã¹ããªã«ã¢ã¯ã»ã¹ã§ãã¾ãã
require "readline"
while buf = Readline.readline("> ", true)
p Readline::HISTORY.to_a
print("-> ", buf, "\n")
end
使ç¨ããã©ã¤ãã©ãªã«ãããããã¤ãã®ã¡ã½ããã§ä¾å¤ NotImplementedError
ãçºçãã¾ãã
== Readline ã¢ã¸ã¥ã¼ã«
=== ã¢ã¸ã¥ã¼ã«é¢æ°
readline([prompt, [add_hist]]) -> String | nil
prompt ãåºåããã¦ã¼ã¶ããã®ãã¼å
¥åãå¾
ã¡ã¾ãã
ã¨ã³ã¿ã¼ãã¼ã®æ¼ä¸ãªã©ã§ã¦ã¼ã¶ãæååãå
¥åãçµããã¨ã
å
¥åããæååãè¿ãã¾ãã
ãã®ã¨ããadd_hist ã true ã§ããã°ãå
¥åããæååããã¹ããªã«è¿½å ãã¾ãã
ä½ãå
¥åãã¦ããªãç¶æ
㧠EOF(UNIX ã§ã¯ ^D) ãå
¥åãããªã©ã§ã
ã¦ã¼ã¶ããã®å
¥åããªãå ´å㯠nil ãè¿ãã¾ãã
次ã®æ¡ä»¶ãå
¨ã¦æºããå ´åãä¾å¤ IOError ãçºçãã¾ãã
1. æ¨æºå
¥åã tty ã§ãªãã
2. æ¨æºå
¥åãã¯ãã¼ãºãã¦ããã(isatty(2) ã® errno ã EBADF ã§ããã)
æ¬ã¡ã½ããã¯ã¹ã¬ããã«å¯¾å¿ãã¦ãã¾ãã
å
¥åå¾
ã¡ç¶æ
ã®ã¨ãã¯ã¹ã¬ããã³ã³ããã¹ãã®åæ¿ããçºçãã¾ãã
å
¥åæã«ã¯è¡å
ç·¨éãå¯è½ã§ãvi ã¢ã¼ã㨠Emacs ã¢ã¼ããç¨æããã¦ãã¾ãã
ããã©ã«ã㯠Emacs ã¢ã¼ãã§ãã
æ¬ã¡ã½ããã«ã¯æ³¨æäºé
ãããã¾ãã
å
¥åå¾
ã¡ã®ç¶æ
㧠^C ãã㨠ruby ã¤ã³ã¿ããªã¿ãçµäºãã端æ«ç¶æ
ã復帰ãã¾ããã
ãããåé¿ããããã®ä¾ã3ã¤æãã¾ãã
* ^Cã«ããInterruptä¾å¤ãè£è¶³ãã¦ã端æ«ç¶æ
ã復帰ãã¾ã:
require "readline"
stty_save = `stty -g`.chomp
begin
while buf = Readline.readline
p buf
end
rescue Interrupt
system("stty", stty_save)
exit
end
end
end
* INTã·ã°ãã«ãè£è¶³ãã¦ã端æ«ç¶æ
ã復帰ãã¾ã:
require "readline"
stty_save = `stty -g`.chomp
trap("INT") { system "stty", stty_save; exit }
while buf = Readline.readline
p buf
end
* åã« ^C ãç¡è¦ããæ¹æ³ãããã¾ã:
require "readline"
trap("INT", "SIG_IGN")
while buf = Readline.readline
p buf
end
å
¥åå±¥æ´ Readline::HISTORY ã使ç¨ãã¦ã空è¡ãç´åã®å
¥åã¨åãå
容ã¯å
¥å
å±¥æ´ã«æ®ããªãã¨ãããã¨ãã§ãã¾ãã
require "readline"
while buf = Readline.readline("> ", true)
# p Readline::HISTORY.to_a
Readline::HISTORY.pop if /^\s*$/ =~ buf
begin
if Readline::HISTORY[Readline::HISTORY.length-2] == buf
Readline::HISTORY.pop
end
rescue IndexError
end
# p Readline::HISTORY.to_a
print "-> ", buf, "\n"
end
=== ã¯ã©ã¹ã¡ã½ãã
Readline.input = input
Readline.readline ã¡ã½ããã§ä½¿ç¨ããå
¥åç¨ã® File ãªãã¸ã§ã¯ã input
ãæå®ãã¾ãã
Readline.output = output
Readline.readline ã¡ã½ããã§ä½¿ç¨ããåºåç¨ã® File ãªãã¸ã§ã¯ã
output ãæå®ãã¾ãã
Readline.completion_proc = proc
ã¦ã¼ã¶ããã®å
¥åãè£å®ããæã®åè£ãåå¾ãã Proc ãªãã¸ã§ã¯ã proc ã
æå®ãã¾ããproc ã¯ã次ã®ãã®ãæ³å®ãã¦ãã¾ãã
1. call ã¡ã½ãããæã¡ã¾ãã
call ã¡ã½ãããæããªãå ´åãä¾å¤ ArgumentError ãçºçãã¾ãã
2. å¼æ°ã«ã¦ã¼ã¶ããã®å
¥åæåå(注1)ãåãã¾ãã
3. åè£ã®æååã®é
åãè¿ãã¾ãã
注1:ã/var/lib /vãã®å¾ã§è£å®ãè¡ãã¨ã
ããã©ã«ãã§ã¯ proc ã®å¼æ°ã«ã/vãã渡ããã¾ãã
ãã®ããã«ãã¦ã¼ã¶ãå
¥åããæååã
Readline.completer_word_break_characters ã«å«ã¾ããæåã§åºåã£ãã
ã®ãåèªã¨ããã¨ãã«ã¼ã½ã«ãããåèªã®æåã®æåããç¾å¨ã®ã«ã¼ã½ã«ä½
ç½®ã¾ã§ã®æååã proc ã®å¼æ°ã«æ¸¡ããã¾ãã
Readline.completion_proc -> proc
ã¦ã¼ã¶ããã®å
¥åãè£å®ããæã®åè£ãåå¾ãã Proc ãªãã¸ã§ã¯ã proc
ãåå¾ãã¾ãã
Readline.completion_case_fold = bool
ã¦ã¼ã¶ã®å
¥åãè£å®ããéã大æåã¨å°æåãåºå¥ããï¼ããªããæå®ãã¾ãã
bool ãçãªãã°åºå¥ãã¾ãããbool ãå½ãªãã°åºå¥ãã¾ãã
Readline.completion_case_fold -> bool
ã¦ã¼ã¶ã®å
¥åãè£å®ããéã大æåã¨å°æåãåºå¥ããï¼ããªããåå¾ãã¾ãã
bool ãçãªãã°åºå¥ãã¾ãããbool ãå½ãªãã°åºå¥ãã¾ãã
ãªããReadline.completion_case_fold= ã¡ã½ããã§æå®ãããªãã¸ã§ã¯ãã
ãã®ã¾ã¾åå¾ããã®ã§ã次ã®ãããªåä½ããã¾ãã
require "readline"
Readline.completion_case_fold = "This is a String."
p Readline.completion_case_fold # => "This is a String."
Readline.line_buffer -> string
å
¥åä¸ã®è¡å
¨ä½ãè¿ãã¾ããcomplete_proc ã®ä¸ã§ä½¿ç¨ãããã¨ãæ³å®ã
ã¦ãã¾ããReadline.line_buffer ã®é·ã㯠GNU Readline ã® rl_end å¤æ°ã®
å¤ã¨ä¸è´ãã¾ãã
Readline.point -> int
ç¾å¨ã®ã«ã¼ã½ã«ã®ä½ç½®ãè¿ãã¾ãã
Readline ã¢ã¸ã¥ã¼ã«ã¯è£å®å¯¾è±¡ã®åèªã®éå§ä½ç½®ã®æ
å ±ãæä¾ãã¦ãã¾ããã
ããããªããã completion_proc ã®ä¸ã§å
¥åããåèª text ã¨
Readline.point ã使ç¨ãããã¨ã§éå§ä½ç½®ãå°ããã¨ãã§ãã¾ãã
éå§ä½ç½® = å
¥åããåèªã®é·ã - Readline.point
Readline.vi_editing_mode -> nil
ç·¨éã¢ã¼ãã vi ã¢ã¼ãã«ãã¾ãã
vi ã¢ã¼ãã®è©³ç´°ã¯ãGNU Readline ã®ããã¥ã¢ã«ãåç
§ãã¦ãã ããã
ãµãã¼ããã¦ããªãç°å¢ã§ã¯ãä¾å¤ NotImplementedError ãçºçãã¾ãã
Readline.vi_editing_mode? -> bool
ç·¨éã¢ã¼ãã vi ã¢ã¼ãã®å ´åãtrue ãè¿ãã¾ããããã§ãªããã° false
ãè¿ãã¾ãã
ãµãã¼ããã¦ããªãç°å¢ã§ã¯ãä¾å¤ NotImplementedError ãçºçãã¾ãã
Readline.emacs_editing_mode -> nil
ç·¨éã¢ã¼ãã Emacs ã¢ã¼ãã«ãã¾ãã
ããã©ã«ã㯠Emacs ã¢ã¼ãã§ãã
Emacs ã¢ã¼ãã®è©³ç´°ã¯ãGNU Readline ã®ããã¥ã¢ã«ãåç
§ãã¦ãã ããã
ãµãã¼ããã¦ããªãç°å¢ã§ã¯ãä¾å¤ NotImplementedError ãçºçãã¾ãã
Readline.emacs_editing_mode? -> bool
ç·¨éã¢ã¼ãã Emacs ã¢ã¼ãã®å ´åãtrue ãè¿ãã¾ããããã§ãªããã° false
ãè¿ãã¾ãã
ãµãã¼ããã¦ããªãç°å¢ã§ã¯ãä¾å¤ NotImplementedError ãçºçãã¾ãã
Readline.completion_append_character = char
ã¦ã¼ã¶ã®å
¥åã®è£å®ãå®äºããå ´åã«ãæå¾ã«ä»å ããæå char ãæå®ã
ã¾ããåè§ã¹ãã¼ã¹ã" "ããªã©ã®åèªãåºåãæåãæå®ããã°ãé£ç¶ãã¦
å
¥åããéã«ä¾¿å©ã§ãã
使ç¨ä¾:
require "readline"
Readline.readline("> ", true)
Readline.completion_append_character = " "
å®è¡ä¾:
>
ãã㧠"/var/li" ãå
¥åãã¾ãã
> /var/li
ãã㧠TAB ãã¼ãå
¥åãã¾ãã
> /var/lib
"b" ãè£å®ãããæå¾ã« " " ã追å ãããã®ã§ã"/usr" ãé£ç¶ãã¦å
¥åã§ãã¾ãã
> /var/lib /usr
ãªãã1æåããæå®ãããã¨ã¯ã§ãã¾ããã
ä¾ãã°ã"string"ãæå®ããå ´åã¯æåã®æåã§ãã"s"ã ãã使ç¨ãã¾ãã
require "readline"
Readline.completion_append_character = "string"
p Readline.completion_append_character # => "s"
ãµãã¼ããã¦ããªãç°å¢ã§ã¯ãä¾å¤ NotImplementedError ãçºçãã¾ãã
Readline.completion_append_character -> char
ã¦ã¼ã¶ã®å
¥åã®è£å®ãå®äºããå ´åã«ãæå¾ã«ä»å ããæåãåå¾ãã¾ãã
ããã©ã«ãã¯ç©ºç½ (" ") ã§ãã
ãµãã¼ããã¦ããªãç°å¢ã§ã¯ãä¾å¤ NotImplementedError ãçºçãã¾ãã
Readline.basic_word_break_characters = string
ã¦ã¼ã¶ã®å
¥åã®è£å®ãè¡ãéãåèªã®åºåãã示ãè¤æ°ã®æåã§æ§æããã
æåå string ãæå®ãã¾ãã
GNU Readline ã®ããã©ã«ãã®å¤ã¯ãBash ã®è£å®å¦çã§ä½¿ç¨ãã¦ããæåå
" \t\n\"\\'`@$><=;|&{(" (ã¹ãã¼ã¹ãå«ã) ã«ãªã£ã¦ãã¾ãã
ãµãã¼ããã¦ããªãç°å¢ã§ã¯ãä¾å¤ NotImplementedError ãçºçãã¾ãã
Readline.basic_word_break_characters -> string
ã¦ã¼ã¶ã®å
¥åã®è£å®ãè¡ãéãåèªã®åºåãã示ãè¤æ°ã®æåã§æ§æããã
æååãåå¾ãã¾ãã
ãµãã¼ããã¦ããªãç°å¢ã§ã¯ãä¾å¤ NotImplementedError ãçºçãã¾ãã
Readline.completer_word_break_characters = string
ã¦ã¼ã¶ã®å
¥åã®è£å®ãè¡ãéãåèªã®åºåãã示ãè¤æ°ã®æåã§æ§æããã
æåå string ãæå®ãã¾ãã
Readline.basic_word_break_characters= ã¨ã®éãã¯ã
GNU Readline ã® rl_complete_internal é¢æ°ã§ä½¿ç¨ããããã¨ã§ãã
GNU Readline ã®ããã©ã«ãã®å¤ã¯ã
Readline.basic_word_break_characters ã¨åãã§ãã
ãµãã¼ããã¦ããªãç°å¢ã§ã¯ãä¾å¤ NotImplementedError ãçºçãã¾ãã
Readline.completer_word_break_characters -> string
ã¦ã¼ã¶ã®å
¥åã®è£å®ãè¡ãéãåèªã®åºåãã示ãè¤æ°ã®æåã§æ§æããã
æååãåå¾ãã¾ãã
ãµãã¼ããã¦ããªãç°å¢ã§ã¯ãä¾å¤ NotImplementedError ãçºçãã¾ãã
Readline.basic_quote_characters = string
ã¹ãã¼ã¹ãªã©ã®åèªã®åºåããã¯ãªã¼ãããããã®è¤æ°ã®æåã§æ§æããã
æåå string ãæå®ãã¾ãã
ãµãã¼ããã¦ããªãç°å¢ã§ã¯ãä¾å¤ NotImplementedError ãçºçãã¾ãã
Readline.basic_quote_characters -> string
ã¹ãã¼ã¹ãªã©ã®åèªã®åºåããã¯ãªã¼ãããããã®è¤æ°ã®æåã§æ§æããã
æååãåå¾ãã¾ãã
ãµãã¼ããã¦ããªãç°å¢ã§ã¯ãä¾å¤ NotImplementedError ãçºçãã¾ãã
Readline.completer_quote_characters = string
ã¦ã¼ã¶ã®å
¥åã®è£å®ãè¡ãéãã¹ãã¼ã¹ãªã©ã®åèªã®åºåãã
ã¯ãªã¼ãããããã®è¤æ°ã®æåã§æ§æãããæåå string ãæå®ãã¾ãã
æå®ããæåã®éã§ã¯ãReadline.completer_word_break_characters=
ã§æå®ããæååã«å«ã¾ããæåããæ®éã®æååã¨ãã¦æ±ããã¾ãã
ãµãã¼ããã¦ããªãç°å¢ã§ã¯ãä¾å¤ NotImplementedError ãçºçãã¾ãã
Readline.completer_quote_characters -> string
ã¦ã¼ã¶ã®å
¥åã®è£å®ãè¡ãéãã¹ãã¼ã¹ãªã©ã®åèªã®åºåãã
ã¯ãªã¼ãããããã®è¤æ°ã®æåã§æ§æãããæååãåå¾ãã¾ãã
ãµãã¼ããã¦ããªãç°å¢ã§ã¯ãä¾å¤ NotImplementedError ãçºçãã¾ãã
Readline.filename_quote_characters = string
ã¦ã¼ã¶ã®å
¥åæã«ãã¡ã¤ã«åã®è£å®ãè¡ãéãã¹ãã¼ã¹ãªã©ã®åèªã®åºåãã
ã¯ãªã¼ãããããã®è¤æ°ã®æåã§æ§æãããæåå string ãæå®ãã¾ãã
GNU Readline ã®ããã©ã«ãå¤ã¯ nil ã§ãã
ãµãã¼ããã¦ããªãç°å¢ã§ã¯ãä¾å¤ NotImplementedError ãçºçãã¾ãã
Readline.filename_quote_characters -> string
ã¦ã¼ã¶ã®å
¥åæã«ãã¡ã¤ã«åã®è£å®ãè¡ãéãã¹ãã¼ã¹ãªã©ã®åèªã®åºåãã
ã¯ãªã¼ãããããã®è¤æ°ã®æåã§æ§æãããæååãåå¾ãã¾ãã
ãµãã¼ããã¦ããªãç°å¢ã§ã¯ãä¾å¤ NotImplementedError ãçºçãã¾ãã
=== ã¯ã©ã¹å®æ°
HISTORY
å®æ° HISTORY ã使ç¨ãã¦ãã¹ããªã«ã¢ã¯ã»ã¹ã§ãã¾ãã
Enumerable ã¢ã¸ã¥ã¼ã«ã extend ãã¦ããã
é
åã®ããã«æ¯ãèããã¨ãã§ãã¾ãã
ä¾ãã°ãHISTORY[4] ã«ãã 5 çªç®ã«å
¥åããå
容ãåãåºããã¨ãã§ãã¾ãã
require "readline"
Readline::HISTORY.push("a", "b", "c", "d", "e")
p Readline::HISTORY[4] # => "e"
å®è£
ãã¦ããã¡ã½ããã次ã«æãã¾ãã
* HISTORY.to_s -> "HISTORY"
* HISTORY[index] -> string
* HISTORY[index] = string
* HISTORY.push(string[, string, ...]) -> self
* HISTORY << string -> self
* HISTORY.pop -> string
* HISTORY.shift -> string
* HISTORY.each -> Enumerator
* HISTORY.each { |i| } -> [string]
* HISTORY.length -> Integer
* HISTORY.empty? -> true or false
* HISTORY.delete_at(index) -> string
* HISTORY.clear -> self
ãµãã¼ããã¦ããªãç°å¢ã§ã¯ã次ã®ã¡ã½ããã§ä¾å¤ NotImplementedError ã
çºçãã¾ãã
* HISTORY[index] = string
* HISTORY.pop -> string
* HISTORY.shift -> string
* HISTORY.delete_at(index) -> string
* HISTORY.clear -> self
FILENAME_COMPLETION_PROC
ãã¡ã¤ã«åã®è£å®ãè¡ã call ã¡ã½ãããæã¤ãªãã¸ã§ã¯ãã§ãã
Readline.completion_proc= ã«ãããã¦ã¼ã¶ã®å
¥åæã«ãã¡ã¤ã«åã®è£å®ã
è¡ãããã«è¨å®ããããã«ä½¿ç¨ãããã¨ãæ³å®ãã¦ã¾ãã
USERNAME_COMPLETION_PROC
ã¦ã¼ã¶åã®è£å®ãè¡ã call ã¡ã½ãããæã¤ãªãã¸ã§ã¯ãã§ãã
Readline.completion_proc= ã«ãããã¦ã¼ã¶ã®å
¥åæã«ã¦ã¼ã¶åã®è£å®ãè¡
ãããã«è¨å®ããããã«ä½¿ç¨ãããã¨ãæ³å®ãã¦ã¾ãã
VERSION
使ç¨ãã¦ãã GNU Readline ã¾ã㯠libedit ã®ãã¼ã¸ã§ã³ã§ãã
|