xtd.console - Reference Guide  0.0.1
Modern c++17 library to manage terminal on Windows, macOS and Linux.
basic_console.hpp
Go to the documentation of this file.
1 #pragma once
4 #include "__generic_stream_output.hpp"
5 #include "__get_err_rdbuf.hpp"
6 #include "__get_in_rdbuf.hpp"
7 #include "__get_out_rdbuf.hpp"
8 #include "__opaque_console.hpp"
10 #include <xtd/strings>
11 
13 namespace xtd {
20  template<class Char>
21  class basic_console final {
22  public:
24  static std::basic_ostream<Char> error;
25 
30  static std::basic_istream<Char> in;
31 
36  static std::basic_ostream<Char> out;
37 
45 
47  basic_console() = delete;
49 
56  static console_color background_color() noexcept {return __opaque_console::background_color();}
57 
65  static bool background_color(console_color color) noexcept {return __opaque_console::background_color(color);}
66 
72  static void beep() noexcept {beep(800, 200);}
73 
81  static bool beep(unsigned int frequency, unsigned int duration) noexcept {return __opaque_console::beep(frequency, duration);}
82 
88  static int buffer_height() noexcept {return __opaque_console::buffer_height();}
89 
96  static bool buffer_height(int height) noexcept {return __opaque_console::buffer_height(height);}
97 
103  static int buffer_width() noexcept {return __opaque_console::buffer_width();}
104 
110  static bool buffer_width(int width) noexcept {return __opaque_console::buffer_width(width);}
111 
114  static bool caps_lock() noexcept {return __opaque_console::caps_lock();}
115 
126  static bool clear() noexcept {return __opaque_console::clear();}
127 
133  static int cursor_left() noexcept {return __opaque_console::cursor_left();}
134 
141  static bool cursor_left(int left) noexcept {return set_cursor_position(left, cursor_top());}
142 
148  static int cursor_size() noexcept {return __opaque_console::cursor_size();}
149 
156  static void cursor_size(int size) noexcept {__opaque_console::cursor_size(size);}
157 
163  static int cursor_top() noexcept {return __opaque_console::cursor_top();}
164 
171  static bool cursor_top(int top) noexcept {return set_cursor_position(cursor_left(), top);}
172 
178  static bool cursor_visible() noexcept {return __opaque_console::cursor_visible();}
179 
185  static void cursor_visible(bool visible) noexcept {__opaque_console::cursor_visible(visible);}
186 
192  static console_color foreground_color() noexcept {return __opaque_console::foreground_color();}
193 
199  static bool foreground_color(console_color color) noexcept {return __opaque_console::foreground_color(color);}
200 
203  static int input_code_page() noexcept {return __opaque_console::input_code_page();}
204 
208  static bool input_code_page(int code_page) noexcept {return __opaque_console::input_code_page(code_page);}
209 
212  static bool is_error_redireted() noexcept {return error.rdbuf() != __get_err_rdbuf<Char>();}
213 
216  static bool is_in_redireted() noexcept {return in.rdbuf() != __get_in_rdbuf<Char>();}
217 
220  static bool is_out_redireted() noexcept {return out.rdbuf() != __get_out_rdbuf<Char>();}
221 
226  static bool key_available() noexcept {__opaque_console::key_available();}
227 
230  static int largest_window_height() noexcept {return __opaque_console::largest_window_height();}
231 
234  static int largest_window_width() noexcept {return __opaque_console::largest_window_width();}
235 
238  static bool number_lock() noexcept {return __opaque_console::number_lock();}
239 
243  static std::basic_ostream<Char> open_standard_error() noexcept {return std::basic_ostream<Char>(__get_err_rdbuf<Char>());}
244 
248  static std::basic_istream<Char> open_standard_input() noexcept {return std::basic_istream<Char>(__get_in_rdbuf<Char>());}
249 
253  static std::basic_ostream<Char> open_standard_output() noexcept {return std::basic_ostream<Char>(__get_out_rdbuf<Char>());}
254 
257  static int output_code_page() noexcept {return __opaque_console::output_code_page();}
258 
262  static bool output_code_page(int code_page) noexcept {return __opaque_console::output_code_page(code_page);}
263 
264  static std::basic_string<Char> read_line() noexcept {
265  std::basic_string<Char> result;
266  in >> result;
267  return result;
268  }
272  static console_key_info read_key() {return read_key(false);}
273 
278  static console_key_info read_key(bool intercept) {
279  int key_char = 0;
280  int key_code = 0;
281  bool alt = false;
282  bool shift = false;
283  bool ctrl = false;
284 
285  out.flush();
286  __opaque_console::read_key(key_char, key_code, alt, shift, ctrl);
287  console_key_info key_info = console_key_info(key_char, static_cast<console_key>(key_code), shift, alt, ctrl);
288 
289  if (intercept == false)
290  write(Char(key_info.key_char()));
291  return key_info;
292  }
293 
294 
295  static bool reset_color() noexcept {return __opaque_console::reset_color();}
296 
297  static bool set_cursor_position(int left, int top) noexcept {return __opaque_console::set_cursor_position(left, top);}
298 
299  static void set_error(const std::ostream& os) noexcept {error.rdbuf(os.rdbuf());}
300 
301  static void set_in(const std::istream& is) noexcept {in.rdbuf(is.rdbuf());}
302 
303  static void set_out(const std::ostream& os) noexcept {out.rdbuf(os.rdbuf());}
304 
305  static std::string title() noexcept {return __opaque_console::title();}
306 
307  static bool title(const std::string& title) noexcept {return __opaque_console::title(title);}
308 
309  static bool treat_control_c_as_input() noexcept {return __opaque_console::treat_control_c_as_input();}
310 
311  static void treat_control_c_as_input(bool treat_control_c_as_input) noexcept {return __opaque_console::treat_control_c_as_input(treat_control_c_as_input);}
312 
313  static int window_height() noexcept {return __opaque_console::window_height();}
314 
315  static int window_left() noexcept {return __opaque_console::window_left();}
316 
317  static int window_top() noexcept {return __opaque_console::window_top();}
318 
319  static int window_width() noexcept {return __opaque_console::window_width();}
320 
321  template<typename Arg>
322  static void write(Arg&& arg) noexcept {out << strings::format(std::basic_string<Char> {'{', '}'}, arg);}
323 
325  template<typename Type>
326  static void write(std::initializer_list<Type>&& il) noexcept {out << strings::format(std::basic_string<Char> {'{', '}'}, il);}
328 
329  template<typename ... Args>
330  static void write(const std::basic_string<Char>& fmt, Args&& ... args) noexcept {out << strings::format(fmt, std::forward<Args>(args)...);}
331 
332  static void write_line() noexcept {out << std::endl;}
333 
334  template<typename Arg>
335  static void write_line(Arg&& arg) noexcept {out << arg << std::endl;}
336 
338  template<typename Type>
339  static void write_line(const std::initializer_list<Type>& il) noexcept {out << strings::format(std::basic_string<Char> {'{', '}'}, il) << std::endl;}
341 
342  template<typename ... Args>
343  static void write_line(const std::basic_string<Char>& fmt, Args&& ... args) noexcept {out << strings::format(fmt, std::forward<Args>(args)...) << std::endl;}
344  };
345 
347  template<class Char>
348  std::basic_ostream<Char> basic_console<Char>::error {__get_err_rdbuf<Char>()};
349  template<class Char>
350  std::basic_istream<Char> basic_console<Char>::in {__get_in_rdbuf<Char>()};
351  template<class Char>
352  std::basic_ostream<Char> basic_console<Char>::out {__get_out_rdbuf<Char>()};
353  template<class Char>
356 }
static bool cursor_left(int left) noexcept
Sets the column position of the cursor within the buffer area.
Definition: basic_console.hpp:141
static bool beep(unsigned int frequency, unsigned int duration) noexcept
Plays the sound of a beep of a specified frequency and duration through the console speaker...
Definition: basic_console.hpp:81
static void cursor_visible(bool visible) noexcept
Sets a value indicating whether the cursor is visible.
Definition: basic_console.hpp:185
static bool number_lock() noexcept
Gets a value indicating whether the NUM LOCK keyboard toggle is turned on or turned off...
Definition: basic_console.hpp:238
static std::basic_ostream< Char > error
Gets the error output stream. A std::basic_ostream<Char> that represents the error output stream...
Definition: basic_console.hpp:24
static bool caps_lock() noexcept
Gets a value indicating whether the CAPS LOCK keyboard toggle is turned on or turned off...
Definition: basic_console.hpp:114
The left or right SHIFT modifier key.
The left or right ALT modifier key.
console_color
Specifies constants that define foreground and background colors for the console. ...
Definition: console_color.hpp:12
static console_cancel_event_handler cancel_key_press
Occurs when the Control modifier key (Ctrl) and either the ConsoleKey.C console key (C) or the Break ...
Definition: basic_console.hpp:44
char32_t key_char()
Gets the Unicode character represented by the current console_key_info object.
Definition: console_key_info.hpp:50
static int largest_window_height() noexcept
Gets the largest possible number of console window rows, based on the current font and screen resolut...
Definition: basic_console.hpp:230
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: background_color.hpp:7
xtd::delegate< void(console_cancel_event_args &)> console_cancel_event_handler
Represents the method that will handle the cancel_key_press event of a xtd::console.
Definition: console_cancel_event_handler.hpp:12
static std::basic_istream< Char > in
Gets the standard input stream. A std::basic_istream<Char> that represents the standard input stream...
Definition: basic_console.hpp:30
static bool cursor_top(int top) noexcept
Sets the row position of the cursor within the buffer area.
Definition: basic_console.hpp:171
static bool key_available() noexcept
Gets a value indicating whether a key press is available in the input stream.
Definition: basic_console.hpp:226
static int cursor_size() noexcept
Gets or sets the height of the cursor within a character cell.
Definition: basic_console.hpp:148
static int input_code_page() noexcept
Gets the code page the console uses to read input.
Definition: basic_console.hpp:203
static bool clear() noexcept
Clears the console buffer and corresponding console window of display information.
Definition: basic_console.hpp:126
static bool input_code_page(int code_page) noexcept
Sets the code page the console uses to read input.
Definition: basic_console.hpp:208
static void beep() noexcept
Plays the sound of a beep through the console speaker.
Definition: basic_console.hpp:72
static console_key_info read_key(bool intercept)
Obtains the next character or function key pressed by the user. The pressed key is optionally display...
Definition: basic_console.hpp:278
static int buffer_width() noexcept
Gets the width of the buffer area.
Definition: basic_console.hpp:103
static bool is_out_redireted() noexcept
Gets a value that indicates whether the output stream has been redirected from the standard output st...
Definition: basic_console.hpp:220
static bool is_in_redireted() noexcept
Gets a value that indicates whether the input stream has been redirected from the standard input stre...
Definition: basic_console.hpp:216
static bool output_code_page(int code_page) noexcept
Sets the code page the console uses to write output.
Definition: basic_console.hpp:262
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...
Definition: basic_console.hpp:272
static console_color background_color() noexcept
Gets the background color of the console.
Definition: basic_console.hpp:56
static bool foreground_color(console_color color) noexcept
Sets the foreground color of the console.
Definition: basic_console.hpp:199
Represent reset color output manipulator class.
Definition: reset_color.hpp:10
static std::basic_ostream< Char > open_standard_error() noexcept
Acquires the standard error stream.
Definition: basic_console.hpp:243
static int largest_window_width() noexcept
Gets the largest possible number of console window columns, based on the current font and screen reso...
Definition: basic_console.hpp:234
static bool cursor_visible() noexcept
Gets a value indicating whether the cursor is visible.
Definition: basic_console.hpp:178
Specifies the standard keys on a console.
Definition: console_key_info.hpp:13
Represents the standard input, output, and error streams for console applications.
Definition: basic_console.hpp:21
static std::basic_istream< Char > open_standard_input() noexcept
Acquires the standard input stream.
Definition: basic_console.hpp:248
static bool buffer_width(int width) noexcept
Sets the width of the buffer area.
Definition: basic_console.hpp:110
static int buffer_height() noexcept
Gets the height of the buffer area.
Definition: basic_console.hpp:88
static void cursor_size(int size) noexcept
Sets the height of the cursor within a character cell.
Definition: basic_console.hpp:156
static bool is_error_redireted() noexcept
Gets a value that indicates whether the error output stream has been redirected from the standard err...
Definition: basic_console.hpp:212
static std::basic_ostream< Char > out
Gets the standard output stream. A std::basic_ostream<Char> that represents the standard output strea...
Definition: basic_console.hpp:36
static bool buffer_height(int height) noexcept
Gets or sets the height of the buffer area.
Definition: basic_console.hpp:96
static std::basic_ostream< Char > open_standard_output() noexcept
Acquires the standard output stream.
Definition: basic_console.hpp:253
static console_color foreground_color() noexcept
Gets the foreground color of the console.
Definition: basic_console.hpp:192
Contains xtd::beep class.
static int output_code_page() noexcept
Gets the code page the console uses to write output.
Definition: basic_console.hpp:257
The CapsLock key.
static int cursor_left() noexcept
Gets the column position of the cursor within the buffer area.
Definition: basic_console.hpp:133
static int cursor_top() noexcept
Gets the row position of the cursor within the buffer area.
Definition: basic_console.hpp:163
static bool background_color(console_color color) noexcept
Sets the background color of the console.
Definition: basic_console.hpp:65