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. Invalid bound statement (not found) 终极解决办法

    网上已经有很多文章说明可能导致这个报错的原因,无非是以下几种:1.检查xml文件的namespace是否正确 2.Mapper.java的方法在Mapper.xml中没有,然后执行Mapper的方法会 ...

  2. golang调用c动态库

    golang调用c动态库 简介 golang调用c语言动态库,动态方式调用,可指定动态库路径,无需系统目录下 核心技术点 封装c动态库 go语言调用c代码 实例代码 封装c动态库 头文件 test_s ...

  3. SSL证书原理讲解

    一直以来都对数字证书的签发,以及信任等事情一知半解.总算有个闲适的周末来总结和深入一下相关的知识. CA: CA(Certificate Authority)是证书的签发机构,它是负责管理和签发证书的 ...

  4. odoo开发笔记 -- 借助模块queue_job实现异步方法调用

    场景描述: 对比了几个定时调度的框架,发现各有优缺点: celery 很强,异步定时调度,异步周期调度,也有延时调度的功能,但是延时调度的案例比较少,遂暂时不使用. queue_job,一个odoo第 ...

  5. python - logging.basicConfig format参数无效

    有这么一段python代码 import threading import time import requests from decimal import Decimal, ROUND_DOWN i ...

  6. coding git push 403 时

    直接修改 项目目录下的 .git/config   url url = https://coding用户名:coding密码@git.coding.net/coding账号/coding项目名称.gi ...

  7. 【并行计算-CUDA开发】【视频开发】ffmpeg Nvidia硬件加速总结

    2017年5月25日 0. 概述 FFmpeg可通过Nvidia的GPU进行加速,其中高层接口是通过Video Codec SDK来实现GPU资源的调用.Video Codec SDK包含完整的的高性 ...

  8. 在ensp上静态路由以及默认路由基本配置

    原理 实验模拟 实验拓扑 实验参数 测试连通性 两台PCping一下,发现超时 为什么呢我们可以看一下这个路由表,发现没有网段为20的信息,所以我们要加上 加入当访问地址为20网段时,设置下一跳路由器 ...

  9. PHP设计模式 - 状态模式

    状态模式当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类.状态模式主要解决的是当控制一个对象状态的条件表达式过于复杂时的情况.把状态的判断逻辑转移到表示不同状态的一系列类中,可以 ...

  10. JAVA知识点总结篇(一)

    JVM(Java Virtual Machine):源文件->编译器->字节码文件->解释器->程序: JDK:Java Development Kit,Java开发工具包: ...