1#ifndef NEFORCE_CORE_SYSTEM_CONSOLE_HPP__
2#define NEFORCE_CORE_SYSTEM_CONSOLE_HPP__
10#include "NeForce/core/exception/system_exception.hpp"
14NEFORCE_BEGIN_NAMESPACE__
26struct console_exception final : device_exception {
27 explicit console_exception(
const char* info =
"Pipe Operation Failed.",
29 device_exception(info, code) {}
31 explicit console_exception(
const exception& e) :
32 device_exception(e) {}
34 ~console_exception()
override =
default;
36 NEFORCE_NODISCARD
const char*
type() const noexcept
override {
return "console_exception"; }
62class NEFORCE_API sys_console {
72 explicit console_size(
const int w = 0,
const int h = 0) :
79 NEFORCE_NODISCARD
bool operator==(
const console_size& other)
const noexcept {
80 return width == other.width &&
height == other.height;
86 NEFORCE_NODISCARD
bool operator!=(
const console_size& other)
const noexcept {
return !(*
this == other); }
94#ifdef NEFORCE_PLATFORM_WINDOWS
95 console_size saved_cursor_pos_{0, 0};
99 console_size last_size_{0, 0};
101 bool alt_buffer_active_ =
false;
102 bool mouse_enabled_ =
false;
103 int pending_char_ = -1;
106 void print_string_unsafe(
const string& str)
const { print_string_unsafe(str.view()); }
107 void print_string_unsafe(string_view str)
const;
108 void print_string_unsafe(
const char* str)
const { print_string_unsafe(string_view{str}); }
110 void print_error_unsafe(
const string& str)
const { print_error_unsafe(str.view()); }
111 void print_error_unsafe(string_view str)
const;
112 void print_error_unsafe(
const char* str)
const { print_error_unsafe(string_view{str}); }
114 void set_color_unsafe(
const color& color,
bool use_256_color)
const;
115 void typewriter_print_unsafe(string_view text, milliseconds delay_per_char,
bool with_sound)
const;
117 string readln_unsafe()
const;
118 string read_unsafe()
const;
119 char read_char_unsafe()
const;
120 console_size get_console_size_unsafe()
const;
122 void flush_unsafe()
const;
123 void ignore_unsafe()
const;
125 void beep_unsafe()
const;
126 void flash_screen_unsafe()
const;
128 void fade_effect_unsafe(string_view text,
const color& from,
const color& to, milliseconds duration,
129 bool is_fade_in)
const;
132 sys_console() noexcept;
135 sys_console(const sys_console&) = delete;
136 sys_console& operator=(const sys_console&) = delete;
137 sys_console(sys_console&&) = delete;
138 sys_console& operator=(sys_console&&) = delete;
140 ~sys_console() = default;
220 template <
typename... Args>
232 template <
typename... Args>
244 template <
typename... Args>
247 this->set_color_unsafe(
color,
false);
249 this->print_string_unsafe(
"\033[0m");
259 template <
typename... Args>
262 this->set_color_unsafe(
color,
false);
264 this->print_string_unsafe(
"\033[0m");
276 template <
typename... Args,
enable_if_t<(
sizeof...(Args) > 0),
int> = 0>
280 this->print_string_unsafe(
"\n");
289 template <
typename... Args>
293 this->print_string_unsafe(
"\n");
302 template <
typename... Args>
305 this->set_color_unsafe(
color,
false);
307 this->print_string_unsafe(
"\033[0m\n");
317 template <
typename... Args>
320 this->set_color_unsafe(
color,
false);
322 this->print_string_unsafe(
"\033[0m\n");
330 template <
typename... Args>
342 template <
typename... Args>
358 template <
typename... Args,
enable_if_t<(
sizeof...(Args) > 0),
int> = 0>
362 this->print_error_unsafe(
"\n");
371 template <
typename... Args>
375 this->print_error_unsafe(
"\n");
530 void progress_bar(
double percentage,
int width = 50,
bool show_percentage =
true,
char fill_char =
'#',
531 char empty_char =
' ');
682template <
typename... Args>
690template <
typename... Args>
698template <
typename... Args>
706template <
typename... Args>
714template <
typename... Args>
722template <
typename... Args>
730template <
typename... Args>
738template <
typename... Args>
746template <
typename... Args>
754template <
typename... Args>
762template <
typename... Args>
770template <
typename... Args>
777NEFORCE_END_NAMESPACE__
static constexpr color black() noexcept
黑色
static constexpr color white() noexcept
白色
void set_reverse(bool enable=true)
设置反色(交换前景色与背景色)
void set_underline(bool enable=true)
设置下划线
void set_cursor_position(int row, int column)
设置光标位置
void hide_cursor(bool enable=true)
隐藏光标
bool supports_unicode() const
检查是否支持Unicode
string read()
读取输入(直到空白字符)
bool confirmation(string_view prompt="Are you sure? (y/n): ", char yes='y', char no='n')
用户确认对话框
void eprintln(Args &&... args)
打印任意类型的值到错误流并换行
void print_string(const char *str)
打印C风格字符串
void fade_in(string_view text, milliseconds duration=milliseconds(1000), const color &start_color=color::black(), const color &end_color=color::white())
淡入效果
void set_window_title(string_view title)
设置控制台窗口标题
void reset_text_attributes()
重置所有文本属性
void printf(const string_view fmt, Args &&... args)
格式化打印
void set_scroll_region(int top, int bottom)
设置滚动区域
console_size query_cursor_position()
查询光标位置
void set_background_color(const color &color, bool use_256_color=true)
设置背景色
console_size get_console_size() const
获取控制台尺寸
void eprint(Args &&... args)
打印任意类型的值到错误流
bool kbhit()
检查是否有键盘输入可读(非阻塞)
void notification(string_view message, milliseconds duration=milliseconds(2000), bool play_sound=true)
显示通知
void printcln(const color &color, Args &&... args)
带颜色的打印并换行
void set_color(const color &color, bool use_256_color=true)
设置颜色
void eprintfln(const string_view fmt, Args &&... args)
格式化打印到错误流并换行
void enable_alternate_screen_buffer(bool enable=true)
启用或禁用交替屏幕缓冲区
void restore_cursor_position()
恢复光标位置
bool supports_colors() const
检查是否支持颜色
void print_string(const string &str)
打印字符串
void print_string(const string_view &view)
打印字符串视图
void pause(string_view msg="Press enter to continue...")
暂停并等待用户按键
bool supports_truecolor() const
检查是否支持真彩色
void printc(const color &color, Args &&... args)
带颜色的打印
void fade_out(string_view text, milliseconds duration=milliseconds(1000), const color &start_color=color::white(), const color &end_color=color::black())
淡出效果
void printcfln(const color &color, const string_view fmt, Args &&... args)
带颜色的格式化打印并换行
void typewriter_println(string_view text, milliseconds delay_per_char=milliseconds(50), bool with_sound=false)
打字机效果打印并换行
void printfln(const string_view fmt, Args &&... args)
格式化打印并换行
void enable_mouse(bool enable=true)
启用或禁用鼠标输入捕获
void typewriter_print(string_view text, milliseconds delay_per_char=milliseconds(50), bool with_sound=false)
打字机效果打印
void set_output_utf8()
设置控制台输出代码页为 UTF-8
bool is_interactive() const
检查是否是交互式终端
void save_cursor_position()
保存光标位置
void progress_bar(double percentage, int width=50, bool show_percentage=true, char fill_char='#', char empty_char=' ')
显示进度条
void print_error(const string_view &view)
打印字符串视图到错误流
bool is_mouse_enabled() const
检测当前是否已启用鼠标输入
void eprintf(const string_view fmt, Args &&... args)
格式化打印到错误流
void set_blink(bool enable=true)
设置闪烁
void print_error(const string &str)
打印字符串到错误流
string console_type() const
获取终端类型
void print_error(const char *str)
打印C风格字符串到错误流
string password(string_view prompt="Password: ", char mask=' *', bool show_length=false)
密码输入
void set_color(const integer32 &color)
设置颜色(使用ANSI代码)
void print(Args &&... args)
打印任意类型的值
void set_bold(bool enable=true)
设置粗体
void reset_scroll_region()
重置滚动区域为整个屏幕
void printcf(const color &color, const string_view fmt, Args &&... args)
带颜色的格式化打印
void fade_in_out(string_view text, milliseconds in_duration=milliseconds(500), milliseconds hold_duration=milliseconds(1000), milliseconds out_duration=milliseconds(500))
淡入-保持-淡出效果
int getch()
读取一个字符(无回显,若无按键则阻塞)
bool is_terminal_resized()
检查终端是否已调整大小
void println(Args &&... args)
打印任意类型的值并换行
void enable_virtual_terminal_processing(bool enable=true)
启用虚拟终端处理
static sys_console & instance() noexcept
获取单例实例
constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
void printf(const string_view fmt, Args &&... args)
格式化打印
void printcf(const color &color, const string_view fmt, Args &&... args)
格式化颜色打印
void eprint(Args &&... args)
打印多个值到错误流
void eprintf(const string_view fmt, Args &&... args)
格式化打印到错误流
void printc(const color &color, Args &&... args)
带颜色打印多个值
void printcln(const color &color, Args &&... args)
带颜色打印多个值并换行
void print(Args &&... args)
打印多个值
void eprintln(Args &&... args)
打印多个值到错误流并换行
sys_console & console
全局控制台实例
void printcfln(const color &color, const string_view fmt, Args &&... args)
格式化颜色打印并换行
void eprintfln(const string_view fmt, Args &&... args)
格式化打印到错误流并换行
void println(Args &&... args)
打印多个值并换行
void printfln(const string_view fmt, Args &&... args)
格式化打印并换行
void * native_handle_type
系统句柄类型
duration< int64_t, milli > milliseconds
毫秒持续时间
error_code last_error() noexcept
获取当前系统错误码
string concatenate(Args &&... args)
拼接参数
basic_string_view< char > string_view
字符字符串视图
typename enable_if< Test, T >::type enable_if_t
enable_if的便捷别名
constexpr string to_string(const CharT &c)
将字符转换为普通字符串
const char * type() const noexcept override
获取异常类型
bool operator!=(const console_size &other) const noexcept
不等比较
bool operator==(const console_size &other) const noexcept
相等比较