@@ -22,7 +22,7 @@ module Shell
22
22
module InputShell
23
23
attr_accessor :prompt , :output
24
24
25
- def pgets ( )
25
+ def pgets
26
26
27
27
output . print ( prompt )
28
28
output . flush
@@ -46,6 +46,8 @@ def initialize(prompt, prompt_char = '>', histfile = nil, framework = nil)
46
46
47
47
# Initialize the prompt
48
48
self . init_prompt = prompt
49
+ self . cont_prompt = ' > '
50
+ self . cont_flag = false
49
51
self . prompt_char = prompt_char
50
52
51
53
self . histfile = histfile
@@ -56,7 +58,8 @@ def initialize(prompt, prompt_char = '>', histfile = nil, framework = nil)
56
58
57
59
def init_tab_complete
58
60
if ( self . input and self . input . supports_readline )
59
- self . input = Input ::Readline . new ( lambda { |str | tab_complete ( str ) } )
61
+ # Unless cont_flag because there's no tab complete for continuation lines
62
+ self . input = Input ::Readline . new ( lambda { |str | tab_complete ( str ) unless cont_flag } )
60
63
if Readline ::HISTORY . length == 0 and histfile and File . exist? ( histfile )
61
64
File . readlines ( histfile ) . each { |e |
62
65
Readline ::HISTORY << e . chomp
@@ -185,17 +188,14 @@ def run(&block)
185
188
self . init_prompt = input . prompt
186
189
end
187
190
188
- output . input = input
189
- line = input . pgets ( )
190
- output . input = nil
191
- log_output ( input . prompt )
191
+ line = get_input_line
192
192
193
193
# If a block was passed in, pass the line to it. If it returns true,
194
194
# break out of the shell loop.
195
195
if ( block )
196
196
break if ( line == nil or block . call ( line ) )
197
197
elsif ( input . eof? or line == nil )
198
- # If you have sessions active, this will give you a shot to exit gravefully
198
+ # If you have sessions active, this will give you a shot to exit gracefully
199
199
# If you really are ambitious, 2 eofs will kick this out
200
200
self . stop_count += 1
201
201
next if ( self . stop_count > 1 )
@@ -350,6 +350,40 @@ def print(msg='')
350
350
351
351
protected
352
352
353
+ #
354
+ # Get a single line of input, following continuation directives as necessary.
355
+ #
356
+ def get_input_line
357
+ line = "\\ \n "
358
+ prompt_needs_reset = false
359
+
360
+ self . cont_flag = false
361
+ while line =~ /(^|[^\\ ])\\ \s *$/
362
+ # Strip \ and all the trailing whitespace
363
+ line . sub! ( /\\ \s */ , '' )
364
+
365
+ if line . length > 0
366
+ # Using update_prompt will overwrite the primary prompt
367
+ input . prompt = output . update_prompt ( self . cont_prompt )
368
+ self . cont_flag = true
369
+ prompt_needs_reset = true
370
+ end
371
+
372
+ output . input = input
373
+ line << input . pgets
374
+ output . input = nil
375
+ log_output ( input . prompt )
376
+ end
377
+ self . cont_flag = false
378
+
379
+ if prompt_needs_reset
380
+ # The continuation prompt was used so reset the prompt
381
+ update_prompt
382
+ end
383
+
384
+ line
385
+ end
386
+
353
387
#
354
388
# Parse a line into an array of arguments.
355
389
#
@@ -389,12 +423,17 @@ def log_output(buf)
389
423
end
390
424
391
425
attr_writer :input , :output # :nodoc:
392
- attr_accessor :stop_flag , :init_prompt # :nodoc:
426
+ attr_accessor :stop_flag , :init_prompt , :cont_prompt # :nodoc:
393
427
attr_accessor :prompt # :nodoc:
394
428
attr_accessor :prompt_char , :tab_complete_proc # :nodoc:
395
429
attr_accessor :histfile # :nodoc:
396
430
attr_accessor :hist_last_saved # the number of history lines when last saved/loaded
397
431
attr_accessor :log_source , :stop_count # :nodoc:
432
+ attr_reader :cont_flag # :nodoc:
433
+
434
+ private
435
+
436
+ attr_writer :cont_flag # :nodoc:
398
437
399
438
end
400
439
0 commit comments