dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.12k stars 1.57k forks source link

Implement PTY APIs #23165

Open DartBot opened 9 years ago

DartBot commented 9 years ago

This issue was originally filed by @kaendfinger


dart:io should support PTYs. This would allow others to write things like CI Servers that can act as a terminal so that they can capture ANSI escape codes.

https://en.wikipedia.org/wiki/Pseudoterminal

Implementation in other languages:

NodeJS: https://github.com/chjj/pty.js Java: https://github.com/traff/pty4j

What if I wanted to write an IDE like WebStorm in Dart. WebStorm has a built-in terminal, which uses PTYs.

This is something that would greatly enhance dart:io. It could allow for example to build a fully-featured command line shell! I attempted to write a shell in Dart, but quickly ran into this problem.

If we want to be able to use Dart for large command line tools like shells, this is essential. It is also something that should be in the standard library, simply because it requires a lot of native code, and is cross platform (not platform specific).

iposva-google commented 9 years ago

Added Library-IO, Area-Library, Triaged labels.

natebosch commented 6 years ago

Is there any chance of this happening? Our new approach for build tools would see a benefit.

We should be able to set up a PTY in linux/mac - can we do the same in Windows?

tejainece commented 6 years ago

Is this fixed by https://github.com/dart-lang/sdk/issues/24716 and https://github.com/dart-lang/sdk/commit/6523896c6fb27a7ead2ced038da4d15164d894d4?

kevmoo commented 6 years ago

Is this fixed by #24716 and 6523896?

@zanderso ? @a-siva ?

natebosch commented 6 years ago

No, #24716 mitigated this issue but it's forwarding the same TTY, rather than a pseudo TTY.

Having PTY APIs would open up new possibilities but the priority is lower given that the direct solution works in many scenarios.

tejainece commented 6 years ago

For the time being, one can use script command on linux.

xtyxtyx commented 3 years ago

I've uploaded a patch that implements the PTY api: https://dart-review.googlesource.com/c/sdk/+/198940

In this patch a new process start mode ProcessStartMode.pseudoTerminal is added as well as a new method on Process that resizes the pseudo terminal.

void main() async {
  final process = await Process.start(
    'bash',
    [],
    mode: ProcessStartMode.pseudoTerminal,
  );

  // ...

  process.resizeTerminal(110, 30);
}

In this new mode a pseudo terminal is created and used to communicate with the child process. The parent process can use stdin and stdout to read from/write to the pseudo terminal.

The Windows implementation uses the ConPTY api which is available since Windows 10 1809 and Windows Server 2019, otherwise an UnsupportedError would be thrown.

azenla commented 3 years ago

@xtyxtyx That's awesome!

I still consider (after ~6 years) this important for inclusion in Dart, the script command workaround is not very portable, and in fact the last time I checked (a long time ago) you need to use different arguments on macOS versus Linux.

zanderso commented 3 years ago

/cc @aam