@@ -392,23 +392,23 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
392
392
true
393
393
}
394
394
395
+ // after process line, OK continue, ERR break, or EOF all done
396
+ object LineResults extends Enumeration {
397
+ type LineResult = Value
398
+ val EOF, ERR, OK = Value
399
+ }
400
+ import LineResults .LineResult
401
+
395
402
// return false if repl should exit
396
403
def processLine (line : String ): Boolean = {
397
404
import scala .concurrent .duration ._
398
405
Await .ready(globalFuture, 10 .minutes) // Long timeout here to avoid test failures under heavy load.
399
406
400
- if (line eq null ) {
401
- // SI-4563: this means the console was properly interrupted (Ctrl+D usually)
402
- // so we display the output message (which by default ends with
403
- // a newline so as not to break the user's terminal)
404
- if (in.interactive) out.print(Properties .shellInterruptedString)
405
-
406
- false
407
- } else (command(line) match {
407
+ command(line) match {
408
408
case Result (false , _) => false
409
409
case Result (_, Some (line)) => addReplay(line) ; true
410
410
case _ => true
411
- })
411
+ }
412
412
}
413
413
414
414
private def readOneLine () = {
@@ -426,18 +426,22 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
426
426
* command() for each line of input, and stops when
427
427
* command() returns false.
428
428
*/
429
- @ tailrec final def loop () {
430
- if ( try processLine(readOneLine()) catch crashRecovery )
431
- loop()
429
+ @ tailrec final def loop (): LineResult = {
430
+ import LineResults ._
431
+ readOneLine() match {
432
+ case null => EOF
433
+ case line => if (try processLine(line) catch crashRecovery) loop() else ERR
434
+ }
432
435
}
433
436
434
437
/** interpret all lines from a specified file */
435
- def interpretAllFrom (file : File ) {
438
+ def interpretAllFrom (file : File , verbose : Boolean = false ) {
436
439
savingReader {
437
440
savingReplayStack {
438
441
file applyReader { reader =>
439
- in = SimpleReader (reader, out, interactive = false )
440
- echo(" Loading " + file + " ..." )
442
+ in = if (verbose) new SimpleReader (reader, out, interactive = true ) with EchoReader
443
+ else SimpleReader (reader, out, interactive = false )
444
+ echo(s " Loading $file... " )
441
445
loop()
442
446
}
443
447
}
@@ -592,13 +596,17 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
592
596
res
593
597
}
594
598
595
- def loadCommand (arg : String ) = {
596
- var shouldReplay : Option [String ] = None
597
- withFile(arg)(f => {
598
- interpretAllFrom(f)
599
- shouldReplay = Some (" :load " + arg)
600
- })
601
- Result (keepRunning = true , shouldReplay)
599
+ def loadCommand (arg : String ): Result = {
600
+ def run (file : String , verbose : Boolean ) = withFile(file) { f =>
601
+ interpretAllFrom(f, verbose)
602
+ Result recording s " :load $arg"
603
+ } getOrElse Result .default
604
+
605
+ words(arg) match {
606
+ case " -v" :: file :: Nil => run(file, verbose = true )
607
+ case file :: Nil => run(file, verbose = false )
608
+ case _ => echo(" usage: :load -v file" ) ; Result .default
609
+ }
602
610
}
603
611
604
612
def saveCommand (filename : String ): Result = (
@@ -685,13 +693,13 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
685
693
}
686
694
val code = file match {
687
695
case Some (name) =>
688
- withFile(name)( f => {
696
+ withFile(name) { f =>
689
697
shouldReplay = Some (s " :paste $arg" )
690
698
val s = f.slurp.trim
691
699
if (s.isEmpty) echo(s " File contains no code: $f" )
692
700
else echo(s " Pasting file $f... " )
693
701
s
694
- }) getOrElse " "
702
+ } getOrElse " "
695
703
case None =>
696
704
echo(" // Entering paste mode (ctrl-D to finish)\n " )
697
705
val text = (readWhile(_ => true ) mkString " \n " ).trim
@@ -820,7 +828,7 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
820
828
)
821
829
catch {
822
830
case ex @ (_ : Exception | _ : NoClassDefFoundError ) =>
823
- echo(" Failed to created JLineReader: " + ex + " \ n Falling back to SimpleReader." )
831
+ echo(f " Failed to created JLineReader: ${ex} % nFalling back to SimpleReader." )
824
832
SimpleReader ()
825
833
}
826
834
}
@@ -847,6 +855,8 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
847
855
case _ =>
848
856
}
849
857
}
858
+
859
+ // start an interpreter with the given settings
850
860
def process (settings : Settings ): Boolean = savingContextLoader {
851
861
this .settings = settings
852
862
createInterpreter()
@@ -861,7 +871,10 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
861
871
loadFiles(settings)
862
872
printWelcome()
863
873
864
- try loop()
874
+ try loop() match {
875
+ case LineResults .EOF => out print Properties .shellInterruptedString
876
+ case _ =>
877
+ }
865
878
catch AbstractOrMissingHandler ()
866
879
finally closeInterpreter()
867
880
0 commit comments