/src/jline/src/main/java/scala/tools/jline/AnsiWindowsTerminal.java

https://github.com/gkossakowski/scala-dev · Java · 90 lines · 46 code · 10 blank · 34 comment · 1 complexity · 2bf07cb6bfed37f230dd876b526dd5c1 MD5 · raw file

  1. /*
  2. * Copyright (C) 2009 the original author(s).
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. * MODIFICATIONS: methods to deal with wrapping the output stream.
  17. */
  18. package scala.tools.jline;
  19. import org.fusesource.jansi.AnsiConsole;
  20. import org.fusesource.jansi.AnsiOutputStream;
  21. import org.fusesource.jansi.WindowsAnsiOutputStream;
  22. import java.io.ByteArrayOutputStream;
  23. import java.io.OutputStream;
  24. /**
  25. * ANSI-supported {@link WindowsTerminal}.
  26. *
  27. * @since 2.0
  28. */
  29. public class AnsiWindowsTerminal
  30. extends WindowsTerminal
  31. {
  32. private final boolean ansiSupported = detectAnsiSupport();
  33. @Override
  34. public OutputStream wrapOutIfNeeded(OutputStream out) {
  35. return wrapOutputStream(out);
  36. }
  37. /**
  38. * Returns an ansi output stream handler. We return whatever was
  39. * passed if we determine we cannot handle ansi based on Kernel32 calls.
  40. *
  41. * @return an @{link AltWindowAnsiOutputStream} instance or the passed
  42. * stream.
  43. */
  44. private static OutputStream wrapOutputStream(final OutputStream stream) {
  45. String os = System.getProperty("os.name");
  46. if( os.startsWith("Windows") ) {
  47. // On windows we know the console does not interpret ANSI codes..
  48. try {
  49. return new WindowsAnsiOutputStream(stream);
  50. } catch (Throwable ignore) {
  51. // this happens when JNA is not in the path.. or
  52. // this happens when the stdout is being redirected to a file.
  53. }
  54. // Use the ANSIOutputStream to strip out the ANSI escape sequences.
  55. return new AnsiOutputStream(stream);
  56. }
  57. return stream;
  58. }
  59. private static boolean detectAnsiSupport() {
  60. OutputStream out = AnsiConsole.wrapOutputStream(new ByteArrayOutputStream());
  61. try {
  62. out.close();
  63. }
  64. catch (Exception e) {
  65. // ignore;
  66. }
  67. return out instanceof WindowsAnsiOutputStream;
  68. }
  69. public AnsiWindowsTerminal() throws Exception {
  70. super();
  71. }
  72. @Override
  73. public boolean isAnsiSupported() {
  74. return ansiSupported;
  75. }
  76. @Override
  77. public boolean hasWeirdWrap() {
  78. return false;
  79. }
  80. }