1#ifndef NEFORCE_CORE_SYSTEM_CONSOLE_HPP__
2#define NEFORCE_CORE_SYSTEM_CONSOLE_HPP__
12NEFORCE_BEGIN_NAMESPACE__
24struct console_exception final : device_exception {
25 explicit console_exception(
const char* info =
"Pipe Operation Failed.",
const char*
type = static_type,
26 const int code = 0) noexcept :
27 device_exception(info,
type,
code) {}
29 explicit console_exception(
const exception& e) :
30 device_exception(e) {}
32 ~console_exception()
override =
default;
33 static constexpr auto static_type =
"console_exception";
59class NEFORCE_API sys_console {
69 explicit console_size(
const int w = 0,
const int h = 0) :
76 NEFORCE_NODISCARD
bool operator==(
const console_size& other)
const noexcept {
77 return width == other.width &&
height == other.height;
83 NEFORCE_NODISCARD
bool operator!=(
const console_size& other)
const noexcept {
return !(*
this == other); }
91#ifdef NEFORCE_PLATFORM_WINDOWS
92 console_size saved_cursor_pos_{0, 0};
96 console_size last_size_{0, 0};
98 bool alt_buffer_active_ =
false;
99 bool mouse_enabled_ =
false;
100 int pending_char_ = -1;
103 void print_string_unsafe(
const string& str)
const { print_string_unsafe(str.
view()); }
105 void print_string_unsafe(
const char* str)
const { print_string_unsafe(
string_view{str}); }
107 void print_error_unsafe(
const string& str)
const { print_error_unsafe(str.
view()); }
109 void print_error_unsafe(
const char* str)
const { print_error_unsafe(
string_view{str}); }
111 void set_color_unsafe(
const color& color,
bool use_256_color)
const;
114 string readln_unsafe()
const;
115 string read_unsafe()
const;
116 char read_char_unsafe()
const;
117 console_size get_console_size_unsafe()
const;
119 void flush_unsafe()
const;
120 void ignore_unsafe()
const;
122 void beep_unsafe()
const;
123 void flash_screen_unsafe()
const;
126 bool is_fade_in)
const;
129 sys_console() noexcept;
132 sys_console(const sys_console&) = delete;
133 sys_console& operator=(const sys_console&) = delete;
134 sys_console(sys_console&&) = delete;
135 sys_console& operator=(sys_console&&) = delete;
137 ~sys_console() = default;
217 template <
typename... Args>
229 template <
typename... Args>
241 template <
typename... Args>
244 this->set_color_unsafe(
color,
false);
246 this->print_string_unsafe(
"\033[0m");
256 template <
typename... Args>
259 this->set_color_unsafe(
color,
false);
261 this->print_string_unsafe(
"\033[0m");
273 template <
typename... Args,
enable_if_t<(
sizeof...(Args) > 0),
int> = 0>
277 this->print_string_unsafe(
"\n");
286 template <
typename... Args>
290 this->print_string_unsafe(
"\n");
299 template <
typename... Args>
302 this->set_color_unsafe(
color,
false);
304 this->print_string_unsafe(
"\033[0m\n");
314 template <
typename... Args>
317 this->set_color_unsafe(
color,
false);
319 this->print_string_unsafe(
"\033[0m\n");
327 template <
typename... Args>
339 template <
typename... Args>
355 template <
typename... Args,
enable_if_t<(
sizeof...(Args) > 0),
int> = 0>
359 this->print_error_unsafe(
"\n");
368 template <
typename... Args>
372 this->print_error_unsafe(
"\n");
526 void progress_bar(
double percentage,
int width = 50,
bool show_percentage =
true,
char fill_char =
'#',
527 char empty_char =
' ');
672template <
typename... Args>
680template <
typename... Args>
688template <
typename... Args>
696template <
typename... Args>
704template <
typename... Args>
712template <
typename... Args>
720template <
typename... Args>
728template <
typename... Args>
736template <
typename... Args>
744template <
typename... Args>
752template <
typename... Args>
760template <
typename... Args>
767NEFORCE_END_NAMESPACE__
constexpr view_type view() const noexcept
获取字符串视图
static constexpr color black() noexcept
黑色
static constexpr color white() noexcept
白色
void set_color(const color &color, bool use_256_color=true)
设置颜色
void set_window_title(string_view title)
设置控制台窗口标题
void printfln(const string_view fmt, Args &&... args)
格式化打印并换行
void printf(const string_view fmt, Args &&... args)
格式化打印
string read()
读取输入(直到空白字符)
void progress_bar(double percentage, int width=50, bool show_percentage=true, char fill_char='#', char empty_char=' ')
显示进度条
void reset_scroll_region()
重置滚动区域为整个屏幕
void set_color(const integer32 &color)
设置颜色(使用ANSI代码)
void fade_in(string_view text, milliseconds duration=milliseconds(1000), const color &start_color=color::black(), const color &end_color=color::white())
淡入效果
void fade_out(string_view text, milliseconds duration=milliseconds(1000), const color &start_color=color::white(), const color &end_color=color::black())
淡出效果
void eprintfln(const string_view fmt, Args &&... args)
格式化打印到错误流并换行
void printcfln(const color &color, const string_view fmt, Args &&... args)
带颜色的格式化打印并换行
bool supports_truecolor() const
检查是否支持真彩色
bool is_mouse_enabled() const
检测当前是否已启用鼠标输入
void printc(const color &color, Args &&... args)
带颜色的打印
string password(string_view prompt="Password: ", char mask=' *', bool show_length=false)
密码输入
void enable_mouse(bool enable=true)
启用或禁用鼠标输入捕获
void print_string(const char *str)
打印C风格字符串
void typewriter_print(string_view text, milliseconds delay_per_char=milliseconds(50), bool with_sound=false)
打字机效果打印
void disable_alternate_screen_buffer()
禁用交替屏幕缓冲区
void notification(string_view message, milliseconds duration=milliseconds(2000), bool play_sound=true)
显示通知
void enable_alternate_screen_buffer(bool enable=true)
启用或禁用交替屏幕缓冲区
void set_cursor_position(int row, int column)
设置光标位置
void print(Args &&... args)
打印任意类型的值
bool confirmation(string_view prompt="Are you sure? (y/n): ", char yes='y', char no='n')
用户确认对话框
void save_cursor_position()
保存光标位置
void set_reverse(bool enable=true)
设置反色(交换前景色与背景色)
void set_bold(bool enable=true)
设置粗体
void print_error(const char *str)
打印C风格字符串到错误流
void eprintf(const string_view fmt, Args &&... args)
格式化打印到错误流
static sys_console & instance() noexcept
获取单例实例
void reset_text_attributes()
重置所有文本属性
void set_blink(bool enable=true)
设置闪烁
string console_type() const
获取终端类型
void pause(string_view msg="Press any char to continue...")
暂停并等待用户按键
void set_background_color(const color &color, bool use_256_color=true)
设置背景色
void fade_in_out(string_view text, milliseconds in_duration=milliseconds(500), milliseconds hold_duration=milliseconds(1000), milliseconds out_duration=milliseconds(500))
淡入-保持-淡出效果
void set_underline(bool enable=true)
设置下划线
bool supports_unicode() const
检查是否支持Unicode
void typewriter_println(string_view text, milliseconds delay_per_char=milliseconds(50), bool with_sound=false)
打字机效果打印并换行
void print_string(const string &str)
打印字符串
bool is_terminal_resized()
检查终端是否已调整大小
void set_scroll_region(int top, int bottom)
设置滚动区域
void eprintln(Args &&... args)
打印任意类型的值到错误流并换行
void disable_mouse()
禁用鼠标输入捕获
void eprint(Args &&... args)
打印任意类型的值到错误流
void restore_cursor_position()
恢复光标位置
int getch()
读取一个字符(无回显,若无按键则阻塞)
console_size get_console_size() const
获取控制台尺寸
bool supports_colors() const
检查是否支持颜色
bool kbhit()
检查是否有键盘输入可读(非阻塞)
bool is_interactive() const
检查是否是交互式终端
void print_string(const string_view &view)
打印字符串视图
void print_error(const string_view &view)
打印字符串视图到错误流
void print_error(const string &str)
打印字符串到错误流
void printcln(const color &color, Args &&... args)
带颜色的打印并换行
void printcf(const color &color, const string_view fmt, Args &&... args)
带颜色的格式化打印
void println(Args &&... args)
打印任意类型的值并换行
constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
void printcf(const color &color, const string_view fmt, Args &&... args)
格式化颜色打印
void printfln(const string_view fmt, Args &&... args)
格式化打印并换行
void eprintfln(const string_view fmt, Args &&... args)
格式化打印到错误流并换行
void printc(const color &color, Args &&... args)
带颜色打印多个值
void eprint(Args &&... args)
打印多个值到错误流
void eprintln(Args &&... args)
打印多个值到错误流并换行
void print(Args &&... args)
打印多个值
void printf(const string_view fmt, Args &&... args)
格式化打印
void printcfln(const color &color, const string_view fmt, Args &&... args)
格式化颜色打印并换行
sys_console & console
全局控制台实例
void eprintf(const string_view fmt, Args &&... args)
格式化打印到错误流
void println(Args &&... args)
打印多个值并换行
void printcln(const color &color, Args &&... args)
带颜色打印多个值并换行
void * native_handle_type
系统句柄类型
duration< int64_t, milli > milliseconds
毫秒持续时间
basic_string_view< char > string_view
字符字符串视图
typename enable_if< Test, T >::type enable_if_t
enable_if的便捷别名
constexpr string to_string(const CharT &c)
将字符转换为普通字符串
exception(const char *info=static_type, const char *type=static_type, const int code=0)
构造函数
const char * type() const noexcept
获取异常类型
int code() const noexcept
获取异常码
bool operator!=(const console_size &other) const noexcept
不等比较
bool operator==(const console_size &other) const noexcept
相等比较