forked from alibaba/AliSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprinter.cpp
More file actions
87 lines (76 loc) · 2.22 KB
/
printer.cpp
File metadata and controls
87 lines (76 loc) · 2.22 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "duckdb/common/printer.hpp"
#include "duckdb/common/progress_bar/progress_bar.hpp"
#include "duckdb/common/windows_util.hpp"
#include "duckdb/common/windows.hpp"
#include <stdio.h>
#ifndef DUCKDB_DISABLE_PRINT
#ifdef DUCKDB_WINDOWS
#include <io.h>
#else
#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>
#endif
#endif
namespace duckdb {
void Printer::RawPrint(OutputStream stream, const string &str) {
#ifndef DUCKDB_DISABLE_PRINT
#ifdef DUCKDB_WINDOWS
if (IsTerminal(stream)) {
// print utf8 to terminal
auto unicode = WindowsUtil::UTF8ToMBCS(str.c_str());
fprintf(stream == OutputStream::STREAM_STDERR ? stderr : stdout, "%s", unicode.c_str());
return;
}
#endif
fprintf(stream == OutputStream::STREAM_STDERR ? stderr : stdout, "%s", str.c_str());
#endif
}
void Printer::DefaultLinePrint(OutputStream stream, const string &str) {
Printer::RawPrint(stream, str);
Printer::RawPrint(stream, "\n");
}
line_printer_f Printer::line_printer = Printer::DefaultLinePrint;
// LCOV_EXCL_START
void Printer::Print(OutputStream stream, const string &str) {
Printer::line_printer(stream, str);
}
void Printer::Flush(OutputStream stream) {
#ifndef DUCKDB_DISABLE_PRINT
fflush(stream == OutputStream::STREAM_STDERR ? stderr : stdout);
#endif
}
void Printer::Print(const string &str) {
Printer::Print(OutputStream::STREAM_STDERR, str);
}
bool Printer::IsTerminal(OutputStream stream) {
#ifndef DUCKDB_DISABLE_PRINT
#ifdef DUCKDB_WINDOWS
auto stream_handle = stream == OutputStream::STREAM_STDERR ? STD_ERROR_HANDLE : STD_OUTPUT_HANDLE;
return GetFileType(GetStdHandle(stream_handle)) == FILE_TYPE_CHAR;
#else
return isatty(stream == OutputStream::STREAM_STDERR ? 2 : 1);
#endif
#else
throw InternalException("IsTerminal called while printing is disabled");
#endif
}
idx_t Printer::TerminalWidth() {
#ifndef DUCKDB_DISABLE_PRINT
#ifdef DUCKDB_WINDOWS
CONSOLE_SCREEN_BUFFER_INFO csbi;
int rows;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
rows = csbi.srWindow.Right - csbi.srWindow.Left + 1;
return rows;
#else
struct winsize w;
ioctl(0, TIOCGWINSZ, &w);
return w.ws_col;
#endif
#else
throw InternalException("TerminalWidth called while printing is disabled");
#endif
}
// LCOV_EXCL_STOP
} // namespace duckdb