6

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);
    });
}
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
Eugen
  • 113
  • 2
  • 7

1 Answers1

18

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.

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Thank you Günter for the help but your proposals are not woking. On 1. proposal i have the same result as in my solution. A curious character '?' was printed. On 2. "?[2J?[0;0H" was printed. It seems that Dart don't support such a notation. On 3. is actually the best solution but it dosn't clear the terminal but scrolls 'x' lines, so the terminal seems empty I also tried this one, without success: `print(Process.runSync("cmd", ['C "cls"'], runInShell: true).stdout);` – Eugen Jan 22 '14 at 21:46
  • I could verify that 2 worked on linux. Maybe the characters have to be encoded differently. I'll take a look tomorrow what else can be tried. 3 could be used but I know it's inconvenient. You have to add a lot of linefeeds after new output to move it on the top of the screen. – Günter Zöchbauer Jan 22 '14 at 21:51
  • @eugen Would you mind adding a comment how you solved the problem? Did you use ANSICON? – Günter Zöchbauer Jan 26 '14 at 14:10
  • 1
    Yes, i have to use the second solution with ANSICON. The simple 'cls' usage seems not working because of strange Windows 7 behavior. It doesn't work with Java too. Only in Ruby i can simple call system("cls"). Too bad ... – Eugen Jan 27 '14 at 15:10
  • Confirmed that the first solution does not work on Windows 11. Second solution with print("\x1B[2J\x1B[0;0H"); is the one working fine. – Gad D Lord Dec 29 '22 at 13:35