|
cgma
|
#include <TtyProgressTool.hpp>
Public Member Functions | |
| TtyProgressTool () | |
| virtual | ~TtyProgressTool () |
| virtual void | start (int lower, int upper, const char *title=0, const char *info=0, CubitBoolean smooth=CUBIT_TRUE, CubitBoolean cancelButton=CUBIT_FALSE) |
| virtual void | end () |
| virtual void | step () |
| virtual void | percent (double p) |
| virtual void | check_interrupt () |
| void | clear_all () |
Private Member Functions | |
| bool | update () |
| void | display (int step, int pct) |
Private Attributes | |
| int | myRange |
| int | currentVal |
| int | currPercent |
| int | prevWidth |
| char * | lineBuffer |
| char * | barStart |
| char * | bufferEnd |
| char * | firstMessage |
| char * | secondMessage |
Definition at line 18 of file TtyProgressTool.hpp.
| TtyProgressTool::TtyProgressTool | ( | ) | [inline] |
Definition at line 60 of file TtyProgressTool.hpp.
: prevWidth(0), lineBuffer(0), firstMessage(0), secondMessage(0) {}
| TtyProgressTool::~TtyProgressTool | ( | ) | [virtual] |
Definition at line 49 of file TtyProgressTool.cpp.
{
clear_all();
}
| void TtyProgressTool::check_interrupt | ( | ) | [virtual] |
| void TtyProgressTool::clear_all | ( | ) |
Definition at line 54 of file TtyProgressTool.cpp.
{
// free allocated buffers
// (messages were created with strdup (CubitUtil::util_strdup),
// which uses malloc so use free() (CubitUtil::util_strdup_free())).
delete [] lineBuffer;
if (firstMessage)
CubitUtil::util_strdup_free(firstMessage);
if (secondMessage)
CubitUtil::util_strdup_free(secondMessage);
// zero data so calls to step or percent fail.
myRange = 0;
prevWidth = 0;
lineBuffer = barStart = bufferEnd = 0;
firstMessage = secondMessage = 0;
}
| void TtyProgressTool::display | ( | int | step, |
| int | pct | ||
| ) | [private] |
Definition at line 257 of file TtyProgressTool.cpp.
{
int prev_width = prevWidth;
bool do_output = false;
// check for change in terminal width and update if necessary.
if (!update())
return;
// if tty size changed or percent changed, need to update
if (prev_width != prevWidth || new_percent != currPercent)
{
currPercent = new_percent;
do_output = true;
}
// calculate start of numeric percent in lineBuffer
char* pctStart = bufferEnd;
if (DISPLAY_NUM_PERCENT)
pctStart -= 4; // start of numeric percent
// check if the progress bar output is changing, and if
// so set do_output to true
size_t bar_width = 0, bar_drawn = 0, old_drawn;
if (barStart && myRange > 0)
{
bar_width = pctStart - barStart - 1;
bar_drawn = new_count >= myRange ? bar_width :
new_count <= 0 ? 0 :
bar_width * new_count / myRange;
old_drawn = currentVal >= myRange ? bar_width :
currentVal <= 0 ? 0 :
bar_width * currentVal / myRange;
if (bar_drawn != old_drawn)
do_output = true;
}
currentVal = new_count;
if (DISPLAY_NUM_PERCENT && do_output)
{
// normalize numeric percent to values that can be displayed
int pct = currPercent;
if (pct < 0)
pct = 0;
else if(pct > 999)
pct = 999;
// display numeric percent in last three chars of line
pctStart[2] = (char)('0' + pct % 10);
pctStart[1] = pctStart[0] = ' ';
for ( int i = 1; i >= 0 && pct > 9; i-- )
{
pct /= 10;
pctStart[i] = (char)('0' + pct % 10);
}
}
// if progress bar is to be displayed, update it.
if (barStart && do_output)
{
// write '=' for filled section of bar
if (bar_drawn > 1)
memset(barStart, PROGRESS_BAR_FILLED, bar_drawn - 1);
// write '>' between filled and unfilled sections of bar
if (bar_drawn > 0)
barStart[bar_drawn - 1] = PROGRESS_BAR_CURRENT;
// write spaces in unfilled part of bar
if (bar_drawn < bar_width)
memset(barStart + bar_drawn, PROGRESS_BAR_EMPTY, bar_width - bar_drawn);
}
// write the buffer to the terminal
if (do_output)
{
fwrite( lineBuffer, bufferEnd - lineBuffer, 1, stdout );
fflush( stdout );
}
}
| void TtyProgressTool::end | ( | ) | [virtual] |
Implements ProgressTool.
Definition at line 238 of file TtyProgressTool.cpp.
{
// clear terminal line
if (lineBuffer)
{
memset( lineBuffer + 1, ' ', bufferEnd - lineBuffer - 1 );
fwrite( lineBuffer, bufferEnd - lineBuffer, 1, stdout );
putchar('\r');
fflush( stdout );
}
// clear internal data
clear_all();
}
| void TtyProgressTool::percent | ( | double | p | ) | [virtual] |
Implements ProgressTool.
Definition at line 233 of file TtyProgressTool.cpp.
| void TtyProgressTool::start | ( | int | lower, |
| int | upper, | ||
| const char * | title = 0, |
||
| const char * | info = 0, |
||
| CubitBoolean | smooth = CUBIT_TRUE, |
||
| CubitBoolean | cancelButton = CUBIT_FALSE |
||
| ) | [virtual] |
Implements ProgressTool.
Definition at line 201 of file TtyProgressTool.cpp.
{
// get rid of any old state if someone forgot to call end().
clear_all();
// store passed range
myRange = upper - lower;
currentVal = currPercent = 0;
prevWidth = 0;
// copy passed messages
const char* empty = "";
if (s1)
firstMessage = CubitUtil::util_strdup((char*)s1);
else
firstMessage = CubitUtil::util_strdup((char*)empty);
if (s2)
secondMessage = CubitUtil::util_strdup((char*)s2);
// generate line buffer
display(0,0);
}
| void TtyProgressTool::step | ( | ) | [virtual] |
Implements ProgressTool.
Definition at line 226 of file TtyProgressTool.cpp.
{
int new_count = currentVal + 1;
if( myRange > 0 )
display( new_count, 100 * new_count / myRange );
}
| bool TtyProgressTool::update | ( | ) | [private] |
Definition at line 73 of file TtyProgressTool.cpp.
{
// Minimum/maximum space for progress bar. (Add 2 to constants
// for the leading and trailing '|' character.)
const int MIN_PROGRESS_WIDTH = MIN_PROGRESS_BAR_WIDTH + 2;
const int MAX_PROGRESS_WIDTH = MAX_PROGRESS_BAR_WIDTH + 2;
// Get terminal width. Assume 80 if query fails.
int width, height;
if ( !AppUtil::instance()->get_terminal_size(height, width) )
width = 80;
#ifdef _WIN32
// Windows cmd.exe wraps the line when the last character
// of the line is output, rather than the first character
// after the end of the line. Stop one short so to avoid
// newlines.
width--;
#endif
// If terminal width hasn't changed then no update is
// required. return.
if ( width && width == prevWidth )
return true;
prevWidth = width;
// If width isn't at least 6 don't try to display anything.
// Clear data and return.
if ( width < 4 )
{
delete [] lineBuffer;
lineBuffer = barStart = bufferEnd = 0;
return false;
}
// Re-create lineBuffer for the new terminal width if necessary.
if ( !lineBuffer || (bufferEnd - lineBuffer - 1 < width) )
{
delete [] lineBuffer;
#ifdef _WIN32
lineBuffer = new char [width+2];
lineBuffer[width+1] = '\r';
#else
lineBuffer = new char [width+1];
#endif
if ( !lineBuffer )
return false;
}
bufferEnd = lineBuffer + width + 1; // one past end of buffer
barStart = 0; // start of progress bar (null if no progress bar)
char* bar_end = 0; // end of progress bar
// Current start and end (fill lineBuffer until start == end)
char* start = lineBuffer;
char* end = bufferEnd;
// Use \r to move cursor to beginning of line without advancing a line.
*(start++) = '\r'; // leading \r
// If displaying numeric percent on end of line, allocate 4 spaces
// at end of line and place '%' char in the fourth one.
if (DISPLAY_NUM_PERCENT && (end - start > 3))
{
end[-1] = '%';
end -= 4;
}
// Copy first message into line
if (firstMessage && (end - start > 2))
{
int len = strlen(firstMessage);
// need to truncate message?
if (len + 2 > end - start)
len = end - start - 2;
memcpy(start, firstMessage, len);
start += len;
// add trailing ": "
*(start++) = ':';
*(start++) = ' ';
}
// Allocate space for minimum progress bar if sufficient space
if (DISPLAY_PROGRESS_BAR && (end - start > MIN_PROGRESS_WIDTH))
{
bar_end = end;
end -= MIN_PROGRESS_WIDTH;
}
// Copy second message into line buffer
if (secondMessage && (end - start > 2))
{
int len = strlen(secondMessage);
// need to truncate message?
if (len + 2 > end - start)
len = end - start - 2;
memcpy(start, secondMessage, len);
start += len;
// add trailing ": "
*(start++) = ':';
*(start++) = ' ';
}
// Finish progress bar setup (if progress bar is displayed at all)
if (bar_end)
{
// Grow progress bar up to MAX_PROGRESS_WIDTH (or all the
// remaining space if MAX_PROGRESS_BAR_WIDTH is unset)
int diff = bar_end - start - MAX_PROGRESS_WIDTH;
end = start;
if (MAX_PROGRESS_BAR_WIDTH && diff > 0)
end += diff;
// Put in bounding '|' chars for progress bar and
// initialize barStart to point *after* the leading '|'
barStart = end;
barStart[0] = PROGRESS_BAR_START;
bar_end[-1] = PROGRESS_BAR_END;
barStart++;
}
// fill any remaining space with spaces
if (start < end)
memset( start, ' ', end - start );
return true;
}
char* TtyProgressTool::barStart [private] |
Definition at line 53 of file TtyProgressTool.hpp.
char* TtyProgressTool::bufferEnd [private] |
Definition at line 54 of file TtyProgressTool.hpp.
int TtyProgressTool::currentVal [private] |
Definition at line 48 of file TtyProgressTool.hpp.
int TtyProgressTool::currPercent [private] |
Definition at line 49 of file TtyProgressTool.hpp.
char* TtyProgressTool::firstMessage [private] |
Definition at line 56 of file TtyProgressTool.hpp.
char* TtyProgressTool::lineBuffer [private] |
Definition at line 52 of file TtyProgressTool.hpp.
int TtyProgressTool::myRange [private] |
Definition at line 47 of file TtyProgressTool.hpp.
int TtyProgressTool::prevWidth [private] |
Definition at line 50 of file TtyProgressTool.hpp.
char* TtyProgressTool::secondMessage [private] |
Definition at line 57 of file TtyProgressTool.hpp.