如何在Dart中运行交互式过程? [英] How do you run an interactive process in Dart?

查看:273
本文介绍了如何在Dart中运行交互式过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的测试尝试运行less pager命令并返回一次
用户退出。问题是它不等待用户输入,它
只是列出整个文件并退出。平台:xubuntu 12.04,Dart
编辑器版本:13049。

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

以下CoffeeScript函数(使用nodejs I / O) >

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

这个工作在Dart?

推荐答案

John有一个很好的例子,但不回答你原来的问题。不幸的是,你的问题不符合Dart的操作方式。你有两个例子,Dart版本和CoffeeScript / Node.js版本,做了两个完全不同的事情。

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.

在您的CoffeeScript版本中,spawn命令实际上是创建一个新进程,然后将执行传递给该新进程。基本上你的程序不是与进程交互式通信,而是你的用户正在与生成的进程交互。

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.

在Dart中,它是不同的,产卵过程。它不是将执行传递给新进程。基本上你正在做的是管道输入/输出往返于新进程到你的程序本身。由于您的程序没有来自终端的窗口高度,它会立即传递所有信息。你在飞镖上做的几乎等同于:

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

您可以使用 Process.start()与进程进行交互式通信。但是它是你的程序,与进程交互式通信,而不是用户。因此,你可以编写一个dart程序,它将启动并自动播放zork或adventure,或者通过查看进程输出的提示登录远程服务器。

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.

然而,在当前,没有办法简单地将执行传递给生成的进程。如果要将过程输出传达给用户,然后还需要用户输入并将其发送回过程,则需要一个附加层。即使这样,并非所有程序(如less)的行为与从shell环境中启动时的行为一样。

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.

这篇关于如何在Dart中运行交互式过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆