jline.console.ConsoleReader Scala Examples

The following examples show how to use jline.console.ConsoleReader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: LineReader.scala    From eidos   with Apache License 2.0 5 votes vote down vote up
package org.clulab.wm.eidos.utils

import java.io.File



abstract class LineReader {
  def readLine(): String
}

class CliReader(prompt: String, parentProperty: String, child: String) extends LineReader {
  import jline.console.ConsoleReader
  import jline.console.history.FileHistory

  val reader = new ConsoleReader()
  val history = new FileHistory(new File(System.getProperty(parentProperty), child))

  reader.setPrompt(prompt)
  reader.setHistory(history)
  sys addShutdownHook {
    reader.getTerminal.restore()
    reader.shutdown()
    history.flush() // flush file before exiting
  }

  override def readLine(): String = reader.readLine
}

class IdeReader(protected val prompt: String) extends LineReader {
  import java.util.Scanner

  protected val reader = new Scanner(System.in)

  override def readLine(): String = {
    print(prompt)
    Console.flush()
    reader.nextLine
  }
} 
Example 2
Source File: CLIDebuggerRunner.scala    From incubator-daffodil   with Apache License 2.0 5 votes vote down vote up
package org.apache.daffodil.debugger

import java.io.File
import scala.io.Source
import jline.console.ConsoleReader

class CLIDebuggerRunner(cmdsIter: Iterator[String]) extends InteractiveDebuggerRunner {
  def this() {
    this(Iterator.empty)
  }

  def this(file: File) {
    this(Source.fromFile(file).getLines)
  }

  def this(seq: Seq[String]) {
    this(seq.iterator)
  }

  var reader: Option[ConsoleReader] = None

  def init(id: InteractiveDebugger): Unit = {
    val r = new ConsoleReader()
    r.setPrompt("(debug) ")
    r.addCompleter(id.DebugCommandBase.completer)
    r.setExpandEvents(false)
    reader = Some(r)
  }

  def fini(): Unit = {
    reader.map { _.close }
    reader = None
  }

  def getCommand: String = {
    val cmd = if (cmdsIter.hasNext) {
      val line = cmdsIter.next
      if (line.length > 0) {
        reader.get.getHistory.add(line)
      }
      println("%s%s".format(reader.get.getPrompt, line))
      line
    } else {
      val line = reader.get.readLine
      if (line == null) "quit" else line
    }
    cmd.trim
  }

  def lineOutput(line: String): Unit = {
    println("  " + line)
  }
}