00001 /******************************************************************************* 00002 00003 Copyright (C) 2007 by Brendon Costa 00004 00005 This library is free software; you can redistribute it and/or modify 00006 it under the terms of the "LGPL Like" License defined in the file COPYING 00007 that should have been distributed along with this source. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00012 00013 You should have received a copy of the "LGPL Like" License 00014 along with this library; see the file COPYING. if not, it can be 00015 obtained from the EDoc++ website: 00016 http://edoc.sourceforge.net/license.html 00017 00018 *******************************************************************************/ 00019 #include "config.h" 00020 00021 #include "EDoc/ProgressBar.h" 00022 #include "EDoc/utils.h" 00023 00024 #include <iostream> 00025 00026 namespace EDoc 00027 { 00028 //=========================================================================== 00029 const char* ProgressBar::DEFAULT_SPIN_SEQUENCE = "|/-\\"; 00030 //=========================================================================== 00031 ProgressBar::ProgressBar(bool enable_spinner_in, int width_in, 00032 std::string spin_sequence_in) : 00033 00034 enable_spinner(enable_spinner_in), 00035 width(width_in), 00036 spin_sequence(spin_sequence_in) 00037 { 00038 Reset(); 00039 } 00040 //=========================================================================== 00041 void ProgressBar::Reset() 00042 { 00043 bars = 0; 00044 spin_index = 0; 00045 } 00046 //=========================================================================== 00047 void ProgressBar::Start(size_t total_in) 00048 { 00049 current = 0; 00050 total = total_in; 00051 } 00052 //=========================================================================== 00053 void ProgressBar::Progress(Function* EDOC_UNUSED(data)) 00054 { 00055 size_t count = ++current; 00056 int b = width * count / total; 00057 if (b > bars) 00058 { 00059 // If the spinner has been displayed then delete it. 00060 if (enable_spinner && spin_index >= 0) 00061 { 00062 std::cerr << "\b"; 00063 } 00064 00065 // Display the next few bars. 00066 for (int i = 0; i < (b - bars); i++) 00067 { 00068 std::cerr << '='; 00069 } 00070 00071 bars = b; 00072 00073 // After displaying the final bar exit without the spinner. 00074 if (b == width) 00075 { 00076 std::cerr << std::endl; 00077 return; 00078 } 00079 00080 if (enable_spinner) 00081 { 00082 // Write a temp char that is to be overwritten by the spinner. 00083 std::cerr << "?"; 00084 } 00085 } 00086 00087 if (enable_spinner) 00088 { 00089 // Display the spinner if we have not reached 100% 00090 spin_index = (spin_index + 1) % spin_sequence.size(); 00091 std::cerr << "\b" << spin_sequence[spin_index] << std::flush; 00092 } 00093 } 00094 //=========================================================================== 00095 00096 }