Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clearing the terminal screen in a command-line Dart app

Tags:

dart

dart-io

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);
    });
}
like image 522
Eugen Avatar asked Dec 11 '22 08:12

Eugen


1 Answers

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.

like image 107
Günter Zöchbauer Avatar answered Dec 28 '22 08:12

Günter Zöchbauer