Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

load .inputrc file #5973

Merged
merged 1 commit into from
Oct 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ val completeProj = (project in file("internal") / "util-complete")
name := "Completion",
libraryDependencies += jline,
libraryDependencies += jline3Reader,
libraryDependencies += jline3Builtins,
mimaSettings,
// Parser is used publicly, so we can't break bincompat.
mimaBinaryIssueFilters := Seq(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ import org.jline.reader.{
}
import org.jline.utils.ClosedException
import sbt.internal.util.complete.Parser
import sbt.io.syntax._

import scala.annotation.tailrec
import scala.concurrent.duration._
import scala.util.control.NonFatal
import java.nio.channels.ClosedByInterruptException
import java.net.MalformedURLException

import org.jline.builtins.InputRC

trait LineReader extends AutoCloseable {
def readLine(prompt: String, mask: Option[Char] = None): Option[String]
Expand Down Expand Up @@ -70,6 +75,29 @@ object LineReader {
}
}
}
private[this] def inputrcFileUrl(): Option[URL] = {
// keep jline2 compatibility
// https://github.com/jline/jline2/blob/12b98d94589e3bd6a6/src/main/java/jline/console/ConsoleReader.java#L291-L306
sys.props
.get("jline.inputrc")
.flatMap { path =>
try {
Some(url(path))
} catch {
case _: MalformedURLException =>
Some(file(path).toURI.toURL)
}
}
.orElse {
sys.props.get("user.home").map { home =>
val f = file(home) / ".inputrc"
(if (f.isFile) f else file("/etc/inputrc")).toURI.toURL
}
}
}
// cache on memory.
private[this] lazy val inputrcFileContents: Option[Array[Byte]] =
inputrcFileUrl().map(in => sbt.io.IO.readBytes(in.openStream()))
def createReader(
historyPath: Option[File],
parser: Parser[_],
Expand All @@ -81,6 +109,17 @@ object LineReader {
override def readLine(prompt: String, mask: Option[Char]): Option[String] = {
val term = JLine3(terminal)
val reader = LineReaderBuilder.builder().terminal(term).completer(completer(parser)).build()
try {
inputrcFileContents.foreach { bytes =>
InputRC.configure(
reader,
new ByteArrayInputStream(bytes)
)
}
} catch {
case NonFatal(_) =>
// ignore
}
historyPath.foreach(f => reader.setVariable(JLineReader.HISTORY_FILE, f))
try terminal.withRawInput {
Option(mask.map(reader.readLine(prompt, _)).getOrElse(reader.readLine(prompt)))
Expand Down
1 change: 1 addition & 0 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ object Dependencies {
val jline3Jansi = "org.jline" % "jline-terminal-jansi" % jline3Version
val jline3JNA = "org.jline" % "jline-terminal-jna" % jline3Version
val jline3Reader = "org.jline" % "jline-reader" % jline3Version
val jline3Builtins = "org.jline" % "jline-builtins" % jline3Version
val jansi = "org.fusesource.jansi" % "jansi" % "1.18"
val scalatest = "org.scalatest" %% "scalatest" % "3.0.8"
val scalacheck = "org.scalacheck" %% "scalacheck" % "1.14.0"
Expand Down