src/Pure/System/tty_loop.scala
author wenzelm
Mon, 01 Mar 2021 22:22:12 +0100
changeset 73340 0ffcad1f6130
parent 71713 928fd852f3e2
child 73367 77ef8bef0593
permissions -rw-r--r--
tuned --- fewer warnings;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
67802
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/System/tty_loop.scala
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
     3
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
     4
Line-oriented TTY loop.
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
     5
*/
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
     6
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
     7
package isabelle
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
     8
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
     9
67836
74958337214d tuned signature -- more generic;
wenzelm
parents: 67833
diff changeset
    10
import java.io.{IOException, Writer, Reader, InputStreamReader, BufferedReader}
67802
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
    11
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
    12
71713
928fd852f3e2 more robust interrupt handling;
wenzelm
parents: 71383
diff changeset
    13
class TTY_Loop(
928fd852f3e2 more robust interrupt handling;
wenzelm
parents: 71383
diff changeset
    14
  writer: Writer,
928fd852f3e2 more robust interrupt handling;
wenzelm
parents: 71383
diff changeset
    15
  reader: Reader,
928fd852f3e2 more robust interrupt handling;
wenzelm
parents: 71383
diff changeset
    16
  writer_lock: AnyRef = new Object)
67802
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
    17
{
71713
928fd852f3e2 more robust interrupt handling;
wenzelm
parents: 71383
diff changeset
    18
  private val console_output = Future.thread[Unit]("console_output", uninterruptible = true) {
67802
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
    19
    try {
71713
928fd852f3e2 more robust interrupt handling;
wenzelm
parents: 71383
diff changeset
    20
      val result = new StringBuilder(100)
67802
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
    21
      var finished = false
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
    22
      while (!finished) {
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
    23
        var c = -1
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
    24
        var done = false
71383
8313dca6dee9 misc tuning, following hint by IntelliJ;
wenzelm
parents: 70301
diff changeset
    25
        while (!done && (result.isEmpty || reader.ready)) {
67833
e135d03f656f tuned signature;
wenzelm
parents: 67808
diff changeset
    26
          c = reader.read
67802
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
    27
          if (c >= 0) result.append(c.asInstanceOf[Char])
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
    28
          else done = true
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
    29
        }
71383
8313dca6dee9 misc tuning, following hint by IntelliJ;
wenzelm
parents: 70301
diff changeset
    30
        if (result.nonEmpty) {
67802
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
    31
          System.out.print(result.toString)
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
    32
          System.out.flush()
70301
9f2a6856b912 tuned -- accommodate scala-2.13.0-RC3;
wenzelm
parents: 67839
diff changeset
    33
          result.clear
67802
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
    34
        }
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
    35
        else {
67833
e135d03f656f tuned signature;
wenzelm
parents: 67808
diff changeset
    36
          reader.close()
67802
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
    37
          finished = true
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
    38
        }
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
    39
      }
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
    40
    }
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
    41
    catch { case e: IOException => case Exn.Interrupt() => }
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
    42
  }
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
    43
71713
928fd852f3e2 more robust interrupt handling;
wenzelm
parents: 71383
diff changeset
    44
  private val console_input = Future.thread[Unit]("console_input", uninterruptible = true) {
67802
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
    45
    val console_reader = new BufferedReader(new InputStreamReader(System.in))
71713
928fd852f3e2 more robust interrupt handling;
wenzelm
parents: 71383
diff changeset
    46
    try {
928fd852f3e2 more robust interrupt handling;
wenzelm
parents: 71383
diff changeset
    47
      var finished = false
928fd852f3e2 more robust interrupt handling;
wenzelm
parents: 71383
diff changeset
    48
      while (!finished) {
928fd852f3e2 more robust interrupt handling;
wenzelm
parents: 71383
diff changeset
    49
        console_reader.readLine() match {
928fd852f3e2 more robust interrupt handling;
wenzelm
parents: 71383
diff changeset
    50
          case null =>
928fd852f3e2 more robust interrupt handling;
wenzelm
parents: 71383
diff changeset
    51
            writer.close()
928fd852f3e2 more robust interrupt handling;
wenzelm
parents: 71383
diff changeset
    52
            finished = true
928fd852f3e2 more robust interrupt handling;
wenzelm
parents: 71383
diff changeset
    53
          case line =>
928fd852f3e2 more robust interrupt handling;
wenzelm
parents: 71383
diff changeset
    54
            writer_lock.synchronized {
928fd852f3e2 more robust interrupt handling;
wenzelm
parents: 71383
diff changeset
    55
              writer.write(line)
928fd852f3e2 more robust interrupt handling;
wenzelm
parents: 71383
diff changeset
    56
              writer.write("\n")
928fd852f3e2 more robust interrupt handling;
wenzelm
parents: 71383
diff changeset
    57
              writer.flush()
928fd852f3e2 more robust interrupt handling;
wenzelm
parents: 71383
diff changeset
    58
            }
67802
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
    59
        }
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
    60
      }
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
    61
    }
71713
928fd852f3e2 more robust interrupt handling;
wenzelm
parents: 71383
diff changeset
    62
    catch { case e: IOException => case Exn.Interrupt() => }
67802
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
    63
  }
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
    64
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 71713
diff changeset
    65
  def join: Unit = { console_output.join; console_input.join }
67802
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
    66
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 71713
diff changeset
    67
  def cancel: Unit = console_input.cancel
67802
32d76f08023f more general TTY loop;
wenzelm
parents:
diff changeset
    68
}