console: send \r on Enter key pressed for PTY based processes (GO-9805)
[idea/community.git] / platform / lang-impl / src / com / intellij / execution / impl / ConsoleViewRunningState.java
index a0c5306a328da9a9ba8ad46a3cc2d65a494750f3..6fcd42a0cc657586f2f330c78786cef8dd1a0c77 100644 (file)
@@ -1,11 +1,14 @@
 // Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
 package com.intellij.execution.impl;
 
+import com.google.common.base.Ascii;
 import com.intellij.execution.ExecutionBundle;
 import com.intellij.execution.process.*;
 import com.intellij.execution.ui.ConsoleViewContentType;
 import com.intellij.openapi.util.Key;
+import com.intellij.openapi.util.SystemInfo;
 import com.intellij.openapi.vfs.encoding.EncodingManager;
+import com.pty4j.PtyProcess;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 import org.jetbrains.annotations.TestOnly;
@@ -106,10 +109,23 @@ public class ConsoleViewRunningState extends ConsoleState {
     if (myUserInputWriter == null) {
       throw new IOException(ExecutionBundle.message("no.user.process.input.error.message"));
     }
-    myUserInputWriter.write(input);
+    char enterKeyCode = getEnterKeyCode();
+    String inputToSend = input.replace((char)Ascii.LF, enterKeyCode);
+    myUserInputWriter.write(inputToSend);
     myUserInputWriter.flush();
   }
 
+  private char getEnterKeyCode() {
+    if (SystemInfo.isWindows &&
+        myProcessHandler instanceof OSProcessHandler &&
+        ((OSProcessHandler)myProcessHandler).getProcess() instanceof PtyProcess) {
+      // pty4j expects \r as Enter key code
+      // https://github.com/JetBrains/pty4j/blob/0.9.4/test/com/pty4j/PtyTest.java#L54
+      return Ascii.CR;
+    }
+    return Ascii.LF;
+  }
+
   @NotNull
   @Override
   public ConsoleState attachTo(@NotNull final ConsoleViewImpl console, final @NotNull ProcessHandler processHandler) {