Clearing the terminal screen in a command-line Dart app

為{幸葍}努か 提交于 2020-01-13 18:15:29

问题


This one don't work (on Windows in a Cmd-Box):

import 'dart:io';

void main() {
    print("Hello, World!");

    Process.start('cls', [], runInShell: true).then((process) {
        stdout.addStream(process.stdout);
        stderr.addStream(process.stderr);
    });
}

回答1:


EDIT
This seems to have the answer why it doesn't work on windows How to make win32 console recognize ANSI/VT100 escape sequences?

ORIGINAL

if(Platform.isWindows) {
  // not tested, I don't have Windows
  // may not to work because 'cls' is an internal command of the Windows shell
  // not an executeable
  print(Process.runSync("cls", [], runInShell: true).stdout); 
} else {
  print(Process.runSync("clear", [], runInShell: true).stdout);
}

or

print("\x1B[2J\x1B[0;0H"); // clear entire screen, move cursor to 0;0
print("xxx") // just to show where the cursor is
// http://en.wikipedia.org/wiki/ANSI_escape_code#CSI_codes

or

for(int i = 0; i < stdout.terminalLines; i++) {
  stdout.writeln();
}

The cursor position is on the bottom then. You have to add newlines after some output to move it to the top.



来源:https://stackoverflow.com/questions/21269769/clearing-the-terminal-screen-in-a-command-line-dart-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!