xtd 0.2.0
console_clear.cpp

Shows how to use xtd::console::clear method.

#include <xtd/collections/generic/list>
#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 argument_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;
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("Enter a message to display: ");
string text_to_display = console::read_line();
console::foreground_color(new_fore_color);
console::background_color(new_back_color);
console::write_line(text_to_display);
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);
} 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;
do {
key_pressed = console::read_key();
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);
static char to_upper(char32 c) noexcept
Definition box_char.hpp:204
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.hpp:79
const_iterator begin() const noexcept override
Returns an iterator to the first element of the enumarable.
Definition list.hpp:253
const_iterator end() const noexcept override
Returns an iterator to the element following the last element of the enumarable.
Definition list.hpp:366
Specifies the standard keys on a console.
Definition console_key_info.hpp:24
static xtd::string read_line()
Reads the next line of characters from the standard input stream.
static void write(arg_t &&value)
Writes the text representation of the specified value to the standard output stream.
Definition console.hpp:462
static console_color background_color()
Gets the background color of the console.
static console_color foreground_color()
Gets the foreground color of the console.
static void clear()
Clears the console buffer and corresponding console window of display information.
static console_key_info read_key()
Obtains the next character or function key pressed by the user. The pressed key is displayed in the c...
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.hpp:167
xtd::array< xtd::string > argument_collection
Represents the collection of arguments passed to the main entry point method.
Definition argument_collection.hpp:19
@ black
The color black.
Definition console_color.hpp:24
@ dark_blue
The color dark blue.
Definition console_color.hpp:26
@ magenta
The color magenta (purplish red).
Definition console_color.hpp:50
@ dark_red
The color dark red.
Definition console_color.hpp:32
@ green
The color green.
Definition console_color.hpp:44
@ white
The color white.
Definition console_color.hpp:54
@ dark_yellow
The color dark yellow (ochre).
Definition console_color.hpp:36
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.hpp:16
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8