How do you run an interactive process in Dart?
https://stackoverflow.com/questions/12682269/how-do-you-run-an-interactive-process-in-dart
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
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?的更多相关文章
- To run dex in process, the Gradle daemon needs a larger heap
http://blog.csdn.net/u012995856/article/details/52595653
- python多进程的理解 multiprocessing Process join run
最近看了下多进程. 一种接近底层的实现方法是使用 os.fork()方法,fork出子进程.但是这样做事有局限性的.比如windows的os模块里面没有 fork() 方法. windows:.lin ...
- 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 ...
- loadrunner跑场景的时候出现:Abnormal termination, caused by mdrv process termination
1.问题 loadrunner跑场景的时候出现:Abnormal termination, caused by mdrv process termination. 备注:我使用的是RTE协议录制的脚本 ...
- Order to Cash Process
order to cash process steps can be listed as below · Enter the Sales Order · Book the Sales Order · ...
- 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 ...
- WSL(Windows Subsystem for Linux)--Pico Process Overview
[转载] Windows Subsystem for Linux -- Pico Process Overview Overview This post discusses pico processe ...
- 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 ...
- 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 ...
随机推荐
- 【Gamma阶段】第一次Scrum Meeting
冰多多团队-Gamma阶段第一次Scrum会议 工作情况 团队成员 已完成任务 待完成任务 卓培锦 推广软件,发放调查问卷 修改可移动button以及button手感反馈优化,编辑器风格切换(夜间模式 ...
- dateTime格式转换
select Convert(varchar(8),GETDATE(),112) Select replace(CONVERT(varchar(8), GETDATE(), 108),':','')
- 使用MeadCo's ScriptX控件做WEB打印
官方网址:http://www.meadroid.com/scriptx/index.asp MeadCo's ScriptX控件有两种使用方式,一种是做为免费版本使用,一种是做为收费版本使用,免费版 ...
- java对象生成随意属性值
public class RandomObjectValue { public static <T> T getObject(Class<?> clazz) { T t = n ...
- poi导出word表格
代码如下: package com.ksource.pwlp.util; import java.io.FileOutputStream; import java.math.BigInteger; i ...
- mybatis ResultHandler vs ResultSetHandler及自定义扩展
ResultSetHandler是mybatis的关键类之一,用于对jdbc返回的ResultSet进行映射处理,其中包括列前缀处理,逻辑分页,鉴别器(Discriminator,基于值实现动态映射列 ...
- Mysql 排序规则选择
排序规则:是指对指定字符集下不同字符的比较规则.其特征有以下几点: 1. 两个不同的字符集不能有相同的排序规则 2. 两个字符集有一个默认的排序规则 3. 有一些常用的命名规则.如_ci结尾表示大小写 ...
- CORS跨域带来的preflight request
CORS跨域带来的preflight request https://blog.csdn.net/baidu_35407267/article/details/79043515 HTTPS://blo ...
- ocketMQ概念模型
ocketMQ概念模型 https://blog.csdn.net/binzhaomobile/article/details/73332463 HTTPS://blog.CSDN.net/bin找M ...
- SQLServer查看及设置最大连接数(转)
转自:https://blog.csdn.net/duanbeibei/article/details/86573840 很多时候自己本地开发会遇到 ,打开几个连接正常访问 之后就报错误,这时候需要调 ...