How do you run an interactive process in Dart?

   2023-02-09 学习力0
核心提示:https://***.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 en

https://***.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举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • Dart空安全的底层原理与适配
    Dart空安全的底层原理与适配
    一、在空安全推出之前,静态类型系统允许所有类型的表达式中的每一处都可以有 null。从类型理论的角度来说,Null 类型被看作是所有类型的子类;   类型会定义一些操作对象,包括 getters、setters、方法和操作符,在表达式中使用。如果是 List 类型,您可
    03-08
  • Flutter开发指南之理论篇:Dart语法05(单线程模型,事件循环模型,Isolate)
    Flutter开发指南之理论篇:Dart语法05(单线程
    此文转载自:https://blog.csdn.net/AndrExpert/article/details/110823218Flutter开发指南之理论篇:Dart语法05(单线程模型,事件循环模型,Isolate) Flutter开发指南之理论篇:Flutter基础01(架构,设计思想) Dart是一门面向对象语言,它针对web 和移
    03-08
  • Flutter开发指南之理论篇:Dart语法05(单线程模型,事件循环模型,Isolate)
    Flutter开发指南之理论篇:Dart语法05(单线程
    此文转载自:https://blog.csdn.net/AndrExpert/article/details/110823218Flutter开发指南之理论篇:Dart语法05(单线程模型,事件循环模型,Isolate) Flutter开发指南之理论篇:Flutter基础01(架构,设计思想) Dart是一门面向对象语言,它针对web 和移
    03-08
  • Flutter开发指南之理论篇:Dart语法05(单线程模型,事件循环模型,Isolate)
    Flutter开发指南之理论篇:Dart语法05(单线程
    此文转载自:https://blog.csdn.net/AndrExpert/article/details/110823218Flutter开发指南之理论篇:Dart语法05(单线程模型,事件循环模型,Isolate) Flutter开发指南之理论篇:Flutter基础01(架构,设计思想) Dart是一门面向对象语言,它针对web 和移
    03-08
  • oogle的“ JavaScript杀手” Dart 与JavaScript的比较
    oogle的“ JavaScript杀手” Dart 与JavaScript
    JavaScript通常被称为浏览器脚本语言,但它也已扩展到许多服务器端和移动应用程序开发环境。JS已经存在了将近20年,可以肯定地说它确实是一种成熟且稳定的编程语言。在Facebook发布React和React Native框架之后,JS变得越来越流行。JavaScript具有自己的软件
    03-08
  • Flutter开发指南之理论篇:Dart语法05(单线程模型,事件循环模型,Isolate)
    Flutter开发指南之理论篇:Dart语法05(单线程
    此文转载自:https://blog.csdn.net/AndrExpert/article/details/110823218Flutter开发指南之理论篇:Dart语法05(单线程模型,事件循环模型,Isolate) Flutter开发指南之理论篇:Flutter基础01(架构,设计思想) Dart是一门面向对象语言,它针对web 和移
    03-08
  • Flutter开发指南之理论篇:Dart语法05(单线程模型,事件循环模型,Isolate)
    Flutter开发指南之理论篇:Dart语法05(单线程
    此文转载自:https://blog.csdn.net/AndrExpert/article/details/110823218Flutter开发指南之理论篇:Dart语法05(单线程模型,事件循环模型,Isolate) Flutter开发指南之理论篇:Flutter基础01(架构,设计思想) Dart是一门面向对象语言,它针对web 和移
    03-08
  • flutter 填坑之旅(dart学习笔记篇)
    flutter 填坑之旅(dart学习笔记篇)
    俗话说 ‘工欲善其事必先利其器’ 想要撸flutter app 而不懂 dart 那就像一个不会英语的人在和英国人交流,懵!安装 dart 就不用说了,比较简单dart 官网 https://dart.dev/安装完成后就开启学习dart 旅程吧…一、 首先得找个ide (总不能使用记事本撸吧),所
    03-08
  • dart 命令行
       dart build 
    02-09
  • dart系列之:dart语言中的异常 dart新手的错误
    目录简介Exception和ErrorThrow和catchFinally总结简介Exception是程序中的异常情况,在JAVA中exception有checked Exception和unchecked Exception。那么在dart中的情况是不是一样的呢?一起来看看吧。Exception和ErrorDart中表示异常的类有两个,分别是Excep
    02-09
点击排行