Console Library
8.0.0
A header-only library that makes C++ simple
Toggle main menu visibility
Loading...
Searching...
No Matches
term.h
Go to the documentation of this file.
1
8
9
/*
10
Copyright (c) 2026 MrXie1109
11
12
Permission is hereby granted, free of charge, to any person obtaining a copy
13
of this software and associated documentation files (the "Software"), to deal
14
in the Software without restriction, including without limitation the rights
15
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
copies of the Software, and to permit persons to whom the Software is
17
furnished to do so, subject to the following conditions:
18
19
The above copyright notice and this permission notice shall be included in all
20
copies or substantial portions of the Software.
21
22
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
SOFTWARE.
29
*/
30
31
#pragma once
32
#include <cstdio>
33
#include <utility>
34
35
#ifdef _WIN32
36
#define WIN32_LEAN_AND_MEAN
// 排除不常用的 Windows API
37
#define VC_EXTRALEAN
// 排除某些 MFC 扩展
38
#define NOCOMM
// 不包含 comm 相关
39
#define NOCRYPT
// 不包含加密 API
40
#define NOMCX
// 不包含 Modem 扩展
41
#define NOSERVICE
// 不包含服务相关
42
#define NOATOM
// 不包含原子操作
43
#define NOGDICAPMASKS
// 不包含 GDI 掩码
44
#define NOMETAFILE
// 不包含 MetaFile
45
#define NOMINMAX 1
// 不定义 min/max 宏
46
#define NOMSG
// 不包含消息结构
47
#define NOOPENFILE
// 不包含 OpenFile
48
#define NORPC
// 不包含 RPC
49
#define NOSOUND
// 不包含声音
50
#define NOSYSCOMMANDS
// 不包含系统命令
51
#define NOTEXTMETRIC
// 不包含 TEXTMETRIC
52
#define NOWH
// 不包含 WH_* 钩子
53
#define NOKANJI
// 不包含日文支持
54
#define NOHELP
// 不包含帮助
55
#define NODRAWTEXT
// 不包含 DrawText
56
#define NOGDI
// 不包含 GDI
57
#define NOUSER
// 不包含 USER
58
#define NOKERNEL
// 不包含 KERNEL
59
#define NOMB
// 不包含 MB_* 宏
60
#define NOMEMMGR
// 不包含内存管理
61
#define NOMETAFILE
// 不包含 MetaFile
62
#define NOMSG
// 不包含消息
63
#define NOOPENFILE
// 不包含 OpenFile
64
#define NOSCROLL
// 不包含滚动条
65
#define NOSHOW
// 不包含 ShowWindow
66
#define NOSIZE
// 不包含 SIZE
67
#define NOSOUND
// 不包含声音
68
#define NOSTRING
// 不包含字符串
69
#define NOSYSMETRICS
// 不包含系统指标
70
#define NOTEXTMETRIC
// 不包含 TEXTMETRIC
71
#define NOWINMESSAGES
// 不包含窗口消息
72
#define NOWINSTYLES
// 不包含窗口样式
73
#define NOWINOFFSETS
// 不包含窗口偏移
74
#define NOSYSCOMMANDS
// 不包含系统命令
75
#define NOCLIPBOARD
// 不包含剪贴板
76
#define NOCOLOR
// 不包含颜色
77
#define NODEFERWINDOWPOS
// 不包含 DeferWindowPos
78
#define NODRAWTEXT
// 不包含 DrawText
79
#define NOGDI
// 不包含 GDI
80
#define NOGDICAPMASKS
// 不包含 GDI 掩码
81
#define NOHELP
// 不包含帮助
82
#define NOICONS
// 不包含图标
83
#define NOMENUS
// 不包含菜单
84
#define NOMETAFILE
// 不包含 MetaFile
85
#define NOMSG
// 不包含消息
86
#define NOOPENFILE
// 不包含 OpenFile
87
#define NOPROFILER
// 不包含性能分析
88
#define NORASTEROPS
// 不包含光栅操作
89
#define NOSCROLL
// 不包含滚动条
90
#define NOSOUND
// 不包含声音
91
#define NOSYSCOMMANDS
// 不包含系统命令
92
#define NOTEXTMETRIC
// 不包含文本度量
93
#define NOWH
// 不包含 WH_* 钩子
94
#define NOWINOFFSETS
// 不包含窗口偏移
95
#define NOMB
// 不包含 MB_* 宏
96
#include <windows.h>
97
#endif
98
99
#ifdef __unix__
100
#include <sys/ioctl.h>
// ioctl, TIOCGWINSZ
101
#include <unistd.h>
// STDOUT_FILENO
102
#endif
103
104
namespace
console
{
110
inline
void
gotoxy
(
int
x,
int
y)
noexcept
{
111
printf(
"\033[%d;%dH"
, y, x);
112
}
113
117
inline
void
clear
() noexcept {
118
printf(
"\033[2J\033[H"
);
119
}
120
124
inline
void
save_cursor
() noexcept {
125
printf(
"\033[s"
);
126
}
127
131
inline
void
restore_cursor
() noexcept {
132
printf(
"\033[u"
);
133
}
134
138
inline
void
hide_cursor
() noexcept {
139
printf(
"\033[?25l"
);
140
}
141
145
inline
void
show_cursor
() noexcept {
146
printf(
"\033[?25h"
);
147
}
148
154
inline
std::pair<int, int>
get_terminal_size
() noexcept {
155
#ifdef _WIN32
156
CONSOLE_SCREEN_BUFFER_INFO csbi;
157
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
158
if
(GetConsoleScreenBufferInfo(hConsole, &csbi)) {
159
int
cols = csbi.srWindow.Right - csbi.srWindow.Left + 1;
160
int
rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
161
return
{rows, cols};
162
}
163
return
{0, 0};
164
#elif defined(__unix__) || defined(__APPLE__)
165
struct
winsize w;
166
if
(ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == 0)
167
return
{w.ws_row, w.ws_col};
168
return
{0, 0};
169
#else
170
return
{0, 0};
171
#endif
172
}
173
177
inline
void
reset
() noexcept {
178
printf(
"\033[0m\033[?25h"
);
179
}
180
184
inline
void
clear_line
() noexcept {
185
printf(
"\033[2K"
);
186
}
187
191
inline
void
clear_to_eol
() noexcept {
192
printf(
"\033[K"
);
193
}
194
199
inline
void
movel
(
int
n = 1) noexcept {
200
printf(
"\033[%dD"
, n);
201
}
202
207
inline
void
mover
(
int
n = 1) noexcept {
208
printf(
"\033[%dC"
, n);
209
}
210
215
inline
void
moveu
(
int
n = 1) noexcept {
216
printf(
"\033[%dA"
, n);
217
}
218
223
inline
void
moved
(
int
n = 1) noexcept {
224
printf(
"\033[%dB"
, n);
225
}
226
230
inline
void
save_screen
() noexcept {
231
printf(
"\033[?47h"
);
232
}
233
237
inline
void
restore_screen
() noexcept {
238
printf(
"\033[?47l"
);
239
}
240
244
inline
void
clear_screen_save
() noexcept {
245
printf(
"\033[?47h\033[2J\033[H"
);
246
}
247
248
#ifdef _WIN32
253
inline
const
bool
&init_windows_terminal() noexcept {
254
static
bool
initialized = []()
noexcept
->
bool
{
255
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
256
if
(hConsole == INVALID_HANDLE_VALUE || hConsole ==
nullptr
)
257
return
false
;
258
DWORD mode;
259
if
(!GetConsoleMode(hConsole, &mode))
return
false
;
260
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
261
mode |= ENABLE_PROCESSED_OUTPUT;
262
263
return
SetConsoleMode(hConsole, mode) != 0;
264
}();
265
return
initialized;
266
}
267
268
static
const
bool
&
supports_ansi
= init_windows_terminal();
269
#elif defined(__unix__) || defined(__APPLE__)
270
static
const
bool
&
supports_ansi
=
true
;
271
#else
272
static
const
bool
&
supports_ansi
=
false
;
273
#endif
280
}
console
本库所有组件所在的顶层命名空间。
console::show_cursor
void show_cursor() noexcept
显示光标(恢复默认)。
Definition
term.h:145
console::clear_screen_save
void clear_screen_save() noexcept
清空屏幕并保留历史。
Definition
term.h:244
console::gotoxy
void gotoxy(int x, int y) noexcept
将光标移动到指定位置。
Definition
term.h:110
console::moved
void moved(int n=1) noexcept
向下相对移动。
Definition
term.h:223
console::restore_screen
void restore_screen() noexcept
恢复屏幕内容。
Definition
term.h:237
console::hide_cursor
void hide_cursor() noexcept
隐藏光标(提高视觉效果)。
Definition
term.h:138
console::moveu
void moveu(int n=1) noexcept
向上相对移动。
Definition
term.h:215
console::supports_ansi
static const bool & supports_ansi
可能佯败
Definition
term.h:272
console::save_screen
void save_screen() noexcept
保存屏幕内容。
Definition
term.h:230
console::mover
void mover(int n=1) noexcept
向右相对移动。
Definition
term.h:207
console::clear
void clear() noexcept
清空整个屏幕并将光标复位到 (1,1)。
Definition
term.h:117
console::save_cursor
void save_cursor() noexcept
保存当前光标位置。
Definition
term.h:124
console::movel
void movel(int n=1) noexcept
向左相对移动。
Definition
term.h:199
console::get_terminal_size
std::pair< int, int > get_terminal_size() noexcept
获取终端尺寸(行数和列数)。
Definition
term.h:154
console::reset
void reset() noexcept
重置终端所有属性(颜色、样式、光标等)到默认状态。
Definition
term.h:177
console::clear_to_eol
void clear_to_eol() noexcept
清空从光标位置到行尾。
Definition
term.h:191
console::clear_line
void clear_line() noexcept
清空当前光标所在行。
Definition
term.h:184
console::restore_cursor
void restore_cursor() noexcept
恢复之前保存的光标位置。
Definition
term.h:131
include
io
term.h
Generated on
for Console Library by
1.18.0