https://stackoverflow.com/questions/12682269/how-do-you-run-an-interactive-process-in-dart

The test below attempts to run the less pager command and return once the user quits. The problem is that it doesn't wait for user input, it just lists the entire file and exits. Platform: xubuntu 12.04, Dart Editor build: 13049.
 
import 'dart:io';

void main() {
shell('less', ['/etc/mime.types'], (exitCode) => exit(exitCode));
} void shell(String cmd, List<String> opts, void onExit(int exitCode)) {
var p = Process.start(cmd, opts);
p.stdout.pipe(stdout); // Process output to stdout.
stdin.pipe(p.stdin); // stdin to process input.
p.onExit = (exitCode) {
p.close();
onExit(exitCode);
};
}

  

The following CoffeeScript function (using nodejs I/O) works:

shell = (cmd, opts, callback) ->
process.stdin.pause()
child = spawn cmd, opts, customFds: [0, 1, 2]
child.on 'exit', (code) ->
process.stdin.resume()
callback code

  

How can I make this work in Dart?

answer;

John has a good example about how to look at user input. But doesn't answer your original question. Unfortunately your question doesn't fit with how Dart operates. The two examples you have, the Dart version and CoffeeScript/Node.js version, do two completely different things.

In your CoffeeScript version, the spawn command is actually creating a new process and then passing execution over to that new process. Basically you're program is not interactively communicating with the process, rather your user is interacting with the spawned process.

In Dart it is different, your program is interacting with the spawned process. It is not passing off execution to the new process. Basically what you are doing is piping the input/output to and from the new process to your program itself. Since your program doesn't have a 'window height' from the terminal, it passes all the information at once. What you're doing in dart is almost equivalent to:

less /etc/mime.types | cat

You can use Process.start() to interactively communicate with processes. But it is your program which is interactively communicating with the process, not the user. Thus you can write a dart program which will launch and automatically play 'zork' or 'adventure' for instance, or log into a remote server by looking at the prompts from process's output.

However, at current there is no way to simply pass execution to the spawned process. If you want to communicate the process output to a user, and then also take user input and send it back to a process it involves an additional layer. And even then, not all programs (such as less) behave the same as they do when launched from a shell environment.

I planned to include some sample code with the above answer, however at the moment there appears to be a bug preventing output from a process from being read interactively. It looks like the buffers aren't being flushed by the process until after the process terminates the streams. Waiting on dartbug.com/5607 and then I will post the code :) – Matt B Oct 2 '12 at 18:27

Thank you for a great explanation (and the code you posted at issue 5607), I no longer need to keep banging my head against a wall :-) – Stuart Rackham Oct 2 '12 at 20:39

Here's a basic structure for reading console input from the user. This example reads lines of text from the user, and exits on 'q':
import 'dart:io';
import 'dart:isolate'; final StringInputStream textStream = new StringInputStream(stdin); void main() {
textStream.onLine = checkBuffer;
} void checkBuffer(){
final line = textStream.readLine(); if (line == null) return; if (line.trim().toLowerCase() == 'q'){
exit(0);
} print('You wrote "$line". Now write something else!');
}

  

How do you run an interactive process in Dart?的更多相关文章

  1. To run dex in process, the Gradle daemon needs a larger heap

    http://blog.csdn.net/u012995856/article/details/52595653

  2. python多进程的理解 multiprocessing Process join run

    最近看了下多进程. 一种接近底层的实现方法是使用 os.fork()方法,fork出子进程.但是这样做事有局限性的.比如windows的os模块里面没有 fork() 方法. windows:.lin ...

  3. Java JVM、JNI、Native Function Interface、Create New Process Native Function API Analysis

    目录 . JAVA JVM . Java JNI: Java Native Interface . Java Create New Process Native Function API Analys ...

  4. loadrunner跑场景的时候出现:Abnormal termination, caused by mdrv process termination

    1.问题 loadrunner跑场景的时候出现:Abnormal termination, caused by mdrv process termination. 备注:我使用的是RTE协议录制的脚本 ...

  5. Order to Cash Process

    order to cash process steps can be listed as below · Enter the Sales Order · Book the Sales Order · ...

  6. protractor protractor.conf.js [launcher] Process exited with error code 1 undefined:1190

    y@y:karma-t01$ protractor protractor.conf.js [launcher] Process exited with error code undefined: vl ...

  7. WSL(Windows Subsystem for Linux)--Pico Process Overview

    [转载] Windows Subsystem for Linux -- Pico Process Overview Overview This post discusses pico processe ...

  8. linux create a process

    When the system starts up it is running in kernel mode and there is, in a sense, only one process, t ...

  9. Jmeter-Maven-Plugin高级应用:Configuring the jvm that the jmeter process runs in

    Configuring the jvm that the jmeter process runs in The JMeter Maven plugin will run the JMeter proc ...

随机推荐

  1. java 注解,动态代理

    秒懂,Java 注解 (Annotation)你可以这样学 深入理解Java注解类型(@Annotation) 注解可以理解为标签. 当开发者使用了Annotation 修饰了类.方法.Field 等 ...

  2. Tensorflow 损失函数(loss function)及自定义损失函数(三)

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/limiyudianzi/article ...

  3. Java NIO Buffer详解

    一.ByteBuffer类型化的put与get方法 /** * ByteBuffer类型化的put与get方法 */ public class NioTest5 { public static voi ...

  4. Maven编译中使用${env}与直接启动tomcat会报错的问题处理

    Maven编译中使用${env}与直接启动tomcat会报错的问题处理 在Run/Debug Configurations中,设置 Before lauch:Build, Build Artifact ...

  5. linux下使用clamav排查病毒

    clamav wget http://www.clamav.net/downloads/production/clamav-0.102.0.tar.gz ### Installyum -y insta ...

  6. 转载:【TP5.0】TP5 Validate 验证规则

    下面是部分tp5内置的验证规则: 格式验证类: 'name'=>'require' 验证某个字段的值是否为数字(采用filter_var验证),例如: number 或者 integer 'na ...

  7. git clone或者git clone时遇到gnutls_handshake() failed: An unexpected TLS packet was received.如何处理?

    答: 直接将https修改为http即可, 如: $ git clone https://github.com/Jello_Smith/my-example.git ->  git clone ...

  8. Xamarin.FormsShell基础教程(3)Shell项目构成

    Xamarin.FormsShell基础教程(3)Shell项目构成 在创建的ShellDemo解决方案中,有3个子项目,分别为ShellDemo.ShellDemo.Android和ShellDem ...

  9. iOS项目运行出现:[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object

    说白了就是 字典初始化 的时候 放入空的值了 下面这个比较具体 错误原因: NSDictionary *dic = @{@"key":value}这个初始化方法,发现keys co ...

  10. 大幅度改变celery日志外观,修改成日志可点击跳转和鲜艳五彩日志,解决脚本中已添加handler的logger和框架日志重复记录问题。

    大幅度改变celery日志外观,修改成日志可点击跳转和鲜艳五彩日志,解决脚本中已添加handler的logger和框架日志重复记录问题.打猴子补丁. 先把脚本中的所有logger的handlers全部 ...