src/Pure/System/progress.scala
author wenzelm
Sun May 14 17:05:06 2017 +0200 (2017-05-14)
changeset 65828 02dd430d80c5
parent 65826 0b8a6a62114f
child 65888 29a31cf0b4bc
permissions -rw-r--r--
tuned signature;
wenzelm@61276
     1
/*  Title:      Pure/System/progress.scala
wenzelm@61276
     2
    Author:     Makarius
wenzelm@61276
     3
wenzelm@61276
     4
Progress context for system processes.
wenzelm@61276
     5
*/
wenzelm@61276
     6
wenzelm@61276
     7
package isabelle
wenzelm@61276
     8
wenzelm@61276
     9
wenzelm@64201
    10
import java.io.{File => JFile}
wenzelm@64201
    11
wenzelm@64201
    12
wenzelm@61276
    13
class Progress
wenzelm@61276
    14
{
wenzelm@61276
    15
  def echo(msg: String) {}
wenzelm@64049
    16
  def echo_if(cond: Boolean, msg: String) { if (cond) echo(msg) }
wenzelm@61276
    17
  def theory(session: String, theory: String) {}
wenzelm@64909
    18
wenzelm@65826
    19
  def echo_warning(msg: String) { echo(Output.warning_text(msg)) }
wenzelm@65828
    20
  def echo_error_message(msg: String) { echo(Output.error_message_text(msg)) }
wenzelm@65826
    21
wenzelm@61276
    22
  def stopped: Boolean = false
wenzelm@61276
    23
  override def toString: String = if (stopped) "Progress(stopped)" else "Progress"
wenzelm@64201
    24
wenzelm@64201
    25
  def bash(script: String,
wenzelm@64201
    26
    cwd: JFile = null,
wenzelm@64201
    27
    env: Map[String, String] = Isabelle_System.settings(),
wenzelm@64201
    28
    redirect: Boolean = false,
wenzelm@64201
    29
    echo: Boolean = false): Process_Result =
wenzelm@64201
    30
  {
wenzelm@64201
    31
    Isabelle_System.bash(script, cwd = cwd, env = env, redirect = redirect,
wenzelm@64201
    32
      progress_stdout = echo_if(echo, _),
wenzelm@64201
    33
      progress_stderr = echo_if(echo, _))
wenzelm@64201
    34
  }
wenzelm@61276
    35
}
wenzelm@61276
    36
wenzelm@64909
    37
object No_Progress extends Progress
wenzelm@61276
    38
wenzelm@64117
    39
class Console_Progress(verbose: Boolean = false, stderr: Boolean = false) extends Progress
wenzelm@61276
    40
{
wenzelm@64117
    41
  override def echo(msg: String)
wenzelm@64117
    42
  {
wenzelm@64117
    43
    if (stderr) Console.err.println(msg) else Console.println(msg)
wenzelm@64117
    44
  }
wenzelm@64117
    45
wenzelm@61276
    46
  override def theory(session: String, theory: String): Unit =
wenzelm@61276
    47
    if (verbose) echo(session + ": theory " + theory)
wenzelm@61276
    48
wenzelm@61276
    49
  @volatile private var is_stopped = false
wenzelm@61276
    50
  def interrupt_handler[A](e: => A): A = POSIX_Interrupt.handler { is_stopped = true } { e }
wenzelm@61276
    51
  override def stopped: Boolean =
wenzelm@61276
    52
  {
wenzelm@61276
    53
    if (Thread.interrupted) is_stopped = true
wenzelm@61276
    54
    is_stopped
wenzelm@61276
    55
  }
wenzelm@61276
    56
}