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 ...
随机推荐
- Spring Boot集成Mybatis注解相关
mybatis3开始支持java注解,使用java注解可以替代xml配置文件,简化代码.下面来看一下怎么在spring boot中使用mybatis注解. 1 使用mybatis注解需要的配置.如下面 ...
- ModuleNotFoundError: No module named 'pymysql'
出现此提示表示系统中没有安装PyMySQL,可以通过pip PyMySQL进行安装. 安装之后,执行import pymysql仍然不可用! why? 检查后发现系统中存在python2与python ...
- WebRTC搭建前端视频聊天室——数据通道篇
本文翻译自WebRTC data channels 在两个浏览器中,为聊天.游戏.或是文件传输等需求发送信息是十分复杂的.通常情况下,我们需要建立一台服务器来转发数据,当然规模比较大的情况下,会扩展成 ...
- SNF快速开发平台2020版
SNF快速开发平台分如下子平台: 1.CS快速开发平台 2.BS快速开发平台 3.H5移动端快速开发平台 4.软件开发机器人平台 配置型开发零编程 SNF快速开发平台是一个比较成熟的.net领域的商业 ...
- 阶段一-01.万丈高楼,地基首要-第3章 用户登录注册模块开发-3-8 优化Swagger2显示
左侧的全是英文 忽略api 把HelloController也忽略掉 重启api的服务,刷新页面.只剩下了passport的Controller 给passport接口加上注解. 重启服务,并刷新页面 ...
- bootstrap-select
bootstrap-select 周下载量 63,214 是bootstrap中select库下载量最多的. https://www.npmjs.com/package/bootstrap-selec ...
- 在网址前加神秘字母,让你打开新世界(z)
在网址前加神秘字母,让你打开新世界 1.在百度云后面加“wp”可以高速下载百度云资源<ignore_js_op> 2.在任何网址前面加“wn.run”(一定要加到这个网址www的前面)之后 ...
- 在日志中记录Java异常信息的正确姿势
遇到的问题 今天遇到一个线上的BUG,在执行表单提交时失败,但是从程序日志中看不到任何异常信息. 在Review源代码时发现,当catch到异常时只是输出了e.getMessage(),如下所示: l ...
- C# web项目乱码问题解决
在 web.config 文件中添加代码 <globalization requestEncoding="GB2312" responseEncoding="GB ...
- 【转帖】HBase基本概念与基本使用
HBase基本概念与基本使用 https://www.cnblogs.com/swordfall/p/8737328.html 分类: HBase undefined 1. HBase简介 1.1 什 ...