博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
How do you run an interactive process in Dart?
阅读量:4647 次
发布时间:2019-06-09

本文共 3678 字,大约阅读时间需要 12 分钟。

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
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  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  and then I will post the code :) –  

 

 

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 :-) –  

 

 

 

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!');}

  

转载于:https://www.cnblogs.com/pythonClub/p/10892445.html

你可能感兴趣的文章
sprintf 和strcpy 的差别
查看>>
MPEG4与.mp4
查看>>
实验5
查看>>
git 下载 安装
查看>>
录制终端信息并回放
查看>>
JS中window.event事件使用详解
查看>>
ES6深入学习记录(一)class方法相关
查看>>
《BI项目笔记》用Excel2013连接和浏览OLAP多维数据集
查看>>
C语言对mysql数据库的操作
查看>>
SQL Server 数据库备份
查看>>
INNO SETUP 获得命令行参数
查看>>
Charles抓取https请求
查看>>
LAMP环境搭建
查看>>
C语言的变量的内存分配
查看>>
clientcontainerThrift Types
查看>>
链接全局变量再说BSS段的清理
查看>>
hdu 1728 逃离迷宫
查看>>
HTML5与CSS3权威指南之CSS3学习记录
查看>>
docker安装部署
查看>>
AVL树、splay树(伸展树)和红黑树比较
查看>>