blob: 7226328ca4894e198d5cdf82597f80defc03e5d6 (
plain) (
blame)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
#include "interactive.h"
#if defined(_unix_)
#include <sys/ioctl.h>
#include <termios.h>
#elif defined(_win_)
#include <windows.h>
#include <io.h>
#endif
namespace NYdb {
namespace NConsoleClient {
bool AskYesOrNo() {
TString input;
for (;;) {
Cin >> input;
if (to_lower(input) == "y" || to_lower(input) == "yes") {
return true;
} else if (to_lower(input) == "n" || to_lower(input) == "n") {
return false;
} else {
Cout << "Type \"y\" (yes) or \"n\" (no): ";
}
}
return false;
}
bool IsStdinInteractive() {
#if defined(_win32_)
return _isatty(_fileno(stdin));
#elif defined(_unix_)
return isatty(fileno(stdin));
#endif
return true;
}
bool IsStdoutInteractive() {
#if defined(_win32_)
return _isatty(_fileno(stdout));
#elif defined(_unix_)
return isatty(fileno(stdout));
#endif
return true;
}
std::optional<size_t> GetTerminalWidth() {
if (!IsStdoutInteractive())
return {};
#if defined(_win32_)
CONSOLE_SCREEN_BUFFER_INFO screen_buf_info;
if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &screen_buf_info)) {
return screen_buf_info.srWindow.Right - screen_buf_info.srWindow.Left + 1;
}
#elif defined(_unix_)
struct winsize size;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) != -1) {
return size.ws_col;
}
#endif
return {};
}
}
}
|