00001 // -*- C++ -*- 00002 00003 // 00004 // Copyright (C) 2004 Universite de Montreal 00005 // 00006 // Redistribution and use in source and binary forms, with or without 00007 // modification, are permitted provided that the following conditions are met: 00008 // 00009 // 1. Redistributions of source code must retain the above copyright 00010 // notice, this list of conditions and the following disclaimer. 00011 // 00012 // 2. Redistributions in binary form must reproduce the above copyright 00013 // notice, this list of conditions and the following disclaimer in the 00014 // documentation and/or other materials provided with the distribution. 00015 // 00016 // 3. The name of the authors may not be used to endorse or promote 00017 // products derived from this software without specific prior written 00018 // permission. 00019 // 00020 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00021 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00022 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00023 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00024 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00025 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00026 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00027 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00028 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00029 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00030 // 00031 // This file is part of the PLearn library. For more information on the PLearn 00032 // library, go to the PLearn Web site at www.plearn.org 00033 00034 /* ******************************************************* 00035 * $Id: ShellProgressBar.h,v 1.4 2004/07/21 16:30:56 chrish42 Exp $ 00036 ******************************************************* */ 00037 00038 00039 #ifndef ShellProgressBar_H 00040 #define ShellProgressBar_H 00041 #include <string> 00042 #include <plearn/math/pl_math.h> 00043 00044 namespace PLearn { 00045 using namespace std; 00046 00047 // 00048 // This class implements a simple and convenient ASCII 00049 // text progress bar to be displayed in the console. 00050 // 00051 00052 // - Position the Cursor: 00053 // \033[<L>;<C>H 00054 // Or 00055 // \033[<L>;<C>f 00056 // puts the cursor at line L and column C. 00057 // - Move the cursor up N lines: 00058 // \033[<N>A 00059 // - Move the cursor down N lines: 00060 // \033[<N>B 00061 // - Move the cursor forward N columns: 00062 // \033[<N>C 00063 // - Move the cursor backward N columns: 00064 // \033[<N>D 00065 // - Clear the screen, move to (0,0): 00066 // \033[2J 00067 // - Erase to end of line: 00068 // \033[K 00069 // - Save cursor position: 00070 // \033[s 00071 // - Restore cursor position: 00072 // \033[u 00073 00074 class ShellProgressBar 00075 { 00076 00077 public: 00078 00079 ShellProgressBar() : min(0), max(0), caption(""), width(10) {} 00080 ShellProgressBar(int min, int max, string caption = "", int width = 10); 00081 00082 void init(); 00083 void draw(bool simple_mode = false); // must be called before any update; 00084 // simple mode can be used when outputting in file, 00085 // or when using 'gdb', for instance 00086 // (it simply won't perform the positional trick on the cursor) 00087 bool update(int value); 00088 void reset(); 00089 void set(int min, int max, string caption = "", int w = 10) { setMin(min); setMax(max); setCaption(caption); width = w; } 00090 void setCaption(string caption); 00091 void setMin(int min); 00092 void setMax(int max); 00093 void done(); 00094 static string getTime(); 00095 static int getAsciiFileLineCount(string file); // stupid version! 00096 static int getWcAsciiFileLineCount(string file); // this is just for convenience : it is a bit risky! 00097 00098 private: 00099 00100 int min; 00101 int max; 00102 real blockwidth; 00103 int pos; 00104 string caption; 00105 int width; 00106 bool max_reached; 00107 00108 }; 00109 00110 } // end of namespace PLearn 00111 00112 #endif 00113