xtd 0.2.0
Loading...
Searching...
No Matches

◆ clear()

static void xtd::console::clear ( )
static

Clears the console buffer and corresponding console window of display information.

Remarks
Using the clear method is equivalent invoking the MS-DOS cls command in the command prompt window.
When the Clear method is called, the cursor automatically scrolls to the top-left corner of the window and the contents of the screen buffer are set to blanks using the current foreground background colors.
Examples
The following example uses the clear method to clear the console before it executes a loop, prompts the user to select a foreground and background color and to enter a string to display. If the user chooses not to exit the program, the console's original foreground and background colors are restored and the Clear method is called again before re-executing the loop.
#include <xtd/char32_object>
#include <xtd/console>
#include <xtd/startup>
using namespace xtd;
using namespace xtd::collections::generic;
namespace console_clear_example {
class program {
public:
// The main entry point for the application.
static auto main(const string_collection& args) {
// Save colors so they can be restored when use finishes input.
auto dft_fore_color = console::foreground_color();
auto dft_back_color = console::background_color();
auto continue_flag = true;
console::clear();
do {
auto new_fore_color = console_color::white;
auto new_back_color = console_color::black;
auto fore_color_selection = get_key_press("Select Text Color (B for Blue, R for Red, Y for Yellow): ", list<char32_t> { 'B', 'R', 'Y' });
switch (fore_color_selection) {
case 'B':
case 'b': new_fore_color = console_color::dark_blue; break;
case 'R':
case 'r': new_fore_color = console_color::dark_red; break;
case 'Y':
case 'y': new_fore_color = console_color::dark_yellow; break;
}
auto back_color_selection = get_key_press("Select Background Color (W for White, G for Green, M for Magenta): ", list<char32_t> { 'W', 'G', 'M' });
switch (back_color_selection) {
case 'W':
case 'w': new_back_color = console_color::white; break;
case 'G':
case 'g': new_back_color = console_color::green; break;
case 'M':
case 'm': new_back_color = console_color::magenta; break;
}
console::write_line();
console::write("Enter a message to display: ");
string text_to_display = console::read_line();
console::write_line();
console::foreground_color(new_fore_color);
console::background_color(new_back_color);
console::write_line(text_to_display);
console::write_line();
if (char32_object::to_upper(get_key_press("Display another message (Y/N): ", list<char32_t> { 'Y', 'N' })) == 'N')
continue_flag = false;
// Restore the default settings and clear the screen.
console::foreground_color(dft_fore_color);
console::background_color(dft_back_color);
console::clear();
} while (continue_flag);
}
private:
static char32_t get_key_press(const string& msg, const list<char32_t>& valid_chars) {
auto key_pressed = console_key_info {};
auto valid = false;
console::write_line();
do {
console::write(msg);
key_pressed = console::read_key();
console::write_line();
if (std::find(valid_chars.begin(), valid_chars.end(), char32_object::to_upper(key_pressed.key_char())) != valid_chars.end())
valid = true;
} while (!valid);
return key_pressed.key_char();
}
};
}
startup_(console_clear_example::program::main);
const_iterator begin() const noexcept override
Returns an iterator to the first element of the enumarable.
Definition list.h:205
const_iterator end() const noexcept override
Returns an iterator to the element following the last element of the enumarable.
Definition list.h:316
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.h:72
Specifies the standard keys on a console.
Definition console_key_info.h:24
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.h:175
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.h:15
The xtd::collections::specialized namespace contains specialized and strongly-typed collections; for ...
Definition string_collection.h:13
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
The example relies on a get_key_press method to validate the user's selection of a foreground and background color.
Examples
This example demonstrates the cursor_left and cursor_top properties, and the set_cursor_position and clear methods. The example positions the cursor, which determines where the next write will occur, to draw a 5 character by 5 character rectangle using a combination of "+", "|", and "-" strings. Note that the rectangle could be drawn with fewer steps using a combination of other strings.
#include <xtd/console>
using namespace xtd;
auto orig_row = 0;
auto orig_col = 0;
void write_at(const string& s, int x, int y) {
console::set_cursor_position(orig_col + x, orig_row + y);
}
auto main() -> int {
// Clear the screen, then save the top and left coordinates.
orig_row = console::cursor_top();
orig_col = console::cursor_left();
// Draw the left side of a 5x5 rectangle, from top to bottom.
write_at("+", 0, 0);
write_at("|", 0, 1);
write_at("|", 0, 2);
write_at("|", 0, 3);
write_at("+", 0, 4);
// Draw the bottom side, from left to right.
write_at("-", 1, 4); // shortcut: write_at("---", 1, 4)
write_at("-", 2, 4); // ...
write_at("-", 3, 4); // ...
write_at("+", 4, 4);
// Draw the right side, from bottom to top.
write_at("|", 4, 3);
write_at("|", 4, 2);
write_at("|", 4, 1);
write_at("+", 4, 0);
// Draw the top side, from right to left.
write_at("-", 3, 0); // shortcut: write_at("---", 1, 0)
write_at("-", 2, 0); // ...
write_at("-", 1, 0); // ...
//
write_at("All done!", 0, 6);
}
// This code produces the following output :
//
// +---+
// | |
// | |
// | |
// +---+
//
// All done!
static void write(arg_t &&value)
Writes the text representation of the specified value to the standard output stream.
Definition console.h:462
static int32 cursor_top()
Gets the row position of the cursor within the buffer area.
static void clear()
Clears the console buffer and corresponding console window of display information.
static void set_cursor_position(int32 left, int32 top)
Sets the position of the cursor.
static int32 cursor_left()
Gets the column position of the cursor within the buffer area.
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
@ s
The S key.
@ y
The Y key.
@ x
The X key.