~ubuntu-branches/debian/sid/scala/sid

« back to all changes in this revision

Viewing changes to src/library/scala/runtime/WorksheetSupport.scala

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg, Mehdi Dogguy, Lucas Satabin, Frank S. Thomas, Emmanuel Bourg
  • Date: 2015-06-05 23:52:59 UTC
  • mfrom: (1.2.11)
  • Revision ID: package-import@ubuntu.com-20150605235259-wk00vgk83dh8o19g
Tags: 2.10.5-1
* Team upload.

[ Mehdi Dogguy ]
* New upstream release (Closes: #744278).

[ Lucas Satabin ]
* Update patches
* Update the clean target
* Update paths of elements to install
* Update watch file

[ Frank S. Thomas ]
* Remove myself from Uploaders.

[ Emmanuel Bourg ]
* The package has been adopted by the Java Team (Closes: #754935)
* Patched the build to avoid downloading libraries from the Internet
* Replaced the minified JavaScript files with unobfuscated ones
* No longer build scala-partest.jar until diffutils is packaged or replaced
* debian/watch: Fixed the versions matched (x.y.z instead of x.y.z..z)
* debian/rules:
  - Added the missing get-orig-source target (Closes: #724704)
  - Improved the clean target
* debian/control:
  - Build depend on scala (>= 2.10) and bnd
  - Use canonical URLs for the Vcs-* fields
  - Standards-Version updated to 3.9.6 (no changes)
* Switch to debhelper level 9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package scala.runtime
 
2
import java.io.{OutputStream, PrintStream}
 
3
import scala.runtime.ScalaRunTime.stringOf
 
4
 
 
5
/** A utility object that's needed by the code that executes a worksheet.
 
6
 */
 
7
@deprecated("SI-6458: Instrumentation logic will be moved out of the compiler.","2.10.0")
 
8
object WorksheetSupport {
 
9
 
 
10
  /** The offset in the source which should be printed */
 
11
  private var currentOffset = 0
 
12
 
 
13
  /** A stream that flushes in regular intervals so that output can be captured
 
14
   *  in real time. The flush interval is determined by the field "flushInterval".
 
15
   *  By default it is 30ms.
 
16
   */
 
17
  private class FlushedOutputStream(out: OutputStream) extends OutputStream {
 
18
    protected def flushInterval = 30000000L // interval between flushes, by default 30ms
 
19
    protected def width = 80                // output width, by default 80 characters
 
20
    protected def tabInc = 8                // tab increment, by default 8 characters
 
21
    private var lastFlush: Long = 0L
 
22
    private var col = -1
 
23
    override def write(b: Array[Byte], off: Int, len: Int) = {
 
24
      for (idx <- off until (off + len min b.length)) writeOne(b(idx))
 
25
      flush()
 
26
    }
 
27
    override def write(c: Int) {
 
28
      writeOne(c)
 
29
      flush()
 
30
    }
 
31
    override def flush() {
 
32
      val current = System.nanoTime
 
33
      if (current - lastFlush >= flushInterval) {
 
34
        out.flush()
 
35
        lastFlush = current
 
36
      }
 
37
    }
 
38
    def writeOne(c: Int) {
 
39
      if (col < 0) {
 
40
        col = 0
 
41
        write((currentOffset+" ").getBytes)
 
42
      }
 
43
      out.write(c)
 
44
      col =
 
45
        if (c == '\n') -1
 
46
        else if (c == '\t') (col / tabInc) * tabInc + tabInc
 
47
        else col + 1
 
48
      if (col >= width) writeOne('\n')
 
49
    }
 
50
    def ensureNewLine() = if (col > 0) writeOne('\n')
 
51
  }
 
52
 
 
53
  private val flushedOut = new FlushedOutputStream(System.out)
 
54
  private val printOut = new PrintStream(flushedOut)
 
55
 
 
56
  private def redirected(op: => Unit) = {
 
57
    val oldSysOut = System.out
 
58
    val oldSysErr = System.err
 
59
    val oldConsOut = Console.out
 
60
    val oldConsErr = Console.err
 
61
    System.setOut(printOut)
 
62
    System.setErr(printOut)
 
63
    Console.setOut(printOut)
 
64
    Console.setErr(printOut)
 
65
    try op
 
66
    finally {
 
67
      printOut.close()
 
68
      System.setOut(oldSysOut)
 
69
      System.setErr(oldSysErr)
 
70
      Console.setOut(oldConsOut)
 
71
      Console.setErr(oldConsErr)
 
72
    }
 
73
  }
 
74
 
 
75
  def $execute(op: => Unit) = redirected {
 
76
    try op
 
77
    catch {
 
78
      case ex: StopException => ;
 
79
      case ex: Throwable => ex.printStackTrace()
 
80
    }
 
81
  }
 
82
 
 
83
  def $skip(n: Int) = {
 
84
    flushedOut.ensureNewLine()
 
85
    currentOffset += n
 
86
  }
 
87
 
 
88
  def $stop() = throw new StopException
 
89
 
 
90
  def $show(x: Any): String = stringOf(x)
 
91
}
 
92
 
 
93
class StopException extends Exception
 
94