Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo

How to clear the console screen with ANSI (Any Language)

console

  • Please log in to reply
3 replies to this topic

#1 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 28 September 2010 - 01:12 AM

How to clear console screen the proper way.

Language Agnostic Clearing of Console Screen: Without clear() or cls()

To cut it short: I had often heard of people asking how to properly clear the screen, and no one seems to know what to use other than calling a system function, or they suggest a library such as ncurses to handle screen formatting. We can leave this to ANSI as we had for many years.

What about ANSI? Does your modern computer even support ANSI? The answer: Yes.
Windows has a nice ansi.sys, Linux terminals accept ANSI without trouble, OSX has some ANSI support in terminal emulators.

ANSI stands for American National Standard Intitution. The ANSI X3.4 standard, which is simply the ANSI organisation's ratified version of ASCII, is more properly referred to as Windows-1252 (mostly on Western systems, it can represent specific other Windows code pages on other systems). This is essentially an extension of the ASCII character set in that it includes all the ASCII characters with an additional 127 character codes, being an 8-bit encoding.

Within the ANSI standard, there lays an escape ESC character: ASCII decimal 27/hex 0x1B/octal 033 which can take care of formatting, colouring and alike with escape sequences. Here we will be using the ESC character to format and clear the screen.

-----------------------------------------------------------------------------------

Examples:
First a simple example to show you how an escape works, would be to colour the text: 'Hello CodeCall':

printf("Hello %c[32mCodeCall!", (char) 27);

screenshot119z.png
 

The escape sequence in this case is [32m for green.

For a list of escape codes including what we will use, can be found here:
ASCII Table - ANSI Escape sequences (ANSI Escape codes) - VT100 / VT52

Clearing of screen:
The following escape sequence clears all characters above and below the cursor location: [2J and this sets cursor position to 0,0: [;H .

A sample call in another language, such as PHP in CLI mode:

<?php
print str_repeat("--=-------======------======-----=\n\n", 1024);
print chr(27) . "[2J" . chr(27) . "[;H";
print "I am at cursor position 0,0 on a clean screen.";
?>
Or in Java:
public class ansiclrscr {
public static void main(String[] argv) {
System.out.print("foo\nbar\nbaz\nquux");
System.out.print("\033[2J\033[;H");
System.out.print( "I am at cursor position 0,0 on a clean screen.");
}
}

screenshot120h.png
 

This may be the best way depending on implementation, as it is standards-conforming and is widely supported on most platforms, allowing console formatting without the need of an external library.

 

Cleaning up:

 

After any attributes have been set, such as colour, the console can be reset with the following attribute:

\033[0m

(Thanks: Dargueta.)


Edited by Alexander, 23 July 2014 - 06:09 AM.

  • 1

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#2 FireGator

FireGator

    CC Regular

  • Just Joined
  • PipPipPip
  • 35 posts

Posted 28 September 2010 - 10:51 PM

In many years I had not seen an instruction on implementing ANSI in code (kids these days?), +1 showing the simplicity of implementation in different languages. Great work.

Maybe you should consider writing more ANSI, or rather a code class for formatting text (that can be reused in code), I'd love to see it.
  • 0

#3 neil

neil

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 373 posts
  • Location:Philadelphia
  • Programming Language:C++, Perl, Bash
  • Learning:C, Java, C++, Python, JavaScript, Perl, Bash, Others

Posted 19 August 2012 - 04:21 AM

Here's one for C++

cout << "\x1b[2J\x1b[1;1H" << std::flush;

(before)
Posted Image

(after)
Posted Image

full code:
#include <iostream>
using namespace std;

int main()
{
	 cout<<"The terminal is cramped now.\n Would you like to clear it (y/n)?";
	 char ch;
	 cin>>ch;
	 if(ch == 'y')
			 cout << "\x1b[2J\x1b[1;1H" << std::flush;
	 return 0;
}

  • 0

#4 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 06 June 2014 - 07:44 AM

FYI

You should clear attributes before your program exits using \033[0m or else those attributes will still be applied after your program is done.

This will not work on versions of Windows beyond 98 without manually enabling ANSI.SYS. Otherwise you'll actually see all the escape codes.

You can find out how to do that here:
http://support.microsoft.com/kb/101875
  • 1

sudo rm -rf / && echo $'Sanitize your inputs!'






Also tagged with one or more of these keywords: console

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download