Skip to content

Commit

Permalink
TRestStringOutput: fixed an std::length_error when the window is resi…
Browse files Browse the repository at this point in the history
…zed. issue #17
  • Loading branch information
nkx111 committed Mar 2, 2021
1 parent 1d23fb0 commit f8a2e8b
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions source/framework/tools/src/TRestStringOutput.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ void Console::ClearLinesAfterCursor() {
fflush(stdout);
}

#define TRestStringOutput_BestLength 100
TRestStringOutput::TRestStringOutput(string _color, string BorderOrHeader, REST_Display_Format style) {
color = _color;
formatstring = BorderOrHeader;
Expand All @@ -158,9 +159,7 @@ TRestStringOutput::TRestStringOutput(string _color, string BorderOrHeader, REST_
useborder = false;
}

length = 100;
if (length > Console::GetWidth() - 2) length = Console::GetWidth() - 2;

setlength(TRestStringOutput_BestLength);
resetstring();
if (length > 500 || length < 20) // unsupported console, we will fall back to compatibility modes
{
Expand Down Expand Up @@ -259,10 +258,15 @@ string TRestStringOutput::FormattingPrintString(string input) {

void TRestStringOutput::setlength(int n) {
if (!Console::CompatibilityMode) {
if (n < Console::GetWidth() - 2)
length = n;
else
if (n >= Console::GetWidth() - 2) {
length = Console::GetWidth() - 2;
} else if (n <= 0) {
length = Console::GetWidth() - 2;
} else {
length = n;
}
} else {
length = n;
}
}

Expand All @@ -271,11 +275,14 @@ void TRestStringOutput::flushstring() {
{
std::cout << buf.str() << std::endl;
} else {
int consolewidth = Console::GetWidth() - 2;
printf("\033[K");
if (orientation == 0) {
std::cout << color << string((consolewidth - length) / 2, ' ') << FormattingPrintString(buf.str())
<< string((consolewidth - length) / 2, ' ') << COLOR_RESET << std::endl;
// we always reset the length of TRestStringOutput in case the console is resized
setlength(TRestStringOutput_BestLength);
int blankwidth = (Console::GetWidth() - 2 - length) / 2;

std::cout << color << string(blankwidth, ' ') << FormattingPrintString(buf.str())
<< string(blankwidth, ' ') << COLOR_RESET << std::endl;
} else {
std::cout << color << FormattingPrintString(buf.str()) << COLOR_RESET << std::endl;
}
Expand Down

0 comments on commit f8a2e8b

Please sign in to comment.