-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathterminal.cppm
More file actions
52 lines (41 loc) · 1.13 KB
/
terminal.cppm
File metadata and controls
52 lines (41 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// mcpp.platform.terminal — terminal capability detection.
//
// Provides:
// is_tty() — whether stdout is a terminal
// terminal_cols() — terminal width in columns
module;
#include <cstdio>
#include <cstdlib>
#ifdef __unix__
#include <unistd.h>
#include <sys/ioctl.h>
#endif
export module mcpp.platform.terminal;
import std;
export namespace mcpp::platform::terminal {
// Returns true if stdout is connected to a terminal (TTY).
bool is_tty();
// Returns the terminal width in columns. Tries TIOCGWINSZ on Unix,
// falls back to $COLUMNS, then defaults to 80.
std::size_t cols();
} // namespace mcpp::platform::terminal
namespace mcpp::platform::terminal {
bool is_tty() {
#ifdef __unix__
return ::isatty(::fileno(stdout)) != 0;
#else
return false;
#endif
}
std::size_t cols() {
#ifdef __unix__
struct winsize w{};
if (::ioctl(::fileno(stdout), TIOCGWINSZ, &w) == 0 && w.ws_col > 0)
return w.ws_col;
#endif
if (auto* e = std::getenv("COLUMNS"); e && *e) {
try { auto n = std::stoul(e); if (n > 0) return n; } catch (...) {}
}
return 80;
}
} // namespace mcpp::platform::terminal