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); }
87#ifdef NEFORCE_PLATFORM_WINDOWS
90 console_size saved_cursor_pos_{0, 0};
95 mutable mutex mutex_{};
96 console_size last_size_{0, 0};
99 void print_string_unsafe(
const string& str)
const { print_string_unsafe(str.
view()); }
101 void print_string_unsafe(
const char* str)
const { print_string_unsafe(
string_view{str}); }
103 void set_color_unsafe(
const color& color,
bool use_256_color)
const;
106 string readln_unsafe()
const;
107 string read_unsafe()
const;
108 char read_char_unsafe()
const;
109 console_size get_console_size_unsafe()
const;
111 void flush_unsafe()
const;
113 void beep_unsafe()
const;
114 void flash_screen_unsafe()
const;
117 bool is_fade_in)
const;
120 sys_console() noexcept;
123 sys_console(const sys_console&) = delete;
124 sys_console& operator=(const sys_console&) = delete;
125 sys_console(sys_console&&) = delete;
126 sys_console& operator=(sys_console&&) = delete;
128 ~sys_console() = default;
185 template <
typename... Args>
197 template <
typename... Args>
209 template <
typename... Args>
212 this->set_color_unsafe(
color,
false);
214 this->print_string_unsafe(
"\033[0m");
224 template <
typename... Args>
227 this->set_color_unsafe(
color,
false);
229 this->print_string_unsafe(
"\033[0m");
241 template <
typename... Args,
enable_if_t<(
sizeof...(Args) > 0),
int> = 0>
245 this->print_string_unsafe(
"\n");
254 template <
typename... Args>
258 this->print_string_unsafe(
"\n");
267 template <
typename... Args>
270 this->set_color_unsafe(
color,
false);
272 this->print_string_unsafe(
"\033[0m\n");
282 template <
typename... Args>
285 this->set_color_unsafe(
color,
false);
287 this->print_string_unsafe(
"\033[0m\n");
353 void progress_bar(
double percentage,
int width = 50,
bool show_percentage =
true,
char fill_char =
'#',
354 char empty_char =
' ');
499template <
typename... Args>
507template <
typename... Args>
515template <
typename... Args>
523template <
typename... Args>
531template <
typename... Args>
539template <
typename... Args>
547template <
typename... Args>
555template <
typename... Args>
562NEFORCE_END_NAMESPACE__
NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 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 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=' ')
显示进度条
NEFORCE_NODISCARD bool is_terminal_resized()
检查终端是否已调整大小
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())
淡出效果
NEFORCE_NODISCARD console_size get_console_size() const
获取控制台尺寸
void printcfln(const color &color, const string_view fmt, Args &&... args)
带颜色的格式化打印并换行
NEFORCE_NODISCARD bool supports_colors() const
检查是否支持颜色
void printc(const color &color, Args &&... args)
带颜色的打印
string password(string_view prompt="Password: ", char mask=' *', bool show_length=false)
密码输入
void print_string(const char *str)
打印C风格字符串
void typewriter_print(string_view text, milliseconds delay_per_char=milliseconds(50), bool with_sound=false)
打字机效果打印
void notification(string_view message, milliseconds duration=milliseconds(2000), bool play_sound=true)
显示通知
NEFORCE_NODISCARD bool supports_truecolor() const
检查是否支持真彩色
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()
保存光标位置
NEFORCE_NODISCARD bool is_interactive() const
检查是否是交互式终端
static sys_console & instance() noexcept
获取单例实例
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 typewriter_println(string_view text, milliseconds delay_per_char=milliseconds(50), bool with_sound=false)
打字机效果打印并换行
void print_string(const string &str)
打印字符串
NEFORCE_NODISCARD bool supports_unicode() const
检查是否支持Unicode
NEFORCE_NODISCARD string console_type() const
获取终端类型
void restore_cursor_position()
恢复光标位置
void print_string(const string_view &view)
打印字符串视图
void printcln(const color &color, Args &&... args)
带颜色的打印并换行
void printcf(const color &color, const string_view fmt, Args &&... args)
带颜色的格式化打印
void println(Args &&... args)
打印任意类型的值并换行
NEFORCE_NODISCARD 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 printc(const color &color, 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)
格式化颜色打印并换行
void println(Args &&... args)
打印多个值并换行
void printcln(const color &color, Args &&... args)
带颜色打印多个值并换行
NEFORCE_API sys_console & console
全局控制台实例
int 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的便捷别名
NEFORCE_CONSTEXPR20 string to_string(const CharT &c)
将字符转换为普通字符串
exception(const char *info=static_type, const char *type=static_type, const int code=0)
构造函数
NEFORCE_NODISCARD int code() const noexcept
获取异常码
NEFORCE_NODISCARD const char * type() const noexcept
获取异常类型
NEFORCE_NODISCARD bool operator!=(const console_size &other) const noexcept
不等比较
NEFORCE_NODISCARD bool operator==(const console_size &other) const noexcept
相等比较