在命令行Dart应用程序中清除终端屏幕

时间:2014-01-21 21:47:10

标签: dart dart-io

这个不起作用(在Cmd-Box的Windows上):

import 'dart:io';

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

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

1 个答案:

答案 0 :(得分:3)

修改
这似乎有答案为什么它不适用于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);
}

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

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

光标位置在底部。 您必须在输出后添加换行符才能将其移至顶部。