• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

HowdoyourunaninteractiveprocessinDart?

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

  


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Dart语言学习(十)Dart流程控制语句发布时间:2022-07-13
下一篇:
dart学习(四)之方法发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap