1#include "TRestStringOutput.h"
3#include "TRestStringHelper.h"
16int Console::GetWidth() {
18 CONSOLE_SCREEN_BUFFER_INFO csbi;
19 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
20 return csbi.srWindow.Right - csbi.srWindow.Left + 1;
22 if (isatty(fileno(stdout))) {
24 ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
31int Console::GetHeight() {
33 CONSOLE_SCREEN_BUFFER_INFO csbi;
34 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
35 return csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
37 if (isatty(fileno(stdout))) {
39 ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
46bool Console::kbhit() {
50 struct termios oldt, newt;
53 tcgetattr(STDIN_FILENO, &oldt);
55 newt.c_lflag &= ~(ICANON | ECHO);
56 tcsetattr(STDIN_FILENO, TCSANOW, &newt);
57 oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
58 fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
60 tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
61 fcntl(STDIN_FILENO, F_SETFL, oldf);
74int Console::Read() {
return getchar(); }
76int Console::ReadKey() {
80 struct termios tm, tm_old;
83 if (tcgetattr(fd, &tm) < 0) {
89 if (tcsetattr(fd, TCSANOW, &tm) < 0) {
94 if (tcsetattr(fd, TCSANOW, &tm_old) < 0) {
102string Console::ReadLine() {
108void Console::WriteLine(
string content) {
109 printf(
"%s", content.c_str());
113void Console::CursorUp(
int n) {
114 printf(
"\033[%dA", n);
118void Console::CursorDown(
int n) {
119 printf(
"\033[%dB", n);
123void Console::CursorRight(
int n) {
124 printf(
"\033[%dC", n);
128void Console::CursorLeft(
int n) {
129 printf(
"\033[%dD", n);
133void Console::CursorToXY(
int x,
int y) {
134 printf(
"\033[%d%dH", x, y);
138void Console::ClearScreen() {
143void Console::ClearCurrentLine() {
153void Console::ClearLinesAfterCursor() {
155 for (
int i = 0; i < 50; i++) {
163char mirrorchar(
char c) {
198#define TRestStringOutput_BestLength 100
199TRestStringOutput::TRestStringOutput(COLORCODE_TYPE _color,
string formatter,
200 REST_Display_Orientation _orientation) {
203 orientation = _orientation;
206 if (formatter.size() < 2) {
209 for (
unsigned int i = 0; i < formatter.size() / 2; i++) {
210 if (mirrorchar(formatter[i]) != formatter[formatter.size() - i - 1]) {
215 if (formatter[formatter.size() / 2] !=
' ') {
220 formatstring = formatter.substr(0, formatter.size() / 2);
221 formatstring =
Replace(formatstring,
" ",
"");
223 formatstring = formatter;
226 setlength(TRestStringOutput_BestLength);
228 if (length > 500 || length < 20)
231 REST_Display_CompatibilityMode =
true;
237void TRestStringOutput::resetstring() {
242string TRestStringOutput::FormattingPrintString(
string input) {
243 if (input ==
"")
return "";
246 if (input[0] == input[input.size() - 1] &&
247 (input[0] ==
'=' || input[0] ==
'-' || input[0] ==
'*' || input[0] ==
'+')) {
248 return string(length, input[0]);
252 string output(length,
' ');
254 int Lstr = input.size();
255 int Lfmt = formatstring.size();
258 if (useborder || orientation == TRestStringOutput::REST_Display_Orientation::kMiddle) {
259 startblank = (length - Lstr) / 2;
263 if (startblank < 0) {
267 string& border = formatstring;
268 for (
int i = 0; i < Lfmt && i < length; i++) {
269 output[i] = border[i];
270 output[length - i - 1] = mirrorchar(border[i]);
273 for (
int i = 0; i < Lstr; i++) {
274 if (startblank + i > length - 1) {
275 output[length - 3] =
'.';
276 output[length - 2] =
'.';
277 output[length - 1] =
'.';
280 output[startblank + i] = input[i];
285 return formatstring + input;
289void TRestStringOutput::setlength(
int n) {
290 if (!REST_Display_CompatibilityMode) {
291 if (n >= Console::GetWidth() - 2) {
292 length = Console::GetWidth() - 2;
294 length = Console::GetWidth() - 2;
305#define SET_COLOR(color) \
306 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (COLOR_RESET >> 4 << 4) + color);
307#define RESET_COLOR() SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), COLOR_RESET);
309#define SET_COLOR(color) std::cout << color;
310#define RESET_COLOR() std::cout << COLOR_RESET;
313void TRestStringOutput::flushstring() {
314 if (REST_Display_CompatibilityMode)
316 std::cout << buf.str() << std::endl;
318 Console::ClearCurrentLine();
319 if (orientation == TRestStringOutput::REST_Display_Orientation::kMiddle) {
321 setlength(TRestStringOutput_BestLength);
322 int blankwidth = (Console::GetWidth() - 2 - length) / 2;
325 std::cout << string(blankwidth,
' ') << FormattingPrintString(buf.str())
326 << string(blankwidth,
' ');
328 std::cout << std::endl;
331 std::cout << FormattingPrintString(buf.str());
333 std::cout << std::endl;
340 if (gVerbose >= verbose) {
@ REST_Essential
+show some essential information, as well as warnings
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.