1 #include "TRestStringOutput.h"
3 #include "TRestStringHelper.h"
12 int Console::GetWidth() {
14 CONSOLE_SCREEN_BUFFER_INFO csbi;
15 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
16 return csbi.srWindow.Right - csbi.srWindow.Left + 1;
18 if (isatty(fileno(stdout))) {
20 ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
27 int Console::GetHeight() {
29 CONSOLE_SCREEN_BUFFER_INFO csbi;
30 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
31 return csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
33 if (isatty(fileno(stdout))) {
35 ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
42 bool Console::kbhit() {
46 struct termios oldt, newt;
49 tcgetattr(STDIN_FILENO, &oldt);
51 newt.c_lflag &= ~(ICANON | ECHO);
52 tcsetattr(STDIN_FILENO, TCSANOW, &newt);
53 oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
54 fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
56 tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
57 fcntl(STDIN_FILENO, F_SETFL, oldf);
70 int Console::Read() {
return getchar(); }
72 int Console::ReadKey() {
76 struct termios tm, tm_old;
79 if (tcgetattr(fd, &tm) < 0) {
85 if (tcsetattr(fd, TCSANOW, &tm) < 0) {
90 if (tcsetattr(fd, TCSANOW, &tm_old) < 0) {
98 string Console::ReadLine() {
104 void Console::WriteLine(
string content) {
105 printf(
"%s", content.c_str());
109 void Console::CursorUp(
int n) {
110 printf(
"\033[%dA", n);
114 void Console::CursorDown(
int n) {
115 printf(
"\033[%dB", n);
119 void Console::CursorRight(
int n) {
120 printf(
"\033[%dC", n);
124 void Console::CursorLeft(
int n) {
125 printf(
"\033[%dD", n);
129 void Console::CursorToXY(
int x,
int y) {
130 printf(
"\033[%d%dH", x, y);
134 void Console::ClearScreen() {
139 void Console::ClearCurrentLine() {
149 void Console::ClearLinesAfterCursor() {
151 for (
int i = 0; i < 50; i++) {
159 char mirrorchar(
char c) {
194 #define TRestStringOutput_BestLength 100
195 TRestStringOutput::TRestStringOutput(COLORCODE_TYPE _color,
string formatter,
196 REST_Display_Orientation _orientation) {
199 orientation = _orientation;
202 if (formatter.size() < 2) {
205 for (
unsigned int i = 0; i < formatter.size() / 2; i++) {
206 if (mirrorchar(formatter[i]) != formatter[formatter.size() - i - 1]) {
211 if (formatter[formatter.size() / 2] !=
' ') {
216 formatstring = formatter.substr(0, formatter.size() / 2);
217 formatstring =
Replace(formatstring,
" ",
"");
219 formatstring = formatter;
222 setlength(TRestStringOutput_BestLength);
224 if (length > 500 || length < 20)
227 REST_Display_CompatibilityMode =
true;
230 verbose = REST_Verbose_Level::REST_Essential;
233 void TRestStringOutput::resetstring() {
238 string TRestStringOutput::FormattingPrintString(
string input) {
239 if (input ==
"")
return "";
242 if (input[0] == input[input.size() - 1] &&
243 (input[0] ==
'=' || input[0] ==
'-' || input[0] ==
'*' || input[0] ==
'+')) {
244 return string(length, input[0]);
248 string output(length,
' ');
250 int Lstr = input.size();
251 int Lfmt = formatstring.size();
254 if (useborder || orientation == TRestStringOutput::REST_Display_Orientation::kMiddle) {
255 startblank = (length - Lstr) / 2;
259 if (startblank < 0) {
263 string& border = formatstring;
264 for (
int i = 0; i < Lfmt && i < length; i++) {
265 output[i] = border[i];
266 output[length - i - 1] = mirrorchar(border[i]);
269 for (
int i = 0; i < Lstr; i++) {
270 if (startblank + i > length - 1) {
271 output[length - 3] =
'.';
272 output[length - 2] =
'.';
273 output[length - 1] =
'.';
276 output[startblank + i] = input[i];
281 return formatstring + input;
285 void TRestStringOutput::setlength(
int n) {
286 if (!REST_Display_CompatibilityMode) {
287 if (n >= Console::GetWidth() - 2) {
288 length = Console::GetWidth() - 2;
290 length = Console::GetWidth() - 2;
301 #define SET_COLOR(color) \
302 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (COLOR_RESET >> 4 << 4) + color);
303 #define RESET_COLOR() SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), COLOR_RESET);
305 #define SET_COLOR(color) std::cout << color;
306 #define RESET_COLOR() std::cout << COLOR_RESET;
309 void TRestStringOutput::flushstring() {
310 if (REST_Display_CompatibilityMode)
312 std::cout << buf.str() << std::endl;
314 Console::ClearCurrentLine();
315 if (orientation == TRestStringOutput::REST_Display_Orientation::kMiddle) {
317 setlength(TRestStringOutput_BestLength);
318 int blankwidth = (Console::GetWidth() - 2 - length) / 2;
321 std::cout << string(blankwidth,
' ') << FormattingPrintString(buf.str())
322 << string(blankwidth,
' ');
324 std::cout << std::endl;
327 std::cout << FormattingPrintString(buf.str());
329 std::cout << std::endl;
336 if (gVerbose >= verbose) {
std::string Replace(std::string in, std::string thisString, std::string byThisString, size_t fromPosition=0, Int_t N=0)
Replace any occurences of thisSring by byThisString inside string in.