From 45580f1562b52abbb3022354a90c27bacc76a67f Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Fri, 21 Jan 2011 18:40:25 +0000 Subject: Updated to new jline sources with it moved into... Updated to new jline sources with it moved into scala.tools.jline. I transitioned the jline build from maven to sbt, and this commit includes the first sbt-built binary. Review by jsuereth. --- src/jline/README.md | 13 +- src/jline/pom.xml | 2 +- src/jline/project/build.properties | 8 + src/jline/project/build/JlineProject.scala | 33 + src/jline/project/plugins/Plugins.scala | 5 + src/jline/project/plugins/project/build.properties | 3 + .../src/main/java/jline/AnsiWindowsTerminal.java | 90 - .../main/java/jline/NoInterruptUnixTerminal.java | 44 - src/jline/src/main/java/jline/Terminal.java | 59 - src/jline/src/main/java/jline/TerminalFactory.java | 173 -- src/jline/src/main/java/jline/TerminalSupport.java | 179 -- src/jline/src/main/java/jline/UnixTerminal.java | 232 --- .../src/main/java/jline/UnsupportedTerminal.java | 25 - src/jline/src/main/java/jline/WindowsTerminal.java | 466 ----- .../src/main/java/jline/console/ConsoleReader.java | 2160 -------------------- .../src/main/java/jline/console/CursorBuffer.java | 93 - src/jline/src/main/java/jline/console/Key.java | 74 - .../src/main/java/jline/console/Operation.java | 285 --- .../console/completer/AggregateCompleter.java | 108 - .../jline/console/completer/ArgumentCompleter.java | 398 ---- .../completer/CandidateListCompletionHandler.java | 192 -- .../java/jline/console/completer/Completer.java | 37 - .../jline/console/completer/CompletionHandler.java | 25 - .../jline/console/completer/EnumCompleter.java | 35 - .../jline/console/completer/FileNameCompleter.java | 133 -- .../jline/console/completer/NullCompleter.java | 39 - .../jline/console/completer/StringsCompleter.java | 79 - .../java/jline/console/completer/package-info.java | 22 - .../java/jline/console/history/FileHistory.java | 106 - .../main/java/jline/console/history/History.java | 71 - .../java/jline/console/history/MemoryHistory.java | 318 --- .../jline/console/history/PersistentHistory.java | 34 - .../java/jline/console/history/package-info.java | 22 - .../src/main/java/jline/console/package-info.java | 22 - .../main/java/jline/internal/Configuration.java | 127 -- src/jline/src/main/java/jline/internal/Log.java | 112 - .../internal/ReplayPrefixOneCharInputStream.java | 95 - .../java/jline/internal/TerminalLineSettings.java | 217 -- .../src/main/java/jline/internal/package-info.java | 22 - src/jline/src/main/java/jline/package-info.java | 22 - .../scala/tools/jline/AnsiWindowsTerminal.java | 90 + .../scala/tools/jline/NoInterruptUnixTerminal.java | 44 + .../src/main/java/scala/tools/jline/Terminal.java | 59 + .../java/scala/tools/jline/TerminalFactory.java | 173 ++ .../java/scala/tools/jline/TerminalSupport.java | 179 ++ .../main/java/scala/tools/jline/UnixTerminal.java | 232 +++ .../scala/tools/jline/UnsupportedTerminal.java | 25 + .../java/scala/tools/jline/WindowsTerminal.java | 466 +++++ .../scala/tools/jline/console/ConsoleReader.java | 2160 ++++++++++++++++++++ .../scala/tools/jline/console/CursorBuffer.java | 93 + .../main/java/scala/tools/jline/console/Key.java | 74 + .../java/scala/tools/jline/console/Operation.java | 285 +++ .../console/completer/AggregateCompleter.java | 108 + .../jline/console/completer/ArgumentCompleter.java | 398 ++++ .../completer/CandidateListCompletionHandler.java | 192 ++ .../tools/jline/console/completer/Completer.java | 37 + .../jline/console/completer/CompletionHandler.java | 25 + .../jline/console/completer/EnumCompleter.java | 35 + .../jline/console/completer/FileNameCompleter.java | 133 ++ .../jline/console/completer/NullCompleter.java | 39 + .../jline/console/completer/StringsCompleter.java | 79 + .../jline/console/completer/package-info.java | 22 + .../tools/jline/console/history/FileHistory.java | 106 + .../scala/tools/jline/console/history/History.java | 71 + .../tools/jline/console/history/MemoryHistory.java | 318 +++ .../jline/console/history/PersistentHistory.java | 34 + .../tools/jline/console/history/package-info.java | 22 + .../scala/tools/jline/console/package-info.java | 22 + .../scala/tools/jline/internal/Configuration.java | 127 ++ .../main/java/scala/tools/jline/internal/Log.java | 112 + .../internal/ReplayPrefixOneCharInputStream.java | 95 + .../tools/jline/internal/TerminalLineSettings.java | 217 ++ .../scala/tools/jline/internal/package-info.java | 22 + .../main/java/scala/tools/jline/package-info.java | 22 + .../CandidateListCompletionHandler.properties | 4 - .../main/resources/jline/keybindings.properties | 65 - .../resources/jline/windowsbindings.properties | 71 - .../CandidateListCompletionHandler.properties | 4 + .../scala/tools/jline/keybindings.properties | 65 + .../scala/tools/jline/windowsbindings.properties | 71 + .../src/test/java/jline/TerminalFactoryTest.java | 34 - .../test/java/jline/console/ConsoleReaderTest.java | 261 --- .../jline/console/ConsoleReaderTestSupport.java | 142 -- .../src/test/java/jline/console/EditLineTest.java | 172 -- .../console/completer/ArgumentCompleterTest.java | 46 - .../jline/console/completer/NullCompleterTest.java | 39 - .../console/completer/StringsCompleterTest.java | 40 - .../java/jline/console/history/HistoryTest.java | 79 - .../jline/console/history/MemoryHistoryTest.java | 99 - src/jline/src/test/java/jline/example/Example.java | 107 - .../jline/internal/TerminalLineSettingsTest.java | 146 -- .../scala/tools/jline/TerminalFactoryTest.java | 34 + .../tools/jline/console/ConsoleReaderTest.java | 261 +++ .../jline/console/ConsoleReaderTestSupport.java | 142 ++ .../scala/tools/jline/console/EditLineTest.java | 172 ++ .../console/completer/ArgumentCompleterTest.java | 46 + .../jline/console/completer/NullCompleterTest.java | 39 + .../console/completer/StringsCompleterTest.java | 40 + .../tools/jline/console/history/HistoryTest.java | 79 + .../jline/console/history/MemoryHistoryTest.java | 99 + .../java/scala/tools/jline/example/Example.java | 107 + .../jline/internal/TerminalLineSettingsTest.java | 146 ++ 102 files changed, 7476 insertions(+), 7430 deletions(-) create mode 100644 src/jline/project/build.properties create mode 100644 src/jline/project/build/JlineProject.scala create mode 100644 src/jline/project/plugins/Plugins.scala create mode 100644 src/jline/project/plugins/project/build.properties delete mode 100644 src/jline/src/main/java/jline/AnsiWindowsTerminal.java delete mode 100644 src/jline/src/main/java/jline/NoInterruptUnixTerminal.java delete mode 100644 src/jline/src/main/java/jline/Terminal.java delete mode 100644 src/jline/src/main/java/jline/TerminalFactory.java delete mode 100644 src/jline/src/main/java/jline/TerminalSupport.java delete mode 100644 src/jline/src/main/java/jline/UnixTerminal.java delete mode 100644 src/jline/src/main/java/jline/UnsupportedTerminal.java delete mode 100644 src/jline/src/main/java/jline/WindowsTerminal.java delete mode 100644 src/jline/src/main/java/jline/console/ConsoleReader.java delete mode 100644 src/jline/src/main/java/jline/console/CursorBuffer.java delete mode 100644 src/jline/src/main/java/jline/console/Key.java delete mode 100644 src/jline/src/main/java/jline/console/Operation.java delete mode 100644 src/jline/src/main/java/jline/console/completer/AggregateCompleter.java delete mode 100644 src/jline/src/main/java/jline/console/completer/ArgumentCompleter.java delete mode 100644 src/jline/src/main/java/jline/console/completer/CandidateListCompletionHandler.java delete mode 100644 src/jline/src/main/java/jline/console/completer/Completer.java delete mode 100644 src/jline/src/main/java/jline/console/completer/CompletionHandler.java delete mode 100644 src/jline/src/main/java/jline/console/completer/EnumCompleter.java delete mode 100644 src/jline/src/main/java/jline/console/completer/FileNameCompleter.java delete mode 100644 src/jline/src/main/java/jline/console/completer/NullCompleter.java delete mode 100644 src/jline/src/main/java/jline/console/completer/StringsCompleter.java delete mode 100644 src/jline/src/main/java/jline/console/completer/package-info.java delete mode 100644 src/jline/src/main/java/jline/console/history/FileHistory.java delete mode 100644 src/jline/src/main/java/jline/console/history/History.java delete mode 100644 src/jline/src/main/java/jline/console/history/MemoryHistory.java delete mode 100644 src/jline/src/main/java/jline/console/history/PersistentHistory.java delete mode 100644 src/jline/src/main/java/jline/console/history/package-info.java delete mode 100644 src/jline/src/main/java/jline/console/package-info.java delete mode 100644 src/jline/src/main/java/jline/internal/Configuration.java delete mode 100644 src/jline/src/main/java/jline/internal/Log.java delete mode 100644 src/jline/src/main/java/jline/internal/ReplayPrefixOneCharInputStream.java delete mode 100644 src/jline/src/main/java/jline/internal/TerminalLineSettings.java delete mode 100644 src/jline/src/main/java/jline/internal/package-info.java delete mode 100644 src/jline/src/main/java/jline/package-info.java create mode 100644 src/jline/src/main/java/scala/tools/jline/AnsiWindowsTerminal.java create mode 100644 src/jline/src/main/java/scala/tools/jline/NoInterruptUnixTerminal.java create mode 100644 src/jline/src/main/java/scala/tools/jline/Terminal.java create mode 100644 src/jline/src/main/java/scala/tools/jline/TerminalFactory.java create mode 100644 src/jline/src/main/java/scala/tools/jline/TerminalSupport.java create mode 100644 src/jline/src/main/java/scala/tools/jline/UnixTerminal.java create mode 100644 src/jline/src/main/java/scala/tools/jline/UnsupportedTerminal.java create mode 100644 src/jline/src/main/java/scala/tools/jline/WindowsTerminal.java create mode 100644 src/jline/src/main/java/scala/tools/jline/console/ConsoleReader.java create mode 100644 src/jline/src/main/java/scala/tools/jline/console/CursorBuffer.java create mode 100644 src/jline/src/main/java/scala/tools/jline/console/Key.java create mode 100644 src/jline/src/main/java/scala/tools/jline/console/Operation.java create mode 100644 src/jline/src/main/java/scala/tools/jline/console/completer/AggregateCompleter.java create mode 100644 src/jline/src/main/java/scala/tools/jline/console/completer/ArgumentCompleter.java create mode 100644 src/jline/src/main/java/scala/tools/jline/console/completer/CandidateListCompletionHandler.java create mode 100644 src/jline/src/main/java/scala/tools/jline/console/completer/Completer.java create mode 100644 src/jline/src/main/java/scala/tools/jline/console/completer/CompletionHandler.java create mode 100644 src/jline/src/main/java/scala/tools/jline/console/completer/EnumCompleter.java create mode 100644 src/jline/src/main/java/scala/tools/jline/console/completer/FileNameCompleter.java create mode 100644 src/jline/src/main/java/scala/tools/jline/console/completer/NullCompleter.java create mode 100644 src/jline/src/main/java/scala/tools/jline/console/completer/StringsCompleter.java create mode 100644 src/jline/src/main/java/scala/tools/jline/console/completer/package-info.java create mode 100644 src/jline/src/main/java/scala/tools/jline/console/history/FileHistory.java create mode 100644 src/jline/src/main/java/scala/tools/jline/console/history/History.java create mode 100644 src/jline/src/main/java/scala/tools/jline/console/history/MemoryHistory.java create mode 100644 src/jline/src/main/java/scala/tools/jline/console/history/PersistentHistory.java create mode 100644 src/jline/src/main/java/scala/tools/jline/console/history/package-info.java create mode 100644 src/jline/src/main/java/scala/tools/jline/console/package-info.java create mode 100644 src/jline/src/main/java/scala/tools/jline/internal/Configuration.java create mode 100644 src/jline/src/main/java/scala/tools/jline/internal/Log.java create mode 100644 src/jline/src/main/java/scala/tools/jline/internal/ReplayPrefixOneCharInputStream.java create mode 100644 src/jline/src/main/java/scala/tools/jline/internal/TerminalLineSettings.java create mode 100644 src/jline/src/main/java/scala/tools/jline/internal/package-info.java create mode 100644 src/jline/src/main/java/scala/tools/jline/package-info.java delete mode 100644 src/jline/src/main/resources/jline/console/completer/CandidateListCompletionHandler.properties delete mode 100644 src/jline/src/main/resources/jline/keybindings.properties delete mode 100644 src/jline/src/main/resources/jline/windowsbindings.properties create mode 100644 src/jline/src/main/resources/scala/tools/jline/console/completer/CandidateListCompletionHandler.properties create mode 100644 src/jline/src/main/resources/scala/tools/jline/keybindings.properties create mode 100644 src/jline/src/main/resources/scala/tools/jline/windowsbindings.properties delete mode 100644 src/jline/src/test/java/jline/TerminalFactoryTest.java delete mode 100644 src/jline/src/test/java/jline/console/ConsoleReaderTest.java delete mode 100644 src/jline/src/test/java/jline/console/ConsoleReaderTestSupport.java delete mode 100644 src/jline/src/test/java/jline/console/EditLineTest.java delete mode 100644 src/jline/src/test/java/jline/console/completer/ArgumentCompleterTest.java delete mode 100644 src/jline/src/test/java/jline/console/completer/NullCompleterTest.java delete mode 100644 src/jline/src/test/java/jline/console/completer/StringsCompleterTest.java delete mode 100644 src/jline/src/test/java/jline/console/history/HistoryTest.java delete mode 100644 src/jline/src/test/java/jline/console/history/MemoryHistoryTest.java delete mode 100644 src/jline/src/test/java/jline/example/Example.java delete mode 100644 src/jline/src/test/java/jline/internal/TerminalLineSettingsTest.java create mode 100644 src/jline/src/test/java/scala/tools/jline/TerminalFactoryTest.java create mode 100644 src/jline/src/test/java/scala/tools/jline/console/ConsoleReaderTest.java create mode 100644 src/jline/src/test/java/scala/tools/jline/console/ConsoleReaderTestSupport.java create mode 100644 src/jline/src/test/java/scala/tools/jline/console/EditLineTest.java create mode 100644 src/jline/src/test/java/scala/tools/jline/console/completer/ArgumentCompleterTest.java create mode 100644 src/jline/src/test/java/scala/tools/jline/console/completer/NullCompleterTest.java create mode 100644 src/jline/src/test/java/scala/tools/jline/console/completer/StringsCompleterTest.java create mode 100644 src/jline/src/test/java/scala/tools/jline/console/history/HistoryTest.java create mode 100644 src/jline/src/test/java/scala/tools/jline/console/history/MemoryHistoryTest.java create mode 100644 src/jline/src/test/java/scala/tools/jline/example/Example.java create mode 100644 src/jline/src/test/java/scala/tools/jline/internal/TerminalLineSettingsTest.java (limited to 'src/jline') diff --git a/src/jline/README.md b/src/jline/README.md index 6d129e721d..829476145d 100644 --- a/src/jline/README.md +++ b/src/jline/README.md @@ -13,15 +13,12 @@ Building ### Requirements -* Maven 2+ +* SBT * Java 5+ -Check out and build: +This is a fork with scala specific modifications. +The original repository was: git://github.com/jdillon/jline2.git - git clone git://github.com/jdillon/jline2.git - cd jline2 - mvn install +You can now build with sbt: -To build the jdk14 jar: - - mvn install -Dretro + sbt update proguard diff --git a/src/jline/pom.xml b/src/jline/pom.xml index 71043e32c2..08a59b054d 100644 --- a/src/jline/pom.xml +++ b/src/jline/pom.xml @@ -167,7 +167,7 @@ - !jline*,javax.swing;resolution:=optional,* + !scala.tools.jline*,javax.swing;resolution:=optional,* * diff --git a/src/jline/project/build.properties b/src/jline/project/build.properties new file mode 100644 index 0000000000..c6143fe8df --- /dev/null +++ b/src/jline/project/build.properties @@ -0,0 +1,8 @@ +#Project properties +project.organization=org.improving +project.name=jline +sbt.version=0.7.5.RC0 +project.version=0.98 +#build.scala.versions=2.8.1 +build.scala.versions=2.9.0-SNAPSHOT +project.initialize=false diff --git a/src/jline/project/build/JlineProject.scala b/src/jline/project/build/JlineProject.scala new file mode 100644 index 0000000000..721cf607d3 --- /dev/null +++ b/src/jline/project/build/JlineProject.scala @@ -0,0 +1,33 @@ +import sbt._ + +/** I'm sure much of this is done the hard way, but it's done! + */ +class JlineProject(info: ProjectInfo) extends DefaultProject(info) with ProguardProject { + val snapShots = "Snapshots" at "http://scala-tools.org/repo-snapshots/" + val jansi = "org.fusesource.jansi" % "jansi" % "1.4" + val junitInterface = "com.novocode" % "junit-interface" % "0.5" % "test->default" + + // val junit = "junit" % "junit" % "4.8.1" % "test" + // lazy val jansiPath = (managedDependencyPath / "compile" ** "jansi*").get.toList.head.absolutePath + + override def makeInJarFilter(file: String) = { + if (!file.startsWith("jansi")) super.makeInJarFilter(file) + else List( + "!META-INF/MANIFEST.MF", + "org/fusesource/hawtjni/runtime", + "org/fusesource/hawtjni/runtime/Callback.class", + "org/fusesource/hawtjni/runtime/Library.class", + "!org/fusesource/hawtjni/**", + "!META-INF/maven/org.fusesource.hawtjni", + "!META-INF/maven/org.fusesource.jansi", + "!META-INF/maven/org.fusesource.hawtjni/**", + "!META-INF/maven/org.fusesource.jansi/**" + ) mkString ", " + } + + override def proguardOptions = List( + "-dontshrink", + "-keep class *", + "-keepdirectories" + ) +} diff --git a/src/jline/project/plugins/Plugins.scala b/src/jline/project/plugins/Plugins.scala new file mode 100644 index 0000000000..8c336f0abf --- /dev/null +++ b/src/jline/project/plugins/Plugins.scala @@ -0,0 +1,5 @@ +import sbt._ + +class Plugins(info: ProjectInfo) extends PluginDefinition(info) { + val proguard = "org.scala-tools.sbt" % "sbt-proguard-plugin" % "0.0.5" +} diff --git a/src/jline/project/plugins/project/build.properties b/src/jline/project/plugins/project/build.properties new file mode 100644 index 0000000000..f39984bd73 --- /dev/null +++ b/src/jline/project/plugins/project/build.properties @@ -0,0 +1,3 @@ +#Project properties +#Fri Jan 21 08:49:59 PST 2011 +plugin.uptodate=true diff --git a/src/jline/src/main/java/jline/AnsiWindowsTerminal.java b/src/jline/src/main/java/jline/AnsiWindowsTerminal.java deleted file mode 100644 index aefbfff363..0000000000 --- a/src/jline/src/main/java/jline/AnsiWindowsTerminal.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright (C) 2009 the original author(s). - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * MODIFICATIONS: methods to deal with wrapping the output stream. - */ - -package jline; - -import org.fusesource.jansi.AnsiConsole; -import org.fusesource.jansi.AnsiOutputStream; -import org.fusesource.jansi.WindowsAnsiOutputStream; - -import java.io.ByteArrayOutputStream; -import java.io.OutputStream; - -/** - * ANSI-supported {@link WindowsTerminal}. - * - * @since 2.0 - */ -public class AnsiWindowsTerminal - extends WindowsTerminal -{ - private final boolean ansiSupported = detectAnsiSupport(); - - @Override - public OutputStream wrapOutIfNeeded(OutputStream out) { - return wrapOutputStream(out); - } - - /** - * Returns an ansi output stream handler. We return whatever was - * passed if we determine we cannot handle ansi based on Kernel32 calls. - * - * @return an @{link AltWindowAnsiOutputStream} instance or the passed - * stream. - */ - private static OutputStream wrapOutputStream(final OutputStream stream) { - String os = System.getProperty("os.name"); - if( os.startsWith("Windows") ) { - // On windows we know the console does not interpret ANSI codes.. - try { - return new WindowsAnsiOutputStream(stream); - } catch (Throwable ignore) { - // this happens when JNA is not in the path.. or - // this happens when the stdout is being redirected to a file. - } - // Use the ANSIOutputStream to strip out the ANSI escape sequences. - return new AnsiOutputStream(stream); - } - return stream; - } - - private static boolean detectAnsiSupport() { - OutputStream out = AnsiConsole.wrapOutputStream(new ByteArrayOutputStream()); - try { - out.close(); - } - catch (Exception e) { - // ignore; - } - return out instanceof WindowsAnsiOutputStream; - } - - public AnsiWindowsTerminal() throws Exception { - super(); - } - - @Override - public boolean isAnsiSupported() { - return ansiSupported; - } - - @Override - public boolean hasWeirdWrap() { - return false; - } -} diff --git a/src/jline/src/main/java/jline/NoInterruptUnixTerminal.java b/src/jline/src/main/java/jline/NoInterruptUnixTerminal.java deleted file mode 100644 index b2cd22b5c7..0000000000 --- a/src/jline/src/main/java/jline/NoInterruptUnixTerminal.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (C) 2009 the original author(s). - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package jline; - -// Based on Apache Karaf impl - -/** - * Non-interruptable (via CTRL-C) {@link UnixTerminal}. - * - * @since 2.0 - */ -public class NoInterruptUnixTerminal - extends UnixTerminal -{ - public NoInterruptUnixTerminal() throws Exception { - super(); - } - - @Override - public void init() throws Exception { - super.init(); - getSettings().set("intr undef"); - } - - @Override - public void restore() throws Exception { - getSettings().set("intr ^C"); - super.restore(); - } -} diff --git a/src/jline/src/main/java/jline/Terminal.java b/src/jline/src/main/java/jline/Terminal.java deleted file mode 100644 index a9ede87814..0000000000 --- a/src/jline/src/main/java/jline/Terminal.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. - * - * This software is distributable under the BSD license. See the terms of the - * BSD license in the documentation provided with this software. - */ - -package jline; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; - -/** - * Representation of the input terminal for a platform. - * - * @author Marc Prud'hommeaux - * @author Jason Dillon - * @since 2.0 - */ -public interface Terminal -{ - void init() throws Exception; - - void restore() throws Exception; - - void reset() throws Exception; - - boolean isSupported(); - - int getWidth(); - - int getHeight(); - - boolean isAnsiSupported(); - - /** - * When ANSI is not natively handled, the output will have to be wrapped. - */ - OutputStream wrapOutIfNeeded(OutputStream out); - - /** - * For terminals that don't wrap when character is written in last column, - * only when the next character is written. - * These are the ones that have 'am' and 'xn' termcap attributes (xterm and - * rxvt flavors falls under that category) - */ - boolean hasWeirdWrap(); - - boolean isEchoEnabled(); - - void setEchoEnabled(boolean enabled); - - int readCharacter(InputStream in) throws IOException; - - int readVirtualKey(InputStream in) throws IOException; - - InputStream getDefaultBindings(); -} diff --git a/src/jline/src/main/java/jline/TerminalFactory.java b/src/jline/src/main/java/jline/TerminalFactory.java deleted file mode 100644 index 7a45cc9c51..0000000000 --- a/src/jline/src/main/java/jline/TerminalFactory.java +++ /dev/null @@ -1,173 +0,0 @@ -/* - * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. - * - * This software is distributable under the BSD license. See the terms of the - * BSD license in the documentation provided with this software. - */ - -package jline; - -import jline.internal.Configuration; -import jline.internal.Log; - -import java.text.MessageFormat; -import java.util.HashMap; -import java.util.Map; - -/** - * Creates terminal instances. - * - * @author Jason Dillon - * @since 2.0 - */ -public class TerminalFactory -{ - public static final String JLINE_TERMINAL = "jline.terminal"; - - public static final String AUTO = "auto"; - - public static final String UNIX = "unix"; - - public static final String WIN = "win"; - - public static final String WINDOWS = "windows"; - - public static final String NONE = "none"; - - public static final String OFF = "off"; - - public static final String FALSE = "false"; - - private static final InheritableThreadLocal holder = new InheritableThreadLocal(); - - public static synchronized Terminal create() { - if (Log.TRACE) { - //noinspection ThrowableInstanceNeverThrown - Log.trace(new Throwable("CREATE MARKER")); - } - - String type = Configuration.getString(JLINE_TERMINAL); - if (type == null) { - type = AUTO; - } - - Log.debug("Creating terminal; type=", type); - - Terminal t; - try { - String tmp = type.toLowerCase(); - - if (tmp.equals(UNIX)) { - t = getFlavor(Flavor.UNIX); - } - else if (tmp.equals(WIN) | tmp.equals(WINDOWS)) { - t = getFlavor(Flavor.WINDOWS); - } - else if (tmp.equals(NONE) || tmp.equals(OFF) || tmp.equals(FALSE)) { - t = new UnsupportedTerminal(); - } - else { - if (tmp.equals(AUTO)) { - String os = Configuration.getOsName(); - Flavor flavor = Flavor.UNIX; - if (os.contains(WINDOWS)) { - flavor = Flavor.WINDOWS; - } - t = getFlavor(flavor); - } - else { - try { - t = (Terminal) Thread.currentThread().getContextClassLoader().loadClass(type).newInstance(); - } - catch (Exception e) { - throw new IllegalArgumentException(MessageFormat.format("Invalid terminal type: {0}", type), e); - } - } - } - } - catch (Exception e) { - Log.error("Failed to construct terminal; falling back to unsupported", e); - t = new UnsupportedTerminal(); - } - - Log.debug("Created Terminal: ", t); - - try { - t.init(); - } - catch (Exception e) { - Log.error("Terminal initialization failed; falling back to unsupported", e); - return new UnsupportedTerminal(); - } - - return t; - } - - public static synchronized void reset() { - holder.remove(); - } - - public static synchronized void resetIf(final Terminal t) { - if (holder.get() == t) { - reset(); - } - } - - public static enum Type - { - AUTO, - WINDOWS, - UNIX, - NONE - } - - public static synchronized void configure(final String type) { - assert type != null; - System.setProperty(JLINE_TERMINAL, type); - } - - public static synchronized void configure(final Type type) { - assert type != null; - configure(type.name().toLowerCase()); - } - - // - // Flavor Support - // - - public static enum Flavor - { - WINDOWS, - UNIX - } - - private static final Map> FLAVORS = new HashMap>(); - - static { - registerFlavor(Flavor.WINDOWS, AnsiWindowsTerminal.class); - registerFlavor(Flavor.UNIX, UnixTerminal.class); - } - - public static synchronized Terminal get() { - Terminal t = holder.get(); - if (t == null) { - t = create(); - holder.set(t); - } - return t; - } - - public static Terminal getFlavor(final Flavor flavor) throws Exception { - Class type = FLAVORS.get(flavor); - if (type != null) { - return type.newInstance(); - } - - throw new InternalError(); - } - - public static void registerFlavor(final Flavor flavor, final Class type) { - FLAVORS.put(flavor, type); - } - -} \ No newline at end of file diff --git a/src/jline/src/main/java/jline/TerminalSupport.java b/src/jline/src/main/java/jline/TerminalSupport.java deleted file mode 100644 index ee7d2008ca..0000000000 --- a/src/jline/src/main/java/jline/TerminalSupport.java +++ /dev/null @@ -1,179 +0,0 @@ -/* - * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. - * - * This software is distributable under the BSD license. See the terms of the - * BSD license in the documentation provided with this software. - */ - -package jline; - -import jline.internal.Log; -import jline.internal.Configuration; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; - -/** - * Provides support for {@link Terminal} instances. - * - * @author Jason Dillon - * @since 2.0 - */ -public abstract class TerminalSupport - implements Terminal -{ - public static String DEFAULT_KEYBINDINGS_PROPERTIES = "keybindings.properties"; - - public static final String JLINE_SHUTDOWNHOOK = "jline.shutdownhook"; - - public static final int DEFAULT_WIDTH = 80; - - public static final int DEFAULT_HEIGHT = 24; - - private Thread shutdownHook; - - private boolean shutdownHookEnabled; - - private boolean supported; - - private boolean echoEnabled; - - private boolean ansiSupported; - - protected TerminalSupport(final boolean supported) { - this.supported = supported; - this.shutdownHookEnabled = Configuration.getBoolean(JLINE_SHUTDOWNHOOK, false); - } - - public void init() throws Exception { - installShutdownHook(new RestoreHook()); - } - - public void restore() throws Exception { - TerminalFactory.resetIf(this); - removeShutdownHook(); - } - - public void reset() throws Exception { - restore(); - init(); - } - - // Shutdown hooks causes classloader leakage in sbt, - // so they are only installed if -Djline.shutdownhook is true. - protected void installShutdownHook(final Thread hook) { - if (!shutdownHookEnabled) { - Log.debug("Not install shutdown hook " + hook + " because they are disabled."); - return; - } - - assert hook != null; - - if (shutdownHook != null) { - throw new IllegalStateException("Shutdown hook already installed"); - } - - try { - Runtime.getRuntime().addShutdownHook(hook); - shutdownHook = hook; - } - catch (AbstractMethodError e) { - // JDK 1.3+ only method. Bummer. - Log.trace("Failed to register shutdown hook: ", e); - } - } - - protected void removeShutdownHook() { - if (!shutdownHookEnabled) - return; - - if (shutdownHook != null) { - try { - Runtime.getRuntime().removeShutdownHook(shutdownHook); - } - catch (AbstractMethodError e) { - // JDK 1.3+ only method. Bummer. - Log.trace("Failed to remove shutdown hook: ", e); - } - catch (IllegalStateException e) { - // The VM is shutting down, not a big deal; ignore - } - shutdownHook = null; - } - } - - public final boolean isSupported() { - return supported; - } - - public synchronized boolean isAnsiSupported() { - return ansiSupported; - } - - protected synchronized void setAnsiSupported(final boolean supported) { - this.ansiSupported = supported; - Log.debug("Ansi supported: ", supported); - } - - /** - * Subclass to change behavior if needed. - * @return the passed out - */ - public OutputStream wrapOutIfNeeded(OutputStream out) { - return out; - } - - /** - * Defaults to true which was the behaviour before this method was added. - */ - public boolean hasWeirdWrap() { - return true; - } - - public int getWidth() { - return DEFAULT_WIDTH; - } - - public int getHeight() { - return DEFAULT_HEIGHT; - } - - public synchronized boolean isEchoEnabled() { - return echoEnabled; - } - - public synchronized void setEchoEnabled(final boolean enabled) { - this.echoEnabled = enabled; - Log.debug("Echo enabled: ", enabled); - } - - public int readCharacter(final InputStream in) throws IOException { - return in.read(); - } - - public int readVirtualKey(final InputStream in) throws IOException { - return readCharacter(in); - } - - public InputStream getDefaultBindings() { - return TerminalSupport.class.getResourceAsStream(DEFAULT_KEYBINDINGS_PROPERTIES); - } - - // - // RestoreHook - // - - protected class RestoreHook - extends Thread - { - public void start() { - try { - restore(); - } - catch (Exception e) { - Log.trace("Failed to restore: ", e); - } - } - } -} \ No newline at end of file diff --git a/src/jline/src/main/java/jline/UnixTerminal.java b/src/jline/src/main/java/jline/UnixTerminal.java deleted file mode 100644 index 755d899765..0000000000 --- a/src/jline/src/main/java/jline/UnixTerminal.java +++ /dev/null @@ -1,232 +0,0 @@ -/* - * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. - * - * This software is distributable under the BSD license. See the terms of the - * BSD license in the documentation provided with this software. - */ - -package jline; - -import jline.console.Key; -import jline.internal.Configuration; -import jline.internal.Log; -import jline.internal.ReplayPrefixOneCharInputStream; -import jline.internal.TerminalLineSettings; - -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.util.HashMap; -import java.util.Map; - -import static jline.UnixTerminal.UnixKey.*; -import static jline.console.Key.*; - -/** - * Terminal that is used for unix platforms. Terminal initialization - * is handled by issuing the stty command against the - * /dev/tty file to disable character echoing and enable - * character input. All known unix systems (including - * Linux and Macintosh OS X) support the stty), so this - * implementation should work for an reasonable POSIX system. - * - * @author Marc Prud'hommeaux - * @author Dale Kemp - * @author Jason Dillon - * @author Jean-Baptiste Onofré - * @since 2.0 - */ -public class UnixTerminal - extends TerminalSupport -{ - private final TerminalLineSettings settings = new TerminalLineSettings(); - - private final ReplayPrefixOneCharInputStream replayStream; - - private final InputStreamReader replayReader; - - public UnixTerminal() throws Exception { - super(true); - - this.replayStream = new ReplayPrefixOneCharInputStream(Configuration.getInputEncoding()); - this.replayReader = new InputStreamReader(replayStream, replayStream.getEncoding()); - } - - protected TerminalLineSettings getSettings() { - return settings; - } - - /** - * Remove line-buffered input by invoking "stty -icanon min 1" - * against the current terminal. - */ - @Override - public void init() throws Exception { - super.init(); - - setAnsiSupported(true); - - // set the console to be character-buffered instead of line-buffered - settings.set("-icanon min 1"); - - setEchoEnabled(false); - } - - /** - * Restore the original terminal configuration, which can be used when - * shutting down the console reader. The ConsoleReader cannot be - * used after calling this method. - */ - @Override - public void restore() throws Exception { - settings.restore(); - super.restore(); - // print a newline after the terminal exits. - // this should probably be a configurable. - System.out.println(); - } - - /** - * Returns the value of stty columns param. - */ - @Override - public int getWidth() { - int w = settings.getProperty("columns"); - return w < 1 ? DEFAULT_WIDTH : w; - } - - /** - * Returns the value of stty rows>/tt> param. - */ - @Override - public int getHeight() { - int h = settings.getProperty("rows"); - return h < 1 ? DEFAULT_HEIGHT : h; - } - - @Override - public synchronized void setEchoEnabled(final boolean enabled) { - try { - if (enabled) { - settings.set("echo"); - } - else { - settings.set("-echo"); - } - super.setEchoEnabled(enabled); - } - catch (Exception e) { - Log.error("Failed to ", (enabled ? "enable" : "disable"), " echo: ", e); - } - } - - @Override - public int readVirtualKey(final InputStream in) throws IOException { - int c = readCharacter(in); - - if (Key.valueOf(c) == DELETE && settings.getProperty("erase") == DELETE.code) { - c = BACKSPACE.code; - } - - UnixKey key = UnixKey.valueOf(c); - - // in Unix terminals, arrow keys are represented by a sequence of 3 characters. E.g., the up arrow key yields 27, 91, 68 - if (key == ARROW_START) { - // also the escape key is 27 thats why we read until we have something different than 27 - // this is a bugfix, because otherwise pressing escape and than an arrow key was an undefined state - while (key == ARROW_START) { - c = readCharacter(in); - key = UnixKey.valueOf(c); - } - - if (key == ARROW_PREFIX || key == O_PREFIX) { - c = readCharacter(in); - key = UnixKey.valueOf(c); - - if (key == ARROW_UP) { - return CTRL_P.code; - } - else if (key == ARROW_DOWN) { - return CTRL_N.code; - } - else if (key == ARROW_LEFT) { - return CTRL_B.code; - } - else if (key == ARROW_RIGHT) { - return CTRL_F.code; - } - else if (key == HOME_CODE) { - return CTRL_A.code; - } - else if (key == END_CODE) { - return CTRL_E.code; - } - else if (key == DEL_THIRD) { - readCharacter(in); // read 4th & ignore - return DELETE.code; - } - } - } - - // handle unicode characters, thanks for a patch from amyi@inf.ed.ac.uk - if (c > 128) { - // handle unicode characters longer than 2 bytes, - // thanks to Marc.Herbert@continuent.com - replayStream.setInput(c, in); - // replayReader = new InputStreamReader(replayStream, encoding); - c = replayReader.read(); - } - - return c; - } - - /** - * Unix keys. - */ - public static enum UnixKey - { - ARROW_START(27), - - ARROW_PREFIX(91), - - ARROW_LEFT(68), - - ARROW_RIGHT(67), - - ARROW_UP(65), - - ARROW_DOWN(66), - - O_PREFIX(79), - - HOME_CODE(72), - - END_CODE(70), - - DEL_THIRD(51), - - DEL_SECOND(126),; - - public final short code; - - UnixKey(final int code) { - this.code = (short) code; - } - - private static final Map codes; - - static { - Map map = new HashMap(); - - for (UnixKey key : UnixKey.values()) { - map.put(key.code, key); - } - - codes = map; - } - - public static UnixKey valueOf(final int code) { - return codes.get((short) code); - } - } -} \ No newline at end of file diff --git a/src/jline/src/main/java/jline/UnsupportedTerminal.java b/src/jline/src/main/java/jline/UnsupportedTerminal.java deleted file mode 100644 index 55c399c311..0000000000 --- a/src/jline/src/main/java/jline/UnsupportedTerminal.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. - * - * This software is distributable under the BSD license. See the terms of the - * BSD license in the documentation provided with this software. - */ - -package jline; - -/** - * An unsupported terminal. - * - * @author Marc Prud'hommeaux - * @author Jason Dillon - * @since 2.0 - */ -public class UnsupportedTerminal - extends TerminalSupport -{ - public UnsupportedTerminal() { - super(false); - setAnsiSupported(false); - setEchoEnabled(true); - } -} diff --git a/src/jline/src/main/java/jline/WindowsTerminal.java b/src/jline/src/main/java/jline/WindowsTerminal.java deleted file mode 100644 index dcaa3a9cde..0000000000 --- a/src/jline/src/main/java/jline/WindowsTerminal.java +++ /dev/null @@ -1,466 +0,0 @@ -/* - * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. - * - * This software is distributable under the BSD license. See the terms of the - * BSD license in the documentation provided with this software. - */ - -package jline; - -import java.io.FileDescriptor; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.util.HashMap; -import java.util.Map; - -import jline.internal.Configuration; -import org.fusesource.jansi.internal.WindowsSupport; - -import jline.internal.Log; -import jline.internal.ReplayPrefixOneCharInputStream; - -import static jline.WindowsTerminal.ConsoleMode.*; -import static jline.WindowsTerminal.WindowsKey.*; -import static jline.console.Key.*; - -/** - * Terminal implementation for Microsoft Windows. Terminal initialization in - * {@link #init} is accomplished by extracting the - * jline_version.dll, saving it to the system temporary - * directoy (determined by the setting of the java.io.tmpdir System - * property), loading the library, and then calling the Win32 APIs SetConsoleMode and - * GetConsoleMode to - * disable character echoing. - *

- *

- * By default, the {@link #readCharacter} method will attempt to test to see if - * the specified {@link InputStream} is {@link System#in} or a wrapper around - * {@link FileDescriptor#in}, and if so, will bypass the character reading to - * directly invoke the readc() method in the JNI library. This is so the class - * can read special keys (like arrow keys) which are otherwise inaccessible via - * the {@link System#in} stream. Using JNI reading can be bypassed by setting - * the jline.WindowsTerminal.directConsole system property - * to false. - *

- * - * @author Marc Prud'hommeaux - * @author Jason Dillon - * @since 2.0 - */ -public class WindowsTerminal - extends TerminalSupport -{ - public static final String JLINE_WINDOWS_TERMINAL_INPUT_ENCODING = "jline.WindowsTerminal.input.encoding"; - - public static final String JLINE_WINDOWS_TERMINAL_OUTPUT_ENCODING = "jline.WindowsTerminal.output.encoding"; - - public static final String JLINE_WINDOWS_TERMINAL_DIRECT_CONSOLE = "jline.WindowsTerminal.directConsole"; - - public static final String WINDOWSBINDINGS_PROPERTIES = "windowsbindings.properties"; - - public static final String ANSI = WindowsTerminal.class.getName() + ".ansi"; - - private boolean directConsole; - - private int originalMode; - - private final ReplayPrefixOneCharInputStream replayStream; - - private final InputStreamReader replayReader; - - public WindowsTerminal() throws Exception { - super(true); - - this.replayStream = - new ReplayPrefixOneCharInputStream(Configuration.getString(JLINE_WINDOWS_TERMINAL_INPUT_ENCODING, Configuration.getFileEncoding())); - this.replayReader = new InputStreamReader(replayStream, replayStream.getEncoding()); - } - - @Override - public void init() throws Exception { - super.init(); - - setAnsiSupported(Boolean.getBoolean(ANSI)); - - // - // FIXME: Need a way to disable direct console and sysin detection muck - // - - setDirectConsole(Boolean.getBoolean(JLINE_WINDOWS_TERMINAL_DIRECT_CONSOLE)); - - this.originalMode = getConsoleMode(); - setConsoleMode(originalMode & ~ENABLE_ECHO_INPUT.code); - setEchoEnabled(false); - } - - /** - * Restore the original terminal configuration, which can be used when - * shutting down the console reader. The ConsoleReader cannot be - * used after calling this method. - */ - @Override - public void restore() throws Exception { - // restore the old console mode - setConsoleMode(originalMode); - super.restore(); - } - - @Override - public int getWidth() { - int w = getWindowsTerminalWidth(); - return w < 1 ? DEFAULT_WIDTH : w; - } - - @Override - public int getHeight() { - int h = getWindowsTerminalHeight(); - return h < 1 ? DEFAULT_HEIGHT : h; - } - - @Override - public void setEchoEnabled(final boolean enabled) { - // Must set these four modes at the same time to make it work fine. - if (enabled) { - setConsoleMode(getConsoleMode() | - ENABLE_ECHO_INPUT.code | - ENABLE_LINE_INPUT.code | - ENABLE_PROCESSED_INPUT.code | - ENABLE_WINDOW_INPUT.code); - } - else { - setConsoleMode(getConsoleMode() & - ~(ENABLE_LINE_INPUT.code | - ENABLE_ECHO_INPUT.code | - ENABLE_PROCESSED_INPUT.code | - ENABLE_WINDOW_INPUT.code)); - } - super.setEchoEnabled(enabled); - } - - /** - * Whether or not to allow the use of the JNI console interaction. - */ - public void setDirectConsole(final boolean flag) { - this.directConsole = flag; - Log.debug("Direct console: ", flag); - } - - /** - * Whether or not to allow the use of the JNI console interaction. - */ - public Boolean getDirectConsole() { - return directConsole; - } - - - @Override - public int readCharacter(final InputStream in) throws IOException { - // if we can detect that we are directly wrapping the system - // input, then bypass the input stream and read directly (which - // allows us to access otherwise unreadable strokes, such as - // the arrow keys) - - if (directConsole || isSystemIn(in)) { - return readByte(); - } - else { - return super.readCharacter(in); - } - } - - private boolean isSystemIn(final InputStream in) throws IOException { - assert in != null; - - if (in == System.in) { - return true; - } - else if (in instanceof FileInputStream && ((FileInputStream) in).getFD() == FileDescriptor.in) { - return true; - } - - return false; - } - - @Override - public int readVirtualKey(final InputStream in) throws IOException { - int indicator = readCharacter(in); - - // in Windows terminals, arrow keys are represented by - // a sequence of 2 characters. E.g., the up arrow - // key yields 224, 72 - if (indicator == SPECIAL_KEY_INDICATOR.code || indicator == NUMPAD_KEY_INDICATOR.code) { - int c = readCharacter(in); - WindowsKey key = WindowsKey.valueOf(c); - - switch (key) { - case UP_ARROW_KEY: - return CTRL_P.code; // translate UP -> CTRL-P - - case LEFT_ARROW_KEY: - return CTRL_B.code; // translate LEFT -> CTRL-B - - case RIGHT_ARROW_KEY: - return CTRL_F.code; // translate RIGHT -> CTRL-F - - case DOWN_ARROW_KEY: - return CTRL_N.code; // translate DOWN -> CTRL-N - - case DELETE_KEY: - return CTRL_QM.code; // translate DELETE -> CTRL-? - - case HOME_KEY: - return CTRL_A.code; - - case END_KEY: - return CTRL_E.code; - - case PAGE_UP_KEY: - return CTRL_K.code; - - case PAGE_DOWN_KEY: - return CTRL_L.code; - - case ESCAPE_KEY: - return CTRL_OB.code; // translate ESCAPE -> CTRL-[ - - case INSERT_KEY: - return CTRL_C.code; - - default: - return 0; - } - } - else if (indicator > 128) { - // handle unicode characters longer than 2 bytes, - // thanks to Marc.Herbert@continuent.com - replayStream.setInput(indicator, in); - // replayReader = new InputStreamReader(replayStream, encoding); - indicator = replayReader.read(); - - } - - return indicator; - } - - @Override - public InputStream getDefaultBindings() { - return WindowsTerminal.class.getResourceAsStream(WINDOWSBINDINGS_PROPERTIES); - } - - // - // Native Bits - // - private int getConsoleMode() { - return WindowsSupport.getConsoleMode(); - } - - private void setConsoleMode(int mode) { - WindowsSupport.setConsoleMode(mode); - } - - private int readByte() { - return WindowsSupport.readByte(); - } - - private int getWindowsTerminalWidth() { - return WindowsSupport.getWindowsTerminalWidth(); - } - - private int getWindowsTerminalHeight() { - return WindowsSupport.getWindowsTerminalHeight(); - } - - /** - * Console mode - *

- * Constants copied wincon.h. - */ - public static enum ConsoleMode - { - /** - * The ReadFile or ReadConsole function returns only when a carriage return - * character is read. If this mode is disable, the functions return when one - * or more characters are available. - */ - ENABLE_LINE_INPUT(2), - - /** - * Characters read by the ReadFile or ReadConsole function are written to - * the active screen buffer as they are read. This mode can be used only if - * the ENABLE_LINE_INPUT mode is also enabled. - */ - ENABLE_ECHO_INPUT(4), - - /** - * CTRL+C is processed by the system and is not placed in the input buffer. - * If the input buffer is being read by ReadFile or ReadConsole, other - * control keys are processed by the system and are not returned in the - * ReadFile or ReadConsole buffer. If the ENABLE_LINE_INPUT mode is also - * enabled, backspace, carriage return, and linefeed characters are handled - * by the system. - */ - ENABLE_PROCESSED_INPUT(1), - - /** - * User interactions that change the size of the console screen buffer are - * reported in the console's input buffee. Information about these events - * can be read from the input buffer by applications using - * theReadConsoleInput function, but not by those using ReadFile - * orReadConsole. - */ - ENABLE_WINDOW_INPUT(8), - - /** - * If the mouse pointer is within the borders of the console window and the - * window has the keyboard focus, mouse events generated by mouse movement - * and button presses are placed in the input buffer. These events are - * discarded by ReadFile or ReadConsole, even when this mode is enabled. - */ - ENABLE_MOUSE_INPUT(16), - - /** - * When enabled, text entered in a console window will be inserted at the - * current cursor location and all text following that location will not be - * overwritten. When disabled, all following text will be overwritten. An OR - * operation must be performed with this flag and the ENABLE_EXTENDED_FLAGS - * flag to enable this functionality. - */ - ENABLE_PROCESSED_OUTPUT(1), - - /** - * This flag enables the user to use the mouse to select and edit text. To - * enable this option, use the OR to combine this flag with - * ENABLE_EXTENDED_FLAGS. - */ - ENABLE_WRAP_AT_EOL_OUTPUT(2),; - - public final int code; - - ConsoleMode(final int code) { - this.code = code; - } - } - - /** - * Windows keys. - *

- * Constants copied wincon.h. - */ - public static enum WindowsKey - { - /** - * On windows terminals, this character indicates that a 'special' key has - * been pressed. This means that a key such as an arrow key, or delete, or - * home, etc. will be indicated by the next character. - */ - SPECIAL_KEY_INDICATOR(224), - - /** - * On windows terminals, this character indicates that a special key on the - * number pad has been pressed. - */ - NUMPAD_KEY_INDICATOR(0), - - /** - * When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR, - * this character indicates an left arrow key press. - */ - LEFT_ARROW_KEY(75), - - /** - * When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR - * this character indicates an - * right arrow key press. - */ - RIGHT_ARROW_KEY(77), - - /** - * When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR - * this character indicates an up - * arrow key press. - */ - UP_ARROW_KEY(72), - - /** - * When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR - * this character indicates an - * down arrow key press. - */ - DOWN_ARROW_KEY(80), - - /** - * When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR - * this character indicates that - * the delete key was pressed. - */ - DELETE_KEY(83), - - /** - * When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR - * this character indicates that - * the home key was pressed. - */ - HOME_KEY(71), - - /** - * When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR - * this character indicates that - * the end key was pressed. - */ - END_KEY(79), - - /** - * When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR - * this character indicates that - * the page up key was pressed. - */ - PAGE_UP_KEY(73), - - /** - * When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR - * this character indicates that - * the page down key was pressed. - */ - PAGE_DOWN_KEY(81), - - /** - * When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR - * this character indicates that - * the insert key was pressed. - */ - INSERT_KEY(82), - - /** - * When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR, - * this character indicates that the escape key was pressed. - */ - ESCAPE_KEY(0),; - - public final int code; - - WindowsKey(final int code) { - this.code = code; - } - - private static final Map codes; - - static { - Map map = new HashMap(); - - for (WindowsKey key : WindowsKey.values()) { - map.put(key.code, key); - } - - codes = map; - } - - public static WindowsKey valueOf(final int code) { - return codes.get(code); - } - } -} diff --git a/src/jline/src/main/java/jline/console/ConsoleReader.java b/src/jline/src/main/java/jline/console/ConsoleReader.java deleted file mode 100644 index 660bb0cbc2..0000000000 --- a/src/jline/src/main/java/jline/console/ConsoleReader.java +++ /dev/null @@ -1,2160 +0,0 @@ -/* - * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. - * - * This software is distributable under the BSD license. See the terms of the - * BSD license in the documentation provided with this software. - */ - -package jline.console; - -import jline.Terminal; -import jline.TerminalFactory; -import jline.console.completer.CandidateListCompletionHandler; -import jline.console.completer.Completer; -import jline.console.completer.CompletionHandler; -import jline.console.history.History; -import jline.console.history.MemoryHistory; -import jline.internal.Configuration; -import jline.internal.Log; -import org.fusesource.jansi.AnsiOutputStream; - -import java.awt.Toolkit; -import java.awt.datatransfer.Clipboard; -import java.awt.datatransfer.DataFlavor; -import java.awt.datatransfer.Transferable; -import java.awt.datatransfer.UnsupportedFlavorException; -import java.awt.event.ActionListener; -import java.io.*; -import java.util.*; - -/** - * A reader for console applications. It supports custom tab-completion, - * saveable command history, and command line editing. On some platforms, - * platform-specific commands will need to be issued before the reader will - * function properly. See {@link jline.Terminal#init} for convenience - * methods for issuing platform-specific setup commands. - * - * @author Marc Prud'hommeaux - * @author Jason Dillon - */ -public class ConsoleReader -{ - public static final String JLINE_NOBELL = "jline.nobell"; - - public static final String JLINE_EXPANDEVENTS = "jline.expandevents"; - - public static final char BACKSPACE = '\b'; - - public static final char RESET_LINE = '\r'; - - public static final char KEYBOARD_BELL = '\07'; - - public static final char NULL_MASK = 0; - - public static final int TAB_WIDTH = 4; - - private static final ResourceBundle - resources = ResourceBundle.getBundle(CandidateListCompletionHandler.class.getName()); - - private final Terminal terminal; - - private InputStream in; - - private final Writer out; - - private final CursorBuffer buf = new CursorBuffer(); - - private String prompt; - - private boolean bellEnabled = true; - - private boolean expandEvents = false; - - private Character mask; - - private Character echoCharacter; - - private StringBuffer searchTerm = null; - - private String previousSearchTerm = ""; - - private int searchIndex = -1; - - public ConsoleReader(final InputStream in, final OutputStream out, final InputStream bindings, final Terminal term) throws - IOException - { - this.in = in; - this.terminal = term != null ? term : TerminalFactory.get(); - this.out = new PrintWriter(terminal.wrapOutIfNeeded(out)); - this.keyBindings = loadKeyBindings(bindings); - - setBellEnabled(!Configuration.getBoolean(JLINE_NOBELL, false)); - setExpandEvents(Configuration.getBoolean(JLINE_EXPANDEVENTS, false)); - } - - /** - * @deprecated use {@link #ConsoleReader(InputStream, OutputStream, InputStream, Terminal)} - * to let the terminal wrap the output stream if needed. - */ - public ConsoleReader(final InputStream in, final Writer out, final InputStream bindings, final Terminal term) throws - IOException - { - this.in = in; - this.out = out; - this.terminal = term != null ? term : TerminalFactory.get(); - this.keyBindings = loadKeyBindings(bindings); - - setBellEnabled(!Configuration.getBoolean(JLINE_NOBELL, false)); - } - - /** - * @deprecated use {@link #ConsoleReader(InputStream, OutputStream, InputStream, Terminal)} - * to let the terminal wrap the output stream if needed. - */ - public ConsoleReader(final InputStream in, final Writer out, final Terminal term) throws IOException { - this(in, out, null, term); - } - - /** - * @deprecated use {@link #ConsoleReader(InputStream, OutputStream, InputStream, Terminal)} - * to let the terminal wrap the output stream if needed. - */ - public ConsoleReader(final InputStream in, final Writer out) throws IOException - { - this(in, out, null, null); - } - - /** - * Create a new reader using {@link FileDescriptor#in} for input and - * {@link System#out} for output. - *

- * {@link FileDescriptor#in} is used because it has a better chance of not being buffered. - */ - public ConsoleReader() throws IOException { - this(new FileInputStream(FileDescriptor.in), System.out, null, null ); - } - - // FIXME: Only used for tests - - void setInput(final InputStream in) { - this.in = in; - } - - public InputStream getInput() { - return in; - } - - public Writer getOutput() { - return out; - } - - public Terminal getTerminal() { - return terminal; - } - - public CursorBuffer getCursorBuffer() { - return buf; - } - - public void setBellEnabled(final boolean enabled) { - this.bellEnabled = enabled; - } - - public boolean isBellEnabled() { - return bellEnabled; - } - - public void setExpandEvents(final boolean expand) { - this.expandEvents = expand; - } - - public boolean getExpandEvents() { - return expandEvents; - } - - public void setPrompt(final String prompt) { - this.prompt = prompt; - } - - public String getPrompt() { - return prompt; - } - - /** - * Set the echo character. For example, to have "*" entered when a password is typed: - *

- *

-     * myConsoleReader.setEchoCharacter(new Character('*'));
-     * 
- *

- * Setting the character to - *

- *

-     * null
-     * 
- *

- * will restore normal character echoing. Setting the character to - *

- *

-     * new Character(0)
-     * 
- *

- * will cause nothing to be echoed. - * - * @param c the character to echo to the console in place of the typed character. - */ - public void setEchoCharacter(final Character c) { - this.echoCharacter = c; - } - - /** - * Returns the echo character. - */ - public Character getEchoCharacter() { - return echoCharacter; - } - - /** - * Erase the current line. - * - * @return false if we failed (e.g., the buffer was empty) - */ - final boolean resetLine() throws IOException { - if (buf.cursor == 0) { - return false; - } - - backspaceAll(); - - return true; - } - - int getCursorPosition() { - // FIXME: does not handle anything but a line with a prompt absolute position - String prompt = getPrompt(); - return ((prompt == null) ? 0 : stripAnsi(lastLine(prompt)).length()) + buf.cursor; - } - - /** - * Returns the text after the last '\n'. - * prompt is returned if no '\n' characters are present. - * null is returned if prompt is null. - */ - private String lastLine(String str) { - if (str == null) return ""; - int last = str.lastIndexOf("\n"); - - if (last >= 0) { - return str.substring(last + 1, str.length()); - } - - return str; - } - - private String stripAnsi(String str) { - if (str == null) return ""; - try { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - AnsiOutputStream aos = new AnsiOutputStream(baos); - aos.write(str.getBytes()); - aos.flush(); - return baos.toString(); - } catch (IOException e) { - return str; - } - } - - /** - * Move the cursor position to the specified absolute index. - */ - public final boolean setCursorPosition(final int position) throws IOException { - return moveCursor(position - buf.cursor) != 0; - } - - /** - * Set the current buffer's content to the specified {@link String}. The - * visual console will be modified to show the current buffer. - * - * @param buffer the new contents of the buffer. - */ - private void setBuffer(final String buffer) throws IOException { - // don't bother modifying it if it is unchanged - if (buffer.equals(buf.buffer.toString())) { - return; - } - - // obtain the difference between the current buffer and the new one - int sameIndex = 0; - - for (int i = 0, l1 = buffer.length(), l2 = buf.buffer.length(); (i < l1) - && (i < l2); i++) { - if (buffer.charAt(i) == buf.buffer.charAt(i)) { - sameIndex++; - } - else { - break; - } - } - - int diff = buf.cursor - sameIndex; - if (diff < 0) { // we can't backspace here so try from the end of the buffer - moveToEnd(); - diff = buf.buffer.length() - sameIndex; - } - - backspace(diff); // go back for the differences - killLine(); // clear to the end of the line - buf.buffer.setLength(sameIndex); // the new length - putString(buffer.substring(sameIndex)); // append the differences - } - - private void setBuffer(final CharSequence buffer) throws IOException { - setBuffer(String.valueOf(buffer)); - } - - /** - * Output put the prompt + the current buffer - */ - public final void drawLine() throws IOException { - String prompt = getPrompt(); - if (prompt != null) { - print(prompt); - } - - print(buf.buffer.toString()); - - if (buf.length() != buf.cursor) { // not at end of line - back(buf.length() - buf.cursor - 1); - } - // force drawBuffer to check for weird wrap (after clear screen) - drawBuffer(); - } - - /** - * Clear the line and redraw it. - */ - public final void redrawLine() throws IOException { - print(RESET_LINE); -// flush(); - drawLine(); - } - - /** - * Clear the buffer and add its contents to the history. - * - * @return the former contents of the buffer. - */ - final String finishBuffer() throws IOException { // FIXME: Package protected because used by tests - String str = buf.buffer.toString(); - - if (expandEvents) { - str = expandEvents(str); - } - - // we only add it to the history if the buffer is not empty - // and if mask is null, since having a mask typically means - // the string was a password. We clear the mask after this call - if (str.length() > 0) { - if (mask == null && isHistoryEnabled()) { - history.add(str); - } - else { - mask = null; - } - } - - history.moveToEnd(); - - buf.buffer.setLength(0); - buf.cursor = 0; - - return str; - } - - /** - * Expand event designator such as !!, !#, !3, etc... - * See http://www.gnu.org/software/bash/manual/html_node/Event-Designators.html - * - * @param str - * @return - */ - final String expandEvents(String str) throws IOException { - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < str.length(); i++) { - char c = str.charAt(i); - switch (c) { - case '!': - if (i + 1 < str.length()) { - c = str.charAt(++i); - boolean neg = false; - String rep = null; - int i1, idx; - switch (c) { - case '!': - if (history.size() == 0) { - throw new IllegalArgumentException("!!: event not found"); - } - rep = history.get(history.index() - 1).toString(); - break; - case '#': - sb.append(sb.toString()); - break; - case '?': - i1 = str.indexOf('?', i + 1); - if (i1 < 0) { - i1 = str.length(); - } - String sc = str.substring(i + 1, i1); - i = i1; - idx = searchBackwards(sc); - if (idx < 0) { - throw new IllegalArgumentException("!?" + sc + ": event not found"); - } else { - rep = history.get(idx).toString(); - } - break; - case ' ': - case '\t': - sb.append('!'); - sb.append(c); - break; - case '-': - neg = true; - i++; - // fall through - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - i1 = i; - for (; i < str.length(); i++) { - c = str.charAt(i); - if (c < '0' || c > '9') { - break; - } - } - idx = 0; - try { - idx = Integer.parseInt(str.substring(i1, i)); - } catch (NumberFormatException e) { - throw new IllegalArgumentException((neg ? "!-" : "!") + str.substring(i1, i) + ": event not found"); - } - if (neg) { - if (idx < history.size()) { - rep = (history.get(history.index() - idx)).toString(); - } else { - throw new IllegalArgumentException((neg ? "!-" : "!") + str.substring(i1, i) + ": event not found"); - } - } else { - if (idx >= history.index() - history.size() && idx < history.index()) { - rep = (history.get(idx)).toString(); - } else { - throw new IllegalArgumentException((neg ? "!-" : "!") + str.substring(i1, i) + ": event not found"); - } - } - break; - default: - String ss = str.substring(i); - i = str.length(); - idx = searchBackwards(ss, history.index(), true); - if (idx < 0) { - throw new IllegalArgumentException("!" + ss + ": event not found"); - } else { - rep = history.get(idx).toString(); - } - break; - } - if (rep != null) { - sb.append(rep); - } - } else { - sb.append(c); - } - break; - case '^': - if (i == 0) { - int i1 = str.indexOf('^', i + 1); - int i2 = str.indexOf('^', i1 + 1); - if (i2 < 0) { - i2 = str.length(); - } - if (i1 > 0 && i2 > 0) { - String s1 = str.substring(i + 1, i1); - String s2 = str.substring(i1 + 1, i2); - String s = history.get(history.index() - 1).toString().replace(s1, s2); - sb.append(s); - i = i2 + 1; - break; - } - } - sb.append(c); - break; - default: - sb.append(c); - break; - } - } - String result = sb.toString(); - if (!str.equals(result)) { - print(result); - println(); - flush(); - } - return result; - - } - - /** - * Write out the specified string to the buffer and the output stream. - */ - public final void putString(final CharSequence str) throws IOException { - buf.write(str); - print(str); - drawBuffer(); - } - - /** - * Output the specified character, both to the buffer and the output stream. - */ - private void putChar(final int c, final boolean print) throws IOException { - buf.write((char) c); - - if (print) { - if (mask == null) { - // no masking - print(c); - } - else if (mask == NULL_MASK) { - // Don't print anything - } - else { - print(mask); - } - - drawBuffer(); - } - } - - /** - * Redraw the rest of the buffer from the cursor onwards. This is necessary - * for inserting text into the buffer. - * - * @param clear the number of characters to clear after the end of the buffer - */ - private void drawBuffer(final int clear) throws IOException { - // debug ("drawBuffer: " + clear); - if (buf.cursor == buf.length() && clear == 0) { - } else { - char[] chars = buf.buffer.substring(buf.cursor).toCharArray(); - if (mask != null) { - Arrays.fill(chars, mask); - } - if (terminal.hasWeirdWrap()) { - // need to determine if wrapping will occur: - int width = terminal.getWidth(); - int pos = getCursorPosition(); - for (int i = 0; i < chars.length; i++) { - print(chars[i]); - if ((pos + i + 1) % width == 0) { - print(32); // move cursor to next line by printing dummy space - print(13); // CR / not newline. - } - } - } else { - print(chars); - } - clearAhead(clear, chars.length); - if (terminal.isAnsiSupported()) { - if (chars.length > 0) { - back(chars.length); - } - } else { - back(chars.length); - } - } - if (terminal.hasWeirdWrap()) { - int width = terminal.getWidth(); - // best guess on whether the cursor is in that weird location... - // Need to do this without calling ansi cursor location methods - // otherwise it breaks paste of wrapped lines in xterm. - if (getCursorPosition() > 0 && (getCursorPosition() % width == 0) - && buf.cursor == buf.length() && clear == 0) { - // the following workaround is reverse-engineered from looking - // at what bash sent to the terminal in the same situation - print(32); // move cursor to next line by printing dummy space - print(13); // CR / not newline. - } - } - } - - /** - * Redraw the rest of the buffer from the cursor onwards. This is necessary - * for inserting text into the buffer. - */ - private void drawBuffer() throws IOException { - drawBuffer(0); - } - - /** - * Clear ahead the specified number of characters without moving the cursor. - * - * @param num the number of characters to clear - * @param delta the difference between the internal cursor and the screen - * cursor - if > 0, assume some stuff was printed and weird wrap has to be - * checked - */ - private void clearAhead(final int num, int delta) throws IOException { - if (num == 0) { - return; - } - - if (terminal.isAnsiSupported()) { - int width = terminal.getWidth(); - int screenCursorCol = getCursorPosition() + delta; - // clear current line - printAnsiSequence("K"); - // if cursor+num wraps, then we need to clear the line(s) below too - int curCol = screenCursorCol % width; - int endCol = (screenCursorCol + num - 1) % width; - int lines = num / width; - if (endCol < curCol) lines++; - for (int i = 0; i < lines; i++) { - printAnsiSequence("B"); - printAnsiSequence("2K"); - } - for (int i = 0; i < lines; i++) { - printAnsiSequence("A"); - } - return; - } - - // print blank extra characters - print(' ', num); - - // we need to flush here so a "clever" console doesn't just ignore the redundancy - // of a space followed by a backspace. -// flush(); - - // reset the visual cursor - back(num); - -// flush(); - } - - /** - * Move the visual cursor backwards without modifying the buffer cursor. - */ - private void back(final int num) throws IOException { - if (num == 0) return; - if (terminal.isAnsiSupported()) { - int width = getTerminal().getWidth(); - int cursor = getCursorPosition(); - int realCursor = cursor + num; - int realCol = realCursor % width; - int newCol = cursor % width; - int moveup = num / width; - int delta = realCol - newCol; - if (delta < 0) moveup++; - if (moveup > 0) { - printAnsiSequence(moveup + "A"); - } - printAnsiSequence((1 + newCol) + "G"); - return; - } - print(BACKSPACE, num); -// flush(); - } - - /** - * Flush the console output stream. This is important for printout out single characters (like a backspace or - * keyboard) that we want the console to handle immediately. - */ - public void flush() throws IOException { - out.flush(); - } - - private int backspaceAll() throws IOException { - return backspace(Integer.MAX_VALUE); - } - - /** - * Issue num backspaces. - * - * @return the number of characters backed up - */ - private int backspace(final int num) throws IOException { - if (buf.cursor == 0) { - return 0; - } - - int count = 0; - - int termwidth = getTerminal().getWidth(); - int lines = getCursorPosition() / termwidth; - count = moveCursor(-1 * num) * -1; - buf.buffer.delete(buf.cursor, buf.cursor + count); - if (getCursorPosition() / termwidth != lines) { - if (terminal.isAnsiSupported()) { - // debug("doing backspace redraw: " + getCursorPosition() + " on " + termwidth + ": " + lines); - printAnsiSequence("K"); - // if cursor+num wraps, then we need to clear the line(s) below too - // last char printed is one pos less than cursor so we subtract - // one -/* - // TODO: fixme (does not work - test with reverse search with wrapping line and CTRL-E) - int endCol = (getCursorPosition() + num - 1) % termwidth; - int curCol = getCursorPosition() % termwidth; - if (endCol < curCol) lines++; - for (int i = 1; i < lines; i++) { - printAnsiSequence("B"); - printAnsiSequence("2K"); - } - for (int i = 1; i < lines; i++) { - printAnsiSequence("A"); - } - return count; -*/ - } - } - drawBuffer(count); - - return count; - } - - /** - * Issue a backspace. - * - * @return true if successful - */ - public boolean backspace() throws IOException { - return backspace(1) == 1; - } - - private boolean moveToEnd() throws IOException { - return moveCursor(buf.length() - buf.cursor) > 0; - } - - /** - * Delete the character at the current position and redraw the remainder of the buffer. - */ - private boolean deleteCurrentCharacter() throws IOException { - if (buf.length() == 0 || buf.cursor == buf.length()) { - return false; - } - - buf.buffer.deleteCharAt(buf.cursor); - drawBuffer(1); - return true; - } - - private boolean previousWord() throws IOException { - while (isDelimiter(buf.current()) && (moveCursor(-1) != 0)) { - // nothing - } - - while (!isDelimiter(buf.current()) && (moveCursor(-1) != 0)) { - // nothing - } - - return true; - } - - private boolean nextWord() throws IOException { - while (isDelimiter(buf.current()) && (moveCursor(1) != 0)) { - // nothing - } - - while (!isDelimiter(buf.current()) && (moveCursor(1) != 0)) { - // nothing - } - - return true; - } - - private boolean deletePreviousWord() throws IOException { - while (isDelimiter(buf.current()) && backspace()) { - // nothing - } - - while (!isDelimiter(buf.current()) && backspace()) { - // nothing - } - - return true; - } - - /** - * Move the cursor where characters. - * - * @param num If less than 0, move abs(where) to the left, otherwise move where to the right. - * @return The number of spaces we moved - */ - public int moveCursor(final int num) throws IOException { - int where = num; - - if ((buf.cursor == 0) && (where <= 0)) { - return 0; - } - - if ((buf.cursor == buf.buffer.length()) && (where >= 0)) { - return 0; - } - - if ((buf.cursor + where) < 0) { - where = -buf.cursor; - } - else if ((buf.cursor + where) > buf.buffer.length()) { - where = buf.buffer.length() - buf.cursor; - } - - moveInternal(where); - - return where; - } - - /** - * Move the cursor where characters, without checking the current buffer. - * - * @param where the number of characters to move to the right or left. - */ - private void moveInternal(final int where) throws IOException { - // debug ("move cursor " + where + " (" - // + buf.cursor + " => " + (buf.cursor + where) + ")"); - buf.cursor += where; - - if (terminal.isAnsiSupported()) { - if (where < 0) { - back(Math.abs(where)); - } else { - int width = getTerminal().getWidth(); - int cursor = getCursorPosition(); - int oldLine = (cursor - where) / width; - int newLine = cursor / width; - if (newLine > oldLine) { - if (terminal.hasWeirdWrap()) { - // scroll up if at bottom - // note: - // on rxvt cywgin terminal.getHeight() is incorrect - // MacOs xterm does not seem to support scrolling - if (getCurrentAnsiRow() == terminal.getHeight()) { - printAnsiSequence((newLine - oldLine) + "S"); - } - } - printAnsiSequence((newLine - oldLine) + "B"); - } - printAnsiSequence(1 +(cursor % width) + "G"); - } -// flush(); - return; - } - - char c; - - if (where < 0) { - int len = 0; - for (int i = buf.cursor; i < buf.cursor - where; i++) { - if (buf.buffer.charAt(i) == '\t') { - len += TAB_WIDTH; - } - else { - len++; - } - } - - char chars[] = new char[len]; - Arrays.fill(chars, BACKSPACE); - out.write(chars); - - return; - } - else if (buf.cursor == 0) { - return; - } - else if (mask != null) { - c = mask; - } - else { - print(buf.buffer.substring(buf.cursor - where, buf.cursor).toCharArray()); - return; - } - - // null character mask: don't output anything - if (mask == NULL_MASK) { - return; - } - - print(c, Math.abs(where)); - } - - // FIXME: replace() is not used - - public final boolean replace(final int num, final String replacement) { - buf.buffer.replace(buf.cursor - num, buf.cursor, replacement); - try { - moveCursor(-num); - drawBuffer(Math.max(0, num - replacement.length())); - moveCursor(replacement.length()); - } - catch (IOException e) { - e.printStackTrace(); - return false; - } - return true; - } - - // - // Key reading - // - - /** - * Read a character from the console. - * - * @return the character, or -1 if an EOF is received. - */ - public final int readVirtualKey() throws IOException { - int c = terminal.readVirtualKey(in); - - Log.trace("Keystroke: ", c); - - // clear any echo characters - clearEcho(c); - - return c; - } - - /** - * Clear the echoed characters for the specified character code. - */ - private int clearEcho(final int c) throws IOException { - // if the terminal is not echoing, then ignore - if (!terminal.isEchoEnabled()) { - return 0; - } - - // otherwise, clear - int num = countEchoCharacters((char) c); - back(num); - drawBuffer(num); - - return num; - } - - private int countEchoCharacters(final char c) { - // tabs as special: we need to determine the number of spaces - // to cancel based on what out current cursor position is - if (c == 9) { - int tabStop = 8; // will this ever be different? - int position = getCursorPosition(); - - return tabStop - (position % tabStop); - } - - return getPrintableCharacters(c).length(); - } - - /** - * Return the number of characters that will be printed when the specified - * character is echoed to the screen - * - * Adapted from cat by Torbjorn Granlund, as repeated in stty by David MacKenzie. - */ - private StringBuilder getPrintableCharacters(final char ch) { - StringBuilder sbuff = new StringBuilder(); - - if (ch >= 32) { - if (ch < 127) { - sbuff.append(ch); - } - else if (ch == 127) { - sbuff.append('^'); - sbuff.append('?'); - } - else { - sbuff.append('M'); - sbuff.append('-'); - - if (ch >= (128 + 32)) { - if (ch < (128 + 127)) { - sbuff.append((char) (ch - 128)); - } - else { - sbuff.append('^'); - sbuff.append('?'); - } - } - else { - sbuff.append('^'); - sbuff.append((char) (ch - 128 + 64)); - } - } - } - else { - sbuff.append('^'); - sbuff.append((char) (ch + 64)); - } - - return sbuff; - } - - public final int readCharacter(final char... allowed) throws IOException { - // if we restrict to a limited set and the current character is not in the set, then try again. - char c; - - Arrays.sort(allowed); // always need to sort before binarySearch - - while (Arrays.binarySearch(allowed, c = (char) readVirtualKey()) < 0) { - // nothing - } - - return c; - } - - // - // Key Bindings - // - - public static final String JLINE_COMPLETION_THRESHOLD = "jline.completion.threshold"; - - public static final String JLINE_KEYBINDINGS = "jline.keybindings"; - - public static final String JLINEBINDINGS_PROPERTIES = ".jlinebindings.properties"; - - /** - * The map for logical operations. - */ - private final short[] keyBindings; - - private short[] loadKeyBindings(InputStream input) throws IOException { - if (input == null) { - try { - File file = new File(Configuration.getUserHome(), JLINEBINDINGS_PROPERTIES); - - String path = Configuration.getString(JLINE_KEYBINDINGS); - if (path != null) { - file = new File(path); - } - - if (file.isFile()) { - Log.debug("Loading user bindings from: ", file); - input = new FileInputStream(file); - } - } - catch (Exception e) { - Log.error("Failed to load user bindings", e); - } - } - - if (input == null) { - Log.debug("Using default bindings"); - input = getTerminal().getDefaultBindings(); - } - - short[] keyBindings = new short[Character.MAX_VALUE * 2]; - - Arrays.fill(keyBindings, Operation.UNKNOWN.code); - - // Loads the key bindings. Bindings file is in the format: - // - // keycode: operation name - - if (input != null) { - input = new BufferedInputStream(input); - Properties p = new Properties(); - p.load(input); - input.close(); - - for (Object key : p.keySet()) { - String val = (String) key; - - try { - short code = Short.parseShort(val); - String name = p.getProperty(val); - Operation op = Operation.valueOf(name); - keyBindings[code] = op.code; - } - catch (NumberFormatException e) { - Log.error("Failed to convert binding code: ", val, e); - } - } - - // hardwired arrow key bindings - // keybindings[VK_UP] = PREV_HISTORY; - // keybindings[VK_DOWN] = NEXT_HISTORY; - // keybindings[VK_LEFT] = PREV_CHAR; - // keybindings[VK_RIGHT] = NEXT_CHAR; - } - - return keyBindings; - } - - int getKeyForAction(final short logicalAction) { - for (int i = 0; i < keyBindings.length; i++) { - if (keyBindings[i] == logicalAction) { - return i; - } - } - - return -1; - } - - int getKeyForAction(final Operation op) { - assert op != null; - return getKeyForAction(op.code); - } - - /** - * Reads the console input and returns an array of the form [raw, key binding]. - */ - private int[] readBinding() throws IOException { - int c = readVirtualKey(); - - if (c == -1) { - return null; - } - - // extract the appropriate key binding - short code = keyBindings[c]; - - Log.trace("Translated: ", c, " -> ", code); - - return new int[]{c, code}; - } - - // - // Line Reading - // - - /** - * Read the next line and return the contents of the buffer. - */ - public String readLine() throws IOException { - return readLine((String) null); - } - - /** - * Read the next line with the specified character mask. If null, then - * characters will be echoed. If 0, then no characters will be echoed. - */ - public String readLine(final Character mask) throws IOException { - return readLine(null, mask); - } - - public String readLine(final String prompt) throws IOException { - return readLine(prompt, null); - } - - /** - * Read a line from the in {@link InputStream}, and return the line - * (without any trailing newlines). - * - * @param prompt The prompt to issue to the console, may be null. - * @return A line that is read from the terminal, or null if there was null input (e.g., CTRL-D - * was pressed). - */ - public String readLine(String prompt, final Character mask) throws IOException { - // prompt may be null - // mask may be null - - // FIXME: This blows, each call to readLine will reset the console's state which doesn't seem very nice. - this.mask = mask; - if (prompt != null) { - setPrompt(prompt); - } - else { - prompt = getPrompt(); - } - - try { - if (!terminal.isSupported()) { - beforeReadLine(prompt, mask); - } - - if (prompt != null && prompt.length() > 0) { - out.write(prompt); - out.flush(); - } - - // if the terminal is unsupported, just use plain-java reading - if (!terminal.isSupported()) { - return readLine(in); - } - - String originalPrompt = this.prompt; - - final int NORMAL = 1; - final int SEARCH = 2; - int state = NORMAL; - - boolean success = true; - - while (true) { - int[] next = readBinding(); - - if (next == null) { - return null; - } - - int c = next[0]; - // int code = next[1]; - Operation code = Operation.valueOf(next[1]); - - if (c == -1) { - return null; - } - - // Search mode. - // - // Note that we have to do this first, because if there is a command - // not linked to a search command, we leave the search mode and fall - // through to the normal state. - if (state == SEARCH) { - int cursorDest = -1; - - switch (code) { - // This doesn't work right now, it seems CTRL-G is not passed - // down correctly. :( - case ABORT: - state = NORMAL; - break; - - case SEARCH_PREV: - if (searchTerm.length() == 0) { - searchTerm.append(previousSearchTerm); - } - - if (searchIndex == -1) { - searchIndex = searchBackwards(searchTerm.toString()); - } else { - searchIndex = searchBackwards(searchTerm.toString(), searchIndex); - } - break; - - case DELETE_PREV_CHAR: - if (searchTerm.length() > 0) { - searchTerm.deleteCharAt(searchTerm.length() - 1); - searchIndex = searchBackwards(searchTerm.toString()); - } - break; - - case UNKNOWN: - searchTerm.appendCodePoint(c); - searchIndex = searchBackwards(searchTerm.toString()); - break; - - default: - // Set buffer and cursor position to the found string. - if (searchIndex != -1) { - history.moveTo(searchIndex); - // set cursor position to the found string - cursorDest = history.current().toString().indexOf(searchTerm.toString()); - } - state = NORMAL; - break; - } - - // if we're still in search mode, print the search status - if (state == SEARCH) { - if (searchTerm.length() == 0) { - printSearchStatus("", ""); - searchIndex = -1; - } else { - if (searchIndex == -1) { - beep(); - } else { - printSearchStatus(searchTerm.toString(), history.get(searchIndex).toString()); - } - } - } - // otherwise, restore the line - else { - restoreLine(originalPrompt, cursorDest); - } - } - - if (state == NORMAL) { - switch (code) { - case EXIT: // ctrl-d - if (buf.buffer.length() == 0) { - return null; - } else { - deleteCurrentCharacter(); - } - break; - - case COMPLETE: // tab - success = complete(); - break; - - case MOVE_TO_BEG: - success = setCursorPosition(0); - break; - - case KILL_LINE: // CTRL-K - success = killLine(); - break; - - case CLEAR_SCREEN: // CTRL-L - success = clearScreen(); - break; - - case KILL_LINE_PREV: // CTRL-U - success = resetLine(); - break; - - case NEWLINE: // enter - moveToEnd(); - println(); // output newline - flush(); - return finishBuffer(); - - case DELETE_PREV_CHAR: // backspace - success = backspace(); - break; - - case DELETE_NEXT_CHAR: // delete - success = deleteCurrentCharacter(); - break; - - case MOVE_TO_END: - success = moveToEnd(); - break; - - case PREV_CHAR: - success = moveCursor(-1) != 0; - break; - - case NEXT_CHAR: - success = moveCursor(1) != 0; - break; - - case NEXT_HISTORY: - success = moveHistory(true); - break; - - case PREV_HISTORY: - success = moveHistory(false); - break; - - case ABORT: - case REDISPLAY: - break; - - case PASTE: - success = paste(); - break; - - case DELETE_PREV_WORD: - success = deletePreviousWord(); - break; - - case PREV_WORD: - success = previousWord(); - break; - - case NEXT_WORD: - success = nextWord(); - break; - - case START_OF_HISTORY: - success = history.moveToFirst(); - if (success) { - setBuffer(history.current()); - } - break; - - case END_OF_HISTORY: - success = history.moveToLast(); - if (success) { - setBuffer(history.current()); - } - break; - - case CLEAR_LINE: - moveInternal(-(buf.cursor)); - killLine(); - break; - - case INSERT: - buf.setOverTyping(!buf.isOverTyping()); - break; - - case SEARCH_PREV: // CTRL-R - if (searchTerm != null) { - previousSearchTerm = searchTerm.toString(); - } - searchTerm = new StringBuffer(buf.buffer); - state = SEARCH; - if (searchTerm.length() > 0) { - searchIndex = searchBackwards(searchTerm.toString()); - if (searchIndex == -1) { - beep(); - } - printSearchStatus(searchTerm.toString(), - searchIndex > -1 ? history.get(searchIndex).toString() : ""); - } else { - searchIndex = -1; - printSearchStatus("", ""); - } - break; - - case UNKNOWN: - default: - if (c != 0) { // ignore null chars - ActionListener action = triggeredActions.get((char) c); - if (action != null) { - action.actionPerformed(null); - } - else { - putChar(c, true); - } - } - else { - success = false; - } - } - - if (!success) { - beep(); - } - - flush(); - } - } - } - finally { - if (!terminal.isSupported()) { - afterReadLine(); - } - } - } - - /** - * Read a line for unsupported terminals. - */ - private String readLine(final InputStream in) throws IOException { - StringBuilder buff = new StringBuilder(); - - while (true) { - int i = in.read(); - - if (i == -1 || i == '\n' || i == '\r') { - return buff.toString(); - } - - buff.append((char) i); - } - - // return new BufferedReader (new InputStreamReader (in)).readLine (); - } - - // - // Completion - // - - private final List completers = new LinkedList(); - - private CompletionHandler completionHandler = new CandidateListCompletionHandler(); - - /** - * Add the specified {@link jline.console.completer.Completer} to the list of handlers for tab-completion. - * - * @param completer the {@link jline.console.completer.Completer} to add - * @return true if it was successfully added - */ - public boolean addCompleter(final Completer completer) { - return completers.add(completer); - } - - /** - * Remove the specified {@link jline.console.completer.Completer} from the list of handlers for tab-completion. - * - * @param completer The {@link Completer} to remove - * @return True if it was successfully removed - */ - public boolean removeCompleter(final Completer completer) { - return completers.remove(completer); - } - - /** - * Returns an unmodifiable list of all the completers. - */ - public Collection getCompleters() { - return Collections.unmodifiableList(completers); - } - - public void setCompletionHandler(final CompletionHandler handler) { - assert handler != null; - this.completionHandler = handler; - } - - public CompletionHandler getCompletionHandler() { - return this.completionHandler; - } - - /** - * Use the completers to modify the buffer with the appropriate completions. - * - * @return true if successful - */ - private boolean complete() throws IOException { - // debug ("tab for (" + buf + ")"); - if (completers.size() == 0) { - return false; - } - - List candidates = new LinkedList(); - String bufstr = buf.buffer.toString(); - int cursor = buf.cursor; - - int position = -1; - - for (Completer comp : completers) { - if ((position = comp.complete(bufstr, cursor, candidates)) != -1) { - break; - } - } - - return candidates.size() != 0 && getCompletionHandler().complete(this, candidates, position); - } - - /** - * The number of tab-completion candidates above which a warning will be - * prompted before showing all the candidates. - */ - private int autoprintThreshold = Integer.getInteger(JLINE_COMPLETION_THRESHOLD, 100); // same default as bash - - /** - * @param threshold the number of candidates to print without issuing a warning. - */ - public void setAutoprintThreshold(final int threshold) { - this.autoprintThreshold = threshold; - } - - /** - * @return the number of candidates to print without issuing a warning. - */ - public int getAutoprintThreshold() { - return autoprintThreshold; - } - - private boolean paginationEnabled; - - /** - * Whether to use pagination when the number of rows of candidates exceeds the height of the terminal. - */ - public void setPaginationEnabled(final boolean enabled) { - this.paginationEnabled = enabled; - } - - /** - * Whether to use pagination when the number of rows of candidates exceeds the height of the terminal. - */ - public boolean isPaginationEnabled() { - return paginationEnabled; - } - - // - // History - // - - private History history = new MemoryHistory(); - - public void setHistory(final History history) { - this.history = history; - } - - public History getHistory() { - return history; - } - - private boolean historyEnabled = true; - - /** - * Whether or not to add new commands to the history buffer. - */ - public void setHistoryEnabled(final boolean enabled) { - this.historyEnabled = enabled; - } - - /** - * Whether or not to add new commands to the history buffer. - */ - public boolean isHistoryEnabled() { - return historyEnabled; - } - - /** - * Move up or down the history tree. - */ - private boolean moveHistory(final boolean next) throws IOException { - if (next && !history.next()) { - return false; - } - else if (!next && !history.previous()) { - return false; - } - - setBuffer(history.current()); - - return true; - } - - // - // Printing - // - - public static final String CR = System.getProperty("line.separator"); - - /** - * Output the specified character to the output stream without manipulating the current buffer. - */ - private void print(final int c) throws IOException { - if (c == '\t') { - char chars[] = new char[TAB_WIDTH]; - Arrays.fill(chars, ' '); - out.write(chars); - return; - } - - out.write(c); - } - - /** - * Output the specified characters to the output stream without manipulating the current buffer. - */ - private void print(final char... buff) throws IOException { - int len = 0; - for (char c : buff) { - if (c == '\t') { - len += TAB_WIDTH; - } - else { - len++; - } - } - - char chars[]; - if (len == buff.length) { - chars = buff; - } - else { - chars = new char[len]; - int pos = 0; - for (char c : buff) { - if (c == '\t') { - Arrays.fill(chars, pos, pos + TAB_WIDTH, ' '); - pos += TAB_WIDTH; - } - else { - chars[pos] = c; - pos++; - } - } - } - - out.write(chars); - } - - private void print(final char c, final int num) throws IOException { - if (num == 1) { - print(c); - } - else { - char[] chars = new char[num]; - Arrays.fill(chars, c); - print(chars); - } - } - - /** - * Output the specified string to the output stream (but not the buffer). - */ - public final void print(final CharSequence s) throws IOException { - assert s != null; - print(s.toString().toCharArray()); - } - - public final void println(final CharSequence s) throws IOException { - assert s != null; - print(s.toString().toCharArray()); - println(); - } - - /** - * Output a platform-dependant newline. - */ - public final void println() throws IOException { - print(CR); -// flush(); - } - - // - // Actions - // - - /** - * Issue a delete. - * - * @return true if successful - */ - public final boolean delete() throws IOException { - return delete(1) == 1; - } - - // FIXME: delete(int) only used by above + the return is always 1 and num is ignored - - /** - * Issue num deletes. - * - * @return the number of characters backed up - */ - private int delete(final int num) throws IOException { - // TODO: Try to use jansi for this - - /* Commented out because of DWA-2949: - if (buf.cursor == 0) { - return 0; - } - */ - - buf.buffer.delete(buf.cursor, buf.cursor + 1); - drawBuffer(1); - - return 1; - } - - /** - * Kill the buffer ahead of the current cursor position. - * - * @return true if successful - */ - public boolean killLine() throws IOException { - int cp = buf.cursor; - int len = buf.buffer.length(); - - if (cp >= len) { - return false; - } - - int num = buf.buffer.length() - cp; - clearAhead(num, 0); - - for (int i = 0; i < num; i++) { - buf.buffer.deleteCharAt(len - i - 1); - } - - return true; - } - - /** - * Clear the screen by issuing the ANSI "clear screen" code. - */ - public boolean clearScreen() throws IOException { - if (!terminal.isAnsiSupported()) { - return false; - } - - // send the ANSI code to clear the screen - printAnsiSequence("2J"); - - // then send the ANSI code to go to position 1,1 - printAnsiSequence("1;1H"); - - redrawLine(); - - return true; - } - - /** - * Issue an audible keyboard bell, if {@link #isBellEnabled} return true. - */ - public void beep() throws IOException { - if (isBellEnabled()) { - print(KEYBOARD_BELL); - // need to flush so the console actually beeps - flush(); - } - } - - /** - * Paste the contents of the clipboard into the console buffer - * - * @return true if clipboard contents pasted - */ - public boolean paste() throws IOException { - Clipboard clipboard; - try { // May throw ugly exception on system without X - clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); - } - catch (Exception e) { - return false; - } - - if (clipboard == null) { - return false; - } - - Transferable transferable = clipboard.getContents(null); - - if (transferable == null) { - return false; - } - - try { - Object content = transferable.getTransferData(DataFlavor.plainTextFlavor); - - // This fix was suggested in bug #1060649 at - // http://sourceforge.net/tracker/index.php?func=detail&aid=1060649&group_id=64033&atid=506056 - // to get around the deprecated DataFlavor.plainTextFlavor, but it - // raises a UnsupportedFlavorException on Mac OS X - - if (content == null) { - try { - content = new DataFlavor().getReaderForText(transferable); - } - catch (Exception e) { - // ignore - } - } - - if (content == null) { - return false; - } - - String value; - - if (content instanceof Reader) { - // TODO: we might want instead connect to the input stream - // so we can interpret individual lines - value = ""; - String line; - - BufferedReader read = new BufferedReader((Reader) content); - while ((line = read.readLine()) != null) { - if (value.length() > 0) { - value += "\n"; - } - - value += line; - } - } - else { - value = content.toString(); - } - - if (value == null) { - return true; - } - - putString(value); - - return true; - } - catch (UnsupportedFlavorException e) { - Log.error("Paste failed: ", e); - - return false; - } - } - - // - // Triggered Actions - // - - private final Map triggeredActions = new HashMap(); - - /** - * Adding a triggered Action allows to give another curse of action if a character passed the pre-processing. - *

- * Say you want to close the application if the user enter q. - * addTriggerAction('q', new ActionListener(){ System.exit(0); }); would do the trick. - */ - public void addTriggeredAction(final char c, final ActionListener listener) { - triggeredActions.put(c, listener); - } - - // - // Formatted Output - // - - /** - * Output the specified {@link Collection} in proper columns. - */ - public void printColumns(final Collection items) throws IOException { - if (items == null || items.isEmpty()) { - return; - } - - int width = getTerminal().getWidth(); - int height = getTerminal().getHeight(); - - int maxWidth = 0; - for (CharSequence item : items) { - maxWidth = Math.max(maxWidth, item.length()); - } - Log.debug("Max width: ", maxWidth); - - int showLines; - if (isPaginationEnabled()) { - showLines = height - 1; // page limit - } - else { - showLines = Integer.MAX_VALUE; - } - - StringBuilder buff = new StringBuilder(); - for (CharSequence item : items) { - if ((buff.length() + maxWidth) > width) { - println(buff); - buff.setLength(0); - - if (--showLines == 0) { - // Overflow - print(resources.getString("display-more")); - flush(); - int c = readVirtualKey(); - if (c == '\r' || c == '\n') { - // one step forward - showLines = 1; - } - else if (c != 'q') { - // page forward - showLines = height - 1; - } - - back(resources.getString("display-more").length()); - if (c == 'q') { - // cancel - break; - } - } - } - - // NOTE: toString() is important here due to AnsiString being retarded - buff.append(item.toString()); - for (int i = 0; i < (maxWidth + 3 - item.length()); i++) { - buff.append(' '); - } - } - - if (buff.length() > 0) { - println(buff); - } - } - - // - // Non-supported Terminal Support - // - - private Thread maskThread; - - private void beforeReadLine(final String prompt, final Character mask) { - if (mask != null && maskThread == null) { - final String fullPrompt = "\r" + prompt - + " " - + " " - + " " - + "\r" + prompt; - - maskThread = new Thread() - { - public void run() { - while (!interrupted()) { - try { - Writer out = getOutput(); - out.write(fullPrompt); - out.flush(); - sleep(3); - } - catch (IOException e) { - return; - } - catch (InterruptedException e) { - return; - } - } - } - }; - - maskThread.setPriority(Thread.MAX_PRIORITY); - maskThread.setDaemon(true); - maskThread.start(); - } - } - - private void afterReadLine() { - if (maskThread != null && maskThread.isAlive()) { - maskThread.interrupt(); - } - - maskThread = null; - } - - /** - * Erases the current line with the existing prompt, then redraws the line - * with the provided prompt and buffer - * @param prompt - * the new prompt - * @param buffer - * the buffer to be drawn - * @param cursorDest - * where you want the cursor set when the line has been drawn. - * -1 for end of line. - * */ - public void resetPromptLine(String prompt, String buffer, int cursorDest) throws IOException { - // move cursor to end of line - moveToEnd(); - - // backspace all text, including prompt - buf.buffer.append(this.prompt); - buf.cursor += this.prompt.length(); - this.prompt = ""; - backspaceAll(); - - this.prompt = prompt; - redrawLine(); - setBuffer(buffer); - - // move cursor to destination (-1 will move to end of line) - if (cursorDest < 0) cursorDest = buffer.length(); - setCursorPosition(cursorDest); - - flush(); - } - - public void printSearchStatus(String searchTerm, String match) throws IOException { - String prompt = "(reverse-i-search)`" + searchTerm + "': "; - String buffer = match; - int cursorDest = match.indexOf(searchTerm); - resetPromptLine(prompt, buffer, cursorDest); - } - - public void restoreLine(String originalPrompt, int cursorDest) throws IOException { - // TODO move cursor to matched string - String prompt = lastLine(originalPrompt); - String buffer = buf.buffer.toString(); - resetPromptLine(prompt, buffer, cursorDest); - } - - // - // History search - // - /** - * Search backward in history from a given position. - * - * @param searchTerm substring to search for. - * @param startIndex the index from which on to search - * @return index where this substring has been found, or -1 else. - */ - public int searchBackwards(String searchTerm, int startIndex) { - return searchBackwards(searchTerm, startIndex, false); - } - - /** - * Search backwards in history from the current position. - * - * @param searchTerm substring to search for. - * @return index where the substring has been found, or -1 else. - */ - public int searchBackwards(String searchTerm) { - return searchBackwards(searchTerm, history.index()); - } - - - public int searchBackwards(String searchTerm, int startIndex, boolean startsWith) { - ListIterator it = history.entries(startIndex); - while (it.hasPrevious()) { - History.Entry e = it.previous(); - if (startsWith) { - if (e.value().toString().startsWith(searchTerm)) { - return e.index(); - } - } else { - if (e.value().toString().contains(searchTerm)) { - return e.index(); - } - } - } - return -1; - } - - // - // Helpers - // - - /** - * Checks to see if the specified character is a delimiter. We consider a - * character a delimiter if it is anything but a letter or digit. - * - * @param c The character to test - * @return True if it is a delimiter - */ - private boolean isDelimiter(final char c) { - return !Character.isLetterOrDigit(c); - } - - private void printAnsiSequence(String sequence) throws IOException { - print(27); - print('['); - print(sequence); - flush(); // helps with step debugging - } - - // return column position, reported by the terminal - private int getCurrentPosition() { - // check for ByteArrayInputStream to disable for unit tests - if (terminal.isAnsiSupported() && !(in instanceof ByteArrayInputStream)) { - try { - printAnsiSequence("6n"); - flush(); - StringBuffer b = new StringBuffer(8); - // position is sent as [{ROW};{COLUMN}R - int r; - while((r = in.read()) > -1 && r != 'R') { - if (r != 27 && r != '[') { - b.append((char) r); - } - } - String[] pos = b.toString().split(";"); - return Integer.parseInt(pos[1]); - } catch (Exception x) { - // no luck - } - } - - return -1; // TODO: throw exception instead? - } - - // return row position, reported by the terminal - // needed to know whether to scroll up on cursor move in last col for weird - // wrapping terminals - not tested for anything else - private int getCurrentAnsiRow() { - // check for ByteArrayInputStream to disable for unit tests - if (terminal.isAnsiSupported() && !(in instanceof ByteArrayInputStream)) { - try { - printAnsiSequence("6n"); - flush(); - StringBuffer b = new StringBuffer(8); - // position is sent as [{ROW};{COLUMN}R - int r; - while((r = in.read()) > -1 && r != 'R') { - if (r != 27 && r != '[') { - b.append((char) r); - } - } - String[] pos = b.toString().split(";"); - return Integer.parseInt(pos[0]); - } catch (Exception x) { - // no luck - } - } - - return -1; // TODO: throw exception instead? - } -} diff --git a/src/jline/src/main/java/jline/console/CursorBuffer.java b/src/jline/src/main/java/jline/console/CursorBuffer.java deleted file mode 100644 index ef9932f43a..0000000000 --- a/src/jline/src/main/java/jline/console/CursorBuffer.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. - * - * This software is distributable under the BSD license. See the terms of the - * BSD license in the documentation provided with this software. - */ - -package jline.console; - -/** - * A holder for a {@link StringBuilder} that also contains the current cursor position. - * - * @author Marc Prud'hommeaux - * @author Jason Dillon - * @since 2.0 - */ -public class CursorBuffer -{ - private boolean overTyping = false; - - public int cursor = 0; - - public final StringBuilder buffer = new StringBuilder(); - - public boolean isOverTyping() { - return overTyping; - } - - public void setOverTyping(final boolean b) { - overTyping = b; - } - - public int length() { - return buffer.length(); - } - - public char current() { - if (cursor <= 0) { - return 0; - } - - return buffer.charAt(cursor - 1); - } - - /** - * Write the specific character into the buffer, setting the cursor position - * ahead one. The text may overwrite or insert based on the current setting - * of {@link #isOverTyping}. - * - * @param c the character to insert - */ - public void write(final char c) { - buffer.insert(cursor++, c); - if (isOverTyping() && cursor < buffer.length()) { - buffer.deleteCharAt(cursor); - } - } - - /** - * Insert the specified chars into the buffer, setting the cursor to the end of the insertion point. - */ - public void write(final CharSequence str) { - assert str != null; - - if (buffer.length() == 0) { - buffer.append(str); - } - else { - buffer.insert(cursor, str); - } - - cursor += str.length(); - - if (isOverTyping() && cursor < buffer.length()) { - buffer.delete(cursor, (cursor + str.length())); - } - } - - public boolean clear() { - if (buffer.length() == 0) { - return false; - } - - buffer.delete(0, buffer.length()); - cursor = 0; - return true; - } - - @Override - public String toString() { - return buffer.toString(); - } -} diff --git a/src/jline/src/main/java/jline/console/Key.java b/src/jline/src/main/java/jline/console/Key.java deleted file mode 100644 index 8997985dcc..0000000000 --- a/src/jline/src/main/java/jline/console/Key.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. - * - * This software is distributable under the BSD license. See the terms of the - * BSD license in the documentation provided with this software. - */ - -package jline.console; - -import java.util.HashMap; -import java.util.Map; - -/** - * Map from key name to key codes. - * - * @author Marc Prud'hommeaux - * @author Jason Dillon - * @see java.awt.event.KeyEvent - * @since 2.0 - */ -public enum Key -{ - CTRL_A(1), - - CTRL_B(2), - - CTRL_C(3), - - CTRL_D(4), - - CTRL_E(5), - - CTRL_F(6), - - CTRL_G(7), - - CTRL_K(11), - - CTRL_L(12), - - CTRL_N(14), - - CTRL_P(16), - - CTRL_OB(27), - - CTRL_QM(127), - - BACKSPACE('\b'), - - DELETE(127),; - - public final short code; - - Key(final int code) { - this.code = (short) code; - } - - private static final Map codes; - - static { - Map map = new HashMap(); - - for (Key op : Key.values()) { - map.put(op.code, op); - } - - codes = map; - } - - public static Key valueOf(final int code) { - return codes.get((short) code); - } -} \ No newline at end of file diff --git a/src/jline/src/main/java/jline/console/Operation.java b/src/jline/src/main/java/jline/console/Operation.java deleted file mode 100644 index e2d9ea564b..0000000000 --- a/src/jline/src/main/java/jline/console/Operation.java +++ /dev/null @@ -1,285 +0,0 @@ -/* - * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. - * - * This software is distributable under the BSD license. See the terms of the - * BSD license in the documentation provided with this software. - */ - -package jline.console; - -import java.util.HashMap; -import java.util.Map; - -/** - * Map for console operation to virtual key bindings. - * - * @author Marc Prud'hommeaux - * @author Jason Dillon - * @see java.awt.event.KeyEvent - * @since 2.0 - */ -public enum Operation -{ - /** - * Unknown operation. - */ - UNKNOWN(-99), - - /** - * Operation that moves to the beginning of the buffer. - */ - MOVE_TO_BEG(-1), - - /** - * Operation that moves to the end of the buffer. - */ - MOVE_TO_END(-3), - - /** - * Operation that moved to the previous character in the buffer. - */ - PREV_CHAR(-4), - - /** - * Operation that issues a newline. - */ - NEWLINE(-6), - - /** - * Operation that deletes the buffer from the current character to the end. - */ - KILL_LINE(-7), - - /** - * Operation that clears the screen. - */ - CLEAR_SCREEN(-8), - - /** - * Operation that sets the buffer to the next history item. - */ - NEXT_HISTORY(-9), - - /** - * Operation that sets the buffer to the previous history item. - */ - PREV_HISTORY(-11), - - /** - * Operation that redisplays the current buffer. - */ - REDISPLAY(-13), - - /** - * Operation that deletes the buffer from the cursor to the beginning. - */ - KILL_LINE_PREV(-15), - - /** - * Operation that deletes the previous word in the buffer. - */ - DELETE_PREV_WORD(-16), - - /** - * Operation that moves to the next character in the buffer. - */ - NEXT_CHAR(-19), - - /** - * Operation that moves to the previous character in the buffer. - */ - REPEAT_PREV_CHAR(-20), - - /** - * Operation that searches backwards in the command history. - */ - SEARCH_PREV(-21), - - /** - * Operation that repeats the character. - */ - REPEAT_NEXT_CHAR(-24), - - /** - * Operation that searches forward in the command history. - */ - SEARCH_NEXT(-25), - - /** - * Operation that moved to the previous whitespace. - */ - PREV_SPACE_WORD(-27), - - /** - * Operation that moved to the end of the current word. - */ - TO_END_WORD(-29), - - /** - * Operation that - */ - REPEAT_SEARCH_PREV(-34), - - /** - * Operation that - */ - PASTE_PREV(-36), - - /** - * Operation that - */ - REPLACE_MODE(-37), - - /** - * Operation that - */ - SUBSTITUTE_LINE(-38), - - /** - * Operation that - */ - TO_PREV_CHAR(-39), - - /** - * Operation that - */ - NEXT_SPACE_WORD(-40), - - /** - * Operation that - */ - DELETE_PREV_CHAR(-41), - - /** - * Operation that - */ - ADD(-42), - - /** - * Operation that - */ - PREV_WORD(-43), - - /** - * Operation that - */ - CHANGE_META(-44), - - /** - * Operation that - */ - DELETE_META(-45), - - /** - * Operation that - */ - END_WORD(-46), - - /** - * Operation that toggles insert/overtype - */ - INSERT(-48), - - /** - * Operation that - */ - REPEAT_SEARCH_NEXT(-49), - - /** - * Operation that - */ - PASTE_NEXT(-50), - - /** - * Operation that - */ - REPLACE_CHAR(-51), - - /** - * Operation that - */ - SUBSTITUTE_CHAR(-52), - - /** - * Operation that - */ - TO_NEXT_CHAR(-53), - - /** - * Operation that undoes the previous operation. - */ - UNDO(-54), - - /** - * Operation that moved to the next word. - */ - NEXT_WORD(-55), - - /** - * Operation that deletes the previous character. - */ - DELETE_NEXT_CHAR(-56), - - /** - * Operation that toggles between uppercase and lowercase. - */ - CHANGE_CASE(-57), - - /** - * Operation that performs completion operation on the current word. - */ - COMPLETE(-58), - - /** - * Operation that exits the command prompt. - */ - EXIT(-59), - - /** - * Operation that pastes the contents of the clipboard into the line - */ - PASTE(-60), - - /** - * Operation that moves the current History to the beginning. - */ - START_OF_HISTORY(-61), - - /** - * Operation that moves the current History to the end. - */ - END_OF_HISTORY(-62), - - /** - * Operation that clears whatever text is on the current line. - */ - CLEAR_LINE(-63), - - /** - * Cancel search - */ - ABORT(-64), - ; - - public final short code; - - Operation(final int code) { - this.code = (short) code; - } - - private static final Map codes; - - static { - Map map = new HashMap(); - - for (Operation op : Operation.values()) { - map.put(op.code, op); - } - - codes = map; - } - - public static Operation valueOf(final int code) { - return codes.get((short) code); - } -} \ No newline at end of file diff --git a/src/jline/src/main/java/jline/console/completer/AggregateCompleter.java b/src/jline/src/main/java/jline/console/completer/AggregateCompleter.java deleted file mode 100644 index 621ac5f6d8..0000000000 --- a/src/jline/src/main/java/jline/console/completer/AggregateCompleter.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package jline.console.completer; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.LinkedList; -import java.util.List; - -/** - * Completer which contains multiple completers and aggregates them together. - * - * @author Jason Dillon - * @since 2.3 - */ -public class AggregateCompleter - implements Completer -{ - private final List completers = new ArrayList(); - - public AggregateCompleter() { - // empty - } - - public AggregateCompleter(final Collection completers) { - assert completers != null; - this.completers.addAll(completers); - } - - public AggregateCompleter(final Completer... completers) { - this(Arrays.asList(completers)); - } - - public Collection getCompleters() { - return completers; - } - - public int complete(final String buffer, final int cursor, final List candidates) { - // buffer could be null - assert candidates != null; - - List completions = new ArrayList(completers.size()); - - // Run each completer, saving its completion results - int max = -1; - for (Completer completer : completers) { - Completion completion = new Completion(candidates); - completion.complete(completer, buffer, cursor); - - // Compute the max cursor position - max = Math.max(max, completion.cursor); - - completions.add(completion); - } - - // Append candidates from completions which have the same cursor position as max - for (Completion completion : completions) { - if (completion.cursor == max) { - candidates.addAll(completion.candidates); - } - } - - return max; - } - - @Override - public String toString() { - return getClass().getSimpleName() + "{" + - "completers=" + completers + - '}'; - } - - private class Completion - { - public final List candidates; - - public int cursor; - - public Completion(final List candidates) { - assert candidates != null; - this.candidates = new LinkedList(candidates); - } - - public void complete(final Completer completer, final String buffer, final int cursor) { - assert completer != null; - - this.cursor = completer.complete(buffer, cursor, candidates); - } - } -} diff --git a/src/jline/src/main/java/jline/console/completer/ArgumentCompleter.java b/src/jline/src/main/java/jline/console/completer/ArgumentCompleter.java deleted file mode 100644 index 542d4b4424..0000000000 --- a/src/jline/src/main/java/jline/console/completer/ArgumentCompleter.java +++ /dev/null @@ -1,398 +0,0 @@ -/* - * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. - * - * This software is distributable under the BSD license. See the terms of the - * BSD license in the documentation provided with this software. - */ - -package jline.console.completer; - -import jline.internal.Log; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.LinkedList; -import java.util.List; - -/** - * A {@link Completer} implementation that invokes a child completer using the appropriate separator argument. - * This can be used instead of the individual completers having to know about argument parsing semantics. - * - * @author Marc Prud'hommeaux - * @author Jason Dillon - * @since 2.3 - */ -public class ArgumentCompleter - implements Completer -{ - private final ArgumentDelimiter delimiter; - - private final List completers = new ArrayList(); - - private boolean strict = true; - - /** - * Create a new completer with the specified argument delimiter. - * - * @param delimiter The delimiter for parsing arguments - * @param completers The embedded completers - */ - public ArgumentCompleter(final ArgumentDelimiter delimiter, final Collection completers) { - assert delimiter != null; - this.delimiter = delimiter; - assert completers != null; - this.completers.addAll(completers); - } - - /** - * Create a new completer with the specified argument delimiter. - * - * @param delimiter The delimiter for parsing arguments - * @param completers The embedded completers - */ - public ArgumentCompleter(final ArgumentDelimiter delimiter, final Completer... completers) { - this(delimiter, Arrays.asList(completers)); - } - - /** - * Create a new completer with the default {@link WhitespaceArgumentDelimiter}. - * - * @param completers The embedded completers - */ - public ArgumentCompleter(final Completer... completers) { - this(new WhitespaceArgumentDelimiter(), completers); - } - - /** - * Create a new completer with the default {@link WhitespaceArgumentDelimiter}. - * - * @param completers The embedded completers - */ - public ArgumentCompleter(final List completers) { - this(new WhitespaceArgumentDelimiter(), completers); - } - - /** - * If true, a completion at argument index N will only succeed - * if all the completions from 0-(N-1) also succeed. - */ - public void setStrict(final boolean strict) { - this.strict = strict; - } - - /** - * Returns whether a completion at argument index N will success - * if all the completions from arguments 0-(N-1) also succeed. - * - * @return True if strict. - * @since 2.3 - */ - public boolean isStrict() { - return this.strict; - } - - /** - * @since 2.3 - */ - public ArgumentDelimiter getDelimiter() { - return delimiter; - } - - /** - * @since 2.3 - */ - public List getCompleters() { - return completers; - } - - public int complete(final String buffer, final int cursor, final List candidates) { - // buffer can be null - assert candidates != null; - - ArgumentDelimiter delim = getDelimiter(); - ArgumentList list = delim.delimit(buffer, cursor); - int argpos = list.getArgumentPosition(); - int argIndex = list.getCursorArgumentIndex(); - - if (argIndex < 0) { - return -1; - } - - List completers = getCompleters(); - Completer completer; - - // if we are beyond the end of the completers, just use the last one - if (argIndex >= completers.size()) { - completer = completers.get(completers.size() - 1); - } - else { - completer = completers.get(argIndex); - } - - // ensure that all the previous completers are successful before allowing this completer to pass (only if strict). - for (int i = 0; isStrict() && (i < argIndex); i++) { - Completer sub = completers.get(i >= completers.size() ? (completers.size() - 1) : i); - String[] args = list.getArguments(); - String arg = (args == null || i >= args.length) ? "" : args[i]; - - List subCandidates = new LinkedList(); - - if (sub.complete(arg, arg.length(), subCandidates) == -1) { - return -1; - } - - if (subCandidates.size() == 0) { - return -1; - } - } - - int ret = completer.complete(list.getCursorArgument(), argpos, candidates); - - if (ret == -1) { - return -1; - } - - int pos = ret + list.getBufferPosition() - argpos; - - // Special case: when completing in the middle of a line, and the area under the cursor is a delimiter, - // then trim any delimiters from the candidates, since we do not need to have an extra delimiter. - // - // E.g., if we have a completion for "foo", and we enter "f bar" into the buffer, and move to after the "f" - // and hit TAB, we want "foo bar" instead of "foo bar". - - if ((cursor != buffer.length()) && delim.isDelimiter(buffer, cursor)) { - for (int i = 0; i < candidates.size(); i++) { - CharSequence val = candidates.get(i); - - while (val.length() > 0 && delim.isDelimiter(val, val.length() - 1)) { - val = val.subSequence(0, val.length() - 1); - } - - candidates.set(i, val); - } - } - - Log.trace("Completing ", buffer, " (pos=", cursor, ") with: ", candidates, ": offset=", pos); - - return pos; - } - - /** - * The {@link ArgumentCompleter.ArgumentDelimiter} allows custom breaking up of a {@link String} into individual - * arguments in order to dispatch the arguments to the nested {@link Completer}. - * - * @author Marc Prud'hommeaux - */ - public static interface ArgumentDelimiter - { - /** - * Break the specified buffer into individual tokens that can be completed on their own. - * - * @param buffer The buffer to split - * @param pos The current position of the cursor in the buffer - * @return The tokens - */ - ArgumentList delimit(CharSequence buffer, int pos); - - /** - * Returns true if the specified character is a whitespace parameter. - * - * @param buffer The complete command buffer - * @param pos The index of the character in the buffer - * @return True if the character should be a delimiter - */ - boolean isDelimiter(CharSequence buffer, int pos); - } - - /** - * Abstract implementation of a delimiter that uses the {@link #isDelimiter} method to determine if a particular - * character should be used as a delimiter. - * - * @author Marc Prud'hommeaux - */ - public abstract static class AbstractArgumentDelimiter - implements ArgumentDelimiter - { - // TODO: handle argument quoting and escape characters - - private char[] quoteChars = {'\'', '"'}; - - private char[] escapeChars = {'\\'}; - - public void setQuoteChars(final char[] chars) { - this.quoteChars = chars; - } - - public char[] getQuoteChars() { - return this.quoteChars; - } - - public void setEscapeChars(final char[] chars) { - this.escapeChars = chars; - } - - public char[] getEscapeChars() { - return this.escapeChars; - } - - public ArgumentList delimit(final CharSequence buffer, final int cursor) { - List args = new LinkedList(); - StringBuilder arg = new StringBuilder(); - int argpos = -1; - int bindex = -1; - - for (int i = 0; (buffer != null) && (i <= buffer.length()); i++) { - // once we reach the cursor, set the - // position of the selected index - if (i == cursor) { - bindex = args.size(); - // the position in the current argument is just the - // length of the current argument - argpos = arg.length(); - } - - if ((i == buffer.length()) || isDelimiter(buffer, i)) { - if (arg.length() > 0) { - args.add(arg.toString()); - arg.setLength(0); // reset the arg - } - } - else { - arg.append(buffer.charAt(i)); - } - } - - return new ArgumentList(args.toArray(new String[args.size()]), bindex, argpos, cursor); - } - - /** - * Returns true if the specified character is a whitespace parameter. Check to ensure that the character is not - * escaped by any of {@link #getQuoteChars}, and is not escaped by ant of the {@link #getEscapeChars}, and - * returns true from {@link #isDelimiterChar}. - * - * @param buffer The complete command buffer - * @param pos The index of the character in the buffer - * @return True if the character should be a delimiter - */ - public boolean isDelimiter(final CharSequence buffer, final int pos) { - return !isQuoted(buffer, pos) && !isEscaped(buffer, pos) && isDelimiterChar(buffer, pos); - } - - public boolean isQuoted(final CharSequence buffer, final int pos) { - return false; - } - - public boolean isEscaped(final CharSequence buffer, final int pos) { - if (pos <= 0) { - return false; - } - - for (int i = 0; (escapeChars != null) && (i < escapeChars.length); - i++) { - if (buffer.charAt(pos) == escapeChars[i]) { - return !isEscaped(buffer, pos - 1); // escape escape - } - } - - return false; - } - - /** - * Returns true if the character at the specified position if a delimiter. This method will only be called if - * the character is not enclosed in any of the {@link #getQuoteChars}, and is not escaped by ant of the - * {@link #getEscapeChars}. To perform escaping manually, override {@link #isDelimiter} instead. - */ - public abstract boolean isDelimiterChar(CharSequence buffer, int pos); - } - - /** - * {@link ArgumentCompleter.ArgumentDelimiter} implementation that counts all whitespace (as reported by - * {@link Character#isWhitespace}) as being a delimiter. - * - * @author Marc Prud'hommeaux - */ - public static class WhitespaceArgumentDelimiter - extends AbstractArgumentDelimiter - { - /** - * The character is a delimiter if it is whitespace, and the - * preceding character is not an escape character. - */ - @Override - public boolean isDelimiterChar(final CharSequence buffer, final int pos) { - return Character.isWhitespace(buffer.charAt(pos)); - } - } - - /** - * The result of a delimited buffer. - * - * @author Marc Prud'hommeaux - */ - public static class ArgumentList - { - private String[] arguments; - - private int cursorArgumentIndex; - - private int argumentPosition; - - private int bufferPosition; - - /** - * @param arguments The array of tokens - * @param cursorArgumentIndex The token index of the cursor - * @param argumentPosition The position of the cursor in the current token - * @param bufferPosition The position of the cursor in the whole buffer - */ - public ArgumentList(final String[] arguments, final int cursorArgumentIndex, final int argumentPosition, final int bufferPosition) { - assert arguments != null; - - this.arguments = arguments; - this.cursorArgumentIndex = cursorArgumentIndex; - this.argumentPosition = argumentPosition; - this.bufferPosition = bufferPosition; - } - - public void setCursorArgumentIndex(final int i) { - this.cursorArgumentIndex = i; - } - - public int getCursorArgumentIndex() { - return this.cursorArgumentIndex; - } - - public String getCursorArgument() { - if ((cursorArgumentIndex < 0) || (cursorArgumentIndex >= arguments.length)) { - return null; - } - - return arguments[cursorArgumentIndex]; - } - - public void setArgumentPosition(final int pos) { - this.argumentPosition = pos; - } - - public int getArgumentPosition() { - return this.argumentPosition; - } - - public void setArguments(final String[] arguments) { - this.arguments = arguments; - } - - public String[] getArguments() { - return this.arguments; - } - - public void setBufferPosition(final int pos) { - this.bufferPosition = pos; - } - - public int getBufferPosition() { - return this.bufferPosition; - } - } -} diff --git a/src/jline/src/main/java/jline/console/completer/CandidateListCompletionHandler.java b/src/jline/src/main/java/jline/console/completer/CandidateListCompletionHandler.java deleted file mode 100644 index 3f6ede6fc4..0000000000 --- a/src/jline/src/main/java/jline/console/completer/CandidateListCompletionHandler.java +++ /dev/null @@ -1,192 +0,0 @@ -/* - * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. - * - * This software is distributable under the BSD license. See the terms of the - * BSD license in the documentation provided with this software. - */ - -package jline.console.completer; - -import jline.console.ConsoleReader; -import jline.console.CursorBuffer; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashSet; -import java.util.List; -import java.util.Locale; -import java.util.ResourceBundle; -import java.util.Set; - -/** - * A {@link CompletionHandler} that deals with multiple distinct completions - * by outputting the complete list of possibilities to the console. This - * mimics the behavior of the - * readline library. - * - * @author Marc Prud'hommeaux - * @author Jason Dillon - * @since 2.3 - */ -public class CandidateListCompletionHandler - implements CompletionHandler -{ - // TODO: handle quotes and escaped quotes && enable automatic escaping of whitespace - - public boolean complete(final ConsoleReader reader, final List candidates, final int pos) throws - IOException - { - CursorBuffer buf = reader.getCursorBuffer(); - - // if there is only one completion, then fill in the buffer - if (candidates.size() == 1) { - CharSequence value = candidates.get(0); - - // fail if the only candidate is the same as the current buffer - if (value.equals(buf.toString())) { - return false; - } - - setBuffer(reader, value, pos); - - return true; - } - else if (candidates.size() > 1) { - String value = getUnambiguousCompletions(candidates); - setBuffer(reader, value, pos); - } - - printCandidates(reader, candidates); - - // redraw the current console buffer - reader.drawLine(); - - return true; - } - - public static void setBuffer(final ConsoleReader reader, final CharSequence value, final int offset) throws - IOException - { - while ((reader.getCursorBuffer().cursor > offset) && reader.backspace()) { - // empty - } - - reader.putString(value); - reader.setCursorPosition(offset + value.length()); - } - - /** - * Print out the candidates. If the size of the candidates is greater than the - * {@link ConsoleReader#getAutoprintThreshold}, they prompt with a warning. - * - * @param candidates the list of candidates to print - */ - public static void printCandidates(final ConsoleReader reader, Collection candidates) throws - IOException - { - Set distinct = new HashSet(candidates); - - if (distinct.size() > reader.getAutoprintThreshold()) { - //noinspection StringConcatenation - reader.print(Messages.DISPLAY_CANDIDATES.format(candidates.size())); - reader.flush(); - - int c; - - String noOpt = Messages.DISPLAY_CANDIDATES_NO.format(); - String yesOpt = Messages.DISPLAY_CANDIDATES_YES.format(); - char[] allowed = {yesOpt.charAt(0), noOpt.charAt(0)}; - - while ((c = reader.readCharacter(allowed)) != -1) { - String tmp = new String(new char[]{(char) c}); - - if (noOpt.startsWith(tmp)) { - reader.println(); - return; - } - else if (yesOpt.startsWith(tmp)) { - break; - } - else { - reader.beep(); - } - } - } - - // copy the values and make them distinct, without otherwise affecting the ordering. Only do it if the sizes differ. - if (distinct.size() != candidates.size()) { - Collection copy = new ArrayList(); - - for (CharSequence next : candidates) { - if (!copy.contains(next)) { - copy.add(next); - } - } - - candidates = copy; - } - - reader.println(); - reader.printColumns(candidates); - } - - /** - * Returns a root that matches all the {@link String} elements of the specified {@link List}, - * or null if there are no commonalities. For example, if the list contains - * foobar, foobaz, foobuz, the method will return foob. - */ - private String getUnambiguousCompletions(final List candidates) { - if (candidates == null || candidates.isEmpty()) { - return null; - } - - // convert to an array for speed - String[] strings = candidates.toArray(new String[candidates.size()]); - - String first = strings[0]; - StringBuilder candidate = new StringBuilder(); - - for (int i = 0; i < first.length(); i++) { - if (startsWith(first.substring(0, i + 1), strings)) { - candidate.append(first.charAt(i)); - } - else { - break; - } - } - - return candidate.toString(); - } - - /** - * @return true is all the elements of candidates start with starts - */ - private boolean startsWith(final String starts, final String[] candidates) { - for (String candidate : candidates) { - if (!candidate.startsWith(starts)) { - return false; - } - } - - return true; - } - - private static enum Messages - { - DISPLAY_CANDIDATES, - DISPLAY_CANDIDATES_YES, - DISPLAY_CANDIDATES_NO,; - - private static final - ResourceBundle - bundle = - ResourceBundle.getBundle(CandidateListCompletionHandler.class.getName(), - Locale.getDefault(), - CandidateListCompletionHandler.class.getClassLoader()); - - public String format(final Object... args) { - return String.format(bundle.getString(name()), args); - } - } -} diff --git a/src/jline/src/main/java/jline/console/completer/Completer.java b/src/jline/src/main/java/jline/console/completer/Completer.java deleted file mode 100644 index ff5d22b108..0000000000 --- a/src/jline/src/main/java/jline/console/completer/Completer.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. - * - * This software is distributable under the BSD license. See the terms of the - * BSD license in the documentation provided with this software. - */ - -package jline.console.completer; - -import java.util.List; - -/** - * A completer is the mechanism by which tab-completion candidates will be resolved. - * - * @author Marc Prud'hommeaux - * @author Jason Dillon - * @since 2.3 - */ -public interface Completer -{ - // - // FIXME: Check if we can use CharSequece for buffer? - // - - /** - * Populates candidates with a list of possible completions for the buffer. - * - * The candidates list will not be sorted before being displayed to the user: thus, the - * complete method should sort the {@link List} before returning. - * - * @param buffer The buffer - * @param cursor The current position of the cursor in the buffer - * @param candidates The {@link List} of candidates to populate - * @return The index of the buffer for which the completion will be relative - */ - int complete(String buffer, int cursor, List candidates); -} diff --git a/src/jline/src/main/java/jline/console/completer/CompletionHandler.java b/src/jline/src/main/java/jline/console/completer/CompletionHandler.java deleted file mode 100644 index 8f32f2c7f6..0000000000 --- a/src/jline/src/main/java/jline/console/completer/CompletionHandler.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. - * - * This software is distributable under the BSD license. See the terms of the - * BSD license in the documentation provided with this software. - */ - -package jline.console.completer; - -import jline.console.ConsoleReader; - -import java.io.IOException; -import java.util.List; - -/** - * Handler for dealing with candidates for tab-completion. - * - * @author Marc Prud'hommeaux - * @author Jason Dillon - * @since 2.3 - */ -public interface CompletionHandler -{ - boolean complete(ConsoleReader reader, List candidates, int position) throws IOException; -} diff --git a/src/jline/src/main/java/jline/console/completer/EnumCompleter.java b/src/jline/src/main/java/jline/console/completer/EnumCompleter.java deleted file mode 100644 index 738689e8a6..0000000000 --- a/src/jline/src/main/java/jline/console/completer/EnumCompleter.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (C) 2009 the original author(s). - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package jline.console.completer; - -/** - * {@link Completer} for {@link Enum} names. - * - * @author Jason Dillon - * @since 2.3 - */ -public class EnumCompleter - extends StringsCompleter -{ - public EnumCompleter(Class source) { - assert source != null; - - for (Enum n : source.getEnumConstants()) { - this.getStrings().add(n.name().toLowerCase()); - } - } -} \ No newline at end of file diff --git a/src/jline/src/main/java/jline/console/completer/FileNameCompleter.java b/src/jline/src/main/java/jline/console/completer/FileNameCompleter.java deleted file mode 100644 index 69667aa9bf..0000000000 --- a/src/jline/src/main/java/jline/console/completer/FileNameCompleter.java +++ /dev/null @@ -1,133 +0,0 @@ -/* - * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. - * - * This software is distributable under the BSD license. See the terms of the - * BSD license in the documentation provided with this software. - */ - -package jline.console.completer; - -import jline.internal.Configuration; - -import java.io.File; -import java.util.List; - -/** - * A file name completer takes the buffer and issues a list of - * potential completions. - *

- * This completer tries to behave as similar as possible to - * bash's file name completion (using GNU readline) - * with the following exceptions: - *

- *

    - *
  • Candidates that are directories will end with "/"
  • - *
  • Wildcard regular expressions are not evaluated or replaced
  • - *
  • The "~" character can be used to represent the user's home, - * but it cannot complete to other users' homes, since java does - * not provide any way of determining that easily
  • - *
- * - * @author Marc Prud'hommeaux - * @author Jason Dillon - * @since 2.3 - */ -public class FileNameCompleter - implements Completer -{ - // TODO: Handle files with spaces in them - - private static final boolean OS_IS_WINDOWS; - - static { - String os = Configuration.getOsName(); - OS_IS_WINDOWS = os.contains("windows"); - } - - public int complete(String buffer, final int cursor, final List candidates) { - // buffer can be null - assert candidates != null; - - if (buffer == null) { - buffer = ""; - } - - if (OS_IS_WINDOWS) { - buffer = buffer.replace('/', '\\'); - } - - String translated = buffer; - - File homeDir = getUserHome(); - - // Special character: ~ maps to the user's home directory - if (translated.startsWith("~" + separator())) { - translated = homeDir.getPath() + translated.substring(1); - } - else if (translated.startsWith("~")) { - translated = homeDir.getParentFile().getAbsolutePath(); - } - else if (!(translated.startsWith(separator()))) { - String cwd = getUserDir().getAbsolutePath(); - translated = cwd + separator() + translated; - } - - File file = new File(translated); - final File dir; - - if (translated.endsWith(separator())) { - dir = file; - } - else { - dir = file.getParentFile(); - } - - File[] entries = dir == null ? new File[0] : dir.listFiles(); - - return matchFiles(buffer, translated, entries, candidates); - } - - protected String separator() { - return File.separator; - } - - protected File getUserHome() { - return Configuration.getUserHome(); - } - - protected File getUserDir() { - return new File("."); - } - - protected int matchFiles(final String buffer, final String translated, final File[] files, final List candidates) { - if (files == null) { - return -1; - } - - int matches = 0; - - // first pass: just count the matches - for (File file : files) { - if (file.getAbsolutePath().startsWith(translated)) { - matches++; - } - } - for (File file : files) { - if (file.getAbsolutePath().startsWith(translated)) { - CharSequence name = file.getName() + (matches == 1 && file.isDirectory() ? separator() : " "); - candidates.add(render(file, name).toString()); - } - } - - final int index = buffer.lastIndexOf(separator()); - - return index + separator().length(); - } - - protected CharSequence render(final File file, final CharSequence name) { - assert file != null; - assert name != null; - - return name; - } -} diff --git a/src/jline/src/main/java/jline/console/completer/NullCompleter.java b/src/jline/src/main/java/jline/console/completer/NullCompleter.java deleted file mode 100644 index 872aee7c84..0000000000 --- a/src/jline/src/main/java/jline/console/completer/NullCompleter.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package jline.console.completer; - -import java.util.List; - -/** - * Null completer. - * - * @author Marc Prud'hommeaux - * @author Jason Dillon - * @since 2.3 - */ -public final class NullCompleter - implements Completer -{ - public static final NullCompleter INSTANCE = new NullCompleter(); - - public int complete(final String buffer, final int cursor, final List candidates) { - return -1; - } -} \ No newline at end of file diff --git a/src/jline/src/main/java/jline/console/completer/StringsCompleter.java b/src/jline/src/main/java/jline/console/completer/StringsCompleter.java deleted file mode 100644 index afd2abbcd1..0000000000 --- a/src/jline/src/main/java/jline/console/completer/StringsCompleter.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package jline.console.completer; - -import java.util.Arrays; -import java.util.Collection; -import java.util.List; -import java.util.SortedSet; -import java.util.TreeSet; - -/** - * Completer for a set of strings. - * - * @author Jason Dillon - * @since 2.3 - */ -public class StringsCompleter - implements Completer -{ - private final SortedSet strings = new TreeSet(); - - public StringsCompleter() { - // empty - } - - public StringsCompleter(final Collection strings) { - assert strings != null; - getStrings().addAll(strings); - } - - public StringsCompleter(final String... strings) { - this(Arrays.asList(strings)); - } - - public Collection getStrings() { - return strings; - } - - public int complete(final String buffer, final int cursor, final List candidates) { - // buffer could be null - assert candidates != null; - - if (buffer == null) { - candidates.addAll(strings); - } - else { - for (String match : strings.tailSet(buffer)) { - if (!match.startsWith(buffer)) { - break; - } - - candidates.add(match); - } - } - - if (candidates.size() == 1) { - candidates.set(0, candidates.get(0) + " "); - } - - return candidates.isEmpty() ? -1 : 0; - } -} \ No newline at end of file diff --git a/src/jline/src/main/java/jline/console/completer/package-info.java b/src/jline/src/main/java/jline/console/completer/package-info.java deleted file mode 100644 index 4f8fc08a76..0000000000 --- a/src/jline/src/main/java/jline/console/completer/package-info.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (C) 2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * Console completer support. - * - * @since 2.3 - */ -package jline.console.completer; \ No newline at end of file diff --git a/src/jline/src/main/java/jline/console/history/FileHistory.java b/src/jline/src/main/java/jline/console/history/FileHistory.java deleted file mode 100644 index a32e05ab75..0000000000 --- a/src/jline/src/main/java/jline/console/history/FileHistory.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. - * - * This software is distributable under the BSD license. See the terms of the - * BSD license in the documentation provided with this software. - */ - -package jline.console.history; - -import jline.internal.Log; - -import java.io.BufferedOutputStream; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileOutputStream; -import java.io.FileReader; -import java.io.Flushable; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.PrintStream; -import java.io.Reader; -import java.util.ListIterator; - -/** - * {@link History} using a file for persistent backing. - *

- * Implementers should install shutdown hook to call {@link FileHistory#flush} - * to save history to disk. - * - * @author Jason Dillon - * @since 2.0 - */ -public class FileHistory - extends MemoryHistory - implements PersistentHistory, Flushable -{ - private final File file; - - public FileHistory(final File file) throws IOException { - assert file != null; - this.file = file; - load(file); - } - - public File getFile() { - return file; - } - - public void load(final File file) throws IOException { - assert file != null; - if (file.exists()) { - Log.trace("Loading history from: ", file); - load(new FileReader(file)); - } - } - - public void load(final InputStream input) throws IOException { - assert input != null; - load(new InputStreamReader(input)); - } - - public void load(final Reader reader) throws IOException { - assert reader != null; - BufferedReader input = new BufferedReader(reader); - - String item; - while ((item = input.readLine()) != null) { - add(item); - } - } - - public void flush() throws IOException { - Log.trace("Flushing history"); - - if (!file.exists()) { - File dir = file.getParentFile(); - if (!dir.exists() && !dir.mkdirs()) { - Log.warn("Failed to create directory: ", dir); - } - if (!file.createNewFile()) { - Log.warn("Failed to create file: ", file); - } - } - - PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream(file))); - try { - for (Entry entry : this) { - out.println(entry.value()); - } - } - finally { - out.close(); - } - } - - public void purge() throws IOException { - Log.trace("Purging history"); - - clear(); - - if (!file.delete()) { - Log.warn("Failed to delete history file: ", file); - } - } -} \ No newline at end of file diff --git a/src/jline/src/main/java/jline/console/history/History.java b/src/jline/src/main/java/jline/console/history/History.java deleted file mode 100644 index 9e098c9985..0000000000 --- a/src/jline/src/main/java/jline/console/history/History.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. - * - * This software is distributable under the BSD license. See the terms of the - * BSD license in the documentation provided with this software. - */ - -package jline.console.history; - -import java.util.Iterator; -import java.util.ListIterator; - -/** - * Console history. - * - * @author Marc Prud'hommeaux - * @author Jason Dillon - * @since 2.3 - */ -public interface History - extends Iterable -{ - int size(); - - boolean isEmpty(); - - int index(); - - void clear(); - - CharSequence get(int index); - - void add(CharSequence line); - - void replace(CharSequence item); - - // - // Entries - // - - interface Entry - { - int index(); - - CharSequence value(); - } - - ListIterator entries(int index); - - ListIterator entries(); - - Iterator iterator(); - - // - // Navigation - // - - CharSequence current(); - - boolean previous(); - - boolean next(); - - boolean moveToFirst(); - - boolean moveToLast(); - - boolean moveTo(int index); - - void moveToEnd(); -} diff --git a/src/jline/src/main/java/jline/console/history/MemoryHistory.java b/src/jline/src/main/java/jline/console/history/MemoryHistory.java deleted file mode 100644 index 5b67d8801b..0000000000 --- a/src/jline/src/main/java/jline/console/history/MemoryHistory.java +++ /dev/null @@ -1,318 +0,0 @@ -/* - * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. - * - * This software is distributable under the BSD license. See the terms of the - * BSD license in the documentation provided with this software. - */ - -package jline.console.history; - -import java.util.Iterator; -import java.util.LinkedList; -import java.util.ListIterator; -import java.util.NoSuchElementException; - -/** - * Non-persistent {@link History}. - * - * @author Marc Prud'hommeaux - * @author Jason Dillon - * @since 2.3 - */ -public class MemoryHistory - implements History -{ - public static final int DEFAULT_MAX_SIZE = 500; - - private final LinkedList items = new LinkedList(); - - private int maxSize = DEFAULT_MAX_SIZE; - - private boolean ignoreDuplicates = true; - - private boolean autoTrim = false; - - // NOTE: These are all ideas from looking at the Bash man page: - - // TODO: Add ignore space? (lines starting with a space are ignored) - - // TODO: Add ignore patterns? - - // TODO: Add history timestamp? - - // TODO: Add erase dups? - - private int offset = 0; - - private int index = 0; - - public void setMaxSize(final int maxSize) { - this.maxSize = maxSize; - maybeResize(); - } - - public int getMaxSize() { - return maxSize; - } - - public boolean isIgnoreDuplicates() { - return ignoreDuplicates; - } - - public void setIgnoreDuplicates(final boolean flag) { - this.ignoreDuplicates = flag; - } - - public boolean isAutoTrim() { - return autoTrim; - } - - public void setAutoTrim(final boolean flag) { - this.autoTrim = flag; - } - - public int size() { - return items.size(); - } - - public boolean isEmpty() { - return items.isEmpty(); - } - - public int index() { - return offset + index; - } - - public void clear() { - items.clear(); - offset = 0; - index = 0; - } - - public CharSequence get(final int index) { - return items.get(index - offset); - } - - public void add(CharSequence item) { - assert item != null; - - if (isAutoTrim()) { - item = String.valueOf(item).trim(); - } - - if (isIgnoreDuplicates()) { - if (!items.isEmpty() && item.equals(items.getLast())) { - return; - } - } - - items.add(item); - - maybeResize(); - } - - public void replace(final CharSequence item) { - items.removeLast(); - add(item); - } - - private void maybeResize() { - while (size() > getMaxSize()) { - items.removeFirst(); - offset++; - } - - index = size(); - } - - public ListIterator entries(final int index) { - return new EntriesIterator(index - offset); - } - - public ListIterator entries() { - return entries(offset); - } - - public Iterator iterator() { - return entries(); - } - - private static class EntryImpl - implements Entry - { - private final int index; - - private final CharSequence value; - - public EntryImpl(int index, CharSequence value) { - this.index = index; - this.value = value; - } - - public int index() { - return index; - } - - public CharSequence value() { - return value; - } - - @Override - public String toString() { - return String.format("%d: %s", index, value); - } - } - - private class EntriesIterator - implements ListIterator - { - private final ListIterator source; - - private EntriesIterator(final int index) { - source = items.listIterator(index); - } - - public Entry next() { - if (!source.hasNext()) { - throw new NoSuchElementException(); - } - return new EntryImpl(offset + source.nextIndex(), source.next()); - } - - public Entry previous() { - if (!source.hasPrevious()) { - throw new NoSuchElementException(); - } - return new EntryImpl(offset + source.previousIndex(), source.previous()); - } - - public int nextIndex() { - return offset + source.nextIndex(); - } - - public int previousIndex() { - return offset + source.previousIndex(); - } - - public boolean hasNext() { - return source.hasNext(); - } - - public boolean hasPrevious() { - return source.hasPrevious(); - } - - public void remove() { - throw new UnsupportedOperationException(); - } - - public void set(final Entry entry) { - throw new UnsupportedOperationException(); - } - - public void add(final Entry entry) { - throw new UnsupportedOperationException(); - } - } - - // - // Navigation - // - - /** - * This moves the history to the last entry. This entry is one position - * before the moveToEnd() position. - * - * @return Returns false if there were no history entries or the history - * index was already at the last entry. - */ - public boolean moveToLast() { - int lastEntry = size() - 1; - if (lastEntry >= 0 && lastEntry != index) { - index = size() - 1; - return true; - } - - return false; - } - - /** - * Move to the specified index in the history - * @param index - * @return - */ - public boolean moveTo(int index) { - index -= offset; - if (index >= 0 && index < size() ) { - this.index = index; - return true; - } - return false; - } - - /** - * Moves the history index to the first entry. - * - * @return Return false if there are no entries in the history or if the - * history is already at the beginning. - */ - public boolean moveToFirst() { - if (size() > 0 && index != 0) { - index = 0; - return true; - } - - return false; - } - - /** - * Move to the end of the history buffer. This will be a blank entry, after - * all of the other entries. - */ - public void moveToEnd() { - index = size(); - } - - /** - * Return the content of the current buffer. - */ - public CharSequence current() { - if (index >= size()) { - return ""; - } - - return items.get(index); - } - - /** - * Move the pointer to the previous element in the buffer. - * - * @return true if we successfully went to the previous element - */ - public boolean previous() { - if (index <= 0) { - return false; - } - - index--; - - return true; - } - - /** - * Move the pointer to the next element in the buffer. - * - * @return true if we successfully went to the next element - */ - public boolean next() { - if (index >= size()) { - return false; - } - - index++; - - return true; - } - - -} \ No newline at end of file diff --git a/src/jline/src/main/java/jline/console/history/PersistentHistory.java b/src/jline/src/main/java/jline/console/history/PersistentHistory.java deleted file mode 100644 index b4cff792e3..0000000000 --- a/src/jline/src/main/java/jline/console/history/PersistentHistory.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. - * - * This software is distributable under the BSD license. See the terms of the - * BSD license in the documentation provided with this software. - */ - -package jline.console.history; - -import java.io.IOException; - -/** - * Persistent {@link History}. - * - * @author Jason Dillon - * @since 2.3 - */ -public interface PersistentHistory - extends History -{ - /** - * Flush all items to persistent storage. - * - * @throws IOException Flush failed - */ - void flush() throws IOException; - - /** - * Purge persistent storage and {@link #clear}. - * - * @throws IOException Purge failed - */ - void purge() throws IOException; -} \ No newline at end of file diff --git a/src/jline/src/main/java/jline/console/history/package-info.java b/src/jline/src/main/java/jline/console/history/package-info.java deleted file mode 100644 index d001133efb..0000000000 --- a/src/jline/src/main/java/jline/console/history/package-info.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (C) 2009 the original author(s). - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * Console history support. - * - * @since 2.0 - */ -package jline.console.history; \ No newline at end of file diff --git a/src/jline/src/main/java/jline/console/package-info.java b/src/jline/src/main/java/jline/console/package-info.java deleted file mode 100644 index ea2c05651d..0000000000 --- a/src/jline/src/main/java/jline/console/package-info.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (C) 2009 the original author(s). - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * Console support. - * - * @since 2.0 - */ -package jline.console; \ No newline at end of file diff --git a/src/jline/src/main/java/jline/internal/Configuration.java b/src/jline/src/main/java/jline/internal/Configuration.java deleted file mode 100644 index 6c6e0eb843..0000000000 --- a/src/jline/src/main/java/jline/internal/Configuration.java +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package jline.internal; - -import java.io.BufferedInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.Properties; - -/** - * Provides access to configuration values. - * - * @author Jason Dillon - * @since 2.4 - */ -public final class Configuration -{ - public static final String JLINE_RC = ".jline.rc"; - - private static final Properties userprops; - - static { - Properties props = new Properties(); - - File file = new File(getUserHome(), JLINE_RC); - if (file.exists() && file.canRead()) { - try { - InputStream input = new BufferedInputStream(new FileInputStream(file)); - try { - props.load(input); - Log.debug("Loaded user configuration: ", file); - } - finally { - input.close(); - } - } - catch (IOException e) { - Log.warn("Unable to read user configuration: ", file, e); - } - } - else { - Log.trace("User configuration file missing or unreadable: ", file); - } - - userprops = props; - } - - private static boolean isEmpty(final String value) { - return value == null || value.trim().length() == 0; - } - - public static String getString(final String name, final String defaultValue) { - assert name != null; - - String value; - - // Check sysprops first, it always wins - value = System.getProperty(name); - - if (isEmpty(value)) { - // Next try userprops - value = userprops.getProperty(name); - - if (isEmpty(value)) { - // else use the default - value = defaultValue; - } - } - - return value; - } - - public static String getString(final String name) { - return getString(name, null); - } - - public static Boolean getBoolean(final String name, final Boolean defaultValue) { - String value = getString(name); - if (isEmpty(value)) { - return defaultValue; - } - return Boolean.valueOf(value); - } - - public static Boolean getBoolean(final String name) { - return getBoolean(name, null); - } - - // - // System property helpers - // - - public static File getUserHome() { - return new File(System.getProperty("user.home")); - } - - public static String getOsName() { - return System.getProperty("os.name").toLowerCase(); - } - - public static String getFileEncoding() { - return System.getProperty("file.encoding"); - } - - public static String getInputEncoding() { - return System.getProperty("input.encoding", "UTF-8"); - } -} \ No newline at end of file diff --git a/src/jline/src/main/java/jline/internal/Log.java b/src/jline/src/main/java/jline/internal/Log.java deleted file mode 100644 index deb34c89ae..0000000000 --- a/src/jline/src/main/java/jline/internal/Log.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package jline.internal; - -import java.io.PrintStream; - -/** - * Internal logger. - * - * @author Jason Dillon - * @since 2.0 - */ -public final class Log -{ - ///CLOVER:OFF - - public static enum Level - { - TRACE, - DEBUG, - INFO, - WARN, - ERROR - } - - @SuppressWarnings({"StringConcatenation"}) - public static final boolean DEBUG = Boolean.getBoolean(Log.class.getName() + ".debug"); - - @SuppressWarnings({"StringConcatenation"}) - public static final boolean TRACE = Boolean.getBoolean(Log.class.getName() + ".trace"); - - private static PrintStream output = System.err; - - public static PrintStream getOutput() { - return output; - } - - public static void setOutput(final PrintStream out) { - assert out != null; - output = out; - } - - private static void print(final Object message) { - if (message instanceof Throwable) { - ((Throwable) message).printStackTrace(); - } - else if (message.getClass().isArray()) { - Object[] array = (Object[]) message; - - for (int i = 0; i < array.length; i++) { - output.print(array[i]); - if (i + 1 < array.length) { - output.print(","); - } - } - } - else { - output.print(message); - } - } - - private static void log(final Level level, final Object[] messages) { - //noinspection SynchronizeOnNonFinalField - synchronized (output) { - output.format("[%s] ", level); - - for (Object message : messages) { - print(message); - } - - output.println(); - output.flush(); - } - } - - public static void trace(final Object... messages) { - if (TRACE) { - log(Level.TRACE, messages); - } - } - - public static void debug(final Object... messages) { - if (TRACE || DEBUG) { - log(Level.DEBUG, messages); - } - } - - public static void warn(final Object... messages) { - log(Level.WARN, messages); - } - - public static void error(final Object... messages) { - log(Level.ERROR, messages); - } -} \ No newline at end of file diff --git a/src/jline/src/main/java/jline/internal/ReplayPrefixOneCharInputStream.java b/src/jline/src/main/java/jline/internal/ReplayPrefixOneCharInputStream.java deleted file mode 100644 index ae54fbb329..0000000000 --- a/src/jline/src/main/java/jline/internal/ReplayPrefixOneCharInputStream.java +++ /dev/null @@ -1,95 +0,0 @@ -package jline.internal; - -import java.io.IOException; -import java.io.InputStream; -import java.text.MessageFormat; - -/** - * This is awkward and inefficient, but probably the minimal way to add UTF-8 support to JLine - * - * @author Marc Herbert - * @author Jason Dillon - * @since 2.0 - */ -public final class ReplayPrefixOneCharInputStream - extends InputStream -{ - private byte firstByte; - - private int byteLength; - - private InputStream wrappedStream; - - private int byteRead; - - private final String encoding; - - public ReplayPrefixOneCharInputStream(final String encoding) { - assert encoding != null; - this.encoding = encoding; - } - - public String getEncoding() { - return encoding; - } - - public void setInput(final int recorded, final InputStream wrapped) throws IOException { - this.byteRead = 0; - this.firstByte = (byte) recorded; - this.wrappedStream = wrapped; - - byteLength = 1; - if (encoding.equalsIgnoreCase("UTF-8")) { - setInputUTF8(recorded, wrapped); - } - else if (encoding.equalsIgnoreCase("UTF-16")) { - byteLength = 2; - } - else if (encoding.equalsIgnoreCase("UTF-32")) { - byteLength = 4; - } - } - - - public void setInputUTF8(final int recorded, final InputStream wrapped) throws IOException { - // 110yyyyy 10zzzzzz - if ((firstByte & (byte) 0xE0) == (byte) 0xC0) { - this.byteLength = 2; - } - // 1110xxxx 10yyyyyy 10zzzzzz - else if ((firstByte & (byte) 0xF0) == (byte) 0xE0) { - this.byteLength = 3; - } - // 11110www 10xxxxxx 10yyyyyy 10zzzzzz - else if ((firstByte & (byte) 0xF8) == (byte) 0xF0) { - this.byteLength = 4; - } - else { - throw new IOException(MessageFormat.format("Invalid UTF-8 first byte: {0}", firstByte)); - } - } - - public int read() throws IOException { - if (available() == 0) { - return -1; - } - - byteRead++; - - if (byteRead == 1) { - return firstByte; - } - - return wrappedStream.read(); - } - - /** - * InputStreamReader is greedy and will try to read bytes in advance. We - * do NOT want this to happen since we use a temporary/"losing bytes" - * InputStreamReader above, that's why we hide the real - * wrappedStream.available() here. - */ - public int available() { - return byteLength - byteRead; - } -} \ No newline at end of file diff --git a/src/jline/src/main/java/jline/internal/TerminalLineSettings.java b/src/jline/src/main/java/jline/internal/TerminalLineSettings.java deleted file mode 100644 index 4b4b957649..0000000000 --- a/src/jline/src/main/java/jline/internal/TerminalLineSettings.java +++ /dev/null @@ -1,217 +0,0 @@ -/* - * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. - * - * This software is distributable under the BSD license. See the terms of the - * BSD license in the documentation provided with this software. - */ - -package jline.internal; - -import java.io.ByteArrayOutputStream; -import java.io.Closeable; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.text.MessageFormat; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** - * Provides access to terminal line settings via stty. - * - * @author Marc Prud'hommeaux - * @author Dale Kemp - * @author Jason Dillon - * @author Jean-Baptiste Onofré - * @since 2.0 - */ -public final class TerminalLineSettings -{ - public static final String JLINE_STTY = "jline.stty"; - - public static final String DEFAULT_STTY = "stty"; - - public static final String JLINE_SH = "jline.sh"; - - public static final String DEFAULT_SH = "sh"; - - private static String sttyCommand = Configuration.getString(JLINE_STTY, DEFAULT_STTY); - - private static String shCommand = Configuration.getString(JLINE_SH, DEFAULT_SH); - - private String config; - - private long configLastFetched; - - public TerminalLineSettings() throws IOException, InterruptedException { - config = get("-a"); - configLastFetched = System.currentTimeMillis(); - - Log.debug("Config: ", config); - - // sanity check - if (config.length() == 0) { - throw new IOException(MessageFormat.format("Unrecognized stty code: {0}", config)); - } - } - - public String getConfig() { - return config; - } - - public void restore() throws IOException, InterruptedException { - set("sane"); - } - - public String get(final String args) throws IOException, InterruptedException { - return stty(args); - } - - public void set(final String args) throws IOException, InterruptedException { - stty(args); - } - - /** - *

- * Get the value of a stty property, including the management of a cache. - *

- * - * @param name the stty property. - * @return the stty property value. - */ - public int getProperty(String name) { - assert name != null; - try { - // tty properties are cached so we don't have to worry too much about getting term widht/height - if (config == null || System.currentTimeMillis() - configLastFetched > 1000 ) { - config = get("-a"); - configLastFetched = System.currentTimeMillis(); - } - return this.getProperty(name, config); - } catch (Exception e) { - Log.warn("Failed to query stty ", name, e); - return -1; - } - } - - /** - *

- * Parses a stty output (provided by stty -a) and return the value of a given property. - *

- * - * @param name property name. - * @param stty string resulting of stty -a execution. - * @return value of the given property. - */ - protected int getProperty(String name, String stty) { - // try the first kind of regex - Pattern pattern = Pattern.compile(name + "\\s+=\\s+([^;]*)[;\\n\\r]"); - Matcher matcher = pattern.matcher(stty); - if (!matcher.find()) { - // try a second kind of regex - pattern = Pattern.compile(name + "\\s+([^;]*)[;\\n\\r]"); - matcher = pattern.matcher(stty); - if (!matcher.find()) { - // try a second try of regex - pattern = Pattern.compile("(\\S*)\\s+" + name); - matcher = pattern.matcher(stty); - if (!matcher.find()) { - return -1; - } - } - } - return parseControlChar(matcher.group(1)); - } - - private int parseControlChar(String str) { - // under - if ("".equals(str)) { - return -1; - } - // octal - if (str.charAt(0) == '0') { - return Integer.parseInt(str, 8); - } - // decimal - if (str.charAt(0) >= '1' && str.charAt(0) <= '9') { - return Integer.parseInt(str, 10); - } - // control char - if (str.charAt(0) == '^') { - if (str.charAt(1) == '?') { - return 127; - } else { - return str.charAt(1) - 64; - } - } else if (str.charAt(0) == 'M' && str.charAt(1) == '-') { - if (str.charAt(2) == '^') { - if (str.charAt(3) == '?') { - return 127 + 128; - } else { - return str.charAt(3) - 64 + 128; - } - } else { - return str.charAt(2) + 128; - } - } else { - return str.charAt(0); - } - } - - private static String stty(final String args) throws IOException, InterruptedException { - assert args != null; - return exec(String.format("%s %s < /dev/tty", sttyCommand, args)); - } - - private static String exec(final String cmd) throws IOException, InterruptedException { - assert cmd != null; - return exec(shCommand, "-c", cmd); - } - - private static String exec(final String... cmd) throws IOException, InterruptedException { - assert cmd != null; - - ByteArrayOutputStream bout = new ByteArrayOutputStream(); - - Log.trace("Running: ", cmd); - - Process p = Runtime.getRuntime().exec(cmd); - - InputStream in = null; - InputStream err = null; - OutputStream out = null; - try { - int c; - in = p.getInputStream(); - while ((c = in.read()) != -1) { - bout.write(c); - } - err = p.getErrorStream(); - while ((c = err.read()) != -1) { - bout.write(c); - } - out = p.getOutputStream(); - p.waitFor(); - } - finally { - close(in, out, err); - } - - String result = bout.toString(); - - Log.trace("Result: ", result); - - return result; - } - - private static void close(final Closeable... closeables) { - for (Closeable c : closeables) { - try { - c.close(); - } - catch (Exception e) { - // Ignore - } - } - } -} \ No newline at end of file diff --git a/src/jline/src/main/java/jline/internal/package-info.java b/src/jline/src/main/java/jline/internal/package-info.java deleted file mode 100644 index e15ddfb25c..0000000000 --- a/src/jline/src/main/java/jline/internal/package-info.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (C) 2009 the original author(s). - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * Internal support. - * - * @since 2.0 - */ -package jline.internal; \ No newline at end of file diff --git a/src/jline/src/main/java/jline/package-info.java b/src/jline/src/main/java/jline/package-info.java deleted file mode 100644 index 78924e262a..0000000000 --- a/src/jline/src/main/java/jline/package-info.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (C) 2009 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * JLine 2. - * - * @since 2.0 - */ -package jline; \ No newline at end of file diff --git a/src/jline/src/main/java/scala/tools/jline/AnsiWindowsTerminal.java b/src/jline/src/main/java/scala/tools/jline/AnsiWindowsTerminal.java new file mode 100644 index 0000000000..94697137d3 --- /dev/null +++ b/src/jline/src/main/java/scala/tools/jline/AnsiWindowsTerminal.java @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2009 the original author(s). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * MODIFICATIONS: methods to deal with wrapping the output stream. + */ + +package scala.tools.jline; + +import org.fusesource.jansi.AnsiConsole; +import org.fusesource.jansi.AnsiOutputStream; +import org.fusesource.jansi.WindowsAnsiOutputStream; + +import java.io.ByteArrayOutputStream; +import java.io.OutputStream; + +/** + * ANSI-supported {@link WindowsTerminal}. + * + * @since 2.0 + */ +public class AnsiWindowsTerminal + extends WindowsTerminal +{ + private final boolean ansiSupported = detectAnsiSupport(); + + @Override + public OutputStream wrapOutIfNeeded(OutputStream out) { + return wrapOutputStream(out); + } + + /** + * Returns an ansi output stream handler. We return whatever was + * passed if we determine we cannot handle ansi based on Kernel32 calls. + * + * @return an @{link AltWindowAnsiOutputStream} instance or the passed + * stream. + */ + private static OutputStream wrapOutputStream(final OutputStream stream) { + String os = System.getProperty("os.name"); + if( os.startsWith("Windows") ) { + // On windows we know the console does not interpret ANSI codes.. + try { + return new WindowsAnsiOutputStream(stream); + } catch (Throwable ignore) { + // this happens when JNA is not in the path.. or + // this happens when the stdout is being redirected to a file. + } + // Use the ANSIOutputStream to strip out the ANSI escape sequences. + return new AnsiOutputStream(stream); + } + return stream; + } + + private static boolean detectAnsiSupport() { + OutputStream out = AnsiConsole.wrapOutputStream(new ByteArrayOutputStream()); + try { + out.close(); + } + catch (Exception e) { + // ignore; + } + return out instanceof WindowsAnsiOutputStream; + } + + public AnsiWindowsTerminal() throws Exception { + super(); + } + + @Override + public boolean isAnsiSupported() { + return ansiSupported; + } + + @Override + public boolean hasWeirdWrap() { + return false; + } +} diff --git a/src/jline/src/main/java/scala/tools/jline/NoInterruptUnixTerminal.java b/src/jline/src/main/java/scala/tools/jline/NoInterruptUnixTerminal.java new file mode 100644 index 0000000000..ef7cf23c4a --- /dev/null +++ b/src/jline/src/main/java/scala/tools/jline/NoInterruptUnixTerminal.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2009 the original author(s). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package scala.tools.jline; + +// Based on Apache Karaf impl + +/** + * Non-interruptable (via CTRL-C) {@link UnixTerminal}. + * + * @since 2.0 + */ +public class NoInterruptUnixTerminal + extends UnixTerminal +{ + public NoInterruptUnixTerminal() throws Exception { + super(); + } + + @Override + public void init() throws Exception { + super.init(); + getSettings().set("intr undef"); + } + + @Override + public void restore() throws Exception { + getSettings().set("intr ^C"); + super.restore(); + } +} diff --git a/src/jline/src/main/java/scala/tools/jline/Terminal.java b/src/jline/src/main/java/scala/tools/jline/Terminal.java new file mode 100644 index 0000000000..79611c244d --- /dev/null +++ b/src/jline/src/main/java/scala/tools/jline/Terminal.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. + * + * This software is distributable under the BSD license. See the terms of the + * BSD license in the documentation provided with this software. + */ + +package scala.tools.jline; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +/** + * Representation of the input terminal for a platform. + * + * @author Marc Prud'hommeaux + * @author Jason Dillon + * @since 2.0 + */ +public interface Terminal +{ + void init() throws Exception; + + void restore() throws Exception; + + void reset() throws Exception; + + boolean isSupported(); + + int getWidth(); + + int getHeight(); + + boolean isAnsiSupported(); + + /** + * When ANSI is not natively handled, the output will have to be wrapped. + */ + OutputStream wrapOutIfNeeded(OutputStream out); + + /** + * For terminals that don't wrap when character is written in last column, + * only when the next character is written. + * These are the ones that have 'am' and 'xn' termcap attributes (xterm and + * rxvt flavors falls under that category) + */ + boolean hasWeirdWrap(); + + boolean isEchoEnabled(); + + void setEchoEnabled(boolean enabled); + + int readCharacter(InputStream in) throws IOException; + + int readVirtualKey(InputStream in) throws IOException; + + InputStream getDefaultBindings(); +} diff --git a/src/jline/src/main/java/scala/tools/jline/TerminalFactory.java b/src/jline/src/main/java/scala/tools/jline/TerminalFactory.java new file mode 100644 index 0000000000..95b7c28bd5 --- /dev/null +++ b/src/jline/src/main/java/scala/tools/jline/TerminalFactory.java @@ -0,0 +1,173 @@ +/* + * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. + * + * This software is distributable under the BSD license. See the terms of the + * BSD license in the documentation provided with this software. + */ + +package scala.tools.jline; + +import scala.tools.jline.internal.Configuration; +import scala.tools.jline.internal.Log; + +import java.text.MessageFormat; +import java.util.HashMap; +import java.util.Map; + +/** + * Creates terminal instances. + * + * @author Jason Dillon + * @since 2.0 + */ +public class TerminalFactory +{ + public static final String JLINE_TERMINAL = "jline.terminal"; + + public static final String AUTO = "auto"; + + public static final String UNIX = "unix"; + + public static final String WIN = "win"; + + public static final String WINDOWS = "windows"; + + public static final String NONE = "none"; + + public static final String OFF = "off"; + + public static final String FALSE = "false"; + + private static final InheritableThreadLocal holder = new InheritableThreadLocal(); + + public static synchronized Terminal create() { + if (Log.TRACE) { + //noinspection ThrowableInstanceNeverThrown + Log.trace(new Throwable("CREATE MARKER")); + } + + String type = Configuration.getString(JLINE_TERMINAL); + if (type == null) { + type = AUTO; + } + + Log.debug("Creating terminal; type=", type); + + Terminal t; + try { + String tmp = type.toLowerCase(); + + if (tmp.equals(UNIX)) { + t = getFlavor(Flavor.UNIX); + } + else if (tmp.equals(WIN) | tmp.equals(WINDOWS)) { + t = getFlavor(Flavor.WINDOWS); + } + else if (tmp.equals(NONE) || tmp.equals(OFF) || tmp.equals(FALSE)) { + t = new UnsupportedTerminal(); + } + else { + if (tmp.equals(AUTO)) { + String os = Configuration.getOsName(); + Flavor flavor = Flavor.UNIX; + if (os.contains(WINDOWS)) { + flavor = Flavor.WINDOWS; + } + t = getFlavor(flavor); + } + else { + try { + t = (Terminal) Thread.currentThread().getContextClassLoader().loadClass(type).newInstance(); + } + catch (Exception e) { + throw new IllegalArgumentException(MessageFormat.format("Invalid terminal type: {0}", type), e); + } + } + } + } + catch (Exception e) { + Log.error("Failed to construct terminal; falling back to unsupported", e); + t = new UnsupportedTerminal(); + } + + Log.debug("Created Terminal: ", t); + + try { + t.init(); + } + catch (Exception e) { + Log.error("Terminal initialization failed; falling back to unsupported", e); + return new UnsupportedTerminal(); + } + + return t; + } + + public static synchronized void reset() { + holder.remove(); + } + + public static synchronized void resetIf(final Terminal t) { + if (holder.get() == t) { + reset(); + } + } + + public static enum Type + { + AUTO, + WINDOWS, + UNIX, + NONE + } + + public static synchronized void configure(final String type) { + assert type != null; + System.setProperty(JLINE_TERMINAL, type); + } + + public static synchronized void configure(final Type type) { + assert type != null; + configure(type.name().toLowerCase()); + } + + // + // Flavor Support + // + + public static enum Flavor + { + WINDOWS, + UNIX + } + + private static final Map> FLAVORS = new HashMap>(); + + static { + registerFlavor(Flavor.WINDOWS, AnsiWindowsTerminal.class); + registerFlavor(Flavor.UNIX, UnixTerminal.class); + } + + public static synchronized Terminal get() { + Terminal t = holder.get(); + if (t == null) { + t = create(); + holder.set(t); + } + return t; + } + + public static Terminal getFlavor(final Flavor flavor) throws Exception { + Class type = FLAVORS.get(flavor); + if (type != null) { + return type.newInstance(); + } + + throw new InternalError(); + } + + public static void registerFlavor(final Flavor flavor, final Class type) { + FLAVORS.put(flavor, type); + } + +} \ No newline at end of file diff --git a/src/jline/src/main/java/scala/tools/jline/TerminalSupport.java b/src/jline/src/main/java/scala/tools/jline/TerminalSupport.java new file mode 100644 index 0000000000..1ca12cb73f --- /dev/null +++ b/src/jline/src/main/java/scala/tools/jline/TerminalSupport.java @@ -0,0 +1,179 @@ +/* + * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. + * + * This software is distributable under the BSD license. See the terms of the + * BSD license in the documentation provided with this software. + */ + +package scala.tools.jline; + +import scala.tools.jline.internal.Log; +import scala.tools.jline.internal.Configuration; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +/** + * Provides support for {@link Terminal} instances. + * + * @author Jason Dillon + * @since 2.0 + */ +public abstract class TerminalSupport + implements Terminal +{ + public static String DEFAULT_KEYBINDINGS_PROPERTIES = "keybindings.properties"; + + public static final String JLINE_SHUTDOWNHOOK = "jline.shutdownhook"; + + public static final int DEFAULT_WIDTH = 80; + + public static final int DEFAULT_HEIGHT = 24; + + private Thread shutdownHook; + + private boolean shutdownHookEnabled; + + private boolean supported; + + private boolean echoEnabled; + + private boolean ansiSupported; + + protected TerminalSupport(final boolean supported) { + this.supported = supported; + this.shutdownHookEnabled = Configuration.getBoolean(JLINE_SHUTDOWNHOOK, false); + } + + public void init() throws Exception { + installShutdownHook(new RestoreHook()); + } + + public void restore() throws Exception { + TerminalFactory.resetIf(this); + removeShutdownHook(); + } + + public void reset() throws Exception { + restore(); + init(); + } + + // Shutdown hooks causes classloader leakage in sbt, + // so they are only installed if -Djline.shutdownhook is true. + protected void installShutdownHook(final Thread hook) { + if (!shutdownHookEnabled) { + Log.debug("Not install shutdown hook " + hook + " because they are disabled."); + return; + } + + assert hook != null; + + if (shutdownHook != null) { + throw new IllegalStateException("Shutdown hook already installed"); + } + + try { + Runtime.getRuntime().addShutdownHook(hook); + shutdownHook = hook; + } + catch (AbstractMethodError e) { + // JDK 1.3+ only method. Bummer. + Log.trace("Failed to register shutdown hook: ", e); + } + } + + protected void removeShutdownHook() { + if (!shutdownHookEnabled) + return; + + if (shutdownHook != null) { + try { + Runtime.getRuntime().removeShutdownHook(shutdownHook); + } + catch (AbstractMethodError e) { + // JDK 1.3+ only method. Bummer. + Log.trace("Failed to remove shutdown hook: ", e); + } + catch (IllegalStateException e) { + // The VM is shutting down, not a big deal; ignore + } + shutdownHook = null; + } + } + + public final boolean isSupported() { + return supported; + } + + public synchronized boolean isAnsiSupported() { + return ansiSupported; + } + + protected synchronized void setAnsiSupported(final boolean supported) { + this.ansiSupported = supported; + Log.debug("Ansi supported: ", supported); + } + + /** + * Subclass to change behavior if needed. + * @return the passed out + */ + public OutputStream wrapOutIfNeeded(OutputStream out) { + return out; + } + + /** + * Defaults to true which was the behaviour before this method was added. + */ + public boolean hasWeirdWrap() { + return true; + } + + public int getWidth() { + return DEFAULT_WIDTH; + } + + public int getHeight() { + return DEFAULT_HEIGHT; + } + + public synchronized boolean isEchoEnabled() { + return echoEnabled; + } + + public synchronized void setEchoEnabled(final boolean enabled) { + this.echoEnabled = enabled; + Log.debug("Echo enabled: ", enabled); + } + + public int readCharacter(final InputStream in) throws IOException { + return in.read(); + } + + public int readVirtualKey(final InputStream in) throws IOException { + return readCharacter(in); + } + + public InputStream getDefaultBindings() { + return TerminalSupport.class.getResourceAsStream(DEFAULT_KEYBINDINGS_PROPERTIES); + } + + // + // RestoreHook + // + + protected class RestoreHook + extends Thread + { + public void start() { + try { + restore(); + } + catch (Exception e) { + Log.trace("Failed to restore: ", e); + } + } + } +} \ No newline at end of file diff --git a/src/jline/src/main/java/scala/tools/jline/UnixTerminal.java b/src/jline/src/main/java/scala/tools/jline/UnixTerminal.java new file mode 100644 index 0000000000..10c98888d3 --- /dev/null +++ b/src/jline/src/main/java/scala/tools/jline/UnixTerminal.java @@ -0,0 +1,232 @@ +/* + * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. + * + * This software is distributable under the BSD license. See the terms of the + * BSD license in the documentation provided with this software. + */ + +package scala.tools.jline; + +import scala.tools.jline.console.Key; +import scala.tools.jline.internal.Configuration; +import scala.tools.jline.internal.Log; +import scala.tools.jline.internal.ReplayPrefixOneCharInputStream; +import scala.tools.jline.internal.TerminalLineSettings; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.HashMap; +import java.util.Map; + +import static scala.tools.jline.UnixTerminal.UnixKey.*; +import static scala.tools.jline.console.Key.*; + +/** + * Terminal that is used for unix platforms. Terminal initialization + * is handled by issuing the stty command against the + * /dev/tty file to disable character echoing and enable + * character input. All known unix systems (including + * Linux and Macintosh OS X) support the stty), so this + * implementation should work for an reasonable POSIX system. + * + * @author Marc Prud'hommeaux + * @author Dale Kemp + * @author Jason Dillon + * @author Jean-Baptiste Onofré + * @since 2.0 + */ +public class UnixTerminal + extends TerminalSupport +{ + private final TerminalLineSettings settings = new TerminalLineSettings(); + + private final ReplayPrefixOneCharInputStream replayStream; + + private final InputStreamReader replayReader; + + public UnixTerminal() throws Exception { + super(true); + + this.replayStream = new ReplayPrefixOneCharInputStream(Configuration.getInputEncoding()); + this.replayReader = new InputStreamReader(replayStream, replayStream.getEncoding()); + } + + protected TerminalLineSettings getSettings() { + return settings; + } + + /** + * Remove line-buffered input by invoking "stty -icanon min 1" + * against the current terminal. + */ + @Override + public void init() throws Exception { + super.init(); + + setAnsiSupported(true); + + // set the console to be character-buffered instead of line-buffered + settings.set("-icanon min 1"); + + setEchoEnabled(false); + } + + /** + * Restore the original terminal configuration, which can be used when + * shutting down the console reader. The ConsoleReader cannot be + * used after calling this method. + */ + @Override + public void restore() throws Exception { + settings.restore(); + super.restore(); + // print a newline after the terminal exits. + // this should probably be a configurable. + System.out.println(); + } + + /** + * Returns the value of stty columns param. + */ + @Override + public int getWidth() { + int w = settings.getProperty("columns"); + return w < 1 ? DEFAULT_WIDTH : w; + } + + /** + * Returns the value of stty rows>/tt> param. + */ + @Override + public int getHeight() { + int h = settings.getProperty("rows"); + return h < 1 ? DEFAULT_HEIGHT : h; + } + + @Override + public synchronized void setEchoEnabled(final boolean enabled) { + try { + if (enabled) { + settings.set("echo"); + } + else { + settings.set("-echo"); + } + super.setEchoEnabled(enabled); + } + catch (Exception e) { + Log.error("Failed to ", (enabled ? "enable" : "disable"), " echo: ", e); + } + } + + @Override + public int readVirtualKey(final InputStream in) throws IOException { + int c = readCharacter(in); + + if (Key.valueOf(c) == DELETE && settings.getProperty("erase") == DELETE.code) { + c = BACKSPACE.code; + } + + UnixKey key = UnixKey.valueOf(c); + + // in Unix terminals, arrow keys are represented by a sequence of 3 characters. E.g., the up arrow key yields 27, 91, 68 + if (key == ARROW_START) { + // also the escape key is 27 thats why we read until we have something different than 27 + // this is a bugfix, because otherwise pressing escape and than an arrow key was an undefined state + while (key == ARROW_START) { + c = readCharacter(in); + key = UnixKey.valueOf(c); + } + + if (key == ARROW_PREFIX || key == O_PREFIX) { + c = readCharacter(in); + key = UnixKey.valueOf(c); + + if (key == ARROW_UP) { + return CTRL_P.code; + } + else if (key == ARROW_DOWN) { + return CTRL_N.code; + } + else if (key == ARROW_LEFT) { + return CTRL_B.code; + } + else if (key == ARROW_RIGHT) { + return CTRL_F.code; + } + else if (key == HOME_CODE) { + return CTRL_A.code; + } + else if (key == END_CODE) { + return CTRL_E.code; + } + else if (key == DEL_THIRD) { + readCharacter(in); // read 4th & ignore + return DELETE.code; + } + } + } + + // handle unicode characters, thanks for a patch from amyi@inf.ed.ac.uk + if (c > 128) { + // handle unicode characters longer than 2 bytes, + // thanks to Marc.Herbert@continuent.com + replayStream.setInput(c, in); + // replayReader = new InputStreamReader(replayStream, encoding); + c = replayReader.read(); + } + + return c; + } + + /** + * Unix keys. + */ + public static enum UnixKey + { + ARROW_START(27), + + ARROW_PREFIX(91), + + ARROW_LEFT(68), + + ARROW_RIGHT(67), + + ARROW_UP(65), + + ARROW_DOWN(66), + + O_PREFIX(79), + + HOME_CODE(72), + + END_CODE(70), + + DEL_THIRD(51), + + DEL_SECOND(126),; + + public final short code; + + UnixKey(final int code) { + this.code = (short) code; + } + + private static final Map codes; + + static { + Map map = new HashMap(); + + for (UnixKey key : UnixKey.values()) { + map.put(key.code, key); + } + + codes = map; + } + + public static UnixKey valueOf(final int code) { + return codes.get((short) code); + } + } +} \ No newline at end of file diff --git a/src/jline/src/main/java/scala/tools/jline/UnsupportedTerminal.java b/src/jline/src/main/java/scala/tools/jline/UnsupportedTerminal.java new file mode 100644 index 0000000000..04fe4f7f16 --- /dev/null +++ b/src/jline/src/main/java/scala/tools/jline/UnsupportedTerminal.java @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. + * + * This software is distributable under the BSD license. See the terms of the + * BSD license in the documentation provided with this software. + */ + +package scala.tools.jline; + +/** + * An unsupported terminal. + * + * @author Marc Prud'hommeaux + * @author Jason Dillon + * @since 2.0 + */ +public class UnsupportedTerminal + extends TerminalSupport +{ + public UnsupportedTerminal() { + super(false); + setAnsiSupported(false); + setEchoEnabled(true); + } +} diff --git a/src/jline/src/main/java/scala/tools/jline/WindowsTerminal.java b/src/jline/src/main/java/scala/tools/jline/WindowsTerminal.java new file mode 100644 index 0000000000..66d2c69f8d --- /dev/null +++ b/src/jline/src/main/java/scala/tools/jline/WindowsTerminal.java @@ -0,0 +1,466 @@ +/* + * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. + * + * This software is distributable under the BSD license. See the terms of the + * BSD license in the documentation provided with this software. + */ + +package scala.tools.jline; + +import java.io.FileDescriptor; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.HashMap; +import java.util.Map; + +import scala.tools.jline.internal.Configuration; +import org.fusesource.jansi.internal.WindowsSupport; + +import scala.tools.jline.internal.Log; +import scala.tools.jline.internal.ReplayPrefixOneCharInputStream; + +import static scala.tools.jline.WindowsTerminal.ConsoleMode.*; +import static scala.tools.jline.WindowsTerminal.WindowsKey.*; +import static scala.tools.jline.console.Key.*; + +/** + * Terminal implementation for Microsoft Windows. Terminal initialization in + * {@link #init} is accomplished by extracting the + * jline_version.dll, saving it to the system temporary + * directoy (determined by the setting of the java.io.tmpdir System + * property), loading the library, and then calling the Win32 APIs SetConsoleMode and + * GetConsoleMode to + * disable character echoing. + *

+ *

+ * By default, the {@link #readCharacter} method will attempt to test to see if + * the specified {@link InputStream} is {@link System#in} or a wrapper around + * {@link FileDescriptor#in}, and if so, will bypass the character reading to + * directly invoke the readc() method in the JNI library. This is so the class + * can read special keys (like arrow keys) which are otherwise inaccessible via + * the {@link System#in} stream. Using JNI reading can be bypassed by setting + * the jline.WindowsTerminal.directConsole system property + * to false. + *

+ * + * @author Marc Prud'hommeaux + * @author Jason Dillon + * @since 2.0 + */ +public class WindowsTerminal + extends TerminalSupport +{ + public static final String JLINE_WINDOWS_TERMINAL_INPUT_ENCODING = "jline.WindowsTerminal.input.encoding"; + + public static final String JLINE_WINDOWS_TERMINAL_OUTPUT_ENCODING = "jline.WindowsTerminal.output.encoding"; + + public static final String JLINE_WINDOWS_TERMINAL_DIRECT_CONSOLE = "jline.WindowsTerminal.directConsole"; + + public static final String WINDOWSBINDINGS_PROPERTIES = "windowsbindings.properties"; + + public static final String ANSI = WindowsTerminal.class.getName() + ".ansi"; + + private boolean directConsole; + + private int originalMode; + + private final ReplayPrefixOneCharInputStream replayStream; + + private final InputStreamReader replayReader; + + public WindowsTerminal() throws Exception { + super(true); + + this.replayStream = + new ReplayPrefixOneCharInputStream(Configuration.getString(JLINE_WINDOWS_TERMINAL_INPUT_ENCODING, Configuration.getFileEncoding())); + this.replayReader = new InputStreamReader(replayStream, replayStream.getEncoding()); + } + + @Override + public void init() throws Exception { + super.init(); + + setAnsiSupported(Boolean.getBoolean(ANSI)); + + // + // FIXME: Need a way to disable direct console and sysin detection muck + // + + setDirectConsole(Boolean.getBoolean(JLINE_WINDOWS_TERMINAL_DIRECT_CONSOLE)); + + this.originalMode = getConsoleMode(); + setConsoleMode(originalMode & ~ENABLE_ECHO_INPUT.code); + setEchoEnabled(false); + } + + /** + * Restore the original terminal configuration, which can be used when + * shutting down the console reader. The ConsoleReader cannot be + * used after calling this method. + */ + @Override + public void restore() throws Exception { + // restore the old console mode + setConsoleMode(originalMode); + super.restore(); + } + + @Override + public int getWidth() { + int w = getWindowsTerminalWidth(); + return w < 1 ? DEFAULT_WIDTH : w; + } + + @Override + public int getHeight() { + int h = getWindowsTerminalHeight(); + return h < 1 ? DEFAULT_HEIGHT : h; + } + + @Override + public void setEchoEnabled(final boolean enabled) { + // Must set these four modes at the same time to make it work fine. + if (enabled) { + setConsoleMode(getConsoleMode() | + ENABLE_ECHO_INPUT.code | + ENABLE_LINE_INPUT.code | + ENABLE_PROCESSED_INPUT.code | + ENABLE_WINDOW_INPUT.code); + } + else { + setConsoleMode(getConsoleMode() & + ~(ENABLE_LINE_INPUT.code | + ENABLE_ECHO_INPUT.code | + ENABLE_PROCESSED_INPUT.code | + ENABLE_WINDOW_INPUT.code)); + } + super.setEchoEnabled(enabled); + } + + /** + * Whether or not to allow the use of the JNI console interaction. + */ + public void setDirectConsole(final boolean flag) { + this.directConsole = flag; + Log.debug("Direct console: ", flag); + } + + /** + * Whether or not to allow the use of the JNI console interaction. + */ + public Boolean getDirectConsole() { + return directConsole; + } + + + @Override + public int readCharacter(final InputStream in) throws IOException { + // if we can detect that we are directly wrapping the system + // input, then bypass the input stream and read directly (which + // allows us to access otherwise unreadable strokes, such as + // the arrow keys) + + if (directConsole || isSystemIn(in)) { + return readByte(); + } + else { + return super.readCharacter(in); + } + } + + private boolean isSystemIn(final InputStream in) throws IOException { + assert in != null; + + if (in == System.in) { + return true; + } + else if (in instanceof FileInputStream && ((FileInputStream) in).getFD() == FileDescriptor.in) { + return true; + } + + return false; + } + + @Override + public int readVirtualKey(final InputStream in) throws IOException { + int indicator = readCharacter(in); + + // in Windows terminals, arrow keys are represented by + // a sequence of 2 characters. E.g., the up arrow + // key yields 224, 72 + if (indicator == SPECIAL_KEY_INDICATOR.code || indicator == NUMPAD_KEY_INDICATOR.code) { + int c = readCharacter(in); + WindowsKey key = WindowsKey.valueOf(c); + + switch (key) { + case UP_ARROW_KEY: + return CTRL_P.code; // translate UP -> CTRL-P + + case LEFT_ARROW_KEY: + return CTRL_B.code; // translate LEFT -> CTRL-B + + case RIGHT_ARROW_KEY: + return CTRL_F.code; // translate RIGHT -> CTRL-F + + case DOWN_ARROW_KEY: + return CTRL_N.code; // translate DOWN -> CTRL-N + + case DELETE_KEY: + return CTRL_QM.code; // translate DELETE -> CTRL-? + + case HOME_KEY: + return CTRL_A.code; + + case END_KEY: + return CTRL_E.code; + + case PAGE_UP_KEY: + return CTRL_K.code; + + case PAGE_DOWN_KEY: + return CTRL_L.code; + + case ESCAPE_KEY: + return CTRL_OB.code; // translate ESCAPE -> CTRL-[ + + case INSERT_KEY: + return CTRL_C.code; + + default: + return 0; + } + } + else if (indicator > 128) { + // handle unicode characters longer than 2 bytes, + // thanks to Marc.Herbert@continuent.com + replayStream.setInput(indicator, in); + // replayReader = new InputStreamReader(replayStream, encoding); + indicator = replayReader.read(); + + } + + return indicator; + } + + @Override + public InputStream getDefaultBindings() { + return WindowsTerminal.class.getResourceAsStream(WINDOWSBINDINGS_PROPERTIES); + } + + // + // Native Bits + // + private int getConsoleMode() { + return WindowsSupport.getConsoleMode(); + } + + private void setConsoleMode(int mode) { + WindowsSupport.setConsoleMode(mode); + } + + private int readByte() { + return WindowsSupport.readByte(); + } + + private int getWindowsTerminalWidth() { + return WindowsSupport.getWindowsTerminalWidth(); + } + + private int getWindowsTerminalHeight() { + return WindowsSupport.getWindowsTerminalHeight(); + } + + /** + * Console mode + *

+ * Constants copied wincon.h. + */ + public static enum ConsoleMode + { + /** + * The ReadFile or ReadConsole function returns only when a carriage return + * character is read. If this mode is disable, the functions return when one + * or more characters are available. + */ + ENABLE_LINE_INPUT(2), + + /** + * Characters read by the ReadFile or ReadConsole function are written to + * the active screen buffer as they are read. This mode can be used only if + * the ENABLE_LINE_INPUT mode is also enabled. + */ + ENABLE_ECHO_INPUT(4), + + /** + * CTRL+C is processed by the system and is not placed in the input buffer. + * If the input buffer is being read by ReadFile or ReadConsole, other + * control keys are processed by the system and are not returned in the + * ReadFile or ReadConsole buffer. If the ENABLE_LINE_INPUT mode is also + * enabled, backspace, carriage return, and linefeed characters are handled + * by the system. + */ + ENABLE_PROCESSED_INPUT(1), + + /** + * User interactions that change the size of the console screen buffer are + * reported in the console's input buffee. Information about these events + * can be read from the input buffer by applications using + * theReadConsoleInput function, but not by those using ReadFile + * orReadConsole. + */ + ENABLE_WINDOW_INPUT(8), + + /** + * If the mouse pointer is within the borders of the console window and the + * window has the keyboard focus, mouse events generated by mouse movement + * and button presses are placed in the input buffer. These events are + * discarded by ReadFile or ReadConsole, even when this mode is enabled. + */ + ENABLE_MOUSE_INPUT(16), + + /** + * When enabled, text entered in a console window will be inserted at the + * current cursor location and all text following that location will not be + * overwritten. When disabled, all following text will be overwritten. An OR + * operation must be performed with this flag and the ENABLE_EXTENDED_FLAGS + * flag to enable this functionality. + */ + ENABLE_PROCESSED_OUTPUT(1), + + /** + * This flag enables the user to use the mouse to select and edit text. To + * enable this option, use the OR to combine this flag with + * ENABLE_EXTENDED_FLAGS. + */ + ENABLE_WRAP_AT_EOL_OUTPUT(2),; + + public final int code; + + ConsoleMode(final int code) { + this.code = code; + } + } + + /** + * Windows keys. + *

+ * Constants copied wincon.h. + */ + public static enum WindowsKey + { + /** + * On windows terminals, this character indicates that a 'special' key has + * been pressed. This means that a key such as an arrow key, or delete, or + * home, etc. will be indicated by the next character. + */ + SPECIAL_KEY_INDICATOR(224), + + /** + * On windows terminals, this character indicates that a special key on the + * number pad has been pressed. + */ + NUMPAD_KEY_INDICATOR(0), + + /** + * When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR, + * this character indicates an left arrow key press. + */ + LEFT_ARROW_KEY(75), + + /** + * When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR + * this character indicates an + * right arrow key press. + */ + RIGHT_ARROW_KEY(77), + + /** + * When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR + * this character indicates an up + * arrow key press. + */ + UP_ARROW_KEY(72), + + /** + * When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR + * this character indicates an + * down arrow key press. + */ + DOWN_ARROW_KEY(80), + + /** + * When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR + * this character indicates that + * the delete key was pressed. + */ + DELETE_KEY(83), + + /** + * When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR + * this character indicates that + * the home key was pressed. + */ + HOME_KEY(71), + + /** + * When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR + * this character indicates that + * the end key was pressed. + */ + END_KEY(79), + + /** + * When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR + * this character indicates that + * the page up key was pressed. + */ + PAGE_UP_KEY(73), + + /** + * When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR + * this character indicates that + * the page down key was pressed. + */ + PAGE_DOWN_KEY(81), + + /** + * When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR + * this character indicates that + * the insert key was pressed. + */ + INSERT_KEY(82), + + /** + * When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR, + * this character indicates that the escape key was pressed. + */ + ESCAPE_KEY(0),; + + public final int code; + + WindowsKey(final int code) { + this.code = code; + } + + private static final Map codes; + + static { + Map map = new HashMap(); + + for (WindowsKey key : WindowsKey.values()) { + map.put(key.code, key); + } + + codes = map; + } + + public static WindowsKey valueOf(final int code) { + return codes.get(code); + } + } +} diff --git a/src/jline/src/main/java/scala/tools/jline/console/ConsoleReader.java b/src/jline/src/main/java/scala/tools/jline/console/ConsoleReader.java new file mode 100644 index 0000000000..bff0a10648 --- /dev/null +++ b/src/jline/src/main/java/scala/tools/jline/console/ConsoleReader.java @@ -0,0 +1,2160 @@ +/* + * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. + * + * This software is distributable under the BSD license. See the terms of the + * BSD license in the documentation provided with this software. + */ + +package scala.tools.jline.console; + +import scala.tools.jline.Terminal; +import scala.tools.jline.TerminalFactory; +import scala.tools.jline.console.completer.CandidateListCompletionHandler; +import scala.tools.jline.console.completer.Completer; +import scala.tools.jline.console.completer.CompletionHandler; +import scala.tools.jline.console.history.History; +import scala.tools.jline.console.history.MemoryHistory; +import scala.tools.jline.internal.Configuration; +import scala.tools.jline.internal.Log; +import org.fusesource.jansi.AnsiOutputStream; + +import java.awt.Toolkit; +import java.awt.datatransfer.Clipboard; +import java.awt.datatransfer.DataFlavor; +import java.awt.datatransfer.Transferable; +import java.awt.datatransfer.UnsupportedFlavorException; +import java.awt.event.ActionListener; +import java.io.*; +import java.util.*; + +/** + * A reader for console applications. It supports custom tab-completion, + * saveable command history, and command line editing. On some platforms, + * platform-specific commands will need to be issued before the reader will + * function properly. See {@link jline.Terminal#init} for convenience + * methods for issuing platform-specific setup commands. + * + * @author Marc Prud'hommeaux + * @author Jason Dillon + */ +public class ConsoleReader +{ + public static final String JLINE_NOBELL = "jline.nobell"; + + public static final String JLINE_EXPANDEVENTS = "jline.expandevents"; + + public static final char BACKSPACE = '\b'; + + public static final char RESET_LINE = '\r'; + + public static final char KEYBOARD_BELL = '\07'; + + public static final char NULL_MASK = 0; + + public static final int TAB_WIDTH = 4; + + private static final ResourceBundle + resources = ResourceBundle.getBundle(CandidateListCompletionHandler.class.getName()); + + private final Terminal terminal; + + private InputStream in; + + private final Writer out; + + private final CursorBuffer buf = new CursorBuffer(); + + private String prompt; + + private boolean bellEnabled = true; + + private boolean expandEvents = false; + + private Character mask; + + private Character echoCharacter; + + private StringBuffer searchTerm = null; + + private String previousSearchTerm = ""; + + private int searchIndex = -1; + + public ConsoleReader(final InputStream in, final OutputStream out, final InputStream bindings, final Terminal term) throws + IOException + { + this.in = in; + this.terminal = term != null ? term : TerminalFactory.get(); + this.out = new PrintWriter(terminal.wrapOutIfNeeded(out)); + this.keyBindings = loadKeyBindings(bindings); + + setBellEnabled(!Configuration.getBoolean(JLINE_NOBELL, false)); + setExpandEvents(Configuration.getBoolean(JLINE_EXPANDEVENTS, false)); + } + + /** + * @deprecated use {@link #ConsoleReader(InputStream, OutputStream, InputStream, Terminal)} + * to let the terminal wrap the output stream if needed. + */ + public ConsoleReader(final InputStream in, final Writer out, final InputStream bindings, final Terminal term) throws + IOException + { + this.in = in; + this.out = out; + this.terminal = term != null ? term : TerminalFactory.get(); + this.keyBindings = loadKeyBindings(bindings); + + setBellEnabled(!Configuration.getBoolean(JLINE_NOBELL, false)); + } + + /** + * @deprecated use {@link #ConsoleReader(InputStream, OutputStream, InputStream, Terminal)} + * to let the terminal wrap the output stream if needed. + */ + public ConsoleReader(final InputStream in, final Writer out, final Terminal term) throws IOException { + this(in, out, null, term); + } + + /** + * @deprecated use {@link #ConsoleReader(InputStream, OutputStream, InputStream, Terminal)} + * to let the terminal wrap the output stream if needed. + */ + public ConsoleReader(final InputStream in, final Writer out) throws IOException + { + this(in, out, null, null); + } + + /** + * Create a new reader using {@link FileDescriptor#in} for input and + * {@link System#out} for output. + *

+ * {@link FileDescriptor#in} is used because it has a better chance of not being buffered. + */ + public ConsoleReader() throws IOException { + this(new FileInputStream(FileDescriptor.in), System.out, null, null ); + } + + // FIXME: Only used for tests + + void setInput(final InputStream in) { + this.in = in; + } + + public InputStream getInput() { + return in; + } + + public Writer getOutput() { + return out; + } + + public Terminal getTerminal() { + return terminal; + } + + public CursorBuffer getCursorBuffer() { + return buf; + } + + public void setBellEnabled(final boolean enabled) { + this.bellEnabled = enabled; + } + + public boolean isBellEnabled() { + return bellEnabled; + } + + public void setExpandEvents(final boolean expand) { + this.expandEvents = expand; + } + + public boolean getExpandEvents() { + return expandEvents; + } + + public void setPrompt(final String prompt) { + this.prompt = prompt; + } + + public String getPrompt() { + return prompt; + } + + /** + * Set the echo character. For example, to have "*" entered when a password is typed: + *

+ *

+     * myConsoleReader.setEchoCharacter(new Character('*'));
+     * 
+ *

+ * Setting the character to + *

+ *

+     * null
+     * 
+ *

+ * will restore normal character echoing. Setting the character to + *

+ *

+     * new Character(0)
+     * 
+ *

+ * will cause nothing to be echoed. + * + * @param c the character to echo to the console in place of the typed character. + */ + public void setEchoCharacter(final Character c) { + this.echoCharacter = c; + } + + /** + * Returns the echo character. + */ + public Character getEchoCharacter() { + return echoCharacter; + } + + /** + * Erase the current line. + * + * @return false if we failed (e.g., the buffer was empty) + */ + final boolean resetLine() throws IOException { + if (buf.cursor == 0) { + return false; + } + + backspaceAll(); + + return true; + } + + int getCursorPosition() { + // FIXME: does not handle anything but a line with a prompt absolute position + String prompt = getPrompt(); + return ((prompt == null) ? 0 : stripAnsi(lastLine(prompt)).length()) + buf.cursor; + } + + /** + * Returns the text after the last '\n'. + * prompt is returned if no '\n' characters are present. + * null is returned if prompt is null. + */ + private String lastLine(String str) { + if (str == null) return ""; + int last = str.lastIndexOf("\n"); + + if (last >= 0) { + return str.substring(last + 1, str.length()); + } + + return str; + } + + private String stripAnsi(String str) { + if (str == null) return ""; + try { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + AnsiOutputStream aos = new AnsiOutputStream(baos); + aos.write(str.getBytes()); + aos.flush(); + return baos.toString(); + } catch (IOException e) { + return str; + } + } + + /** + * Move the cursor position to the specified absolute index. + */ + public final boolean setCursorPosition(final int position) throws IOException { + return moveCursor(position - buf.cursor) != 0; + } + + /** + * Set the current buffer's content to the specified {@link String}. The + * visual console will be modified to show the current buffer. + * + * @param buffer the new contents of the buffer. + */ + private void setBuffer(final String buffer) throws IOException { + // don't bother modifying it if it is unchanged + if (buffer.equals(buf.buffer.toString())) { + return; + } + + // obtain the difference between the current buffer and the new one + int sameIndex = 0; + + for (int i = 0, l1 = buffer.length(), l2 = buf.buffer.length(); (i < l1) + && (i < l2); i++) { + if (buffer.charAt(i) == buf.buffer.charAt(i)) { + sameIndex++; + } + else { + break; + } + } + + int diff = buf.cursor - sameIndex; + if (diff < 0) { // we can't backspace here so try from the end of the buffer + moveToEnd(); + diff = buf.buffer.length() - sameIndex; + } + + backspace(diff); // go back for the differences + killLine(); // clear to the end of the line + buf.buffer.setLength(sameIndex); // the new length + putString(buffer.substring(sameIndex)); // append the differences + } + + private void setBuffer(final CharSequence buffer) throws IOException { + setBuffer(String.valueOf(buffer)); + } + + /** + * Output put the prompt + the current buffer + */ + public final void drawLine() throws IOException { + String prompt = getPrompt(); + if (prompt != null) { + print(prompt); + } + + print(buf.buffer.toString()); + + if (buf.length() != buf.cursor) { // not at end of line + back(buf.length() - buf.cursor - 1); + } + // force drawBuffer to check for weird wrap (after clear screen) + drawBuffer(); + } + + /** + * Clear the line and redraw it. + */ + public final void redrawLine() throws IOException { + print(RESET_LINE); +// flush(); + drawLine(); + } + + /** + * Clear the buffer and add its contents to the history. + * + * @return the former contents of the buffer. + */ + final String finishBuffer() throws IOException { // FIXME: Package protected because used by tests + String str = buf.buffer.toString(); + + if (expandEvents) { + str = expandEvents(str); + } + + // we only add it to the history if the buffer is not empty + // and if mask is null, since having a mask typically means + // the string was a password. We clear the mask after this call + if (str.length() > 0) { + if (mask == null && isHistoryEnabled()) { + history.add(str); + } + else { + mask = null; + } + } + + history.moveToEnd(); + + buf.buffer.setLength(0); + buf.cursor = 0; + + return str; + } + + /** + * Expand event designator such as !!, !#, !3, etc... + * See http://www.gnu.org/software/bash/manual/html_node/Event-Designators.html + * + * @param str + * @return + */ + final String expandEvents(String str) throws IOException { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < str.length(); i++) { + char c = str.charAt(i); + switch (c) { + case '!': + if (i + 1 < str.length()) { + c = str.charAt(++i); + boolean neg = false; + String rep = null; + int i1, idx; + switch (c) { + case '!': + if (history.size() == 0) { + throw new IllegalArgumentException("!!: event not found"); + } + rep = history.get(history.index() - 1).toString(); + break; + case '#': + sb.append(sb.toString()); + break; + case '?': + i1 = str.indexOf('?', i + 1); + if (i1 < 0) { + i1 = str.length(); + } + String sc = str.substring(i + 1, i1); + i = i1; + idx = searchBackwards(sc); + if (idx < 0) { + throw new IllegalArgumentException("!?" + sc + ": event not found"); + } else { + rep = history.get(idx).toString(); + } + break; + case ' ': + case '\t': + sb.append('!'); + sb.append(c); + break; + case '-': + neg = true; + i++; + // fall through + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + i1 = i; + for (; i < str.length(); i++) { + c = str.charAt(i); + if (c < '0' || c > '9') { + break; + } + } + idx = 0; + try { + idx = Integer.parseInt(str.substring(i1, i)); + } catch (NumberFormatException e) { + throw new IllegalArgumentException((neg ? "!-" : "!") + str.substring(i1, i) + ": event not found"); + } + if (neg) { + if (idx < history.size()) { + rep = (history.get(history.index() - idx)).toString(); + } else { + throw new IllegalArgumentException((neg ? "!-" : "!") + str.substring(i1, i) + ": event not found"); + } + } else { + if (idx >= history.index() - history.size() && idx < history.index()) { + rep = (history.get(idx)).toString(); + } else { + throw new IllegalArgumentException((neg ? "!-" : "!") + str.substring(i1, i) + ": event not found"); + } + } + break; + default: + String ss = str.substring(i); + i = str.length(); + idx = searchBackwards(ss, history.index(), true); + if (idx < 0) { + throw new IllegalArgumentException("!" + ss + ": event not found"); + } else { + rep = history.get(idx).toString(); + } + break; + } + if (rep != null) { + sb.append(rep); + } + } else { + sb.append(c); + } + break; + case '^': + if (i == 0) { + int i1 = str.indexOf('^', i + 1); + int i2 = str.indexOf('^', i1 + 1); + if (i2 < 0) { + i2 = str.length(); + } + if (i1 > 0 && i2 > 0) { + String s1 = str.substring(i + 1, i1); + String s2 = str.substring(i1 + 1, i2); + String s = history.get(history.index() - 1).toString().replace(s1, s2); + sb.append(s); + i = i2 + 1; + break; + } + } + sb.append(c); + break; + default: + sb.append(c); + break; + } + } + String result = sb.toString(); + if (!str.equals(result)) { + print(result); + println(); + flush(); + } + return result; + + } + + /** + * Write out the specified string to the buffer and the output stream. + */ + public final void putString(final CharSequence str) throws IOException { + buf.write(str); + print(str); + drawBuffer(); + } + + /** + * Output the specified character, both to the buffer and the output stream. + */ + private void putChar(final int c, final boolean print) throws IOException { + buf.write((char) c); + + if (print) { + if (mask == null) { + // no masking + print(c); + } + else if (mask == NULL_MASK) { + // Don't print anything + } + else { + print(mask); + } + + drawBuffer(); + } + } + + /** + * Redraw the rest of the buffer from the cursor onwards. This is necessary + * for inserting text into the buffer. + * + * @param clear the number of characters to clear after the end of the buffer + */ + private void drawBuffer(final int clear) throws IOException { + // debug ("drawBuffer: " + clear); + if (buf.cursor == buf.length() && clear == 0) { + } else { + char[] chars = buf.buffer.substring(buf.cursor).toCharArray(); + if (mask != null) { + Arrays.fill(chars, mask); + } + if (terminal.hasWeirdWrap()) { + // need to determine if wrapping will occur: + int width = terminal.getWidth(); + int pos = getCursorPosition(); + for (int i = 0; i < chars.length; i++) { + print(chars[i]); + if ((pos + i + 1) % width == 0) { + print(32); // move cursor to next line by printing dummy space + print(13); // CR / not newline. + } + } + } else { + print(chars); + } + clearAhead(clear, chars.length); + if (terminal.isAnsiSupported()) { + if (chars.length > 0) { + back(chars.length); + } + } else { + back(chars.length); + } + } + if (terminal.hasWeirdWrap()) { + int width = terminal.getWidth(); + // best guess on whether the cursor is in that weird location... + // Need to do this without calling ansi cursor location methods + // otherwise it breaks paste of wrapped lines in xterm. + if (getCursorPosition() > 0 && (getCursorPosition() % width == 0) + && buf.cursor == buf.length() && clear == 0) { + // the following workaround is reverse-engineered from looking + // at what bash sent to the terminal in the same situation + print(32); // move cursor to next line by printing dummy space + print(13); // CR / not newline. + } + } + } + + /** + * Redraw the rest of the buffer from the cursor onwards. This is necessary + * for inserting text into the buffer. + */ + private void drawBuffer() throws IOException { + drawBuffer(0); + } + + /** + * Clear ahead the specified number of characters without moving the cursor. + * + * @param num the number of characters to clear + * @param delta the difference between the internal cursor and the screen + * cursor - if > 0, assume some stuff was printed and weird wrap has to be + * checked + */ + private void clearAhead(final int num, int delta) throws IOException { + if (num == 0) { + return; + } + + if (terminal.isAnsiSupported()) { + int width = terminal.getWidth(); + int screenCursorCol = getCursorPosition() + delta; + // clear current line + printAnsiSequence("K"); + // if cursor+num wraps, then we need to clear the line(s) below too + int curCol = screenCursorCol % width; + int endCol = (screenCursorCol + num - 1) % width; + int lines = num / width; + if (endCol < curCol) lines++; + for (int i = 0; i < lines; i++) { + printAnsiSequence("B"); + printAnsiSequence("2K"); + } + for (int i = 0; i < lines; i++) { + printAnsiSequence("A"); + } + return; + } + + // print blank extra characters + print(' ', num); + + // we need to flush here so a "clever" console doesn't just ignore the redundancy + // of a space followed by a backspace. +// flush(); + + // reset the visual cursor + back(num); + +// flush(); + } + + /** + * Move the visual cursor backwards without modifying the buffer cursor. + */ + private void back(final int num) throws IOException { + if (num == 0) return; + if (terminal.isAnsiSupported()) { + int width = getTerminal().getWidth(); + int cursor = getCursorPosition(); + int realCursor = cursor + num; + int realCol = realCursor % width; + int newCol = cursor % width; + int moveup = num / width; + int delta = realCol - newCol; + if (delta < 0) moveup++; + if (moveup > 0) { + printAnsiSequence(moveup + "A"); + } + printAnsiSequence((1 + newCol) + "G"); + return; + } + print(BACKSPACE, num); +// flush(); + } + + /** + * Flush the console output stream. This is important for printout out single characters (like a backspace or + * keyboard) that we want the console to handle immediately. + */ + public void flush() throws IOException { + out.flush(); + } + + private int backspaceAll() throws IOException { + return backspace(Integer.MAX_VALUE); + } + + /** + * Issue num backspaces. + * + * @return the number of characters backed up + */ + private int backspace(final int num) throws IOException { + if (buf.cursor == 0) { + return 0; + } + + int count = 0; + + int termwidth = getTerminal().getWidth(); + int lines = getCursorPosition() / termwidth; + count = moveCursor(-1 * num) * -1; + buf.buffer.delete(buf.cursor, buf.cursor + count); + if (getCursorPosition() / termwidth != lines) { + if (terminal.isAnsiSupported()) { + // debug("doing backspace redraw: " + getCursorPosition() + " on " + termwidth + ": " + lines); + printAnsiSequence("K"); + // if cursor+num wraps, then we need to clear the line(s) below too + // last char printed is one pos less than cursor so we subtract + // one +/* + // TODO: fixme (does not work - test with reverse search with wrapping line and CTRL-E) + int endCol = (getCursorPosition() + num - 1) % termwidth; + int curCol = getCursorPosition() % termwidth; + if (endCol < curCol) lines++; + for (int i = 1; i < lines; i++) { + printAnsiSequence("B"); + printAnsiSequence("2K"); + } + for (int i = 1; i < lines; i++) { + printAnsiSequence("A"); + } + return count; +*/ + } + } + drawBuffer(count); + + return count; + } + + /** + * Issue a backspace. + * + * @return true if successful + */ + public boolean backspace() throws IOException { + return backspace(1) == 1; + } + + private boolean moveToEnd() throws IOException { + return moveCursor(buf.length() - buf.cursor) > 0; + } + + /** + * Delete the character at the current position and redraw the remainder of the buffer. + */ + private boolean deleteCurrentCharacter() throws IOException { + if (buf.length() == 0 || buf.cursor == buf.length()) { + return false; + } + + buf.buffer.deleteCharAt(buf.cursor); + drawBuffer(1); + return true; + } + + private boolean previousWord() throws IOException { + while (isDelimiter(buf.current()) && (moveCursor(-1) != 0)) { + // nothing + } + + while (!isDelimiter(buf.current()) && (moveCursor(-1) != 0)) { + // nothing + } + + return true; + } + + private boolean nextWord() throws IOException { + while (isDelimiter(buf.current()) && (moveCursor(1) != 0)) { + // nothing + } + + while (!isDelimiter(buf.current()) && (moveCursor(1) != 0)) { + // nothing + } + + return true; + } + + private boolean deletePreviousWord() throws IOException { + while (isDelimiter(buf.current()) && backspace()) { + // nothing + } + + while (!isDelimiter(buf.current()) && backspace()) { + // nothing + } + + return true; + } + + /** + * Move the cursor where characters. + * + * @param num If less than 0, move abs(where) to the left, otherwise move where to the right. + * @return The number of spaces we moved + */ + public int moveCursor(final int num) throws IOException { + int where = num; + + if ((buf.cursor == 0) && (where <= 0)) { + return 0; + } + + if ((buf.cursor == buf.buffer.length()) && (where >= 0)) { + return 0; + } + + if ((buf.cursor + where) < 0) { + where = -buf.cursor; + } + else if ((buf.cursor + where) > buf.buffer.length()) { + where = buf.buffer.length() - buf.cursor; + } + + moveInternal(where); + + return where; + } + + /** + * Move the cursor where characters, without checking the current buffer. + * + * @param where the number of characters to move to the right or left. + */ + private void moveInternal(final int where) throws IOException { + // debug ("move cursor " + where + " (" + // + buf.cursor + " => " + (buf.cursor + where) + ")"); + buf.cursor += where; + + if (terminal.isAnsiSupported()) { + if (where < 0) { + back(Math.abs(where)); + } else { + int width = getTerminal().getWidth(); + int cursor = getCursorPosition(); + int oldLine = (cursor - where) / width; + int newLine = cursor / width; + if (newLine > oldLine) { + if (terminal.hasWeirdWrap()) { + // scroll up if at bottom + // note: + // on rxvt cywgin terminal.getHeight() is incorrect + // MacOs xterm does not seem to support scrolling + if (getCurrentAnsiRow() == terminal.getHeight()) { + printAnsiSequence((newLine - oldLine) + "S"); + } + } + printAnsiSequence((newLine - oldLine) + "B"); + } + printAnsiSequence(1 +(cursor % width) + "G"); + } +// flush(); + return; + } + + char c; + + if (where < 0) { + int len = 0; + for (int i = buf.cursor; i < buf.cursor - where; i++) { + if (buf.buffer.charAt(i) == '\t') { + len += TAB_WIDTH; + } + else { + len++; + } + } + + char chars[] = new char[len]; + Arrays.fill(chars, BACKSPACE); + out.write(chars); + + return; + } + else if (buf.cursor == 0) { + return; + } + else if (mask != null) { + c = mask; + } + else { + print(buf.buffer.substring(buf.cursor - where, buf.cursor).toCharArray()); + return; + } + + // null character mask: don't output anything + if (mask == NULL_MASK) { + return; + } + + print(c, Math.abs(where)); + } + + // FIXME: replace() is not used + + public final boolean replace(final int num, final String replacement) { + buf.buffer.replace(buf.cursor - num, buf.cursor, replacement); + try { + moveCursor(-num); + drawBuffer(Math.max(0, num - replacement.length())); + moveCursor(replacement.length()); + } + catch (IOException e) { + e.printStackTrace(); + return false; + } + return true; + } + + // + // Key reading + // + + /** + * Read a character from the console. + * + * @return the character, or -1 if an EOF is received. + */ + public final int readVirtualKey() throws IOException { + int c = terminal.readVirtualKey(in); + + Log.trace("Keystroke: ", c); + + // clear any echo characters + clearEcho(c); + + return c; + } + + /** + * Clear the echoed characters for the specified character code. + */ + private int clearEcho(final int c) throws IOException { + // if the terminal is not echoing, then ignore + if (!terminal.isEchoEnabled()) { + return 0; + } + + // otherwise, clear + int num = countEchoCharacters((char) c); + back(num); + drawBuffer(num); + + return num; + } + + private int countEchoCharacters(final char c) { + // tabs as special: we need to determine the number of spaces + // to cancel based on what out current cursor position is + if (c == 9) { + int tabStop = 8; // will this ever be different? + int position = getCursorPosition(); + + return tabStop - (position % tabStop); + } + + return getPrintableCharacters(c).length(); + } + + /** + * Return the number of characters that will be printed when the specified + * character is echoed to the screen + * + * Adapted from cat by Torbjorn Granlund, as repeated in stty by David MacKenzie. + */ + private StringBuilder getPrintableCharacters(final char ch) { + StringBuilder sbuff = new StringBuilder(); + + if (ch >= 32) { + if (ch < 127) { + sbuff.append(ch); + } + else if (ch == 127) { + sbuff.append('^'); + sbuff.append('?'); + } + else { + sbuff.append('M'); + sbuff.append('-'); + + if (ch >= (128 + 32)) { + if (ch < (128 + 127)) { + sbuff.append((char) (ch - 128)); + } + else { + sbuff.append('^'); + sbuff.append('?'); + } + } + else { + sbuff.append('^'); + sbuff.append((char) (ch - 128 + 64)); + } + } + } + else { + sbuff.append('^'); + sbuff.append((char) (ch + 64)); + } + + return sbuff; + } + + public final int readCharacter(final char... allowed) throws IOException { + // if we restrict to a limited set and the current character is not in the set, then try again. + char c; + + Arrays.sort(allowed); // always need to sort before binarySearch + + while (Arrays.binarySearch(allowed, c = (char) readVirtualKey()) < 0) { + // nothing + } + + return c; + } + + // + // Key Bindings + // + + public static final String JLINE_COMPLETION_THRESHOLD = "jline.completion.threshold"; + + public static final String JLINE_KEYBINDINGS = "jline.keybindings"; + + public static final String JLINEBINDINGS_PROPERTIES = ".jlinebindings.properties"; + + /** + * The map for logical operations. + */ + private final short[] keyBindings; + + private short[] loadKeyBindings(InputStream input) throws IOException { + if (input == null) { + try { + File file = new File(Configuration.getUserHome(), JLINEBINDINGS_PROPERTIES); + + String path = Configuration.getString(JLINE_KEYBINDINGS); + if (path != null) { + file = new File(path); + } + + if (file.isFile()) { + Log.debug("Loading user bindings from: ", file); + input = new FileInputStream(file); + } + } + catch (Exception e) { + Log.error("Failed to load user bindings", e); + } + } + + if (input == null) { + Log.debug("Using default bindings"); + input = getTerminal().getDefaultBindings(); + } + + short[] keyBindings = new short[Character.MAX_VALUE * 2]; + + Arrays.fill(keyBindings, Operation.UNKNOWN.code); + + // Loads the key bindings. Bindings file is in the format: + // + // keycode: operation name + + if (input != null) { + input = new BufferedInputStream(input); + Properties p = new Properties(); + p.load(input); + input.close(); + + for (Object key : p.keySet()) { + String val = (String) key; + + try { + short code = Short.parseShort(val); + String name = p.getProperty(val); + Operation op = Operation.valueOf(name); + keyBindings[code] = op.code; + } + catch (NumberFormatException e) { + Log.error("Failed to convert binding code: ", val, e); + } + } + + // hardwired arrow key bindings + // keybindings[VK_UP] = PREV_HISTORY; + // keybindings[VK_DOWN] = NEXT_HISTORY; + // keybindings[VK_LEFT] = PREV_CHAR; + // keybindings[VK_RIGHT] = NEXT_CHAR; + } + + return keyBindings; + } + + int getKeyForAction(final short logicalAction) { + for (int i = 0; i < keyBindings.length; i++) { + if (keyBindings[i] == logicalAction) { + return i; + } + } + + return -1; + } + + int getKeyForAction(final Operation op) { + assert op != null; + return getKeyForAction(op.code); + } + + /** + * Reads the console input and returns an array of the form [raw, key binding]. + */ + private int[] readBinding() throws IOException { + int c = readVirtualKey(); + + if (c == -1) { + return null; + } + + // extract the appropriate key binding + short code = keyBindings[c]; + + Log.trace("Translated: ", c, " -> ", code); + + return new int[]{c, code}; + } + + // + // Line Reading + // + + /** + * Read the next line and return the contents of the buffer. + */ + public String readLine() throws IOException { + return readLine((String) null); + } + + /** + * Read the next line with the specified character mask. If null, then + * characters will be echoed. If 0, then no characters will be echoed. + */ + public String readLine(final Character mask) throws IOException { + return readLine(null, mask); + } + + public String readLine(final String prompt) throws IOException { + return readLine(prompt, null); + } + + /** + * Read a line from the in {@link InputStream}, and return the line + * (without any trailing newlines). + * + * @param prompt The prompt to issue to the console, may be null. + * @return A line that is read from the terminal, or null if there was null input (e.g., CTRL-D + * was pressed). + */ + public String readLine(String prompt, final Character mask) throws IOException { + // prompt may be null + // mask may be null + + // FIXME: This blows, each call to readLine will reset the console's state which doesn't seem very nice. + this.mask = mask; + if (prompt != null) { + setPrompt(prompt); + } + else { + prompt = getPrompt(); + } + + try { + if (!terminal.isSupported()) { + beforeReadLine(prompt, mask); + } + + if (prompt != null && prompt.length() > 0) { + out.write(prompt); + out.flush(); + } + + // if the terminal is unsupported, just use plain-java reading + if (!terminal.isSupported()) { + return readLine(in); + } + + String originalPrompt = this.prompt; + + final int NORMAL = 1; + final int SEARCH = 2; + int state = NORMAL; + + boolean success = true; + + while (true) { + int[] next = readBinding(); + + if (next == null) { + return null; + } + + int c = next[0]; + // int code = next[1]; + Operation code = Operation.valueOf(next[1]); + + if (c == -1) { + return null; + } + + // Search mode. + // + // Note that we have to do this first, because if there is a command + // not linked to a search command, we leave the search mode and fall + // through to the normal state. + if (state == SEARCH) { + int cursorDest = -1; + + switch (code) { + // This doesn't work right now, it seems CTRL-G is not passed + // down correctly. :( + case ABORT: + state = NORMAL; + break; + + case SEARCH_PREV: + if (searchTerm.length() == 0) { + searchTerm.append(previousSearchTerm); + } + + if (searchIndex == -1) { + searchIndex = searchBackwards(searchTerm.toString()); + } else { + searchIndex = searchBackwards(searchTerm.toString(), searchIndex); + } + break; + + case DELETE_PREV_CHAR: + if (searchTerm.length() > 0) { + searchTerm.deleteCharAt(searchTerm.length() - 1); + searchIndex = searchBackwards(searchTerm.toString()); + } + break; + + case UNKNOWN: + searchTerm.appendCodePoint(c); + searchIndex = searchBackwards(searchTerm.toString()); + break; + + default: + // Set buffer and cursor position to the found string. + if (searchIndex != -1) { + history.moveTo(searchIndex); + // set cursor position to the found string + cursorDest = history.current().toString().indexOf(searchTerm.toString()); + } + state = NORMAL; + break; + } + + // if we're still in search mode, print the search status + if (state == SEARCH) { + if (searchTerm.length() == 0) { + printSearchStatus("", ""); + searchIndex = -1; + } else { + if (searchIndex == -1) { + beep(); + } else { + printSearchStatus(searchTerm.toString(), history.get(searchIndex).toString()); + } + } + } + // otherwise, restore the line + else { + restoreLine(originalPrompt, cursorDest); + } + } + + if (state == NORMAL) { + switch (code) { + case EXIT: // ctrl-d + if (buf.buffer.length() == 0) { + return null; + } else { + deleteCurrentCharacter(); + } + break; + + case COMPLETE: // tab + success = complete(); + break; + + case MOVE_TO_BEG: + success = setCursorPosition(0); + break; + + case KILL_LINE: // CTRL-K + success = killLine(); + break; + + case CLEAR_SCREEN: // CTRL-L + success = clearScreen(); + break; + + case KILL_LINE_PREV: // CTRL-U + success = resetLine(); + break; + + case NEWLINE: // enter + moveToEnd(); + println(); // output newline + flush(); + return finishBuffer(); + + case DELETE_PREV_CHAR: // backspace + success = backspace(); + break; + + case DELETE_NEXT_CHAR: // delete + success = deleteCurrentCharacter(); + break; + + case MOVE_TO_END: + success = moveToEnd(); + break; + + case PREV_CHAR: + success = moveCursor(-1) != 0; + break; + + case NEXT_CHAR: + success = moveCursor(1) != 0; + break; + + case NEXT_HISTORY: + success = moveHistory(true); + break; + + case PREV_HISTORY: + success = moveHistory(false); + break; + + case ABORT: + case REDISPLAY: + break; + + case PASTE: + success = paste(); + break; + + case DELETE_PREV_WORD: + success = deletePreviousWord(); + break; + + case PREV_WORD: + success = previousWord(); + break; + + case NEXT_WORD: + success = nextWord(); + break; + + case START_OF_HISTORY: + success = history.moveToFirst(); + if (success) { + setBuffer(history.current()); + } + break; + + case END_OF_HISTORY: + success = history.moveToLast(); + if (success) { + setBuffer(history.current()); + } + break; + + case CLEAR_LINE: + moveInternal(-(buf.cursor)); + killLine(); + break; + + case INSERT: + buf.setOverTyping(!buf.isOverTyping()); + break; + + case SEARCH_PREV: // CTRL-R + if (searchTerm != null) { + previousSearchTerm = searchTerm.toString(); + } + searchTerm = new StringBuffer(buf.buffer); + state = SEARCH; + if (searchTerm.length() > 0) { + searchIndex = searchBackwards(searchTerm.toString()); + if (searchIndex == -1) { + beep(); + } + printSearchStatus(searchTerm.toString(), + searchIndex > -1 ? history.get(searchIndex).toString() : ""); + } else { + searchIndex = -1; + printSearchStatus("", ""); + } + break; + + case UNKNOWN: + default: + if (c != 0) { // ignore null chars + ActionListener action = triggeredActions.get((char) c); + if (action != null) { + action.actionPerformed(null); + } + else { + putChar(c, true); + } + } + else { + success = false; + } + } + + if (!success) { + beep(); + } + + flush(); + } + } + } + finally { + if (!terminal.isSupported()) { + afterReadLine(); + } + } + } + + /** + * Read a line for unsupported terminals. + */ + private String readLine(final InputStream in) throws IOException { + StringBuilder buff = new StringBuilder(); + + while (true) { + int i = in.read(); + + if (i == -1 || i == '\n' || i == '\r') { + return buff.toString(); + } + + buff.append((char) i); + } + + // return new BufferedReader (new InputStreamReader (in)).readLine (); + } + + // + // Completion + // + + private final List completers = new LinkedList(); + + private CompletionHandler completionHandler = new CandidateListCompletionHandler(); + + /** + * Add the specified {@link jline.console.completer.Completer} to the list of handlers for tab-completion. + * + * @param completer the {@link jline.console.completer.Completer} to add + * @return true if it was successfully added + */ + public boolean addCompleter(final Completer completer) { + return completers.add(completer); + } + + /** + * Remove the specified {@link jline.console.completer.Completer} from the list of handlers for tab-completion. + * + * @param completer The {@link Completer} to remove + * @return True if it was successfully removed + */ + public boolean removeCompleter(final Completer completer) { + return completers.remove(completer); + } + + /** + * Returns an unmodifiable list of all the completers. + */ + public Collection getCompleters() { + return Collections.unmodifiableList(completers); + } + + public void setCompletionHandler(final CompletionHandler handler) { + assert handler != null; + this.completionHandler = handler; + } + + public CompletionHandler getCompletionHandler() { + return this.completionHandler; + } + + /** + * Use the completers to modify the buffer with the appropriate completions. + * + * @return true if successful + */ + private boolean complete() throws IOException { + // debug ("tab for (" + buf + ")"); + if (completers.size() == 0) { + return false; + } + + List candidates = new LinkedList(); + String bufstr = buf.buffer.toString(); + int cursor = buf.cursor; + + int position = -1; + + for (Completer comp : completers) { + if ((position = comp.complete(bufstr, cursor, candidates)) != -1) { + break; + } + } + + return candidates.size() != 0 && getCompletionHandler().complete(this, candidates, position); + } + + /** + * The number of tab-completion candidates above which a warning will be + * prompted before showing all the candidates. + */ + private int autoprintThreshold = Integer.getInteger(JLINE_COMPLETION_THRESHOLD, 100); // same default as bash + + /** + * @param threshold the number of candidates to print without issuing a warning. + */ + public void setAutoprintThreshold(final int threshold) { + this.autoprintThreshold = threshold; + } + + /** + * @return the number of candidates to print without issuing a warning. + */ + public int getAutoprintThreshold() { + return autoprintThreshold; + } + + private boolean paginationEnabled; + + /** + * Whether to use pagination when the number of rows of candidates exceeds the height of the terminal. + */ + public void setPaginationEnabled(final boolean enabled) { + this.paginationEnabled = enabled; + } + + /** + * Whether to use pagination when the number of rows of candidates exceeds the height of the terminal. + */ + public boolean isPaginationEnabled() { + return paginationEnabled; + } + + // + // History + // + + private History history = new MemoryHistory(); + + public void setHistory(final History history) { + this.history = history; + } + + public History getHistory() { + return history; + } + + private boolean historyEnabled = true; + + /** + * Whether or not to add new commands to the history buffer. + */ + public void setHistoryEnabled(final boolean enabled) { + this.historyEnabled = enabled; + } + + /** + * Whether or not to add new commands to the history buffer. + */ + public boolean isHistoryEnabled() { + return historyEnabled; + } + + /** + * Move up or down the history tree. + */ + private boolean moveHistory(final boolean next) throws IOException { + if (next && !history.next()) { + return false; + } + else if (!next && !history.previous()) { + return false; + } + + setBuffer(history.current()); + + return true; + } + + // + // Printing + // + + public static final String CR = System.getProperty("line.separator"); + + /** + * Output the specified character to the output stream without manipulating the current buffer. + */ + private void print(final int c) throws IOException { + if (c == '\t') { + char chars[] = new char[TAB_WIDTH]; + Arrays.fill(chars, ' '); + out.write(chars); + return; + } + + out.write(c); + } + + /** + * Output the specified characters to the output stream without manipulating the current buffer. + */ + private void print(final char... buff) throws IOException { + int len = 0; + for (char c : buff) { + if (c == '\t') { + len += TAB_WIDTH; + } + else { + len++; + } + } + + char chars[]; + if (len == buff.length) { + chars = buff; + } + else { + chars = new char[len]; + int pos = 0; + for (char c : buff) { + if (c == '\t') { + Arrays.fill(chars, pos, pos + TAB_WIDTH, ' '); + pos += TAB_WIDTH; + } + else { + chars[pos] = c; + pos++; + } + } + } + + out.write(chars); + } + + private void print(final char c, final int num) throws IOException { + if (num == 1) { + print(c); + } + else { + char[] chars = new char[num]; + Arrays.fill(chars, c); + print(chars); + } + } + + /** + * Output the specified string to the output stream (but not the buffer). + */ + public final void print(final CharSequence s) throws IOException { + assert s != null; + print(s.toString().toCharArray()); + } + + public final void println(final CharSequence s) throws IOException { + assert s != null; + print(s.toString().toCharArray()); + println(); + } + + /** + * Output a platform-dependant newline. + */ + public final void println() throws IOException { + print(CR); +// flush(); + } + + // + // Actions + // + + /** + * Issue a delete. + * + * @return true if successful + */ + public final boolean delete() throws IOException { + return delete(1) == 1; + } + + // FIXME: delete(int) only used by above + the return is always 1 and num is ignored + + /** + * Issue num deletes. + * + * @return the number of characters backed up + */ + private int delete(final int num) throws IOException { + // TODO: Try to use jansi for this + + /* Commented out because of DWA-2949: + if (buf.cursor == 0) { + return 0; + } + */ + + buf.buffer.delete(buf.cursor, buf.cursor + 1); + drawBuffer(1); + + return 1; + } + + /** + * Kill the buffer ahead of the current cursor position. + * + * @return true if successful + */ + public boolean killLine() throws IOException { + int cp = buf.cursor; + int len = buf.buffer.length(); + + if (cp >= len) { + return false; + } + + int num = buf.buffer.length() - cp; + clearAhead(num, 0); + + for (int i = 0; i < num; i++) { + buf.buffer.deleteCharAt(len - i - 1); + } + + return true; + } + + /** + * Clear the screen by issuing the ANSI "clear screen" code. + */ + public boolean clearScreen() throws IOException { + if (!terminal.isAnsiSupported()) { + return false; + } + + // send the ANSI code to clear the screen + printAnsiSequence("2J"); + + // then send the ANSI code to go to position 1,1 + printAnsiSequence("1;1H"); + + redrawLine(); + + return true; + } + + /** + * Issue an audible keyboard bell, if {@link #isBellEnabled} return true. + */ + public void beep() throws IOException { + if (isBellEnabled()) { + print(KEYBOARD_BELL); + // need to flush so the console actually beeps + flush(); + } + } + + /** + * Paste the contents of the clipboard into the console buffer + * + * @return true if clipboard contents pasted + */ + public boolean paste() throws IOException { + Clipboard clipboard; + try { // May throw ugly exception on system without X + clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); + } + catch (Exception e) { + return false; + } + + if (clipboard == null) { + return false; + } + + Transferable transferable = clipboard.getContents(null); + + if (transferable == null) { + return false; + } + + try { + Object content = transferable.getTransferData(DataFlavor.plainTextFlavor); + + // This fix was suggested in bug #1060649 at + // http://sourceforge.net/tracker/index.php?func=detail&aid=1060649&group_id=64033&atid=506056 + // to get around the deprecated DataFlavor.plainTextFlavor, but it + // raises a UnsupportedFlavorException on Mac OS X + + if (content == null) { + try { + content = new DataFlavor().getReaderForText(transferable); + } + catch (Exception e) { + // ignore + } + } + + if (content == null) { + return false; + } + + String value; + + if (content instanceof Reader) { + // TODO: we might want instead connect to the input stream + // so we can interpret individual lines + value = ""; + String line; + + BufferedReader read = new BufferedReader((Reader) content); + while ((line = read.readLine()) != null) { + if (value.length() > 0) { + value += "\n"; + } + + value += line; + } + } + else { + value = content.toString(); + } + + if (value == null) { + return true; + } + + putString(value); + + return true; + } + catch (UnsupportedFlavorException e) { + Log.error("Paste failed: ", e); + + return false; + } + } + + // + // Triggered Actions + // + + private final Map triggeredActions = new HashMap(); + + /** + * Adding a triggered Action allows to give another curse of action if a character passed the pre-processing. + *

+ * Say you want to close the application if the user enter q. + * addTriggerAction('q', new ActionListener(){ System.exit(0); }); would do the trick. + */ + public void addTriggeredAction(final char c, final ActionListener listener) { + triggeredActions.put(c, listener); + } + + // + // Formatted Output + // + + /** + * Output the specified {@link Collection} in proper columns. + */ + public void printColumns(final Collection items) throws IOException { + if (items == null || items.isEmpty()) { + return; + } + + int width = getTerminal().getWidth(); + int height = getTerminal().getHeight(); + + int maxWidth = 0; + for (CharSequence item : items) { + maxWidth = Math.max(maxWidth, item.length()); + } + Log.debug("Max width: ", maxWidth); + + int showLines; + if (isPaginationEnabled()) { + showLines = height - 1; // page limit + } + else { + showLines = Integer.MAX_VALUE; + } + + StringBuilder buff = new StringBuilder(); + for (CharSequence item : items) { + if ((buff.length() + maxWidth) > width) { + println(buff); + buff.setLength(0); + + if (--showLines == 0) { + // Overflow + print(resources.getString("display-more")); + flush(); + int c = readVirtualKey(); + if (c == '\r' || c == '\n') { + // one step forward + showLines = 1; + } + else if (c != 'q') { + // page forward + showLines = height - 1; + } + + back(resources.getString("display-more").length()); + if (c == 'q') { + // cancel + break; + } + } + } + + // NOTE: toString() is important here due to AnsiString being retarded + buff.append(item.toString()); + for (int i = 0; i < (maxWidth + 3 - item.length()); i++) { + buff.append(' '); + } + } + + if (buff.length() > 0) { + println(buff); + } + } + + // + // Non-supported Terminal Support + // + + private Thread maskThread; + + private void beforeReadLine(final String prompt, final Character mask) { + if (mask != null && maskThread == null) { + final String fullPrompt = "\r" + prompt + + " " + + " " + + " " + + "\r" + prompt; + + maskThread = new Thread() + { + public void run() { + while (!interrupted()) { + try { + Writer out = getOutput(); + out.write(fullPrompt); + out.flush(); + sleep(3); + } + catch (IOException e) { + return; + } + catch (InterruptedException e) { + return; + } + } + } + }; + + maskThread.setPriority(Thread.MAX_PRIORITY); + maskThread.setDaemon(true); + maskThread.start(); + } + } + + private void afterReadLine() { + if (maskThread != null && maskThread.isAlive()) { + maskThread.interrupt(); + } + + maskThread = null; + } + + /** + * Erases the current line with the existing prompt, then redraws the line + * with the provided prompt and buffer + * @param prompt + * the new prompt + * @param buffer + * the buffer to be drawn + * @param cursorDest + * where you want the cursor set when the line has been drawn. + * -1 for end of line. + * */ + public void resetPromptLine(String prompt, String buffer, int cursorDest) throws IOException { + // move cursor to end of line + moveToEnd(); + + // backspace all text, including prompt + buf.buffer.append(this.prompt); + buf.cursor += this.prompt.length(); + this.prompt = ""; + backspaceAll(); + + this.prompt = prompt; + redrawLine(); + setBuffer(buffer); + + // move cursor to destination (-1 will move to end of line) + if (cursorDest < 0) cursorDest = buffer.length(); + setCursorPosition(cursorDest); + + flush(); + } + + public void printSearchStatus(String searchTerm, String match) throws IOException { + String prompt = "(reverse-i-search)`" + searchTerm + "': "; + String buffer = match; + int cursorDest = match.indexOf(searchTerm); + resetPromptLine(prompt, buffer, cursorDest); + } + + public void restoreLine(String originalPrompt, int cursorDest) throws IOException { + // TODO move cursor to matched string + String prompt = lastLine(originalPrompt); + String buffer = buf.buffer.toString(); + resetPromptLine(prompt, buffer, cursorDest); + } + + // + // History search + // + /** + * Search backward in history from a given position. + * + * @param searchTerm substring to search for. + * @param startIndex the index from which on to search + * @return index where this substring has been found, or -1 else. + */ + public int searchBackwards(String searchTerm, int startIndex) { + return searchBackwards(searchTerm, startIndex, false); + } + + /** + * Search backwards in history from the current position. + * + * @param searchTerm substring to search for. + * @return index where the substring has been found, or -1 else. + */ + public int searchBackwards(String searchTerm) { + return searchBackwards(searchTerm, history.index()); + } + + + public int searchBackwards(String searchTerm, int startIndex, boolean startsWith) { + ListIterator it = history.entries(startIndex); + while (it.hasPrevious()) { + History.Entry e = it.previous(); + if (startsWith) { + if (e.value().toString().startsWith(searchTerm)) { + return e.index(); + } + } else { + if (e.value().toString().contains(searchTerm)) { + return e.index(); + } + } + } + return -1; + } + + // + // Helpers + // + + /** + * Checks to see if the specified character is a delimiter. We consider a + * character a delimiter if it is anything but a letter or digit. + * + * @param c The character to test + * @return True if it is a delimiter + */ + private boolean isDelimiter(final char c) { + return !Character.isLetterOrDigit(c); + } + + private void printAnsiSequence(String sequence) throws IOException { + print(27); + print('['); + print(sequence); + flush(); // helps with step debugging + } + + // return column position, reported by the terminal + private int getCurrentPosition() { + // check for ByteArrayInputStream to disable for unit tests + if (terminal.isAnsiSupported() && !(in instanceof ByteArrayInputStream)) { + try { + printAnsiSequence("6n"); + flush(); + StringBuffer b = new StringBuffer(8); + // position is sent as [{ROW};{COLUMN}R + int r; + while((r = in.read()) > -1 && r != 'R') { + if (r != 27 && r != '[') { + b.append((char) r); + } + } + String[] pos = b.toString().split(";"); + return Integer.parseInt(pos[1]); + } catch (Exception x) { + // no luck + } + } + + return -1; // TODO: throw exception instead? + } + + // return row position, reported by the terminal + // needed to know whether to scroll up on cursor move in last col for weird + // wrapping terminals - not tested for anything else + private int getCurrentAnsiRow() { + // check for ByteArrayInputStream to disable for unit tests + if (terminal.isAnsiSupported() && !(in instanceof ByteArrayInputStream)) { + try { + printAnsiSequence("6n"); + flush(); + StringBuffer b = new StringBuffer(8); + // position is sent as [{ROW};{COLUMN}R + int r; + while((r = in.read()) > -1 && r != 'R') { + if (r != 27 && r != '[') { + b.append((char) r); + } + } + String[] pos = b.toString().split(";"); + return Integer.parseInt(pos[0]); + } catch (Exception x) { + // no luck + } + } + + return -1; // TODO: throw exception instead? + } +} diff --git a/src/jline/src/main/java/scala/tools/jline/console/CursorBuffer.java b/src/jline/src/main/java/scala/tools/jline/console/CursorBuffer.java new file mode 100644 index 0000000000..d9cdbec124 --- /dev/null +++ b/src/jline/src/main/java/scala/tools/jline/console/CursorBuffer.java @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. + * + * This software is distributable under the BSD license. See the terms of the + * BSD license in the documentation provided with this software. + */ + +package scala.tools.jline.console; + +/** + * A holder for a {@link StringBuilder} that also contains the current cursor position. + * + * @author Marc Prud'hommeaux + * @author Jason Dillon + * @since 2.0 + */ +public class CursorBuffer +{ + private boolean overTyping = false; + + public int cursor = 0; + + public final StringBuilder buffer = new StringBuilder(); + + public boolean isOverTyping() { + return overTyping; + } + + public void setOverTyping(final boolean b) { + overTyping = b; + } + + public int length() { + return buffer.length(); + } + + public char current() { + if (cursor <= 0) { + return 0; + } + + return buffer.charAt(cursor - 1); + } + + /** + * Write the specific character into the buffer, setting the cursor position + * ahead one. The text may overwrite or insert based on the current setting + * of {@link #isOverTyping}. + * + * @param c the character to insert + */ + public void write(final char c) { + buffer.insert(cursor++, c); + if (isOverTyping() && cursor < buffer.length()) { + buffer.deleteCharAt(cursor); + } + } + + /** + * Insert the specified chars into the buffer, setting the cursor to the end of the insertion point. + */ + public void write(final CharSequence str) { + assert str != null; + + if (buffer.length() == 0) { + buffer.append(str); + } + else { + buffer.insert(cursor, str); + } + + cursor += str.length(); + + if (isOverTyping() && cursor < buffer.length()) { + buffer.delete(cursor, (cursor + str.length())); + } + } + + public boolean clear() { + if (buffer.length() == 0) { + return false; + } + + buffer.delete(0, buffer.length()); + cursor = 0; + return true; + } + + @Override + public String toString() { + return buffer.toString(); + } +} diff --git a/src/jline/src/main/java/scala/tools/jline/console/Key.java b/src/jline/src/main/java/scala/tools/jline/console/Key.java new file mode 100644 index 0000000000..5c13d19860 --- /dev/null +++ b/src/jline/src/main/java/scala/tools/jline/console/Key.java @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. + * + * This software is distributable under the BSD license. See the terms of the + * BSD license in the documentation provided with this software. + */ + +package scala.tools.jline.console; + +import java.util.HashMap; +import java.util.Map; + +/** + * Map from key name to key codes. + * + * @author Marc Prud'hommeaux + * @author Jason Dillon + * @see java.awt.event.KeyEvent + * @since 2.0 + */ +public enum Key +{ + CTRL_A(1), + + CTRL_B(2), + + CTRL_C(3), + + CTRL_D(4), + + CTRL_E(5), + + CTRL_F(6), + + CTRL_G(7), + + CTRL_K(11), + + CTRL_L(12), + + CTRL_N(14), + + CTRL_P(16), + + CTRL_OB(27), + + CTRL_QM(127), + + BACKSPACE('\b'), + + DELETE(127),; + + public final short code; + + Key(final int code) { + this.code = (short) code; + } + + private static final Map codes; + + static { + Map map = new HashMap(); + + for (Key op : Key.values()) { + map.put(op.code, op); + } + + codes = map; + } + + public static Key valueOf(final int code) { + return codes.get((short) code); + } +} \ No newline at end of file diff --git a/src/jline/src/main/java/scala/tools/jline/console/Operation.java b/src/jline/src/main/java/scala/tools/jline/console/Operation.java new file mode 100644 index 0000000000..17b216be57 --- /dev/null +++ b/src/jline/src/main/java/scala/tools/jline/console/Operation.java @@ -0,0 +1,285 @@ +/* + * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. + * + * This software is distributable under the BSD license. See the terms of the + * BSD license in the documentation provided with this software. + */ + +package scala.tools.jline.console; + +import java.util.HashMap; +import java.util.Map; + +/** + * Map for console operation to virtual key bindings. + * + * @author Marc Prud'hommeaux + * @author Jason Dillon + * @see java.awt.event.KeyEvent + * @since 2.0 + */ +public enum Operation +{ + /** + * Unknown operation. + */ + UNKNOWN(-99), + + /** + * Operation that moves to the beginning of the buffer. + */ + MOVE_TO_BEG(-1), + + /** + * Operation that moves to the end of the buffer. + */ + MOVE_TO_END(-3), + + /** + * Operation that moved to the previous character in the buffer. + */ + PREV_CHAR(-4), + + /** + * Operation that issues a newline. + */ + NEWLINE(-6), + + /** + * Operation that deletes the buffer from the current character to the end. + */ + KILL_LINE(-7), + + /** + * Operation that clears the screen. + */ + CLEAR_SCREEN(-8), + + /** + * Operation that sets the buffer to the next history item. + */ + NEXT_HISTORY(-9), + + /** + * Operation that sets the buffer to the previous history item. + */ + PREV_HISTORY(-11), + + /** + * Operation that redisplays the current buffer. + */ + REDISPLAY(-13), + + /** + * Operation that deletes the buffer from the cursor to the beginning. + */ + KILL_LINE_PREV(-15), + + /** + * Operation that deletes the previous word in the buffer. + */ + DELETE_PREV_WORD(-16), + + /** + * Operation that moves to the next character in the buffer. + */ + NEXT_CHAR(-19), + + /** + * Operation that moves to the previous character in the buffer. + */ + REPEAT_PREV_CHAR(-20), + + /** + * Operation that searches backwards in the command history. + */ + SEARCH_PREV(-21), + + /** + * Operation that repeats the character. + */ + REPEAT_NEXT_CHAR(-24), + + /** + * Operation that searches forward in the command history. + */ + SEARCH_NEXT(-25), + + /** + * Operation that moved to the previous whitespace. + */ + PREV_SPACE_WORD(-27), + + /** + * Operation that moved to the end of the current word. + */ + TO_END_WORD(-29), + + /** + * Operation that + */ + REPEAT_SEARCH_PREV(-34), + + /** + * Operation that + */ + PASTE_PREV(-36), + + /** + * Operation that + */ + REPLACE_MODE(-37), + + /** + * Operation that + */ + SUBSTITUTE_LINE(-38), + + /** + * Operation that + */ + TO_PREV_CHAR(-39), + + /** + * Operation that + */ + NEXT_SPACE_WORD(-40), + + /** + * Operation that + */ + DELETE_PREV_CHAR(-41), + + /** + * Operation that + */ + ADD(-42), + + /** + * Operation that + */ + PREV_WORD(-43), + + /** + * Operation that + */ + CHANGE_META(-44), + + /** + * Operation that + */ + DELETE_META(-45), + + /** + * Operation that + */ + END_WORD(-46), + + /** + * Operation that toggles insert/overtype + */ + INSERT(-48), + + /** + * Operation that + */ + REPEAT_SEARCH_NEXT(-49), + + /** + * Operation that + */ + PASTE_NEXT(-50), + + /** + * Operation that + */ + REPLACE_CHAR(-51), + + /** + * Operation that + */ + SUBSTITUTE_CHAR(-52), + + /** + * Operation that + */ + TO_NEXT_CHAR(-53), + + /** + * Operation that undoes the previous operation. + */ + UNDO(-54), + + /** + * Operation that moved to the next word. + */ + NEXT_WORD(-55), + + /** + * Operation that deletes the previous character. + */ + DELETE_NEXT_CHAR(-56), + + /** + * Operation that toggles between uppercase and lowercase. + */ + CHANGE_CASE(-57), + + /** + * Operation that performs completion operation on the current word. + */ + COMPLETE(-58), + + /** + * Operation that exits the command prompt. + */ + EXIT(-59), + + /** + * Operation that pastes the contents of the clipboard into the line + */ + PASTE(-60), + + /** + * Operation that moves the current History to the beginning. + */ + START_OF_HISTORY(-61), + + /** + * Operation that moves the current History to the end. + */ + END_OF_HISTORY(-62), + + /** + * Operation that clears whatever text is on the current line. + */ + CLEAR_LINE(-63), + + /** + * Cancel search + */ + ABORT(-64), + ; + + public final short code; + + Operation(final int code) { + this.code = (short) code; + } + + private static final Map codes; + + static { + Map map = new HashMap(); + + for (Operation op : Operation.values()) { + map.put(op.code, op); + } + + codes = map; + } + + public static Operation valueOf(final int code) { + return codes.get((short) code); + } +} \ No newline at end of file diff --git a/src/jline/src/main/java/scala/tools/jline/console/completer/AggregateCompleter.java b/src/jline/src/main/java/scala/tools/jline/console/completer/AggregateCompleter.java new file mode 100644 index 0000000000..3170bd1c68 --- /dev/null +++ b/src/jline/src/main/java/scala/tools/jline/console/completer/AggregateCompleter.java @@ -0,0 +1,108 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package scala.tools.jline.console.completer; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; + +/** + * Completer which contains multiple completers and aggregates them together. + * + * @author Jason Dillon + * @since 2.3 + */ +public class AggregateCompleter + implements Completer +{ + private final List completers = new ArrayList(); + + public AggregateCompleter() { + // empty + } + + public AggregateCompleter(final Collection completers) { + assert completers != null; + this.completers.addAll(completers); + } + + public AggregateCompleter(final Completer... completers) { + this(Arrays.asList(completers)); + } + + public Collection getCompleters() { + return completers; + } + + public int complete(final String buffer, final int cursor, final List candidates) { + // buffer could be null + assert candidates != null; + + List completions = new ArrayList(completers.size()); + + // Run each completer, saving its completion results + int max = -1; + for (Completer completer : completers) { + Completion completion = new Completion(candidates); + completion.complete(completer, buffer, cursor); + + // Compute the max cursor position + max = Math.max(max, completion.cursor); + + completions.add(completion); + } + + // Append candidates from completions which have the same cursor position as max + for (Completion completion : completions) { + if (completion.cursor == max) { + candidates.addAll(completion.candidates); + } + } + + return max; + } + + @Override + public String toString() { + return getClass().getSimpleName() + "{" + + "completers=" + completers + + '}'; + } + + private class Completion + { + public final List candidates; + + public int cursor; + + public Completion(final List candidates) { + assert candidates != null; + this.candidates = new LinkedList(candidates); + } + + public void complete(final Completer completer, final String buffer, final int cursor) { + assert completer != null; + + this.cursor = completer.complete(buffer, cursor, candidates); + } + } +} diff --git a/src/jline/src/main/java/scala/tools/jline/console/completer/ArgumentCompleter.java b/src/jline/src/main/java/scala/tools/jline/console/completer/ArgumentCompleter.java new file mode 100644 index 0000000000..6f60029a1d --- /dev/null +++ b/src/jline/src/main/java/scala/tools/jline/console/completer/ArgumentCompleter.java @@ -0,0 +1,398 @@ +/* + * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. + * + * This software is distributable under the BSD license. See the terms of the + * BSD license in the documentation provided with this software. + */ + +package scala.tools.jline.console.completer; + +import scala.tools.jline.internal.Log; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; + +/** + * A {@link Completer} implementation that invokes a child completer using the appropriate separator argument. + * This can be used instead of the individual completers having to know about argument parsing semantics. + * + * @author Marc Prud'hommeaux + * @author Jason Dillon + * @since 2.3 + */ +public class ArgumentCompleter + implements Completer +{ + private final ArgumentDelimiter delimiter; + + private final List completers = new ArrayList(); + + private boolean strict = true; + + /** + * Create a new completer with the specified argument delimiter. + * + * @param delimiter The delimiter for parsing arguments + * @param completers The embedded completers + */ + public ArgumentCompleter(final ArgumentDelimiter delimiter, final Collection completers) { + assert delimiter != null; + this.delimiter = delimiter; + assert completers != null; + this.completers.addAll(completers); + } + + /** + * Create a new completer with the specified argument delimiter. + * + * @param delimiter The delimiter for parsing arguments + * @param completers The embedded completers + */ + public ArgumentCompleter(final ArgumentDelimiter delimiter, final Completer... completers) { + this(delimiter, Arrays.asList(completers)); + } + + /** + * Create a new completer with the default {@link WhitespaceArgumentDelimiter}. + * + * @param completers The embedded completers + */ + public ArgumentCompleter(final Completer... completers) { + this(new WhitespaceArgumentDelimiter(), completers); + } + + /** + * Create a new completer with the default {@link WhitespaceArgumentDelimiter}. + * + * @param completers The embedded completers + */ + public ArgumentCompleter(final List completers) { + this(new WhitespaceArgumentDelimiter(), completers); + } + + /** + * If true, a completion at argument index N will only succeed + * if all the completions from 0-(N-1) also succeed. + */ + public void setStrict(final boolean strict) { + this.strict = strict; + } + + /** + * Returns whether a completion at argument index N will success + * if all the completions from arguments 0-(N-1) also succeed. + * + * @return True if strict. + * @since 2.3 + */ + public boolean isStrict() { + return this.strict; + } + + /** + * @since 2.3 + */ + public ArgumentDelimiter getDelimiter() { + return delimiter; + } + + /** + * @since 2.3 + */ + public List getCompleters() { + return completers; + } + + public int complete(final String buffer, final int cursor, final List candidates) { + // buffer can be null + assert candidates != null; + + ArgumentDelimiter delim = getDelimiter(); + ArgumentList list = delim.delimit(buffer, cursor); + int argpos = list.getArgumentPosition(); + int argIndex = list.getCursorArgumentIndex(); + + if (argIndex < 0) { + return -1; + } + + List completers = getCompleters(); + Completer completer; + + // if we are beyond the end of the completers, just use the last one + if (argIndex >= completers.size()) { + completer = completers.get(completers.size() - 1); + } + else { + completer = completers.get(argIndex); + } + + // ensure that all the previous completers are successful before allowing this completer to pass (only if strict). + for (int i = 0; isStrict() && (i < argIndex); i++) { + Completer sub = completers.get(i >= completers.size() ? (completers.size() - 1) : i); + String[] args = list.getArguments(); + String arg = (args == null || i >= args.length) ? "" : args[i]; + + List subCandidates = new LinkedList(); + + if (sub.complete(arg, arg.length(), subCandidates) == -1) { + return -1; + } + + if (subCandidates.size() == 0) { + return -1; + } + } + + int ret = completer.complete(list.getCursorArgument(), argpos, candidates); + + if (ret == -1) { + return -1; + } + + int pos = ret + list.getBufferPosition() - argpos; + + // Special case: when completing in the middle of a line, and the area under the cursor is a delimiter, + // then trim any delimiters from the candidates, since we do not need to have an extra delimiter. + // + // E.g., if we have a completion for "foo", and we enter "f bar" into the buffer, and move to after the "f" + // and hit TAB, we want "foo bar" instead of "foo bar". + + if ((cursor != buffer.length()) && delim.isDelimiter(buffer, cursor)) { + for (int i = 0; i < candidates.size(); i++) { + CharSequence val = candidates.get(i); + + while (val.length() > 0 && delim.isDelimiter(val, val.length() - 1)) { + val = val.subSequence(0, val.length() - 1); + } + + candidates.set(i, val); + } + } + + Log.trace("Completing ", buffer, " (pos=", cursor, ") with: ", candidates, ": offset=", pos); + + return pos; + } + + /** + * The {@link ArgumentCompleter.ArgumentDelimiter} allows custom breaking up of a {@link String} into individual + * arguments in order to dispatch the arguments to the nested {@link Completer}. + * + * @author Marc Prud'hommeaux + */ + public static interface ArgumentDelimiter + { + /** + * Break the specified buffer into individual tokens that can be completed on their own. + * + * @param buffer The buffer to split + * @param pos The current position of the cursor in the buffer + * @return The tokens + */ + ArgumentList delimit(CharSequence buffer, int pos); + + /** + * Returns true if the specified character is a whitespace parameter. + * + * @param buffer The complete command buffer + * @param pos The index of the character in the buffer + * @return True if the character should be a delimiter + */ + boolean isDelimiter(CharSequence buffer, int pos); + } + + /** + * Abstract implementation of a delimiter that uses the {@link #isDelimiter} method to determine if a particular + * character should be used as a delimiter. + * + * @author Marc Prud'hommeaux + */ + public abstract static class AbstractArgumentDelimiter + implements ArgumentDelimiter + { + // TODO: handle argument quoting and escape characters + + private char[] quoteChars = {'\'', '"'}; + + private char[] escapeChars = {'\\'}; + + public void setQuoteChars(final char[] chars) { + this.quoteChars = chars; + } + + public char[] getQuoteChars() { + return this.quoteChars; + } + + public void setEscapeChars(final char[] chars) { + this.escapeChars = chars; + } + + public char[] getEscapeChars() { + return this.escapeChars; + } + + public ArgumentList delimit(final CharSequence buffer, final int cursor) { + List args = new LinkedList(); + StringBuilder arg = new StringBuilder(); + int argpos = -1; + int bindex = -1; + + for (int i = 0; (buffer != null) && (i <= buffer.length()); i++) { + // once we reach the cursor, set the + // position of the selected index + if (i == cursor) { + bindex = args.size(); + // the position in the current argument is just the + // length of the current argument + argpos = arg.length(); + } + + if ((i == buffer.length()) || isDelimiter(buffer, i)) { + if (arg.length() > 0) { + args.add(arg.toString()); + arg.setLength(0); // reset the arg + } + } + else { + arg.append(buffer.charAt(i)); + } + } + + return new ArgumentList(args.toArray(new String[args.size()]), bindex, argpos, cursor); + } + + /** + * Returns true if the specified character is a whitespace parameter. Check to ensure that the character is not + * escaped by any of {@link #getQuoteChars}, and is not escaped by ant of the {@link #getEscapeChars}, and + * returns true from {@link #isDelimiterChar}. + * + * @param buffer The complete command buffer + * @param pos The index of the character in the buffer + * @return True if the character should be a delimiter + */ + public boolean isDelimiter(final CharSequence buffer, final int pos) { + return !isQuoted(buffer, pos) && !isEscaped(buffer, pos) && isDelimiterChar(buffer, pos); + } + + public boolean isQuoted(final CharSequence buffer, final int pos) { + return false; + } + + public boolean isEscaped(final CharSequence buffer, final int pos) { + if (pos <= 0) { + return false; + } + + for (int i = 0; (escapeChars != null) && (i < escapeChars.length); + i++) { + if (buffer.charAt(pos) == escapeChars[i]) { + return !isEscaped(buffer, pos - 1); // escape escape + } + } + + return false; + } + + /** + * Returns true if the character at the specified position if a delimiter. This method will only be called if + * the character is not enclosed in any of the {@link #getQuoteChars}, and is not escaped by ant of the + * {@link #getEscapeChars}. To perform escaping manually, override {@link #isDelimiter} instead. + */ + public abstract boolean isDelimiterChar(CharSequence buffer, int pos); + } + + /** + * {@link ArgumentCompleter.ArgumentDelimiter} implementation that counts all whitespace (as reported by + * {@link Character#isWhitespace}) as being a delimiter. + * + * @author Marc Prud'hommeaux + */ + public static class WhitespaceArgumentDelimiter + extends AbstractArgumentDelimiter + { + /** + * The character is a delimiter if it is whitespace, and the + * preceding character is not an escape character. + */ + @Override + public boolean isDelimiterChar(final CharSequence buffer, final int pos) { + return Character.isWhitespace(buffer.charAt(pos)); + } + } + + /** + * The result of a delimited buffer. + * + * @author Marc Prud'hommeaux + */ + public static class ArgumentList + { + private String[] arguments; + + private int cursorArgumentIndex; + + private int argumentPosition; + + private int bufferPosition; + + /** + * @param arguments The array of tokens + * @param cursorArgumentIndex The token index of the cursor + * @param argumentPosition The position of the cursor in the current token + * @param bufferPosition The position of the cursor in the whole buffer + */ + public ArgumentList(final String[] arguments, final int cursorArgumentIndex, final int argumentPosition, final int bufferPosition) { + assert arguments != null; + + this.arguments = arguments; + this.cursorArgumentIndex = cursorArgumentIndex; + this.argumentPosition = argumentPosition; + this.bufferPosition = bufferPosition; + } + + public void setCursorArgumentIndex(final int i) { + this.cursorArgumentIndex = i; + } + + public int getCursorArgumentIndex() { + return this.cursorArgumentIndex; + } + + public String getCursorArgument() { + if ((cursorArgumentIndex < 0) || (cursorArgumentIndex >= arguments.length)) { + return null; + } + + return arguments[cursorArgumentIndex]; + } + + public void setArgumentPosition(final int pos) { + this.argumentPosition = pos; + } + + public int getArgumentPosition() { + return this.argumentPosition; + } + + public void setArguments(final String[] arguments) { + this.arguments = arguments; + } + + public String[] getArguments() { + return this.arguments; + } + + public void setBufferPosition(final int pos) { + this.bufferPosition = pos; + } + + public int getBufferPosition() { + return this.bufferPosition; + } + } +} diff --git a/src/jline/src/main/java/scala/tools/jline/console/completer/CandidateListCompletionHandler.java b/src/jline/src/main/java/scala/tools/jline/console/completer/CandidateListCompletionHandler.java new file mode 100644 index 0000000000..002f36b52e --- /dev/null +++ b/src/jline/src/main/java/scala/tools/jline/console/completer/CandidateListCompletionHandler.java @@ -0,0 +1,192 @@ +/* + * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. + * + * This software is distributable under the BSD license. See the terms of the + * BSD license in the documentation provided with this software. + */ + +package scala.tools.jline.console.completer; + +import scala.tools.jline.console.ConsoleReader; +import scala.tools.jline.console.CursorBuffer; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Locale; +import java.util.ResourceBundle; +import java.util.Set; + +/** + * A {@link CompletionHandler} that deals with multiple distinct completions + * by outputting the complete list of possibilities to the console. This + * mimics the behavior of the + * readline library. + * + * @author Marc Prud'hommeaux + * @author Jason Dillon + * @since 2.3 + */ +public class CandidateListCompletionHandler + implements CompletionHandler +{ + // TODO: handle quotes and escaped quotes && enable automatic escaping of whitespace + + public boolean complete(final ConsoleReader reader, final List candidates, final int pos) throws + IOException + { + CursorBuffer buf = reader.getCursorBuffer(); + + // if there is only one completion, then fill in the buffer + if (candidates.size() == 1) { + CharSequence value = candidates.get(0); + + // fail if the only candidate is the same as the current buffer + if (value.equals(buf.toString())) { + return false; + } + + setBuffer(reader, value, pos); + + return true; + } + else if (candidates.size() > 1) { + String value = getUnambiguousCompletions(candidates); + setBuffer(reader, value, pos); + } + + printCandidates(reader, candidates); + + // redraw the current console buffer + reader.drawLine(); + + return true; + } + + public static void setBuffer(final ConsoleReader reader, final CharSequence value, final int offset) throws + IOException + { + while ((reader.getCursorBuffer().cursor > offset) && reader.backspace()) { + // empty + } + + reader.putString(value); + reader.setCursorPosition(offset + value.length()); + } + + /** + * Print out the candidates. If the size of the candidates is greater than the + * {@link ConsoleReader#getAutoprintThreshold}, they prompt with a warning. + * + * @param candidates the list of candidates to print + */ + public static void printCandidates(final ConsoleReader reader, Collection candidates) throws + IOException + { + Set distinct = new HashSet(candidates); + + if (distinct.size() > reader.getAutoprintThreshold()) { + //noinspection StringConcatenation + reader.print(Messages.DISPLAY_CANDIDATES.format(candidates.size())); + reader.flush(); + + int c; + + String noOpt = Messages.DISPLAY_CANDIDATES_NO.format(); + String yesOpt = Messages.DISPLAY_CANDIDATES_YES.format(); + char[] allowed = {yesOpt.charAt(0), noOpt.charAt(0)}; + + while ((c = reader.readCharacter(allowed)) != -1) { + String tmp = new String(new char[]{(char) c}); + + if (noOpt.startsWith(tmp)) { + reader.println(); + return; + } + else if (yesOpt.startsWith(tmp)) { + break; + } + else { + reader.beep(); + } + } + } + + // copy the values and make them distinct, without otherwise affecting the ordering. Only do it if the sizes differ. + if (distinct.size() != candidates.size()) { + Collection copy = new ArrayList(); + + for (CharSequence next : candidates) { + if (!copy.contains(next)) { + copy.add(next); + } + } + + candidates = copy; + } + + reader.println(); + reader.printColumns(candidates); + } + + /** + * Returns a root that matches all the {@link String} elements of the specified {@link List}, + * or null if there are no commonalities. For example, if the list contains + * foobar, foobaz, foobuz, the method will return foob. + */ + private String getUnambiguousCompletions(final List candidates) { + if (candidates == null || candidates.isEmpty()) { + return null; + } + + // convert to an array for speed + String[] strings = candidates.toArray(new String[candidates.size()]); + + String first = strings[0]; + StringBuilder candidate = new StringBuilder(); + + for (int i = 0; i < first.length(); i++) { + if (startsWith(first.substring(0, i + 1), strings)) { + candidate.append(first.charAt(i)); + } + else { + break; + } + } + + return candidate.toString(); + } + + /** + * @return true is all the elements of candidates start with starts + */ + private boolean startsWith(final String starts, final String[] candidates) { + for (String candidate : candidates) { + if (!candidate.startsWith(starts)) { + return false; + } + } + + return true; + } + + private static enum Messages + { + DISPLAY_CANDIDATES, + DISPLAY_CANDIDATES_YES, + DISPLAY_CANDIDATES_NO,; + + private static final + ResourceBundle + bundle = + ResourceBundle.getBundle(CandidateListCompletionHandler.class.getName(), + Locale.getDefault(), + CandidateListCompletionHandler.class.getClassLoader()); + + public String format(final Object... args) { + return String.format(bundle.getString(name()), args); + } + } +} diff --git a/src/jline/src/main/java/scala/tools/jline/console/completer/Completer.java b/src/jline/src/main/java/scala/tools/jline/console/completer/Completer.java new file mode 100644 index 0000000000..52d33847f2 --- /dev/null +++ b/src/jline/src/main/java/scala/tools/jline/console/completer/Completer.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. + * + * This software is distributable under the BSD license. See the terms of the + * BSD license in the documentation provided with this software. + */ + +package scala.tools.jline.console.completer; + +import java.util.List; + +/** + * A completer is the mechanism by which tab-completion candidates will be resolved. + * + * @author Marc Prud'hommeaux + * @author Jason Dillon + * @since 2.3 + */ +public interface Completer +{ + // + // FIXME: Check if we can use CharSequece for buffer? + // + + /** + * Populates candidates with a list of possible completions for the buffer. + * + * The candidates list will not be sorted before being displayed to the user: thus, the + * complete method should sort the {@link List} before returning. + * + * @param buffer The buffer + * @param cursor The current position of the cursor in the buffer + * @param candidates The {@link List} of candidates to populate + * @return The index of the buffer for which the completion will be relative + */ + int complete(String buffer, int cursor, List candidates); +} diff --git a/src/jline/src/main/java/scala/tools/jline/console/completer/CompletionHandler.java b/src/jline/src/main/java/scala/tools/jline/console/completer/CompletionHandler.java new file mode 100644 index 0000000000..030dc84205 --- /dev/null +++ b/src/jline/src/main/java/scala/tools/jline/console/completer/CompletionHandler.java @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. + * + * This software is distributable under the BSD license. See the terms of the + * BSD license in the documentation provided with this software. + */ + +package scala.tools.jline.console.completer; + +import scala.tools.jline.console.ConsoleReader; + +import java.io.IOException; +import java.util.List; + +/** + * Handler for dealing with candidates for tab-completion. + * + * @author Marc Prud'hommeaux + * @author Jason Dillon + * @since 2.3 + */ +public interface CompletionHandler +{ + boolean complete(ConsoleReader reader, List candidates, int position) throws IOException; +} diff --git a/src/jline/src/main/java/scala/tools/jline/console/completer/EnumCompleter.java b/src/jline/src/main/java/scala/tools/jline/console/completer/EnumCompleter.java new file mode 100644 index 0000000000..5ad049b857 --- /dev/null +++ b/src/jline/src/main/java/scala/tools/jline/console/completer/EnumCompleter.java @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2009 the original author(s). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package scala.tools.jline.console.completer; + +/** + * {@link Completer} for {@link Enum} names. + * + * @author Jason Dillon + * @since 2.3 + */ +public class EnumCompleter + extends StringsCompleter +{ + public EnumCompleter(Class source) { + assert source != null; + + for (Enum n : source.getEnumConstants()) { + this.getStrings().add(n.name().toLowerCase()); + } + } +} \ No newline at end of file diff --git a/src/jline/src/main/java/scala/tools/jline/console/completer/FileNameCompleter.java b/src/jline/src/main/java/scala/tools/jline/console/completer/FileNameCompleter.java new file mode 100644 index 0000000000..6556138769 --- /dev/null +++ b/src/jline/src/main/java/scala/tools/jline/console/completer/FileNameCompleter.java @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. + * + * This software is distributable under the BSD license. See the terms of the + * BSD license in the documentation provided with this software. + */ + +package scala.tools.jline.console.completer; + +import scala.tools.jline.internal.Configuration; + +import java.io.File; +import java.util.List; + +/** + * A file name completer takes the buffer and issues a list of + * potential completions. + *

+ * This completer tries to behave as similar as possible to + * bash's file name completion (using GNU readline) + * with the following exceptions: + *

+ *

    + *
  • Candidates that are directories will end with "/"
  • + *
  • Wildcard regular expressions are not evaluated or replaced
  • + *
  • The "~" character can be used to represent the user's home, + * but it cannot complete to other users' homes, since java does + * not provide any way of determining that easily
  • + *
+ * + * @author Marc Prud'hommeaux + * @author Jason Dillon + * @since 2.3 + */ +public class FileNameCompleter + implements Completer +{ + // TODO: Handle files with spaces in them + + private static final boolean OS_IS_WINDOWS; + + static { + String os = Configuration.getOsName(); + OS_IS_WINDOWS = os.contains("windows"); + } + + public int complete(String buffer, final int cursor, final List candidates) { + // buffer can be null + assert candidates != null; + + if (buffer == null) { + buffer = ""; + } + + if (OS_IS_WINDOWS) { + buffer = buffer.replace('/', '\\'); + } + + String translated = buffer; + + File homeDir = getUserHome(); + + // Special character: ~ maps to the user's home directory + if (translated.startsWith("~" + separator())) { + translated = homeDir.getPath() + translated.substring(1); + } + else if (translated.startsWith("~")) { + translated = homeDir.getParentFile().getAbsolutePath(); + } + else if (!(translated.startsWith(separator()))) { + String cwd = getUserDir().getAbsolutePath(); + translated = cwd + separator() + translated; + } + + File file = new File(translated); + final File dir; + + if (translated.endsWith(separator())) { + dir = file; + } + else { + dir = file.getParentFile(); + } + + File[] entries = dir == null ? new File[0] : dir.listFiles(); + + return matchFiles(buffer, translated, entries, candidates); + } + + protected String separator() { + return File.separator; + } + + protected File getUserHome() { + return Configuration.getUserHome(); + } + + protected File getUserDir() { + return new File("."); + } + + protected int matchFiles(final String buffer, final String translated, final File[] files, final List candidates) { + if (files == null) { + return -1; + } + + int matches = 0; + + // first pass: just count the matches + for (File file : files) { + if (file.getAbsolutePath().startsWith(translated)) { + matches++; + } + } + for (File file : files) { + if (file.getAbsolutePath().startsWith(translated)) { + CharSequence name = file.getName() + (matches == 1 && file.isDirectory() ? separator() : " "); + candidates.add(render(file, name).toString()); + } + } + + final int index = buffer.lastIndexOf(separator()); + + return index + separator().length(); + } + + protected CharSequence render(final File file, final CharSequence name) { + assert file != null; + assert name != null; + + return name; + } +} diff --git a/src/jline/src/main/java/scala/tools/jline/console/completer/NullCompleter.java b/src/jline/src/main/java/scala/tools/jline/console/completer/NullCompleter.java new file mode 100644 index 0000000000..93cf563bcd --- /dev/null +++ b/src/jline/src/main/java/scala/tools/jline/console/completer/NullCompleter.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package scala.tools.jline.console.completer; + +import java.util.List; + +/** + * Null completer. + * + * @author Marc Prud'hommeaux + * @author Jason Dillon + * @since 2.3 + */ +public final class NullCompleter + implements Completer +{ + public static final NullCompleter INSTANCE = new NullCompleter(); + + public int complete(final String buffer, final int cursor, final List candidates) { + return -1; + } +} \ No newline at end of file diff --git a/src/jline/src/main/java/scala/tools/jline/console/completer/StringsCompleter.java b/src/jline/src/main/java/scala/tools/jline/console/completer/StringsCompleter.java new file mode 100644 index 0000000000..2abfdd0340 --- /dev/null +++ b/src/jline/src/main/java/scala/tools/jline/console/completer/StringsCompleter.java @@ -0,0 +1,79 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package scala.tools.jline.console.completer; + +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.SortedSet; +import java.util.TreeSet; + +/** + * Completer for a set of strings. + * + * @author Jason Dillon + * @since 2.3 + */ +public class StringsCompleter + implements Completer +{ + private final SortedSet strings = new TreeSet(); + + public StringsCompleter() { + // empty + } + + public StringsCompleter(final Collection strings) { + assert strings != null; + getStrings().addAll(strings); + } + + public StringsCompleter(final String... strings) { + this(Arrays.asList(strings)); + } + + public Collection getStrings() { + return strings; + } + + public int complete(final String buffer, final int cursor, final List candidates) { + // buffer could be null + assert candidates != null; + + if (buffer == null) { + candidates.addAll(strings); + } + else { + for (String match : strings.tailSet(buffer)) { + if (!match.startsWith(buffer)) { + break; + } + + candidates.add(match); + } + } + + if (candidates.size() == 1) { + candidates.set(0, candidates.get(0) + " "); + } + + return candidates.isEmpty() ? -1 : 0; + } +} \ No newline at end of file diff --git a/src/jline/src/main/java/scala/tools/jline/console/completer/package-info.java b/src/jline/src/main/java/scala/tools/jline/console/completer/package-info.java new file mode 100644 index 0000000000..8150710cfc --- /dev/null +++ b/src/jline/src/main/java/scala/tools/jline/console/completer/package-info.java @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Console completer support. + * + * @since 2.3 + */ +package scala.tools.jline.console.completer; \ No newline at end of file diff --git a/src/jline/src/main/java/scala/tools/jline/console/history/FileHistory.java b/src/jline/src/main/java/scala/tools/jline/console/history/FileHistory.java new file mode 100644 index 0000000000..5eccba3ce5 --- /dev/null +++ b/src/jline/src/main/java/scala/tools/jline/console/history/FileHistory.java @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. + * + * This software is distributable under the BSD license. See the terms of the + * BSD license in the documentation provided with this software. + */ + +package scala.tools.jline.console.history; + +import scala.tools.jline.internal.Log; + +import java.io.BufferedOutputStream; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.Flushable; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.PrintStream; +import java.io.Reader; +import java.util.ListIterator; + +/** + * {@link History} using a file for persistent backing. + *

+ * Implementers should install shutdown hook to call {@link FileHistory#flush} + * to save history to disk. + * + * @author Jason Dillon + * @since 2.0 + */ +public class FileHistory + extends MemoryHistory + implements PersistentHistory, Flushable +{ + private final File file; + + public FileHistory(final File file) throws IOException { + assert file != null; + this.file = file; + load(file); + } + + public File getFile() { + return file; + } + + public void load(final File file) throws IOException { + assert file != null; + if (file.exists()) { + Log.trace("Loading history from: ", file); + load(new FileReader(file)); + } + } + + public void load(final InputStream input) throws IOException { + assert input != null; + load(new InputStreamReader(input)); + } + + public void load(final Reader reader) throws IOException { + assert reader != null; + BufferedReader input = new BufferedReader(reader); + + String item; + while ((item = input.readLine()) != null) { + add(item); + } + } + + public void flush() throws IOException { + Log.trace("Flushing history"); + + if (!file.exists()) { + File dir = file.getParentFile(); + if (!dir.exists() && !dir.mkdirs()) { + Log.warn("Failed to create directory: ", dir); + } + if (!file.createNewFile()) { + Log.warn("Failed to create file: ", file); + } + } + + PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream(file))); + try { + for (Entry entry : this) { + out.println(entry.value()); + } + } + finally { + out.close(); + } + } + + public void purge() throws IOException { + Log.trace("Purging history"); + + clear(); + + if (!file.delete()) { + Log.warn("Failed to delete history file: ", file); + } + } +} \ No newline at end of file diff --git a/src/jline/src/main/java/scala/tools/jline/console/history/History.java b/src/jline/src/main/java/scala/tools/jline/console/history/History.java new file mode 100644 index 0000000000..d8602f2150 --- /dev/null +++ b/src/jline/src/main/java/scala/tools/jline/console/history/History.java @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. + * + * This software is distributable under the BSD license. See the terms of the + * BSD license in the documentation provided with this software. + */ + +package scala.tools.jline.console.history; + +import java.util.Iterator; +import java.util.ListIterator; + +/** + * Console history. + * + * @author Marc Prud'hommeaux + * @author Jason Dillon + * @since 2.3 + */ +public interface History + extends Iterable +{ + int size(); + + boolean isEmpty(); + + int index(); + + void clear(); + + CharSequence get(int index); + + void add(CharSequence line); + + void replace(CharSequence item); + + // + // Entries + // + + interface Entry + { + int index(); + + CharSequence value(); + } + + ListIterator entries(int index); + + ListIterator entries(); + + Iterator iterator(); + + // + // Navigation + // + + CharSequence current(); + + boolean previous(); + + boolean next(); + + boolean moveToFirst(); + + boolean moveToLast(); + + boolean moveTo(int index); + + void moveToEnd(); +} diff --git a/src/jline/src/main/java/scala/tools/jline/console/history/MemoryHistory.java b/src/jline/src/main/java/scala/tools/jline/console/history/MemoryHistory.java new file mode 100644 index 0000000000..3af936428a --- /dev/null +++ b/src/jline/src/main/java/scala/tools/jline/console/history/MemoryHistory.java @@ -0,0 +1,318 @@ +/* + * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. + * + * This software is distributable under the BSD license. See the terms of the + * BSD license in the documentation provided with this software. + */ + +package scala.tools.jline.console.history; + +import java.util.Iterator; +import java.util.LinkedList; +import java.util.ListIterator; +import java.util.NoSuchElementException; + +/** + * Non-persistent {@link History}. + * + * @author Marc Prud'hommeaux + * @author Jason Dillon + * @since 2.3 + */ +public class MemoryHistory + implements History +{ + public static final int DEFAULT_MAX_SIZE = 500; + + private final LinkedList items = new LinkedList(); + + private int maxSize = DEFAULT_MAX_SIZE; + + private boolean ignoreDuplicates = true; + + private boolean autoTrim = false; + + // NOTE: These are all ideas from looking at the Bash man page: + + // TODO: Add ignore space? (lines starting with a space are ignored) + + // TODO: Add ignore patterns? + + // TODO: Add history timestamp? + + // TODO: Add erase dups? + + private int offset = 0; + + private int index = 0; + + public void setMaxSize(final int maxSize) { + this.maxSize = maxSize; + maybeResize(); + } + + public int getMaxSize() { + return maxSize; + } + + public boolean isIgnoreDuplicates() { + return ignoreDuplicates; + } + + public void setIgnoreDuplicates(final boolean flag) { + this.ignoreDuplicates = flag; + } + + public boolean isAutoTrim() { + return autoTrim; + } + + public void setAutoTrim(final boolean flag) { + this.autoTrim = flag; + } + + public int size() { + return items.size(); + } + + public boolean isEmpty() { + return items.isEmpty(); + } + + public int index() { + return offset + index; + } + + public void clear() { + items.clear(); + offset = 0; + index = 0; + } + + public CharSequence get(final int index) { + return items.get(index - offset); + } + + public void add(CharSequence item) { + assert item != null; + + if (isAutoTrim()) { + item = String.valueOf(item).trim(); + } + + if (isIgnoreDuplicates()) { + if (!items.isEmpty() && item.equals(items.getLast())) { + return; + } + } + + items.add(item); + + maybeResize(); + } + + public void replace(final CharSequence item) { + items.removeLast(); + add(item); + } + + private void maybeResize() { + while (size() > getMaxSize()) { + items.removeFirst(); + offset++; + } + + index = size(); + } + + public ListIterator entries(final int index) { + return new EntriesIterator(index - offset); + } + + public ListIterator entries() { + return entries(offset); + } + + public Iterator iterator() { + return entries(); + } + + private static class EntryImpl + implements Entry + { + private final int index; + + private final CharSequence value; + + public EntryImpl(int index, CharSequence value) { + this.index = index; + this.value = value; + } + + public int index() { + return index; + } + + public CharSequence value() { + return value; + } + + @Override + public String toString() { + return String.format("%d: %s", index, value); + } + } + + private class EntriesIterator + implements ListIterator + { + private final ListIterator source; + + private EntriesIterator(final int index) { + source = items.listIterator(index); + } + + public Entry next() { + if (!source.hasNext()) { + throw new NoSuchElementException(); + } + return new EntryImpl(offset + source.nextIndex(), source.next()); + } + + public Entry previous() { + if (!source.hasPrevious()) { + throw new NoSuchElementException(); + } + return new EntryImpl(offset + source.previousIndex(), source.previous()); + } + + public int nextIndex() { + return offset + source.nextIndex(); + } + + public int previousIndex() { + return offset + source.previousIndex(); + } + + public boolean hasNext() { + return source.hasNext(); + } + + public boolean hasPrevious() { + return source.hasPrevious(); + } + + public void remove() { + throw new UnsupportedOperationException(); + } + + public void set(final Entry entry) { + throw new UnsupportedOperationException(); + } + + public void add(final Entry entry) { + throw new UnsupportedOperationException(); + } + } + + // + // Navigation + // + + /** + * This moves the history to the last entry. This entry is one position + * before the moveToEnd() position. + * + * @return Returns false if there were no history entries or the history + * index was already at the last entry. + */ + public boolean moveToLast() { + int lastEntry = size() - 1; + if (lastEntry >= 0 && lastEntry != index) { + index = size() - 1; + return true; + } + + return false; + } + + /** + * Move to the specified index in the history + * @param index + * @return + */ + public boolean moveTo(int index) { + index -= offset; + if (index >= 0 && index < size() ) { + this.index = index; + return true; + } + return false; + } + + /** + * Moves the history index to the first entry. + * + * @return Return false if there are no entries in the history or if the + * history is already at the beginning. + */ + public boolean moveToFirst() { + if (size() > 0 && index != 0) { + index = 0; + return true; + } + + return false; + } + + /** + * Move to the end of the history buffer. This will be a blank entry, after + * all of the other entries. + */ + public void moveToEnd() { + index = size(); + } + + /** + * Return the content of the current buffer. + */ + public CharSequence current() { + if (index >= size()) { + return ""; + } + + return items.get(index); + } + + /** + * Move the pointer to the previous element in the buffer. + * + * @return true if we successfully went to the previous element + */ + public boolean previous() { + if (index <= 0) { + return false; + } + + index--; + + return true; + } + + /** + * Move the pointer to the next element in the buffer. + * + * @return true if we successfully went to the next element + */ + public boolean next() { + if (index >= size()) { + return false; + } + + index++; + + return true; + } + + +} \ No newline at end of file diff --git a/src/jline/src/main/java/scala/tools/jline/console/history/PersistentHistory.java b/src/jline/src/main/java/scala/tools/jline/console/history/PersistentHistory.java new file mode 100644 index 0000000000..916532e7fc --- /dev/null +++ b/src/jline/src/main/java/scala/tools/jline/console/history/PersistentHistory.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. + * + * This software is distributable under the BSD license. See the terms of the + * BSD license in the documentation provided with this software. + */ + +package scala.tools.jline.console.history; + +import java.io.IOException; + +/** + * Persistent {@link History}. + * + * @author Jason Dillon + * @since 2.3 + */ +public interface PersistentHistory + extends History +{ + /** + * Flush all items to persistent storage. + * + * @throws IOException Flush failed + */ + void flush() throws IOException; + + /** + * Purge persistent storage and {@link #clear}. + * + * @throws IOException Purge failed + */ + void purge() throws IOException; +} \ No newline at end of file diff --git a/src/jline/src/main/java/scala/tools/jline/console/history/package-info.java b/src/jline/src/main/java/scala/tools/jline/console/history/package-info.java new file mode 100644 index 0000000000..4635752898 --- /dev/null +++ b/src/jline/src/main/java/scala/tools/jline/console/history/package-info.java @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2009 the original author(s). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Console history support. + * + * @since 2.0 + */ +package scala.tools.jline.console.history; \ No newline at end of file diff --git a/src/jline/src/main/java/scala/tools/jline/console/package-info.java b/src/jline/src/main/java/scala/tools/jline/console/package-info.java new file mode 100644 index 0000000000..9f284e9c05 --- /dev/null +++ b/src/jline/src/main/java/scala/tools/jline/console/package-info.java @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2009 the original author(s). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Console support. + * + * @since 2.0 + */ +package scala.tools.jline.console; \ No newline at end of file diff --git a/src/jline/src/main/java/scala/tools/jline/internal/Configuration.java b/src/jline/src/main/java/scala/tools/jline/internal/Configuration.java new file mode 100644 index 0000000000..5350d6c19e --- /dev/null +++ b/src/jline/src/main/java/scala/tools/jline/internal/Configuration.java @@ -0,0 +1,127 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package scala.tools.jline.internal; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +/** + * Provides access to configuration values. + * + * @author Jason Dillon + * @since 2.4 + */ +public final class Configuration +{ + public static final String JLINE_RC = ".jline.rc"; + + private static final Properties userprops; + + static { + Properties props = new Properties(); + + File file = new File(getUserHome(), JLINE_RC); + if (file.exists() && file.canRead()) { + try { + InputStream input = new BufferedInputStream(new FileInputStream(file)); + try { + props.load(input); + Log.debug("Loaded user configuration: ", file); + } + finally { + input.close(); + } + } + catch (IOException e) { + Log.warn("Unable to read user configuration: ", file, e); + } + } + else { + Log.trace("User configuration file missing or unreadable: ", file); + } + + userprops = props; + } + + private static boolean isEmpty(final String value) { + return value == null || value.trim().length() == 0; + } + + public static String getString(final String name, final String defaultValue) { + assert name != null; + + String value; + + // Check sysprops first, it always wins + value = System.getProperty(name); + + if (isEmpty(value)) { + // Next try userprops + value = userprops.getProperty(name); + + if (isEmpty(value)) { + // else use the default + value = defaultValue; + } + } + + return value; + } + + public static String getString(final String name) { + return getString(name, null); + } + + public static Boolean getBoolean(final String name, final Boolean defaultValue) { + String value = getString(name); + if (isEmpty(value)) { + return defaultValue; + } + return Boolean.valueOf(value); + } + + public static Boolean getBoolean(final String name) { + return getBoolean(name, null); + } + + // + // System property helpers + // + + public static File getUserHome() { + return new File(System.getProperty("user.home")); + } + + public static String getOsName() { + return System.getProperty("os.name").toLowerCase(); + } + + public static String getFileEncoding() { + return System.getProperty("file.encoding"); + } + + public static String getInputEncoding() { + return System.getProperty("input.encoding", "UTF-8"); + } +} \ No newline at end of file diff --git a/src/jline/src/main/java/scala/tools/jline/internal/Log.java b/src/jline/src/main/java/scala/tools/jline/internal/Log.java new file mode 100644 index 0000000000..b226a10532 --- /dev/null +++ b/src/jline/src/main/java/scala/tools/jline/internal/Log.java @@ -0,0 +1,112 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package scala.tools.jline.internal; + +import java.io.PrintStream; + +/** + * Internal logger. + * + * @author Jason Dillon + * @since 2.0 + */ +public final class Log +{ + ///CLOVER:OFF + + public static enum Level + { + TRACE, + DEBUG, + INFO, + WARN, + ERROR + } + + @SuppressWarnings({"StringConcatenation"}) + public static final boolean DEBUG = Boolean.getBoolean(Log.class.getName() + ".debug"); + + @SuppressWarnings({"StringConcatenation"}) + public static final boolean TRACE = Boolean.getBoolean(Log.class.getName() + ".trace"); + + private static PrintStream output = System.err; + + public static PrintStream getOutput() { + return output; + } + + public static void setOutput(final PrintStream out) { + assert out != null; + output = out; + } + + private static void print(final Object message) { + if (message instanceof Throwable) { + ((Throwable) message).printStackTrace(); + } + else if (message.getClass().isArray()) { + Object[] array = (Object[]) message; + + for (int i = 0; i < array.length; i++) { + output.print(array[i]); + if (i + 1 < array.length) { + output.print(","); + } + } + } + else { + output.print(message); + } + } + + private static void log(final Level level, final Object[] messages) { + //noinspection SynchronizeOnNonFinalField + synchronized (output) { + output.format("[%s] ", level); + + for (Object message : messages) { + print(message); + } + + output.println(); + output.flush(); + } + } + + public static void trace(final Object... messages) { + if (TRACE) { + log(Level.TRACE, messages); + } + } + + public static void debug(final Object... messages) { + if (TRACE || DEBUG) { + log(Level.DEBUG, messages); + } + } + + public static void warn(final Object... messages) { + log(Level.WARN, messages); + } + + public static void error(final Object... messages) { + log(Level.ERROR, messages); + } +} \ No newline at end of file diff --git a/src/jline/src/main/java/scala/tools/jline/internal/ReplayPrefixOneCharInputStream.java b/src/jline/src/main/java/scala/tools/jline/internal/ReplayPrefixOneCharInputStream.java new file mode 100644 index 0000000000..2adabdd2ab --- /dev/null +++ b/src/jline/src/main/java/scala/tools/jline/internal/ReplayPrefixOneCharInputStream.java @@ -0,0 +1,95 @@ +package scala.tools.jline.internal; + +import java.io.IOException; +import java.io.InputStream; +import java.text.MessageFormat; + +/** + * This is awkward and inefficient, but probably the minimal way to add UTF-8 support to JLine + * + * @author Marc Herbert + * @author Jason Dillon + * @since 2.0 + */ +public final class ReplayPrefixOneCharInputStream + extends InputStream +{ + private byte firstByte; + + private int byteLength; + + private InputStream wrappedStream; + + private int byteRead; + + private final String encoding; + + public ReplayPrefixOneCharInputStream(final String encoding) { + assert encoding != null; + this.encoding = encoding; + } + + public String getEncoding() { + return encoding; + } + + public void setInput(final int recorded, final InputStream wrapped) throws IOException { + this.byteRead = 0; + this.firstByte = (byte) recorded; + this.wrappedStream = wrapped; + + byteLength = 1; + if (encoding.equalsIgnoreCase("UTF-8")) { + setInputUTF8(recorded, wrapped); + } + else if (encoding.equalsIgnoreCase("UTF-16")) { + byteLength = 2; + } + else if (encoding.equalsIgnoreCase("UTF-32")) { + byteLength = 4; + } + } + + + public void setInputUTF8(final int recorded, final InputStream wrapped) throws IOException { + // 110yyyyy 10zzzzzz + if ((firstByte & (byte) 0xE0) == (byte) 0xC0) { + this.byteLength = 2; + } + // 1110xxxx 10yyyyyy 10zzzzzz + else if ((firstByte & (byte) 0xF0) == (byte) 0xE0) { + this.byteLength = 3; + } + // 11110www 10xxxxxx 10yyyyyy 10zzzzzz + else if ((firstByte & (byte) 0xF8) == (byte) 0xF0) { + this.byteLength = 4; + } + else { + throw new IOException(MessageFormat.format("Invalid UTF-8 first byte: {0}", firstByte)); + } + } + + public int read() throws IOException { + if (available() == 0) { + return -1; + } + + byteRead++; + + if (byteRead == 1) { + return firstByte; + } + + return wrappedStream.read(); + } + + /** + * InputStreamReader is greedy and will try to read bytes in advance. We + * do NOT want this to happen since we use a temporary/"losing bytes" + * InputStreamReader above, that's why we hide the real + * wrappedStream.available() here. + */ + public int available() { + return byteLength - byteRead; + } +} \ No newline at end of file diff --git a/src/jline/src/main/java/scala/tools/jline/internal/TerminalLineSettings.java b/src/jline/src/main/java/scala/tools/jline/internal/TerminalLineSettings.java new file mode 100644 index 0000000000..151862c14d --- /dev/null +++ b/src/jline/src/main/java/scala/tools/jline/internal/TerminalLineSettings.java @@ -0,0 +1,217 @@ +/* + * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. + * + * This software is distributable under the BSD license. See the terms of the + * BSD license in the documentation provided with this software. + */ + +package scala.tools.jline.internal; + +import java.io.ByteArrayOutputStream; +import java.io.Closeable; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.text.MessageFormat; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Provides access to terminal line settings via stty. + * + * @author Marc Prud'hommeaux + * @author Dale Kemp + * @author Jason Dillon + * @author Jean-Baptiste Onofré + * @since 2.0 + */ +public final class TerminalLineSettings +{ + public static final String JLINE_STTY = "jline.stty"; + + public static final String DEFAULT_STTY = "stty"; + + public static final String JLINE_SH = "jline.sh"; + + public static final String DEFAULT_SH = "sh"; + + private static String sttyCommand = Configuration.getString(JLINE_STTY, DEFAULT_STTY); + + private static String shCommand = Configuration.getString(JLINE_SH, DEFAULT_SH); + + private String config; + + private long configLastFetched; + + public TerminalLineSettings() throws IOException, InterruptedException { + config = get("-a"); + configLastFetched = System.currentTimeMillis(); + + Log.debug("Config: ", config); + + // sanity check + if (config.length() == 0) { + throw new IOException(MessageFormat.format("Unrecognized stty code: {0}", config)); + } + } + + public String getConfig() { + return config; + } + + public void restore() throws IOException, InterruptedException { + set("sane"); + } + + public String get(final String args) throws IOException, InterruptedException { + return stty(args); + } + + public void set(final String args) throws IOException, InterruptedException { + stty(args); + } + + /** + *

+ * Get the value of a stty property, including the management of a cache. + *

+ * + * @param name the stty property. + * @return the stty property value. + */ + public int getProperty(String name) { + assert name != null; + try { + // tty properties are cached so we don't have to worry too much about getting term widht/height + if (config == null || System.currentTimeMillis() - configLastFetched > 1000 ) { + config = get("-a"); + configLastFetched = System.currentTimeMillis(); + } + return this.getProperty(name, config); + } catch (Exception e) { + Log.warn("Failed to query stty ", name, e); + return -1; + } + } + + /** + *

+ * Parses a stty output (provided by stty -a) and return the value of a given property. + *

+ * + * @param name property name. + * @param stty string resulting of stty -a execution. + * @return value of the given property. + */ + protected int getProperty(String name, String stty) { + // try the first kind of regex + Pattern pattern = Pattern.compile(name + "\\s+=\\s+([^;]*)[;\\n\\r]"); + Matcher matcher = pattern.matcher(stty); + if (!matcher.find()) { + // try a second kind of regex + pattern = Pattern.compile(name + "\\s+([^;]*)[;\\n\\r]"); + matcher = pattern.matcher(stty); + if (!matcher.find()) { + // try a second try of regex + pattern = Pattern.compile("(\\S*)\\s+" + name); + matcher = pattern.matcher(stty); + if (!matcher.find()) { + return -1; + } + } + } + return parseControlChar(matcher.group(1)); + } + + private int parseControlChar(String str) { + // under + if ("".equals(str)) { + return -1; + } + // octal + if (str.charAt(0) == '0') { + return Integer.parseInt(str, 8); + } + // decimal + if (str.charAt(0) >= '1' && str.charAt(0) <= '9') { + return Integer.parseInt(str, 10); + } + // control char + if (str.charAt(0) == '^') { + if (str.charAt(1) == '?') { + return 127; + } else { + return str.charAt(1) - 64; + } + } else if (str.charAt(0) == 'M' && str.charAt(1) == '-') { + if (str.charAt(2) == '^') { + if (str.charAt(3) == '?') { + return 127 + 128; + } else { + return str.charAt(3) - 64 + 128; + } + } else { + return str.charAt(2) + 128; + } + } else { + return str.charAt(0); + } + } + + private static String stty(final String args) throws IOException, InterruptedException { + assert args != null; + return exec(String.format("%s %s < /dev/tty", sttyCommand, args)); + } + + private static String exec(final String cmd) throws IOException, InterruptedException { + assert cmd != null; + return exec(shCommand, "-c", cmd); + } + + private static String exec(final String... cmd) throws IOException, InterruptedException { + assert cmd != null; + + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + + Log.trace("Running: ", cmd); + + Process p = Runtime.getRuntime().exec(cmd); + + InputStream in = null; + InputStream err = null; + OutputStream out = null; + try { + int c; + in = p.getInputStream(); + while ((c = in.read()) != -1) { + bout.write(c); + } + err = p.getErrorStream(); + while ((c = err.read()) != -1) { + bout.write(c); + } + out = p.getOutputStream(); + p.waitFor(); + } + finally { + close(in, out, err); + } + + String result = bout.toString(); + + Log.trace("Result: ", result); + + return result; + } + + private static void close(final Closeable... closeables) { + for (Closeable c : closeables) { + try { + c.close(); + } + catch (Exception e) { + // Ignore + } + } + } +} \ No newline at end of file diff --git a/src/jline/src/main/java/scala/tools/jline/internal/package-info.java b/src/jline/src/main/java/scala/tools/jline/internal/package-info.java new file mode 100644 index 0000000000..d27444cfdf --- /dev/null +++ b/src/jline/src/main/java/scala/tools/jline/internal/package-info.java @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2009 the original author(s). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Internal support. + * + * @since 2.0 + */ +package scala.tools.jline.internal; \ No newline at end of file diff --git a/src/jline/src/main/java/scala/tools/jline/package-info.java b/src/jline/src/main/java/scala/tools/jline/package-info.java new file mode 100644 index 0000000000..fde16f98de --- /dev/null +++ b/src/jline/src/main/java/scala/tools/jline/package-info.java @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * JLine 2. + * + * @since 2.0 + */ +package scala.tools.jline; \ No newline at end of file diff --git a/src/jline/src/main/resources/jline/console/completer/CandidateListCompletionHandler.properties b/src/jline/src/main/resources/jline/console/completer/CandidateListCompletionHandler.properties deleted file mode 100644 index fd097efb8a..0000000000 --- a/src/jline/src/main/resources/jline/console/completer/CandidateListCompletionHandler.properties +++ /dev/null @@ -1,4 +0,0 @@ -DISPLAY_CANDIDATES=Display all %d possibilities? (y or n) -DISPLAY_CANDIDATES_YES=y -DISPLAY_CANDIDATES_NO=n -DISPLAY_MORE=--More-- diff --git a/src/jline/src/main/resources/jline/keybindings.properties b/src/jline/src/main/resources/jline/keybindings.properties deleted file mode 100644 index 610a1626aa..0000000000 --- a/src/jline/src/main/resources/jline/keybindings.properties +++ /dev/null @@ -1,65 +0,0 @@ -# Keybinding mapping for JLine. The format is: -# [key code]=[logical operation] - -# CTRL-A: move to the beginning of the line -1=MOVE_TO_BEG - -# CTRL-B: move to the previous character -2=PREV_CHAR - -# CTRL-D: close out the input stream -4=EXIT - -# CTRL-E: move the cursor to the end of the line -5=MOVE_TO_END - -# CTRL-F: move to the next character -6=NEXT_CHAR - -# CTRL-G: abort -7=ABORT - -# BACKSPACE, CTRL-H: delete the previous character -# 8 is the ASCII code for backspace and therefor -# deleting the previous character -8=DELETE_PREV_CHAR - -# TAB, CTRL-I: signal that console completion should be attempted -9=COMPLETE - -# CTRL-J, CTRL-M: newline -10=NEWLINE - -# CTRL-K: erase the current line -11=KILL_LINE - -# CTRL-L: clear screen -12=CLEAR_SCREEN - -# ENTER: newline -13=NEWLINE - -# CTRL-N: scroll to the next element in the history buffer -14=NEXT_HISTORY - -# CTRL-O: move to the previous word -15=PREV_WORD - -# CTRL-P: scroll to the previous element in the history buffer -16=PREV_HISTORY - -# CTRL-R: redraw the current line -18=SEARCH_PREV - -# CTRL-U: delete all the characters before the cursor position -21=KILL_LINE_PREV - -# CTRL-V: paste the contents of the clipboard (useful for Windows terminal) -22=PASTE - -# CTRL-W: delete the word directly before the cursor -23=DELETE_PREV_WORD - -# DELETE, CTRL-?: delete the next character -# 127 is the ASCII code for delete -127=DELETE_NEXT_CHAR diff --git a/src/jline/src/main/resources/jline/windowsbindings.properties b/src/jline/src/main/resources/jline/windowsbindings.properties deleted file mode 100644 index 340b5aa5b9..0000000000 --- a/src/jline/src/main/resources/jline/windowsbindings.properties +++ /dev/null @@ -1,71 +0,0 @@ -# Keybinding mapping for JLine. The format is: -# [key code]=[logical operation] - -# CTRL-A: move to the beginning of the line -1=MOVE_TO_BEG - -# CTRL-B: move to the previous character -2=PREV_CHAR - -# CTRL-C: toggle overtype mode (frankly, I wasn't sure where to bind this) -3=INSERT - -# CTRL-D: close out the input stream -4=EXIT - -# CTRL-E: move the cursor to the end of the line -5=MOVE_TO_END - -# CTRL-F: move to the next character -6=NEXT_CHAR - -# CTRL-G: move to the previous word -7=ABORT - -# CTRL-H: delete the previous character -8=DELETE_PREV_CHAR - -# TAB, CTRL-I: signal that console completion should be attempted -9=COMPLETE - -# CTRL-J, CTRL-M: newline -10=NEWLINE - -# CTRL-K: erase the current line -11=KILL_LINE - -# CTRL-L: clear screen -12=CLEAR_SCREEN - -# ENTER: newline -13=NEWLINE - -# CTRL-N: scroll to the next element in the history buffer -14=NEXT_HISTORY - -# CTRL-O: move to the previous word -15=PREV_WORD - -# CTRL-P: scroll to the previous element in the history buffer -16=PREV_HISTORY - -# CTRL-R: search backwards in history -18=SEARCH_PREV - -# CTRL-S: Move to the end of the history -19=END_OF_HISTORY - -# CTRL-U: delete all the characters before the cursor position -21=KILL_LINE_PREV - -# CTRL-V: paste the contents of the clipboard (useful for Windows terminal) -22=PASTE - -# CTRL-W: delete the word directly before the cursor -23=DELETE_PREV_WORD - -# CTRL-[: escape - clear the current line. -27=CLEAR_LINE - -# CTRL-?: delete the previous character -127=DELETE_NEXT_CHAR diff --git a/src/jline/src/main/resources/scala/tools/jline/console/completer/CandidateListCompletionHandler.properties b/src/jline/src/main/resources/scala/tools/jline/console/completer/CandidateListCompletionHandler.properties new file mode 100644 index 0000000000..fd097efb8a --- /dev/null +++ b/src/jline/src/main/resources/scala/tools/jline/console/completer/CandidateListCompletionHandler.properties @@ -0,0 +1,4 @@ +DISPLAY_CANDIDATES=Display all %d possibilities? (y or n) +DISPLAY_CANDIDATES_YES=y +DISPLAY_CANDIDATES_NO=n +DISPLAY_MORE=--More-- diff --git a/src/jline/src/main/resources/scala/tools/jline/keybindings.properties b/src/jline/src/main/resources/scala/tools/jline/keybindings.properties new file mode 100644 index 0000000000..610a1626aa --- /dev/null +++ b/src/jline/src/main/resources/scala/tools/jline/keybindings.properties @@ -0,0 +1,65 @@ +# Keybinding mapping for JLine. The format is: +# [key code]=[logical operation] + +# CTRL-A: move to the beginning of the line +1=MOVE_TO_BEG + +# CTRL-B: move to the previous character +2=PREV_CHAR + +# CTRL-D: close out the input stream +4=EXIT + +# CTRL-E: move the cursor to the end of the line +5=MOVE_TO_END + +# CTRL-F: move to the next character +6=NEXT_CHAR + +# CTRL-G: abort +7=ABORT + +# BACKSPACE, CTRL-H: delete the previous character +# 8 is the ASCII code for backspace and therefor +# deleting the previous character +8=DELETE_PREV_CHAR + +# TAB, CTRL-I: signal that console completion should be attempted +9=COMPLETE + +# CTRL-J, CTRL-M: newline +10=NEWLINE + +# CTRL-K: erase the current line +11=KILL_LINE + +# CTRL-L: clear screen +12=CLEAR_SCREEN + +# ENTER: newline +13=NEWLINE + +# CTRL-N: scroll to the next element in the history buffer +14=NEXT_HISTORY + +# CTRL-O: move to the previous word +15=PREV_WORD + +# CTRL-P: scroll to the previous element in the history buffer +16=PREV_HISTORY + +# CTRL-R: redraw the current line +18=SEARCH_PREV + +# CTRL-U: delete all the characters before the cursor position +21=KILL_LINE_PREV + +# CTRL-V: paste the contents of the clipboard (useful for Windows terminal) +22=PASTE + +# CTRL-W: delete the word directly before the cursor +23=DELETE_PREV_WORD + +# DELETE, CTRL-?: delete the next character +# 127 is the ASCII code for delete +127=DELETE_NEXT_CHAR diff --git a/src/jline/src/main/resources/scala/tools/jline/windowsbindings.properties b/src/jline/src/main/resources/scala/tools/jline/windowsbindings.properties new file mode 100644 index 0000000000..340b5aa5b9 --- /dev/null +++ b/src/jline/src/main/resources/scala/tools/jline/windowsbindings.properties @@ -0,0 +1,71 @@ +# Keybinding mapping for JLine. The format is: +# [key code]=[logical operation] + +# CTRL-A: move to the beginning of the line +1=MOVE_TO_BEG + +# CTRL-B: move to the previous character +2=PREV_CHAR + +# CTRL-C: toggle overtype mode (frankly, I wasn't sure where to bind this) +3=INSERT + +# CTRL-D: close out the input stream +4=EXIT + +# CTRL-E: move the cursor to the end of the line +5=MOVE_TO_END + +# CTRL-F: move to the next character +6=NEXT_CHAR + +# CTRL-G: move to the previous word +7=ABORT + +# CTRL-H: delete the previous character +8=DELETE_PREV_CHAR + +# TAB, CTRL-I: signal that console completion should be attempted +9=COMPLETE + +# CTRL-J, CTRL-M: newline +10=NEWLINE + +# CTRL-K: erase the current line +11=KILL_LINE + +# CTRL-L: clear screen +12=CLEAR_SCREEN + +# ENTER: newline +13=NEWLINE + +# CTRL-N: scroll to the next element in the history buffer +14=NEXT_HISTORY + +# CTRL-O: move to the previous word +15=PREV_WORD + +# CTRL-P: scroll to the previous element in the history buffer +16=PREV_HISTORY + +# CTRL-R: search backwards in history +18=SEARCH_PREV + +# CTRL-S: Move to the end of the history +19=END_OF_HISTORY + +# CTRL-U: delete all the characters before the cursor position +21=KILL_LINE_PREV + +# CTRL-V: paste the contents of the clipboard (useful for Windows terminal) +22=PASTE + +# CTRL-W: delete the word directly before the cursor +23=DELETE_PREV_WORD + +# CTRL-[: escape - clear the current line. +27=CLEAR_LINE + +# CTRL-?: delete the previous character +127=DELETE_NEXT_CHAR diff --git a/src/jline/src/test/java/jline/TerminalFactoryTest.java b/src/jline/src/test/java/jline/TerminalFactoryTest.java deleted file mode 100644 index 3b38fbec95..0000000000 --- a/src/jline/src/test/java/jline/TerminalFactoryTest.java +++ /dev/null @@ -1,34 +0,0 @@ -package jline; - -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -/** - * Tests for the {@link TerminalFactory}. - */ -public class TerminalFactoryTest -{ - @Before - public void setUp() throws Exception { - TerminalFactory.reset(); - } - - @Test - public void testConfigureNone() { - TerminalFactory.configure(TerminalFactory.NONE); - Terminal t = TerminalFactory.get(); - assertNotNull(t); - assertEquals(UnsupportedTerminal.class.getName(), t.getClass().getName()); - } - - @Test - public void testConfigureUnsupportedTerminal() { - TerminalFactory.configure(UnsupportedTerminal.class.getName()); - Terminal t = TerminalFactory.get(); - assertNotNull(t); - assertEquals(UnsupportedTerminal.class.getName(), t.getClass().getName()); - } -} \ No newline at end of file diff --git a/src/jline/src/test/java/jline/console/ConsoleReaderTest.java b/src/jline/src/test/java/jline/console/ConsoleReaderTest.java deleted file mode 100644 index 45ba4775e1..0000000000 --- a/src/jline/src/test/java/jline/console/ConsoleReaderTest.java +++ /dev/null @@ -1,261 +0,0 @@ -package jline.console; - -import jline.TerminalFactory; -import jline.WindowsTerminal; -import jline.console.history.History; -import jline.console.history.MemoryHistory; -import org.junit.Before; -import org.junit.Test; - -import java.io.ByteArrayInputStream; -import java.io.InputStream; -import java.io.StringWriter; -import java.io.Writer; - -import static jline.WindowsTerminal.WindowsKey.DELETE_KEY; -import static jline.WindowsTerminal.WindowsKey.END_KEY; -import static jline.WindowsTerminal.WindowsKey.ESCAPE_KEY; -import static jline.WindowsTerminal.WindowsKey.HOME_KEY; -import static jline.WindowsTerminal.WindowsKey.INSERT_KEY; -import static jline.WindowsTerminal.WindowsKey.LEFT_ARROW_KEY; -import static jline.WindowsTerminal.WindowsKey.NUMPAD_KEY_INDICATOR; -import static jline.WindowsTerminal.WindowsKey.PAGE_DOWN_KEY; -import static jline.WindowsTerminal.WindowsKey.PAGE_UP_KEY; -import static jline.WindowsTerminal.WindowsKey.SPECIAL_KEY_INDICATOR; -import static jline.console.Operation.DELETE_NEXT_CHAR; -import static jline.console.Operation.DELETE_PREV_CHAR; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -/** - * Tests for the {@link ConsoleReader}. - */ -public class ConsoleReaderTest -{ - @Before - public void setUp() throws Exception { - System.setProperty(WindowsTerminal.JLINE_WINDOWS_TERMINAL_DIRECT_CONSOLE, "false"); - } - - private void assertWindowsKeyBehavior(String expected, char[] input) throws Exception { - StringBuilder buffer = new StringBuilder(); - buffer.append(input); - ConsoleReader reader = createConsole(buffer.toString().getBytes()); - assertNotNull(reader); - String line = reader.readLine(); - assertEquals(expected, line); - } - - private ConsoleReader createConsole(byte[] bytes) throws Exception { - InputStream in = new ByteArrayInputStream(bytes); - Writer writer = new StringWriter(); - ConsoleReader reader = new ConsoleReader(in, writer); - reader.setHistory(createSeededHistory()); - return reader; - } - - private History createSeededHistory() { - History history = new MemoryHistory(); - history.add("dir"); - history.add("cd c:\\"); - history.add("mkdir monkey"); - return history; - } - - @Test - public void testDeleteAndBackspaceKeymappings() throws Exception { - // test only works on Windows - if (!(TerminalFactory.get() instanceof WindowsTerminal)) { - return; - } - - ConsoleReader consoleReader = new ConsoleReader(); - assertNotNull(consoleReader); - assertEquals(127, consoleReader.getKeyForAction(DELETE_NEXT_CHAR)); - assertEquals(8, consoleReader.getKeyForAction(DELETE_PREV_CHAR)); - } - - @Test - public void testReadline() throws Exception { - ConsoleReader consoleReader = createConsole("Sample String\r\n".getBytes()); - assertNotNull(consoleReader); - String line = consoleReader.readLine(); - assertEquals("Sample String", line); - } - - @Test - public void testDeleteOnWindowsTerminal() throws Exception { - // test only works on Windows - if (!(TerminalFactory.get() instanceof WindowsTerminal)) { - return; - } - - char[] characters = new char[]{ - 'S', 's', - (char) SPECIAL_KEY_INDICATOR.code, - (char) LEFT_ARROW_KEY.code, - (char) SPECIAL_KEY_INDICATOR.code, - (char) DELETE_KEY.code, '\r', 'n' - }; - assertWindowsKeyBehavior("S", characters); - } - - @Test - public void testNumpadDeleteOnWindowsTerminal() throws Exception { - // test only works on Windows - if (!(TerminalFactory.get() instanceof WindowsTerminal)) { - return; - } - - char[] characters = new char[]{ - 'S', 's', - (char) NUMPAD_KEY_INDICATOR.code, - (char) LEFT_ARROW_KEY.code, - (char) NUMPAD_KEY_INDICATOR.code, - (char) DELETE_KEY.code, '\r', 'n' - }; - assertWindowsKeyBehavior("S", characters); - } - - @Test - public void testHomeKeyOnWindowsTerminal() throws Exception { - // test only works on Windows - if (!(TerminalFactory.get() instanceof WindowsTerminal)) { - return; - } - - char[] characters = new char[]{ - 'S', 's', - (char) SPECIAL_KEY_INDICATOR.code, - (char) HOME_KEY.code, 'x', '\r', '\n' - }; - assertWindowsKeyBehavior("xSs", characters); - - } - - @Test - public void testEndKeyOnWindowsTerminal() throws Exception { - // test only works on Windows - if (!(TerminalFactory.get() instanceof WindowsTerminal)) { - return; - } - - char[] characters = new char[]{ - 'S', 's', - (char) SPECIAL_KEY_INDICATOR.code, - (char) HOME_KEY.code, 'x', - (char) SPECIAL_KEY_INDICATOR.code, (char) END_KEY.code, - 'j', '\r', '\n' - }; - assertWindowsKeyBehavior("xSsj", characters); - } - - @Test - public void testPageUpOnWindowsTerminal() throws Exception { - // test only works on Windows - if (!(TerminalFactory.get() instanceof WindowsTerminal)) { - return; - } - - char[] characters = new char[]{ - (char) SPECIAL_KEY_INDICATOR.code, - (char) PAGE_UP_KEY.code, '\r', '\n' - }; - assertWindowsKeyBehavior("dir", characters); - } - - @Test - public void testPageDownOnWindowsTerminal() throws Exception { - // test only works on Windows - if (!(TerminalFactory.get() instanceof WindowsTerminal)) { - return; - } - - char[] characters = new char[]{ - (char) SPECIAL_KEY_INDICATOR.code, - (char) PAGE_DOWN_KEY.code, '\r', '\n' - }; - assertWindowsKeyBehavior("mkdir monkey", characters); - } - - @Test - public void testEscapeOnWindowsTerminal() throws Exception { - // test only works on Windows - if (!(TerminalFactory.get() instanceof WindowsTerminal)) { - return; - } - - char[] characters = new char[]{ - 's', 's', 's', - (char) SPECIAL_KEY_INDICATOR.code, - (char) ESCAPE_KEY.code, '\r', '\n' - }; - assertWindowsKeyBehavior("", characters); - } - - @Test - public void testInsertOnWindowsTerminal() throws Exception { - // test only works on Windows - if (!(TerminalFactory.get() instanceof WindowsTerminal)) { - return; - } - - char[] characters = new char[]{ - 'o', 'p', 's', - (char) SPECIAL_KEY_INDICATOR.code, - (char) HOME_KEY.code, - (char) SPECIAL_KEY_INDICATOR.code, - (char) INSERT_KEY.code, 'o', 'o', 'p', 's', '\r', '\n' - }; - assertWindowsKeyBehavior("oops", characters); - } - - @Test - public void testExpansion() throws Exception { - ConsoleReader reader = new ConsoleReader(); - MemoryHistory history = new MemoryHistory(); - history.setMaxSize(3); - history.add("foo"); - history.add("dir"); - history.add("cd c:\\"); - history.add("mkdir monkey"); - reader.setHistory(history); - - assertEquals("echo a!", reader.expandEvents("echo a!")); - assertEquals("mkdir monkey ; echo a!", reader.expandEvents("!! ; echo a!")); - assertEquals("echo ! a", reader.expandEvents("echo ! a")); - assertEquals("echo !\ta", reader.expandEvents("echo !\ta")); - - assertEquals("mkdir barey", reader.expandEvents("^monk^bar^")); - assertEquals("mkdir barey", reader.expandEvents("^monk^bar")); - assertEquals("a^monk^bar", reader.expandEvents("a^monk^bar")); - - assertEquals("mkdir monkey", reader.expandEvents("!!")); - assertEquals("echo echo a", reader.expandEvents("echo !#a")); - - assertEquals("mkdir monkey", reader.expandEvents("!mk")); - try { - reader.expandEvents("!mz"); - } catch (IllegalArgumentException e) { - assertEquals("!mz: event not found", e.getMessage()); - } - - assertEquals("mkdir monkey", reader.expandEvents("!?mo")); - assertEquals("mkdir monkey", reader.expandEvents("!?mo?")); - - assertEquals("mkdir monkey", reader.expandEvents("!-1")); - assertEquals("cd c:\\", reader.expandEvents("!-2")); - assertEquals("cd c:\\", reader.expandEvents("!2")); - assertEquals("mkdir monkey", reader.expandEvents("!3")); - try { - reader.expandEvents("!20"); - } catch (IllegalArgumentException e) { - assertEquals("!20: event not found", e.getMessage()); - } - try { - reader.expandEvents("!-20"); - } catch (IllegalArgumentException e) { - assertEquals("!-20: event not found", e.getMessage()); - } - } -} diff --git a/src/jline/src/test/java/jline/console/ConsoleReaderTestSupport.java b/src/jline/src/test/java/jline/console/ConsoleReaderTestSupport.java deleted file mode 100644 index 7bb65fe3ca..0000000000 --- a/src/jline/src/test/java/jline/console/ConsoleReaderTestSupport.java +++ /dev/null @@ -1,142 +0,0 @@ -/* - * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. - * - * This software is distributable under the BSD license. See the terms of the - * BSD license in the documentation provided with this software. - */ -package jline.console; - -import jline.UnixTerminal; -import org.junit.Before; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.OutputStreamWriter; -import java.io.PrintWriter; - -import static jline.UnixTerminal.UnixKey.ARROW_DOWN; -import static jline.UnixTerminal.UnixKey.ARROW_LEFT; -import static jline.UnixTerminal.UnixKey.ARROW_PREFIX; -import static jline.UnixTerminal.UnixKey.ARROW_RIGHT; -import static jline.UnixTerminal.UnixKey.ARROW_START; -import static jline.UnixTerminal.UnixKey.ARROW_UP; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - -/** - * Provides support for console reader tests. - */ -public abstract class ConsoleReaderTestSupport -{ - protected ConsoleReader console; - - @Before - public void setUp() throws Exception { - console = new ConsoleReader(null, new PrintWriter(new OutputStreamWriter(new ByteArrayOutputStream())), new UnixTerminal()); - } - - protected void assertBuffer(final String expected, final Buffer buffer) throws IOException { - assertBuffer(expected, buffer, true); - } - - protected void assertBuffer(final String expected, final Buffer buffer, final boolean clear) throws IOException { - // clear current buffer, if any - if (clear) { - console.finishBuffer(); - console.getHistory().clear(); - } - - console.setInput(new ByteArrayInputStream(buffer.getBytes())); - - // run it through the reader - while (console.readLine((String) null) != null) { - // ignore - } - - assertEquals(expected, console.getCursorBuffer().toString()); - } - - private int getKeyForAction(final Operation key) { - return getKeyForAction(key.code); - } - - private int getKeyForAction(final short logicalAction) { - int action = console.getKeyForAction(logicalAction); - - if (action == -1) { - fail("Keystroke for logical action " + logicalAction + " was not bound in the console"); - } - - return action; - } - - protected class Buffer - { - private final ByteArrayOutputStream out = new ByteArrayOutputStream(); - - public Buffer() { - // nothing - } - - public Buffer(final String str) { - append(str); - } - - public byte[] getBytes() { - return out.toByteArray(); - } - - public Buffer op(final short operation) { - return append(getKeyForAction(operation)); - } - - public Buffer op(final Operation op) { - return op(op.code); - } - - public Buffer ctrlA() { - return append(getKeyForAction(Operation.MOVE_TO_BEG)); - } - - public Buffer ctrlU() { - return append(getKeyForAction(Operation.KILL_LINE_PREV)); - } - - public Buffer tab() { - return append(getKeyForAction(Operation.COMPLETE)); - } - - public Buffer back() { - return append(getKeyForAction(Operation.DELETE_PREV_CHAR)); - } - - public Buffer left() { - return append(ARROW_START.code).append(ARROW_PREFIX.code).append(ARROW_LEFT.code); - } - - public Buffer right() { - return append(ARROW_START.code).append(ARROW_PREFIX.code).append(ARROW_RIGHT.code); - } - - public Buffer up() { - return append(ARROW_START.code).append(ARROW_PREFIX.code).append(ARROW_UP.code); - } - - public Buffer down() { - return append(ARROW_START.code).append(ARROW_PREFIX.code).append(ARROW_DOWN.code); - } - - public Buffer append(final String str) { - for (byte b : str.getBytes()) { - append(b); - } - return this; - } - - public Buffer append(final int i) { - out.write((byte) i); - return this; - } - } -} diff --git a/src/jline/src/test/java/jline/console/EditLineTest.java b/src/jline/src/test/java/jline/console/EditLineTest.java deleted file mode 100644 index 3471ab4276..0000000000 --- a/src/jline/src/test/java/jline/console/EditLineTest.java +++ /dev/null @@ -1,172 +0,0 @@ -/* - * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. - * - * This software is distributable under the BSD license. See the terms of the - * BSD license in the documentation provided with this software. - */ -package jline.console; - -import org.junit.Test; - -import static jline.console.Operation.DELETE_PREV_WORD; -import static jline.console.Operation.MOVE_TO_END; -import static jline.console.Operation.PREV_WORD; - -/** - * Tests various features of editing lines. - * - * @author Marc Prud'hommeaux - */ -public class EditLineTest - extends ConsoleReaderTestSupport -{ - @Test - public void testDeletePreviousWord() throws Exception { - Buffer b = new Buffer("This is a test"); - - assertBuffer("This is a ", b = b.op(DELETE_PREV_WORD)); - assertBuffer("This is ", b = b.op(DELETE_PREV_WORD)); - assertBuffer("This ", b = b.op(DELETE_PREV_WORD)); - assertBuffer("", b = b.op(DELETE_PREV_WORD)); - assertBuffer("", b = b.op(DELETE_PREV_WORD)); - assertBuffer("", b = b.op(DELETE_PREV_WORD)); - } - - @Test - public void testMoveToEnd() throws Exception { - Buffer b = new Buffer("This is a test"); - - assertBuffer("This is a XtestX", - new Buffer("This is a test").op(PREV_WORD) - .append('X') - .op(MOVE_TO_END) - .append('X')); - - assertBuffer("This is Xa testX", - new Buffer("This is a test").op(PREV_WORD) - .op(PREV_WORD) - .append('X') - .op(MOVE_TO_END) - .append('X')); - - assertBuffer("This Xis a testX", - new Buffer("This is a test").op(PREV_WORD) - .op(PREV_WORD) - .op(PREV_WORD) - .append('X') - .op(MOVE_TO_END) - .append('X')); - } - - @Test - public void testPreviousWord() throws Exception { - assertBuffer("This is a Xtest", - new Buffer("This is a test").op(PREV_WORD) - .append('X')); - assertBuffer("This is Xa test", - new Buffer("This is a test").op(PREV_WORD) - .op(PREV_WORD) - .append('X')); - assertBuffer("This Xis a test", - new Buffer("This is a test").op(PREV_WORD) - .op(PREV_WORD) - .op(PREV_WORD) - .append('X')); - assertBuffer("XThis is a test", - new Buffer("This is a test").op(PREV_WORD) - .op(PREV_WORD) - .op(PREV_WORD) - .op(PREV_WORD) - .append('X')); - assertBuffer("XThis is a test", - new Buffer("This is a test").op(PREV_WORD) - .op(PREV_WORD) - .op(PREV_WORD) - .op(PREV_WORD) - .op(PREV_WORD) - .append('X')); - assertBuffer("XThis is a test", - new Buffer("This is a test").op(PREV_WORD) - .op(PREV_WORD) - .op(PREV_WORD) - .op(PREV_WORD) - .op(PREV_WORD) - .op(PREV_WORD) - .append('X')); - } - - @Test - public void testLineStart() throws Exception { - assertBuffer("XThis is a test", - new Buffer("This is a test").ctrlA().append('X')); - assertBuffer("TXhis is a test", - new Buffer("This is a test").ctrlA().right().append('X')); - } - - @Test - public void testClearLine() throws Exception { - assertBuffer("", new Buffer("This is a test").ctrlU()); - assertBuffer("t", new Buffer("This is a test").left().ctrlU()); - assertBuffer("st", new Buffer("This is a test").left().left().ctrlU()); - } - - @Test - public void testRight() throws Exception { - Buffer b = new Buffer("This is a test"); - b = b.left().right().back(); - assertBuffer("This is a tes", b); - b = b.left().left().left().right().left().back(); - assertBuffer("This is ates", b); - b.append('X'); - assertBuffer("This is aXtes", b); - } - - @Test - public void testLeft() throws Exception { - Buffer b = new Buffer("This is a test"); - b = b.left().left().left(); - assertBuffer("This is a est", b = b.back()); - assertBuffer("This is aest", b = b.back()); - assertBuffer("This is est", b = b.back()); - assertBuffer("This isest", b = b.back()); - assertBuffer("This iest", b = b.back()); - assertBuffer("This est", b = b.back()); - assertBuffer("Thisest", b = b.back()); - assertBuffer("Thiest", b = b.back()); - assertBuffer("Thest", b = b.back()); - assertBuffer("Test", b = b.back()); - assertBuffer("est", b = b.back()); - assertBuffer("est", b = b.back()); - assertBuffer("est", b = b.back()); - assertBuffer("est", b = b.back()); - assertBuffer("est", b = b.back()); - } - - @Test - public void testBackspace() throws Exception { - Buffer b = new Buffer("This is a test"); - assertBuffer("This is a tes", b = b.back()); - assertBuffer("This is a te", b = b.back()); - assertBuffer("This is a t", b = b.back()); - assertBuffer("This is a ", b = b.back()); - assertBuffer("This is a", b = b.back()); - assertBuffer("This is ", b = b.back()); - assertBuffer("This is", b = b.back()); - assertBuffer("This i", b = b.back()); - assertBuffer("This ", b = b.back()); - assertBuffer("This", b = b.back()); - assertBuffer("Thi", b = b.back()); - assertBuffer("Th", b = b.back()); - assertBuffer("T", b = b.back()); - assertBuffer("", b = b.back()); - assertBuffer("", b = b.back()); - assertBuffer("", b = b.back()); - assertBuffer("", b = b.back()); - assertBuffer("", b = b.back()); - } - - @Test - public void testBuffer() throws Exception { - assertBuffer("This is a test", new Buffer("This is a test")); - } -} diff --git a/src/jline/src/test/java/jline/console/completer/ArgumentCompleterTest.java b/src/jline/src/test/java/jline/console/completer/ArgumentCompleterTest.java deleted file mode 100644 index a74acf4adb..0000000000 --- a/src/jline/src/test/java/jline/console/completer/ArgumentCompleterTest.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (C) 2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package jline.console.completer; - -import jline.console.ConsoleReaderTestSupport; -import jline.console.completer.ArgumentCompleter; -import jline.console.completer.StringsCompleter; -import org.junit.Test; - -/** - * Tests for {@link jline.console.completer.ArgumentCompleter}. - * - * @author Marc Prud'hommeaux - */ -public class ArgumentCompleterTest - extends ConsoleReaderTestSupport -{ - @Test - public void test1() throws Exception { - console.addCompleter(new ArgumentCompleter(new StringsCompleter("foo", "bar", "baz"))); - - assertBuffer("foo foo ", new Buffer("foo f").tab()); - assertBuffer("foo ba", new Buffer("foo b").tab()); - assertBuffer("foo ba", new Buffer("foo ba").tab()); - assertBuffer("foo baz ", new Buffer("foo baz").tab()); - - // test completion in the mid range - assertBuffer("foo baz", new Buffer("f baz").left().left().left().left().tab()); - assertBuffer("ba foo", new Buffer("b foo").left().left().left().left().tab()); - assertBuffer("foo ba baz", new Buffer("foo b baz").left().left().left().left().tab()); - assertBuffer("foo foo baz", new Buffer("foo f baz").left().left().left().left().tab()); - } -} \ No newline at end of file diff --git a/src/jline/src/test/java/jline/console/completer/NullCompleterTest.java b/src/jline/src/test/java/jline/console/completer/NullCompleterTest.java deleted file mode 100644 index accdd801ac..0000000000 --- a/src/jline/src/test/java/jline/console/completer/NullCompleterTest.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (C) 2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package jline.console.completer; - -import jline.console.ConsoleReaderTestSupport; -import jline.console.completer.NullCompleter; -import org.junit.Test; - -/** - * Tests for {@link NullCompleter}. - * - * @author Jason Dillon - */ -public class NullCompleterTest - extends ConsoleReaderTestSupport -{ - @Test - public void test1() throws Exception { - console.addCompleter(NullCompleter.INSTANCE); - - assertBuffer("f", new Buffer("f").tab()); - assertBuffer("ba", new Buffer("ba").tab()); - assertBuffer("baz", new Buffer("baz").tab()); - } -} \ No newline at end of file diff --git a/src/jline/src/test/java/jline/console/completer/StringsCompleterTest.java b/src/jline/src/test/java/jline/console/completer/StringsCompleterTest.java deleted file mode 100644 index 6c7c982a25..0000000000 --- a/src/jline/src/test/java/jline/console/completer/StringsCompleterTest.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (C) 2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package jline.console.completer; - -import jline.console.ConsoleReaderTestSupport; -import jline.console.completer.StringsCompleter; -import org.junit.Test; - -/** - * Tests for {@link jline.console.completer.StringsCompleter}. - * - * @author Marc Prud'hommeaux - */ -public class StringsCompleterTest - extends ConsoleReaderTestSupport -{ - @Test - public void test1() throws Exception { - console.addCompleter(new StringsCompleter("foo", "bar", "baz")); - - assertBuffer("foo ", new Buffer("f").tab()); - // single tab completes to unambiguous "ba" - assertBuffer("ba", new Buffer("b").tab()); - assertBuffer("ba", new Buffer("ba").tab()); - assertBuffer("baz ", new Buffer("baz").tab()); - } -} \ No newline at end of file diff --git a/src/jline/src/test/java/jline/console/history/HistoryTest.java b/src/jline/src/test/java/jline/console/history/HistoryTest.java deleted file mode 100644 index c53107d984..0000000000 --- a/src/jline/src/test/java/jline/console/history/HistoryTest.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. - * - * This software is distributable under the BSD license. See the terms of the - * BSD license in the documentation provided with this software. - */ -package jline.console.history; - -import jline.console.ConsoleReaderTestSupport; -import org.junit.Test; - -import static jline.console.Operation.MOVE_TO_BEG; -import static jline.console.Operation.NEWLINE; -import static jline.console.Operation.NEXT_HISTORY; -import static jline.console.Operation.PREV_HISTORY; -import static jline.console.Operation.PREV_CHAR; - -/** - * Tests command history. - * - * @author Marc Prud'hommeaux - */ -public class HistoryTest - extends ConsoleReaderTestSupport -{ - @Test - public void testSingleHistory() throws Exception { - Buffer b = new Buffer(). - append("test line 1").op(NEWLINE). - append("test line 2").op(NEWLINE). - append("test line 3").op(NEWLINE). - append("test line 4").op(NEWLINE). - append("test line 5").op(NEWLINE). - append(""); - - assertBuffer("", b); - - assertBuffer("test line 5", b = b.op(PREV_HISTORY)); - assertBuffer("test line 5", b = b.op(PREV_CHAR)); - assertBuffer("test line 4", b = b.op(PREV_HISTORY)); - assertBuffer("test line 5", b = b.op(NEXT_HISTORY)); - assertBuffer("test line 4", b = b.op(PREV_HISTORY)); - assertBuffer("test line 3", b = b.op(PREV_HISTORY)); - assertBuffer("test line 2", b = b.op(PREV_HISTORY)); - assertBuffer("test line 1", b = b.op(PREV_HISTORY)); - - // beginning of history - assertBuffer("test line 1", b = b.op(PREV_HISTORY)); - assertBuffer("test line 1", b = b.op(PREV_HISTORY)); - assertBuffer("test line 1", b = b.op(PREV_HISTORY)); - assertBuffer("test line 1", b = b.op(PREV_HISTORY)); - - assertBuffer("test line 2", b = b.op(NEXT_HISTORY)); - assertBuffer("test line 3", b = b.op(NEXT_HISTORY)); - assertBuffer("test line 4", b = b.op(NEXT_HISTORY)); - assertBuffer("test line 5", b = b.op(NEXT_HISTORY)); - - // end of history - assertBuffer("", b = b.op(NEXT_HISTORY)); - assertBuffer("", b = b.op(NEXT_HISTORY)); - assertBuffer("", b = b.op(NEXT_HISTORY)); - - assertBuffer("test line 5", b = b.op(PREV_HISTORY)); - assertBuffer("test line 4", b = b.op(PREV_HISTORY)); - b = b.op(MOVE_TO_BEG).append("XXX").op(NEWLINE); - assertBuffer("XXXtest line 4", b = b.op(PREV_HISTORY)); - assertBuffer("test line 5", b = b.op(PREV_HISTORY)); - assertBuffer("test line 4", b = b.op(PREV_HISTORY)); - assertBuffer("test line 5", b = b.op(NEXT_HISTORY)); - assertBuffer("XXXtest line 4", b = b.op(NEXT_HISTORY)); - assertBuffer("", b = b.op(NEXT_HISTORY)); - - assertBuffer("XXXtest line 4", b = b.op(PREV_HISTORY)); - assertBuffer("XXXtest line 4", b = b.op(NEWLINE).op(PREV_HISTORY)); - assertBuffer("XXXtest line 4", b = b.op(NEWLINE).op(PREV_HISTORY)); - assertBuffer("XXXtest line 4", b = b.op(NEWLINE).op(PREV_HISTORY)); - assertBuffer("XXXtest line 4", b = b.op(NEWLINE).op(PREV_HISTORY)); - } -} diff --git a/src/jline/src/test/java/jline/console/history/MemoryHistoryTest.java b/src/jline/src/test/java/jline/console/history/MemoryHistoryTest.java deleted file mode 100644 index 6e5c731a80..0000000000 --- a/src/jline/src/test/java/jline/console/history/MemoryHistoryTest.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Copyright (C) 2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package jline.console.history; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import static junit.framework.Assert.*; - -/** - * Tests for {@link MemoryHistory}. - * - * @author Marc Prud'hommeaux - */ -public class MemoryHistoryTest -{ - private MemoryHistory history; - - @Before - public void setUp() { - history = new MemoryHistory(); - } - - @After - public void tearDown() { - history = null; - } - - @Test - public void testAdd() { - assertEquals(0, history.size()); - - history.add("test"); - - assertEquals(1, history.size()); - assertEquals("test", history.get(0)); - assertEquals(1, history.index()); - } - - private void assertHistoryContains(final int offset, final String... items) { - assertEquals(items.length, history.size()); - int i=0; - for (History.Entry entry : history) { - assertEquals(offset + i, entry.index()); - assertEquals(items[i++], entry.value()); - } - } - - @Test - public void testOffset() { - history.setMaxSize(5); - - assertEquals(0, history.size()); - assertEquals(0, history.index()); - - history.add("a"); - history.add("b"); - history.add("c"); - history.add("d"); - history.add("e"); - - assertEquals(5, history.size()); - assertEquals(5, history.index()); - assertHistoryContains(0, "a", "b", "c", "d", "e"); - - history.add("f"); - - assertEquals(5, history.size()); - assertEquals(6, history.index()); - - assertHistoryContains(1, "b", "c", "d", "e", "f"); - assertEquals("f", history.get(5)); - } - - @Test - public void testReplace() { - assertEquals(0, history.size()); - - history.add("a"); - history.add("b"); - history.replace("c"); - - assertHistoryContains(0, "a", "c"); - } -} \ No newline at end of file diff --git a/src/jline/src/test/java/jline/example/Example.java b/src/jline/src/test/java/jline/example/Example.java deleted file mode 100644 index 84aee10af3..0000000000 --- a/src/jline/src/test/java/jline/example/Example.java +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Copyright (c) 2002-2006, Marc Prud'hommeaux. All rights reserved. - * - * This software is distributable under the BSD license. See the terms of the - * BSD license in the documentation provided with this software. - */ -package jline.example; - -import jline.console.completer.*; -import jline.console.ConsoleReader; - -import java.io.*; -import java.util.*; - -public class Example -{ - public static void usage() { - System.out.println("Usage: java " + Example.class.getName() - + " [none/simple/files/dictionary [trigger mask]]"); - System.out.println(" none - no completors"); - System.out.println(" simple - a simple completor that comples " - + "\"foo\", \"bar\", and \"baz\""); - System.out - .println(" files - a completor that comples " + "file names"); - System.out.println(" classes - a completor that comples " - + "java class names"); - System.out - .println(" trigger - a special word which causes it to assume " - + "the next line is a password"); - System.out.println(" mask - is the character to print in place of " - + "the actual password character"); - System.out.println(" color - colored prompt and feedback"); - System.out.println("\n E.g - java Example simple su '*'\n" - + "will use the simple compleator with 'su' triggering\n" - + "the use of '*' as a password mask."); - } - - public static void main(String[] args) throws IOException { - Character mask = null; - String trigger = null; - boolean color = false; - - ConsoleReader reader = new ConsoleReader(); - - reader.setBellEnabled(false); - reader.setPrompt("prompt> "); - - if ((args == null) || (args.length == 0)) { - usage(); - - return; - } - - List completors = new LinkedList(); - - if (args.length > 0) { - if (args[0].equals("none")) { - } - else if (args[0].equals("files")) { - completors.add(new FileNameCompleter()); - } - else if (args[0].equals("simple")) { - completors.add(new StringsCompleter("foo", "bar", "baz")); - } - else if (args[0].equals("color")) { - color = true; - reader.setPrompt("\u001B[1mfoo\u001B[0m@bar\u001B[32m@baz\u001B[0m> "); - } - else { - usage(); - - return; - } - } - - if (args.length == 3) { - mask = args[2].charAt(0); - trigger = args[1]; - } - - for (Completer c : completors) { - reader.addCompleter(c); - } - - String line; - PrintWriter out = new PrintWriter( - reader.getTerminal().wrapOutIfNeeded(System.out)); - - while ((line = reader.readLine()) != null) { - if (color){ - out.println("\u001B[33m======>\u001B[0m\"" + line + "\""); - } else { - out.println("======>\"" + line + "\""); - } - out.flush(); - - // If we input the special word then we will mask - // the next line. - if ((trigger != null) && (line.compareTo(trigger) == 0)) { - line = reader.readLine("password> ", mask); - } - if (line.equalsIgnoreCase("quit") || line.equalsIgnoreCase("exit")) { - break; - } - } - } -} diff --git a/src/jline/src/test/java/jline/internal/TerminalLineSettingsTest.java b/src/jline/src/test/java/jline/internal/TerminalLineSettingsTest.java deleted file mode 100644 index 80b7f0be0b..0000000000 --- a/src/jline/src/test/java/jline/internal/TerminalLineSettingsTest.java +++ /dev/null @@ -1,146 +0,0 @@ -package jline.internal; - -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * Tests for the {@link TerminalLineSettings}. - * - * @author Jean-Baptiste Onofré - */ -public class TerminalLineSettingsTest -{ - private TerminalLineSettings settings; - - private final String linuxSttySample = "speed 38400 baud; rows 85; columns 244; line = 0;\n" + - "intr = ^C; quit = ^\\; erase = ^?; kill = ^U; eof = ^D; eol = M-^?; eol2 = M-^?; swtch = M-^?; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;\n" + - "-parenb -parodd cs8 hupcl -cstopb cread -clocal -crtscts\n" + - "-ignbrk brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc ixany imaxbel iutf8\n" + - "opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0\n" + - "isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke"; - - private final String solarisSttySample = "speed 38400 baud; \n" + - "rows = 85; columns = 244; ypixels = 0; xpixels = 0;\n" + - "csdata ?\n" + - "eucw 1:0:0:0, scrw 1:0:0:0\n" + - "intr = ^c; quit = ^\\; erase = ^?; kill = ^u;\n" + - "eof = ^d; eol = -^?; eol2 = -^?; swtch = ;\n" + - "start = ^q; stop = ^s; susp = ^z; dsusp = ^y;\n" + - "rprnt = ^r; flush = ^o; werase = ^w; lnext = ^v;\n" + - "-parenb -parodd cs8 -cstopb -hupcl cread -clocal -loblk -crtscts -crtsxoff -parext \n" + - "-ignbrk brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl -iuclc \n" + - "ixon ixany -ixoff imaxbel \n" + - "isig icanon -xcase echo echoe echok -echonl -noflsh \n" + - "-tostop echoctl -echoprt echoke -defecho -flusho -pendin iexten \n" + - "opost -olcuc onlcr -ocrnl -onocr -onlret -ofill -ofdel tab3"; - - private final String aixSttySample = "speed 38400 baud; 85 rows; 244 columns;\n" + - "eucw 1:1:0:0, scrw 1:1:0:0:\n" + - "intr = ^C; quit = ^\\; erase = ^?; kill = ^U; eof = ^D; eol = \n" + - "eol2 = ; start = ^Q; stop = ^S; susp = ^Z; dsusp = ^Y; reprint = ^R\n" + - "discard = ^O; werase = ^W; lnext = ^V\n" + - "-parenb -parodd cs8 -cstopb -hupcl cread -clocal -parext \n" + - "-ignbrk brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl -iuclc \n" + - "ixon ixany -ixoff imaxbel \n" + - "isig icanon -xcase echo echoe echok -echonl -noflsh \n" + - "-tostop echoctl -echoprt echoke -flusho -pending iexten \n" + - "opost -olcuc onlcr -ocrnl -onocr -onlret -ofill -ofdel tab3"; - - private final String macOsSttySample = "speed 9600 baud; 47 rows; 155 columns;\n" + - "lflags: icanon isig iexten echo echoe -echok echoke -echonl echoctl\n" + - "-echoprt -altwerase -noflsh -tostop -flusho pendin -nokerninfo\n" + - "-extproc\n" + - "iflags: -istrip icrnl -inlcr -igncr ixon -ixoff ixany imaxbel iutf8\n" + - "-ignbrk brkint -inpck -ignpar -parmrk\n" + - "oflags: opost onlcr -oxtabs -onocr -onlret\n" + - "cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -dsrflow\n" + - "-dtrflow -mdmbuf\n" + - "cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = ;\n" + - "eol2 = ; erase = ^?; intr = ^C; kill = ^U; lnext = ^V;\n" + - "min = 1; quit = ^\\; reprint = ^R; start = ^Q; status = ^T;\n" + - "stop = ^S; susp = ^Z; time = 0; werase = ^W;"; - - private final String netBsdSttySample = "speed 38400 baud; 85 rows; 244 columns;\n" + - "lflags: icanon isig iexten echo echoe echok echoke -echonl echoctl\n" + - " -echoprt -altwerase -noflsh -tostop -flusho pendin -nokerninfo\n" + - " -extproc\n" + - "iflags: -istrip icrnl -inlcr -igncr ixon -ixoff ixany imaxbel -ignbrk\n" + - " brkint -inpck -ignpar -parmrk\n" + - "oflags: opost onlcr -ocrnl oxtabs onocr onlret\n" + - "cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -mdmbuf\n" + - " -cdtrcts\n" + - "cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = ;\n" + - " eol2 = ; erase = ^?; intr = ^C; kill = ^U; lnext = ^V;\n" + - " min = 1; quit = ^\\; reprint = ^R; start = ^Q; status = ^T;\n" + - " stop = ^S; susp = ^Z; time = 0; werase = ^W;"; - - private final String freeBsdSttySample = "speed 9600 baud; 32 rows; 199 columns;\n" + - "lflags: icanon isig iexten echo echoe echok echoke -echonl echoctl\n" + - " -echoprt -altwerase -noflsh -tostop -flusho -pendin -nokerninfo\n" + - " -extproc\n" + - "iflags: -istrip icrnl -inlcr -igncr ixon -ixoff ixany imaxbel -ignbrk\n" + - " brkint -inpck -ignpar -parmrk\n" + - "oflags: opost onlcr -ocrnl tab0 -onocr -onlret\n" + - "cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -dsrflow\n" + - " -dtrflow -mdmbuf\n" + - "cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = ;\n" + - " eol2 = ; erase = ^?; erase2 = ^H; intr = ^C; kill = ^U;\n" + - " lnext = ^V; min = 1; quit = ^\\; reprint = ^R; start = ^Q;\n" + - " status = ^T; stop = ^S; susp = ^Z; time = 0; werase = ^W;"; - - @Before - public void setUp() throws Exception { - settings = new TerminalLineSettings(); - } - - @Test - public void testGetConfig() { - String config = settings.getConfig(); - System.out.println(config); - } - - @Test - public void testLinuxSttyParsing() { - assertEquals(0x7f, settings.getProperty("erase", linuxSttySample)); - assertEquals(244, settings.getProperty("columns", linuxSttySample)); - assertEquals(85, settings.getProperty("rows", linuxSttySample)); - } - - @Test - public void testSolarisSttyParsing() { - assertEquals(0x7f, settings.getProperty("erase", solarisSttySample)); - assertEquals(244, settings.getProperty("columns", solarisSttySample)); - assertEquals(85, settings.getProperty("rows", solarisSttySample)); - } - - @Test - public void testAixSttyParsing() { - assertEquals(0x7f, settings.getProperty("erase", aixSttySample)); - assertEquals(244, settings.getProperty("columns", aixSttySample)); - assertEquals(85, settings.getProperty("rows", aixSttySample)); - } - - @Test - public void testMacOsSttyParsing() { - assertEquals(0x7f, settings.getProperty("erase", macOsSttySample)); - assertEquals(155, settings.getProperty("columns", macOsSttySample)); - assertEquals(47, settings.getProperty("rows", macOsSttySample)); - } - - @Test - public void testNetBsdSttyParsing() { - assertEquals(0x7f, settings.getProperty("erase", netBsdSttySample)); - assertEquals(244, settings.getProperty("columns", netBsdSttySample)); - assertEquals(85, settings.getProperty("rows", netBsdSttySample)); - } - - @Test - public void testFreeBsdSttyParsing() { - assertEquals(0x7f, settings.getProperty("erase", freeBsdSttySample)); - assertEquals(199, settings.getProperty("columns", freeBsdSttySample)); - assertEquals(32, settings.getProperty("rows", freeBsdSttySample)); - } - -} \ No newline at end of file diff --git a/src/jline/src/test/java/scala/tools/jline/TerminalFactoryTest.java b/src/jline/src/test/java/scala/tools/jline/TerminalFactoryTest.java new file mode 100644 index 0000000000..c0c070bdfd --- /dev/null +++ b/src/jline/src/test/java/scala/tools/jline/TerminalFactoryTest.java @@ -0,0 +1,34 @@ +package scala.tools.jline; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +/** + * Tests for the {@link TerminalFactory}. + */ +public class TerminalFactoryTest +{ + @Before + public void setUp() throws Exception { + TerminalFactory.reset(); + } + + @Test + public void testConfigureNone() { + TerminalFactory.configure(TerminalFactory.NONE); + Terminal t = TerminalFactory.get(); + assertNotNull(t); + assertEquals(UnsupportedTerminal.class.getName(), t.getClass().getName()); + } + + @Test + public void testConfigureUnsupportedTerminal() { + TerminalFactory.configure(UnsupportedTerminal.class.getName()); + Terminal t = TerminalFactory.get(); + assertNotNull(t); + assertEquals(UnsupportedTerminal.class.getName(), t.getClass().getName()); + } +} \ No newline at end of file diff --git a/src/jline/src/test/java/scala/tools/jline/console/ConsoleReaderTest.java b/src/jline/src/test/java/scala/tools/jline/console/ConsoleReaderTest.java new file mode 100644 index 0000000000..0e6cba15a0 --- /dev/null +++ b/src/jline/src/test/java/scala/tools/jline/console/ConsoleReaderTest.java @@ -0,0 +1,261 @@ +package scala.tools.jline.console; + +import scala.tools.jline.TerminalFactory; +import scala.tools.jline.WindowsTerminal; +import scala.tools.jline.console.history.History; +import scala.tools.jline.console.history.MemoryHistory; +import org.junit.Before; +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.io.StringWriter; +import java.io.Writer; + +import static scala.tools.jline.WindowsTerminal.WindowsKey.DELETE_KEY; +import static scala.tools.jline.WindowsTerminal.WindowsKey.END_KEY; +import static scala.tools.jline.WindowsTerminal.WindowsKey.ESCAPE_KEY; +import static scala.tools.jline.WindowsTerminal.WindowsKey.HOME_KEY; +import static scala.tools.jline.WindowsTerminal.WindowsKey.INSERT_KEY; +import static scala.tools.jline.WindowsTerminal.WindowsKey.LEFT_ARROW_KEY; +import static scala.tools.jline.WindowsTerminal.WindowsKey.NUMPAD_KEY_INDICATOR; +import static scala.tools.jline.WindowsTerminal.WindowsKey.PAGE_DOWN_KEY; +import static scala.tools.jline.WindowsTerminal.WindowsKey.PAGE_UP_KEY; +import static scala.tools.jline.WindowsTerminal.WindowsKey.SPECIAL_KEY_INDICATOR; +import static scala.tools.jline.console.Operation.DELETE_NEXT_CHAR; +import static scala.tools.jline.console.Operation.DELETE_PREV_CHAR; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +/** + * Tests for the {@link ConsoleReader}. + */ +public class ConsoleReaderTest +{ + @Before + public void setUp() throws Exception { + System.setProperty(WindowsTerminal.JLINE_WINDOWS_TERMINAL_DIRECT_CONSOLE, "false"); + } + + private void assertWindowsKeyBehavior(String expected, char[] input) throws Exception { + StringBuilder buffer = new StringBuilder(); + buffer.append(input); + ConsoleReader reader = createConsole(buffer.toString().getBytes()); + assertNotNull(reader); + String line = reader.readLine(); + assertEquals(expected, line); + } + + private ConsoleReader createConsole(byte[] bytes) throws Exception { + InputStream in = new ByteArrayInputStream(bytes); + Writer writer = new StringWriter(); + ConsoleReader reader = new ConsoleReader(in, writer); + reader.setHistory(createSeededHistory()); + return reader; + } + + private History createSeededHistory() { + History history = new MemoryHistory(); + history.add("dir"); + history.add("cd c:\\"); + history.add("mkdir monkey"); + return history; + } + + @Test + public void testDeleteAndBackspaceKeymappings() throws Exception { + // test only works on Windows + if (!(TerminalFactory.get() instanceof WindowsTerminal)) { + return; + } + + ConsoleReader consoleReader = new ConsoleReader(); + assertNotNull(consoleReader); + assertEquals(127, consoleReader.getKeyForAction(DELETE_NEXT_CHAR)); + assertEquals(8, consoleReader.getKeyForAction(DELETE_PREV_CHAR)); + } + + @Test + public void testReadline() throws Exception { + ConsoleReader consoleReader = createConsole("Sample String\r\n".getBytes()); + assertNotNull(consoleReader); + String line = consoleReader.readLine(); + assertEquals("Sample String", line); + } + + @Test + public void testDeleteOnWindowsTerminal() throws Exception { + // test only works on Windows + if (!(TerminalFactory.get() instanceof WindowsTerminal)) { + return; + } + + char[] characters = new char[]{ + 'S', 's', + (char) SPECIAL_KEY_INDICATOR.code, + (char) LEFT_ARROW_KEY.code, + (char) SPECIAL_KEY_INDICATOR.code, + (char) DELETE_KEY.code, '\r', 'n' + }; + assertWindowsKeyBehavior("S", characters); + } + + @Test + public void testNumpadDeleteOnWindowsTerminal() throws Exception { + // test only works on Windows + if (!(TerminalFactory.get() instanceof WindowsTerminal)) { + return; + } + + char[] characters = new char[]{ + 'S', 's', + (char) NUMPAD_KEY_INDICATOR.code, + (char) LEFT_ARROW_KEY.code, + (char) NUMPAD_KEY_INDICATOR.code, + (char) DELETE_KEY.code, '\r', 'n' + }; + assertWindowsKeyBehavior("S", characters); + } + + @Test + public void testHomeKeyOnWindowsTerminal() throws Exception { + // test only works on Windows + if (!(TerminalFactory.get() instanceof WindowsTerminal)) { + return; + } + + char[] characters = new char[]{ + 'S', 's', + (char) SPECIAL_KEY_INDICATOR.code, + (char) HOME_KEY.code, 'x', '\r', '\n' + }; + assertWindowsKeyBehavior("xSs", characters); + + } + + @Test + public void testEndKeyOnWindowsTerminal() throws Exception { + // test only works on Windows + if (!(TerminalFactory.get() instanceof WindowsTerminal)) { + return; + } + + char[] characters = new char[]{ + 'S', 's', + (char) SPECIAL_KEY_INDICATOR.code, + (char) HOME_KEY.code, 'x', + (char) SPECIAL_KEY_INDICATOR.code, (char) END_KEY.code, + 'j', '\r', '\n' + }; + assertWindowsKeyBehavior("xSsj", characters); + } + + @Test + public void testPageUpOnWindowsTerminal() throws Exception { + // test only works on Windows + if (!(TerminalFactory.get() instanceof WindowsTerminal)) { + return; + } + + char[] characters = new char[]{ + (char) SPECIAL_KEY_INDICATOR.code, + (char) PAGE_UP_KEY.code, '\r', '\n' + }; + assertWindowsKeyBehavior("dir", characters); + } + + @Test + public void testPageDownOnWindowsTerminal() throws Exception { + // test only works on Windows + if (!(TerminalFactory.get() instanceof WindowsTerminal)) { + return; + } + + char[] characters = new char[]{ + (char) SPECIAL_KEY_INDICATOR.code, + (char) PAGE_DOWN_KEY.code, '\r', '\n' + }; + assertWindowsKeyBehavior("mkdir monkey", characters); + } + + @Test + public void testEscapeOnWindowsTerminal() throws Exception { + // test only works on Windows + if (!(TerminalFactory.get() instanceof WindowsTerminal)) { + return; + } + + char[] characters = new char[]{ + 's', 's', 's', + (char) SPECIAL_KEY_INDICATOR.code, + (char) ESCAPE_KEY.code, '\r', '\n' + }; + assertWindowsKeyBehavior("", characters); + } + + @Test + public void testInsertOnWindowsTerminal() throws Exception { + // test only works on Windows + if (!(TerminalFactory.get() instanceof WindowsTerminal)) { + return; + } + + char[] characters = new char[]{ + 'o', 'p', 's', + (char) SPECIAL_KEY_INDICATOR.code, + (char) HOME_KEY.code, + (char) SPECIAL_KEY_INDICATOR.code, + (char) INSERT_KEY.code, 'o', 'o', 'p', 's', '\r', '\n' + }; + assertWindowsKeyBehavior("oops", characters); + } + + @Test + public void testExpansion() throws Exception { + ConsoleReader reader = new ConsoleReader(); + MemoryHistory history = new MemoryHistory(); + history.setMaxSize(3); + history.add("foo"); + history.add("dir"); + history.add("cd c:\\"); + history.add("mkdir monkey"); + reader.setHistory(history); + + assertEquals("echo a!", reader.expandEvents("echo a!")); + assertEquals("mkdir monkey ; echo a!", reader.expandEvents("!! ; echo a!")); + assertEquals("echo ! a", reader.expandEvents("echo ! a")); + assertEquals("echo !\ta", reader.expandEvents("echo !\ta")); + + assertEquals("mkdir barey", reader.expandEvents("^monk^bar^")); + assertEquals("mkdir barey", reader.expandEvents("^monk^bar")); + assertEquals("a^monk^bar", reader.expandEvents("a^monk^bar")); + + assertEquals("mkdir monkey", reader.expandEvents("!!")); + assertEquals("echo echo a", reader.expandEvents("echo !#a")); + + assertEquals("mkdir monkey", reader.expandEvents("!mk")); + try { + reader.expandEvents("!mz"); + } catch (IllegalArgumentException e) { + assertEquals("!mz: event not found", e.getMessage()); + } + + assertEquals("mkdir monkey", reader.expandEvents("!?mo")); + assertEquals("mkdir monkey", reader.expandEvents("!?mo?")); + + assertEquals("mkdir monkey", reader.expandEvents("!-1")); + assertEquals("cd c:\\", reader.expandEvents("!-2")); + assertEquals("cd c:\\", reader.expandEvents("!2")); + assertEquals("mkdir monkey", reader.expandEvents("!3")); + try { + reader.expandEvents("!20"); + } catch (IllegalArgumentException e) { + assertEquals("!20: event not found", e.getMessage()); + } + try { + reader.expandEvents("!-20"); + } catch (IllegalArgumentException e) { + assertEquals("!-20: event not found", e.getMessage()); + } + } +} diff --git a/src/jline/src/test/java/scala/tools/jline/console/ConsoleReaderTestSupport.java b/src/jline/src/test/java/scala/tools/jline/console/ConsoleReaderTestSupport.java new file mode 100644 index 0000000000..04e98e5bcb --- /dev/null +++ b/src/jline/src/test/java/scala/tools/jline/console/ConsoleReaderTestSupport.java @@ -0,0 +1,142 @@ +/* + * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. + * + * This software is distributable under the BSD license. See the terms of the + * BSD license in the documentation provided with this software. + */ +package scala.tools.jline.console; + +import scala.tools.jline.UnixTerminal; +import org.junit.Before; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; + +import static scala.tools.jline.UnixTerminal.UnixKey.ARROW_DOWN; +import static scala.tools.jline.UnixTerminal.UnixKey.ARROW_LEFT; +import static scala.tools.jline.UnixTerminal.UnixKey.ARROW_PREFIX; +import static scala.tools.jline.UnixTerminal.UnixKey.ARROW_RIGHT; +import static scala.tools.jline.UnixTerminal.UnixKey.ARROW_START; +import static scala.tools.jline.UnixTerminal.UnixKey.ARROW_UP; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +/** + * Provides support for console reader tests. + */ +public abstract class ConsoleReaderTestSupport +{ + protected ConsoleReader console; + + @Before + public void setUp() throws Exception { + console = new ConsoleReader(null, new PrintWriter(new OutputStreamWriter(new ByteArrayOutputStream())), new UnixTerminal()); + } + + protected void assertBuffer(final String expected, final Buffer buffer) throws IOException { + assertBuffer(expected, buffer, true); + } + + protected void assertBuffer(final String expected, final Buffer buffer, final boolean clear) throws IOException { + // clear current buffer, if any + if (clear) { + console.finishBuffer(); + console.getHistory().clear(); + } + + console.setInput(new ByteArrayInputStream(buffer.getBytes())); + + // run it through the reader + while (console.readLine((String) null) != null) { + // ignore + } + + assertEquals(expected, console.getCursorBuffer().toString()); + } + + private int getKeyForAction(final Operation key) { + return getKeyForAction(key.code); + } + + private int getKeyForAction(final short logicalAction) { + int action = console.getKeyForAction(logicalAction); + + if (action == -1) { + fail("Keystroke for logical action " + logicalAction + " was not bound in the console"); + } + + return action; + } + + protected class Buffer + { + private final ByteArrayOutputStream out = new ByteArrayOutputStream(); + + public Buffer() { + // nothing + } + + public Buffer(final String str) { + append(str); + } + + public byte[] getBytes() { + return out.toByteArray(); + } + + public Buffer op(final short operation) { + return append(getKeyForAction(operation)); + } + + public Buffer op(final Operation op) { + return op(op.code); + } + + public Buffer ctrlA() { + return append(getKeyForAction(Operation.MOVE_TO_BEG)); + } + + public Buffer ctrlU() { + return append(getKeyForAction(Operation.KILL_LINE_PREV)); + } + + public Buffer tab() { + return append(getKeyForAction(Operation.COMPLETE)); + } + + public Buffer back() { + return append(getKeyForAction(Operation.DELETE_PREV_CHAR)); + } + + public Buffer left() { + return append(ARROW_START.code).append(ARROW_PREFIX.code).append(ARROW_LEFT.code); + } + + public Buffer right() { + return append(ARROW_START.code).append(ARROW_PREFIX.code).append(ARROW_RIGHT.code); + } + + public Buffer up() { + return append(ARROW_START.code).append(ARROW_PREFIX.code).append(ARROW_UP.code); + } + + public Buffer down() { + return append(ARROW_START.code).append(ARROW_PREFIX.code).append(ARROW_DOWN.code); + } + + public Buffer append(final String str) { + for (byte b : str.getBytes()) { + append(b); + } + return this; + } + + public Buffer append(final int i) { + out.write((byte) i); + return this; + } + } +} diff --git a/src/jline/src/test/java/scala/tools/jline/console/EditLineTest.java b/src/jline/src/test/java/scala/tools/jline/console/EditLineTest.java new file mode 100644 index 0000000000..be70979563 --- /dev/null +++ b/src/jline/src/test/java/scala/tools/jline/console/EditLineTest.java @@ -0,0 +1,172 @@ +/* + * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. + * + * This software is distributable under the BSD license. See the terms of the + * BSD license in the documentation provided with this software. + */ +package scala.tools.jline.console; + +import org.junit.Test; + +import static scala.tools.jline.console.Operation.DELETE_PREV_WORD; +import static scala.tools.jline.console.Operation.MOVE_TO_END; +import static scala.tools.jline.console.Operation.PREV_WORD; + +/** + * Tests various features of editing lines. + * + * @author Marc Prud'hommeaux + */ +public class EditLineTest + extends ConsoleReaderTestSupport +{ + @Test + public void testDeletePreviousWord() throws Exception { + Buffer b = new Buffer("This is a test"); + + assertBuffer("This is a ", b = b.op(DELETE_PREV_WORD)); + assertBuffer("This is ", b = b.op(DELETE_PREV_WORD)); + assertBuffer("This ", b = b.op(DELETE_PREV_WORD)); + assertBuffer("", b = b.op(DELETE_PREV_WORD)); + assertBuffer("", b = b.op(DELETE_PREV_WORD)); + assertBuffer("", b = b.op(DELETE_PREV_WORD)); + } + + @Test + public void testMoveToEnd() throws Exception { + Buffer b = new Buffer("This is a test"); + + assertBuffer("This is a XtestX", + new Buffer("This is a test").op(PREV_WORD) + .append('X') + .op(MOVE_TO_END) + .append('X')); + + assertBuffer("This is Xa testX", + new Buffer("This is a test").op(PREV_WORD) + .op(PREV_WORD) + .append('X') + .op(MOVE_TO_END) + .append('X')); + + assertBuffer("This Xis a testX", + new Buffer("This is a test").op(PREV_WORD) + .op(PREV_WORD) + .op(PREV_WORD) + .append('X') + .op(MOVE_TO_END) + .append('X')); + } + + @Test + public void testPreviousWord() throws Exception { + assertBuffer("This is a Xtest", + new Buffer("This is a test").op(PREV_WORD) + .append('X')); + assertBuffer("This is Xa test", + new Buffer("This is a test").op(PREV_WORD) + .op(PREV_WORD) + .append('X')); + assertBuffer("This Xis a test", + new Buffer("This is a test").op(PREV_WORD) + .op(PREV_WORD) + .op(PREV_WORD) + .append('X')); + assertBuffer("XThis is a test", + new Buffer("This is a test").op(PREV_WORD) + .op(PREV_WORD) + .op(PREV_WORD) + .op(PREV_WORD) + .append('X')); + assertBuffer("XThis is a test", + new Buffer("This is a test").op(PREV_WORD) + .op(PREV_WORD) + .op(PREV_WORD) + .op(PREV_WORD) + .op(PREV_WORD) + .append('X')); + assertBuffer("XThis is a test", + new Buffer("This is a test").op(PREV_WORD) + .op(PREV_WORD) + .op(PREV_WORD) + .op(PREV_WORD) + .op(PREV_WORD) + .op(PREV_WORD) + .append('X')); + } + + @Test + public void testLineStart() throws Exception { + assertBuffer("XThis is a test", + new Buffer("This is a test").ctrlA().append('X')); + assertBuffer("TXhis is a test", + new Buffer("This is a test").ctrlA().right().append('X')); + } + + @Test + public void testClearLine() throws Exception { + assertBuffer("", new Buffer("This is a test").ctrlU()); + assertBuffer("t", new Buffer("This is a test").left().ctrlU()); + assertBuffer("st", new Buffer("This is a test").left().left().ctrlU()); + } + + @Test + public void testRight() throws Exception { + Buffer b = new Buffer("This is a test"); + b = b.left().right().back(); + assertBuffer("This is a tes", b); + b = b.left().left().left().right().left().back(); + assertBuffer("This is ates", b); + b.append('X'); + assertBuffer("This is aXtes", b); + } + + @Test + public void testLeft() throws Exception { + Buffer b = new Buffer("This is a test"); + b = b.left().left().left(); + assertBuffer("This is a est", b = b.back()); + assertBuffer("This is aest", b = b.back()); + assertBuffer("This is est", b = b.back()); + assertBuffer("This isest", b = b.back()); + assertBuffer("This iest", b = b.back()); + assertBuffer("This est", b = b.back()); + assertBuffer("Thisest", b = b.back()); + assertBuffer("Thiest", b = b.back()); + assertBuffer("Thest", b = b.back()); + assertBuffer("Test", b = b.back()); + assertBuffer("est", b = b.back()); + assertBuffer("est", b = b.back()); + assertBuffer("est", b = b.back()); + assertBuffer("est", b = b.back()); + assertBuffer("est", b = b.back()); + } + + @Test + public void testBackspace() throws Exception { + Buffer b = new Buffer("This is a test"); + assertBuffer("This is a tes", b = b.back()); + assertBuffer("This is a te", b = b.back()); + assertBuffer("This is a t", b = b.back()); + assertBuffer("This is a ", b = b.back()); + assertBuffer("This is a", b = b.back()); + assertBuffer("This is ", b = b.back()); + assertBuffer("This is", b = b.back()); + assertBuffer("This i", b = b.back()); + assertBuffer("This ", b = b.back()); + assertBuffer("This", b = b.back()); + assertBuffer("Thi", b = b.back()); + assertBuffer("Th", b = b.back()); + assertBuffer("T", b = b.back()); + assertBuffer("", b = b.back()); + assertBuffer("", b = b.back()); + assertBuffer("", b = b.back()); + assertBuffer("", b = b.back()); + assertBuffer("", b = b.back()); + } + + @Test + public void testBuffer() throws Exception { + assertBuffer("This is a test", new Buffer("This is a test")); + } +} diff --git a/src/jline/src/test/java/scala/tools/jline/console/completer/ArgumentCompleterTest.java b/src/jline/src/test/java/scala/tools/jline/console/completer/ArgumentCompleterTest.java new file mode 100644 index 0000000000..9e2a2ab031 --- /dev/null +++ b/src/jline/src/test/java/scala/tools/jline/console/completer/ArgumentCompleterTest.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package scala.tools.jline.console.completer; + +import scala.tools.jline.console.ConsoleReaderTestSupport; +import scala.tools.jline.console.completer.ArgumentCompleter; +import scala.tools.jline.console.completer.StringsCompleter; +import org.junit.Test; + +/** + * Tests for {@link jline.console.completer.ArgumentCompleter}. + * + * @author Marc Prud'hommeaux + */ +public class ArgumentCompleterTest + extends ConsoleReaderTestSupport +{ + @Test + public void test1() throws Exception { + console.addCompleter(new ArgumentCompleter(new StringsCompleter("foo", "bar", "baz"))); + + assertBuffer("foo foo ", new Buffer("foo f").tab()); + assertBuffer("foo ba", new Buffer("foo b").tab()); + assertBuffer("foo ba", new Buffer("foo ba").tab()); + assertBuffer("foo baz ", new Buffer("foo baz").tab()); + + // test completion in the mid range + assertBuffer("foo baz", new Buffer("f baz").left().left().left().left().tab()); + assertBuffer("ba foo", new Buffer("b foo").left().left().left().left().tab()); + assertBuffer("foo ba baz", new Buffer("foo b baz").left().left().left().left().tab()); + assertBuffer("foo foo baz", new Buffer("foo f baz").left().left().left().left().tab()); + } +} \ No newline at end of file diff --git a/src/jline/src/test/java/scala/tools/jline/console/completer/NullCompleterTest.java b/src/jline/src/test/java/scala/tools/jline/console/completer/NullCompleterTest.java new file mode 100644 index 0000000000..70a4c3b554 --- /dev/null +++ b/src/jline/src/test/java/scala/tools/jline/console/completer/NullCompleterTest.java @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package scala.tools.jline.console.completer; + +import scala.tools.jline.console.ConsoleReaderTestSupport; +import scala.tools.jline.console.completer.NullCompleter; +import org.junit.Test; + +/** + * Tests for {@link NullCompleter}. + * + * @author Jason Dillon + */ +public class NullCompleterTest + extends ConsoleReaderTestSupport +{ + @Test + public void test1() throws Exception { + console.addCompleter(NullCompleter.INSTANCE); + + assertBuffer("f", new Buffer("f").tab()); + assertBuffer("ba", new Buffer("ba").tab()); + assertBuffer("baz", new Buffer("baz").tab()); + } +} \ No newline at end of file diff --git a/src/jline/src/test/java/scala/tools/jline/console/completer/StringsCompleterTest.java b/src/jline/src/test/java/scala/tools/jline/console/completer/StringsCompleterTest.java new file mode 100644 index 0000000000..518b88d031 --- /dev/null +++ b/src/jline/src/test/java/scala/tools/jline/console/completer/StringsCompleterTest.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package scala.tools.jline.console.completer; + +import scala.tools.jline.console.ConsoleReaderTestSupport; +import scala.tools.jline.console.completer.StringsCompleter; +import org.junit.Test; + +/** + * Tests for {@link jline.console.completer.StringsCompleter}. + * + * @author Marc Prud'hommeaux + */ +public class StringsCompleterTest + extends ConsoleReaderTestSupport +{ + @Test + public void test1() throws Exception { + console.addCompleter(new StringsCompleter("foo", "bar", "baz")); + + assertBuffer("foo ", new Buffer("f").tab()); + // single tab completes to unambiguous "ba" + assertBuffer("ba", new Buffer("b").tab()); + assertBuffer("ba", new Buffer("ba").tab()); + assertBuffer("baz ", new Buffer("baz").tab()); + } +} \ No newline at end of file diff --git a/src/jline/src/test/java/scala/tools/jline/console/history/HistoryTest.java b/src/jline/src/test/java/scala/tools/jline/console/history/HistoryTest.java new file mode 100644 index 0000000000..0a987b2b26 --- /dev/null +++ b/src/jline/src/test/java/scala/tools/jline/console/history/HistoryTest.java @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. + * + * This software is distributable under the BSD license. See the terms of the + * BSD license in the documentation provided with this software. + */ +package scala.tools.jline.console.history; + +import scala.tools.jline.console.ConsoleReaderTestSupport; +import org.junit.Test; + +import static scala.tools.jline.console.Operation.MOVE_TO_BEG; +import static scala.tools.jline.console.Operation.NEWLINE; +import static scala.tools.jline.console.Operation.NEXT_HISTORY; +import static scala.tools.jline.console.Operation.PREV_HISTORY; +import static scala.tools.jline.console.Operation.PREV_CHAR; + +/** + * Tests command history. + * + * @author Marc Prud'hommeaux + */ +public class HistoryTest + extends ConsoleReaderTestSupport +{ + @Test + public void testSingleHistory() throws Exception { + Buffer b = new Buffer(). + append("test line 1").op(NEWLINE). + append("test line 2").op(NEWLINE). + append("test line 3").op(NEWLINE). + append("test line 4").op(NEWLINE). + append("test line 5").op(NEWLINE). + append(""); + + assertBuffer("", b); + + assertBuffer("test line 5", b = b.op(PREV_HISTORY)); + assertBuffer("test line 5", b = b.op(PREV_CHAR)); + assertBuffer("test line 4", b = b.op(PREV_HISTORY)); + assertBuffer("test line 5", b = b.op(NEXT_HISTORY)); + assertBuffer("test line 4", b = b.op(PREV_HISTORY)); + assertBuffer("test line 3", b = b.op(PREV_HISTORY)); + assertBuffer("test line 2", b = b.op(PREV_HISTORY)); + assertBuffer("test line 1", b = b.op(PREV_HISTORY)); + + // beginning of history + assertBuffer("test line 1", b = b.op(PREV_HISTORY)); + assertBuffer("test line 1", b = b.op(PREV_HISTORY)); + assertBuffer("test line 1", b = b.op(PREV_HISTORY)); + assertBuffer("test line 1", b = b.op(PREV_HISTORY)); + + assertBuffer("test line 2", b = b.op(NEXT_HISTORY)); + assertBuffer("test line 3", b = b.op(NEXT_HISTORY)); + assertBuffer("test line 4", b = b.op(NEXT_HISTORY)); + assertBuffer("test line 5", b = b.op(NEXT_HISTORY)); + + // end of history + assertBuffer("", b = b.op(NEXT_HISTORY)); + assertBuffer("", b = b.op(NEXT_HISTORY)); + assertBuffer("", b = b.op(NEXT_HISTORY)); + + assertBuffer("test line 5", b = b.op(PREV_HISTORY)); + assertBuffer("test line 4", b = b.op(PREV_HISTORY)); + b = b.op(MOVE_TO_BEG).append("XXX").op(NEWLINE); + assertBuffer("XXXtest line 4", b = b.op(PREV_HISTORY)); + assertBuffer("test line 5", b = b.op(PREV_HISTORY)); + assertBuffer("test line 4", b = b.op(PREV_HISTORY)); + assertBuffer("test line 5", b = b.op(NEXT_HISTORY)); + assertBuffer("XXXtest line 4", b = b.op(NEXT_HISTORY)); + assertBuffer("", b = b.op(NEXT_HISTORY)); + + assertBuffer("XXXtest line 4", b = b.op(PREV_HISTORY)); + assertBuffer("XXXtest line 4", b = b.op(NEWLINE).op(PREV_HISTORY)); + assertBuffer("XXXtest line 4", b = b.op(NEWLINE).op(PREV_HISTORY)); + assertBuffer("XXXtest line 4", b = b.op(NEWLINE).op(PREV_HISTORY)); + assertBuffer("XXXtest line 4", b = b.op(NEWLINE).op(PREV_HISTORY)); + } +} diff --git a/src/jline/src/test/java/scala/tools/jline/console/history/MemoryHistoryTest.java b/src/jline/src/test/java/scala/tools/jline/console/history/MemoryHistoryTest.java new file mode 100644 index 0000000000..91b81548c8 --- /dev/null +++ b/src/jline/src/test/java/scala/tools/jline/console/history/MemoryHistoryTest.java @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package scala.tools.jline.console.history; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static junit.framework.Assert.*; + +/** + * Tests for {@link MemoryHistory}. + * + * @author Marc Prud'hommeaux + */ +public class MemoryHistoryTest +{ + private MemoryHistory history; + + @Before + public void setUp() { + history = new MemoryHistory(); + } + + @After + public void tearDown() { + history = null; + } + + @Test + public void testAdd() { + assertEquals(0, history.size()); + + history.add("test"); + + assertEquals(1, history.size()); + assertEquals("test", history.get(0)); + assertEquals(1, history.index()); + } + + private void assertHistoryContains(final int offset, final String... items) { + assertEquals(items.length, history.size()); + int i=0; + for (History.Entry entry : history) { + assertEquals(offset + i, entry.index()); + assertEquals(items[i++], entry.value()); + } + } + + @Test + public void testOffset() { + history.setMaxSize(5); + + assertEquals(0, history.size()); + assertEquals(0, history.index()); + + history.add("a"); + history.add("b"); + history.add("c"); + history.add("d"); + history.add("e"); + + assertEquals(5, history.size()); + assertEquals(5, history.index()); + assertHistoryContains(0, "a", "b", "c", "d", "e"); + + history.add("f"); + + assertEquals(5, history.size()); + assertEquals(6, history.index()); + + assertHistoryContains(1, "b", "c", "d", "e", "f"); + assertEquals("f", history.get(5)); + } + + @Test + public void testReplace() { + assertEquals(0, history.size()); + + history.add("a"); + history.add("b"); + history.replace("c"); + + assertHistoryContains(0, "a", "c"); + } +} \ No newline at end of file diff --git a/src/jline/src/test/java/scala/tools/jline/example/Example.java b/src/jline/src/test/java/scala/tools/jline/example/Example.java new file mode 100644 index 0000000000..a89a09c5c9 --- /dev/null +++ b/src/jline/src/test/java/scala/tools/jline/example/Example.java @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2002-2006, Marc Prud'hommeaux. All rights reserved. + * + * This software is distributable under the BSD license. See the terms of the + * BSD license in the documentation provided with this software. + */ +package scala.tools.jline.example; + +import scala.tools.jline.console.completer.*; +import scala.tools.jline.console.ConsoleReader; + +import java.io.*; +import java.util.*; + +public class Example +{ + public static void usage() { + System.out.println("Usage: java " + Example.class.getName() + + " [none/simple/files/dictionary [trigger mask]]"); + System.out.println(" none - no completors"); + System.out.println(" simple - a simple completor that comples " + + "\"foo\", \"bar\", and \"baz\""); + System.out + .println(" files - a completor that comples " + "file names"); + System.out.println(" classes - a completor that comples " + + "java class names"); + System.out + .println(" trigger - a special word which causes it to assume " + + "the next line is a password"); + System.out.println(" mask - is the character to print in place of " + + "the actual password character"); + System.out.println(" color - colored prompt and feedback"); + System.out.println("\n E.g - java Example simple su '*'\n" + + "will use the simple compleator with 'su' triggering\n" + + "the use of '*' as a password mask."); + } + + public static void main(String[] args) throws IOException { + Character mask = null; + String trigger = null; + boolean color = false; + + ConsoleReader reader = new ConsoleReader(); + + reader.setBellEnabled(false); + reader.setPrompt("prompt> "); + + if ((args == null) || (args.length == 0)) { + usage(); + + return; + } + + List completors = new LinkedList(); + + if (args.length > 0) { + if (args[0].equals("none")) { + } + else if (args[0].equals("files")) { + completors.add(new FileNameCompleter()); + } + else if (args[0].equals("simple")) { + completors.add(new StringsCompleter("foo", "bar", "baz")); + } + else if (args[0].equals("color")) { + color = true; + reader.setPrompt("\u001B[1mfoo\u001B[0m@bar\u001B[32m@baz\u001B[0m> "); + } + else { + usage(); + + return; + } + } + + if (args.length == 3) { + mask = args[2].charAt(0); + trigger = args[1]; + } + + for (Completer c : completors) { + reader.addCompleter(c); + } + + String line; + PrintWriter out = new PrintWriter( + reader.getTerminal().wrapOutIfNeeded(System.out)); + + while ((line = reader.readLine()) != null) { + if (color){ + out.println("\u001B[33m======>\u001B[0m\"" + line + "\""); + } else { + out.println("======>\"" + line + "\""); + } + out.flush(); + + // If we input the special word then we will mask + // the next line. + if ((trigger != null) && (line.compareTo(trigger) == 0)) { + line = reader.readLine("password> ", mask); + } + if (line.equalsIgnoreCase("quit") || line.equalsIgnoreCase("exit")) { + break; + } + } + } +} diff --git a/src/jline/src/test/java/scala/tools/jline/internal/TerminalLineSettingsTest.java b/src/jline/src/test/java/scala/tools/jline/internal/TerminalLineSettingsTest.java new file mode 100644 index 0000000000..3af10887f1 --- /dev/null +++ b/src/jline/src/test/java/scala/tools/jline/internal/TerminalLineSettingsTest.java @@ -0,0 +1,146 @@ +package scala.tools.jline.internal; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * Tests for the {@link TerminalLineSettings}. + * + * @author Jean-Baptiste Onofré + */ +public class TerminalLineSettingsTest +{ + private TerminalLineSettings settings; + + private final String linuxSttySample = "speed 38400 baud; rows 85; columns 244; line = 0;\n" + + "intr = ^C; quit = ^\\; erase = ^?; kill = ^U; eof = ^D; eol = M-^?; eol2 = M-^?; swtch = M-^?; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;\n" + + "-parenb -parodd cs8 hupcl -cstopb cread -clocal -crtscts\n" + + "-ignbrk brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc ixany imaxbel iutf8\n" + + "opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0\n" + + "isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke"; + + private final String solarisSttySample = "speed 38400 baud; \n" + + "rows = 85; columns = 244; ypixels = 0; xpixels = 0;\n" + + "csdata ?\n" + + "eucw 1:0:0:0, scrw 1:0:0:0\n" + + "intr = ^c; quit = ^\\; erase = ^?; kill = ^u;\n" + + "eof = ^d; eol = -^?; eol2 = -^?; swtch = ;\n" + + "start = ^q; stop = ^s; susp = ^z; dsusp = ^y;\n" + + "rprnt = ^r; flush = ^o; werase = ^w; lnext = ^v;\n" + + "-parenb -parodd cs8 -cstopb -hupcl cread -clocal -loblk -crtscts -crtsxoff -parext \n" + + "-ignbrk brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl -iuclc \n" + + "ixon ixany -ixoff imaxbel \n" + + "isig icanon -xcase echo echoe echok -echonl -noflsh \n" + + "-tostop echoctl -echoprt echoke -defecho -flusho -pendin iexten \n" + + "opost -olcuc onlcr -ocrnl -onocr -onlret -ofill -ofdel tab3"; + + private final String aixSttySample = "speed 38400 baud; 85 rows; 244 columns;\n" + + "eucw 1:1:0:0, scrw 1:1:0:0:\n" + + "intr = ^C; quit = ^\\; erase = ^?; kill = ^U; eof = ^D; eol = \n" + + "eol2 = ; start = ^Q; stop = ^S; susp = ^Z; dsusp = ^Y; reprint = ^R\n" + + "discard = ^O; werase = ^W; lnext = ^V\n" + + "-parenb -parodd cs8 -cstopb -hupcl cread -clocal -parext \n" + + "-ignbrk brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl -iuclc \n" + + "ixon ixany -ixoff imaxbel \n" + + "isig icanon -xcase echo echoe echok -echonl -noflsh \n" + + "-tostop echoctl -echoprt echoke -flusho -pending iexten \n" + + "opost -olcuc onlcr -ocrnl -onocr -onlret -ofill -ofdel tab3"; + + private final String macOsSttySample = "speed 9600 baud; 47 rows; 155 columns;\n" + + "lflags: icanon isig iexten echo echoe -echok echoke -echonl echoctl\n" + + "-echoprt -altwerase -noflsh -tostop -flusho pendin -nokerninfo\n" + + "-extproc\n" + + "iflags: -istrip icrnl -inlcr -igncr ixon -ixoff ixany imaxbel iutf8\n" + + "-ignbrk brkint -inpck -ignpar -parmrk\n" + + "oflags: opost onlcr -oxtabs -onocr -onlret\n" + + "cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -dsrflow\n" + + "-dtrflow -mdmbuf\n" + + "cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = ;\n" + + "eol2 = ; erase = ^?; intr = ^C; kill = ^U; lnext = ^V;\n" + + "min = 1; quit = ^\\; reprint = ^R; start = ^Q; status = ^T;\n" + + "stop = ^S; susp = ^Z; time = 0; werase = ^W;"; + + private final String netBsdSttySample = "speed 38400 baud; 85 rows; 244 columns;\n" + + "lflags: icanon isig iexten echo echoe echok echoke -echonl echoctl\n" + + " -echoprt -altwerase -noflsh -tostop -flusho pendin -nokerninfo\n" + + " -extproc\n" + + "iflags: -istrip icrnl -inlcr -igncr ixon -ixoff ixany imaxbel -ignbrk\n" + + " brkint -inpck -ignpar -parmrk\n" + + "oflags: opost onlcr -ocrnl oxtabs onocr onlret\n" + + "cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -mdmbuf\n" + + " -cdtrcts\n" + + "cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = ;\n" + + " eol2 = ; erase = ^?; intr = ^C; kill = ^U; lnext = ^V;\n" + + " min = 1; quit = ^\\; reprint = ^R; start = ^Q; status = ^T;\n" + + " stop = ^S; susp = ^Z; time = 0; werase = ^W;"; + + private final String freeBsdSttySample = "speed 9600 baud; 32 rows; 199 columns;\n" + + "lflags: icanon isig iexten echo echoe echok echoke -echonl echoctl\n" + + " -echoprt -altwerase -noflsh -tostop -flusho -pendin -nokerninfo\n" + + " -extproc\n" + + "iflags: -istrip icrnl -inlcr -igncr ixon -ixoff ixany imaxbel -ignbrk\n" + + " brkint -inpck -ignpar -parmrk\n" + + "oflags: opost onlcr -ocrnl tab0 -onocr -onlret\n" + + "cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -dsrflow\n" + + " -dtrflow -mdmbuf\n" + + "cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = ;\n" + + " eol2 = ; erase = ^?; erase2 = ^H; intr = ^C; kill = ^U;\n" + + " lnext = ^V; min = 1; quit = ^\\; reprint = ^R; start = ^Q;\n" + + " status = ^T; stop = ^S; susp = ^Z; time = 0; werase = ^W;"; + + @Before + public void setUp() throws Exception { + settings = new TerminalLineSettings(); + } + + @Test + public void testGetConfig() { + String config = settings.getConfig(); + System.out.println(config); + } + + @Test + public void testLinuxSttyParsing() { + assertEquals(0x7f, settings.getProperty("erase", linuxSttySample)); + assertEquals(244, settings.getProperty("columns", linuxSttySample)); + assertEquals(85, settings.getProperty("rows", linuxSttySample)); + } + + @Test + public void testSolarisSttyParsing() { + assertEquals(0x7f, settings.getProperty("erase", solarisSttySample)); + assertEquals(244, settings.getProperty("columns", solarisSttySample)); + assertEquals(85, settings.getProperty("rows", solarisSttySample)); + } + + @Test + public void testAixSttyParsing() { + assertEquals(0x7f, settings.getProperty("erase", aixSttySample)); + assertEquals(244, settings.getProperty("columns", aixSttySample)); + assertEquals(85, settings.getProperty("rows", aixSttySample)); + } + + @Test + public void testMacOsSttyParsing() { + assertEquals(0x7f, settings.getProperty("erase", macOsSttySample)); + assertEquals(155, settings.getProperty("columns", macOsSttySample)); + assertEquals(47, settings.getProperty("rows", macOsSttySample)); + } + + @Test + public void testNetBsdSttyParsing() { + assertEquals(0x7f, settings.getProperty("erase", netBsdSttySample)); + assertEquals(244, settings.getProperty("columns", netBsdSttySample)); + assertEquals(85, settings.getProperty("rows", netBsdSttySample)); + } + + @Test + public void testFreeBsdSttyParsing() { + assertEquals(0x7f, settings.getProperty("erase", freeBsdSttySample)); + assertEquals(199, settings.getProperty("columns", freeBsdSttySample)); + assertEquals(32, settings.getProperty("rows", freeBsdSttySample)); + } + +} \ No newline at end of file -- cgit v1.2.3