ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/utils/ProgressBar.cpp
(Generate patch)

Comparing:
trunk/src/utils/ProgressBar.cpp (file contents), Revision 1420 by gezelter, Fri Mar 26 18:42:55 2010 UTC vs.
branches/development/src/utils/ProgressBar.cpp (file contents), Revision 1618 by gezelter, Mon Sep 12 17:09:26 2011 UTC

# Line 39 | Line 39
39   * [4]  Vardeman, Stocker & Gezelter, in progress (2010).                        
40   */
41  
42 < #include <string>
43 < #include <sstream>
42 > #include <iostream>
43 > #include <cstdlib>
44   #include <sys/ioctl.h>
45 + #include <unistd.h>
46   #ifdef IS_MPI
47   #include <mpi.h>
48 < #endif //is_mpi
48 > #endif
49   #include "utils/ProgressBar.hpp"
50  
51 + using namespace std;
52 +
53   namespace OpenMD {
54  
55    const char * progressSpinner_ = "|/-\\";
# Line 58 | Line 61 | namespace OpenMD {
61   #ifdef IS_MPI
62      if (MPI::COMM_WORLD.Get_rank() == 0) {
63   #endif
64 <      printf("\n");
65 <      fflush(stdout);
64 >      cout << endl;
65 >      cout.flush();
66   #ifdef IS_MPI
67      }
68   #endif
# Line 70 | Line 73 | namespace OpenMD {
73    }
74    
75    void ProgressBar::update() {
76 <    int width = 80;
76 >
77 >    int width;
78 >
79   #ifdef IS_MPI
80      if (MPI::COMM_WORLD.Get_rank() == 0) {
81   #endif
77 #ifndef IS_MPI
78    // get the window width:
79    struct winsize w;
80    ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
81    width = w.ws_col;
82 #endif  
83
84    // We'll use:
85    // 31 characters for the completion estimate,
86    //  6 for the % complete,
87    //  2 characters for the open and closing brackets.
88
89    int avail = width - 31 - 6 - 2;
90
91    ++iteration_;
92    
93    if (maximum_ > 0.0) {
94      // we know the maximum, so
95      // draw a progress bar
82        
83 <      RealType percent = value_ * 100.0 / maximum_;
84 <      int hashes = int(percent * avail / 100.0);
85 <      std::string progressbar;
86 <      progressbar.assign(hashes, '#');
83 >      // only do the progress bar if we are actually running in a tty:
84 >      if (isatty(fileno(stdout))  && (getenv("SGE_TASK_ID")==NULL)) {    
85 >        // get the window width:
86 >        struct winsize w;
87 >        ioctl(fileno(stdout), TIOCGWINSZ, &w);
88 >        width = w.ws_col;
89  
90 <      // add the spinner to the end of the progress bar:
91 <      progressbar += progressSpinner_[iteration_ & 3];
90 >        // handle the case when the width is returned as a nonsensical value.
91 >        if (width <= 0) width = 80;
92  
93 <      // compute the best estimate of the ending time:
94 <      time_t current_ = time(NULL);
95 <      time_t end_ = start_ + (current_ - start_) * (100.0/percent);
96 <      struct tm * ender = localtime(&end_);
97 <      char buffer[24];
98 <      strftime(buffer, 24, "%a %b %d @ %I:%M %p", ender);
93 >        // We'll use:
94 >        // 31 characters for the completion estimate,
95 >        //  6 for the % complete,
96 >        //  2 characters for the open and closing brackets.
97 >        
98 >        int avail = width - 31 - 6 - 2;      
99  
100 <      std::stringstream fmt;
113 <      fmt << "\r%3d%% [%-" << avail << "s] Estimate: %s";
114 <      std::string st = fmt.str();
100 >        ++iteration_;
101  
102 <      printf(st.c_str(), int(percent),
117 <             progressbar.c_str(),
118 <             buffer);
102 >        if (maximum_ > 0.0) {
103  
104 <    } else {
105 <      // we don't know the maximum, so we can't draw a progress bar
106 <      int center = (iteration_ % 48) + 1; // 50 spaces, minus 2
107 <      std::string before;
108 <      std::string after;
109 <      before.assign(std::max(center - 2, 0), ' ');
110 <      after.assign(std::min(center + 2, 50), ' ');
111 <      
112 <      printf("\r[%s###%s]            ",
113 <             before.c_str(), after.c_str());
114 <    }
115 <    fflush(stdout);
104 >          // we know the maximum, so draw a progress bar
105 >          
106 >          RealType percent = value_ * 100.0 / maximum_;
107 >          int hashes = int(percent * avail / 100.0);
108 >          
109 >          // compute the best estimate of the ending time:
110 >          time_t current_ = time(NULL);
111 >          time_t end_ = start_ + (current_ - start_) * (100.0/percent);
112 >          struct tm * ender = localtime(&end_);
113 >          char buffer[22];
114 >          strftime(buffer, 22, "%a %b %d @ %I:%M %p", ender);
115 >          
116 >          cout << '\r';
117 >          cout.width(3);
118 >          cout << right << int(percent);
119 >          cout.width(3);
120 >          cout << "% [";
121 >          cout.fill('#');
122 >          if (hashes+1 < avail) {
123 >            cout.width(hashes+1);
124 >            cout << progressSpinner_[iteration_ & 3];
125 >          } else {
126 >            cout.width(avail);
127 >            cout << '#';            
128 >          }
129 >          cout.fill(' ');
130 >          if (avail - hashes - 1  > 0) {
131 >            cout.width(avail - hashes - 1);
132 >            cout << ' ';
133 >          }
134 >          cout.width(11);
135 >          cout << "] Estimate:";
136 >          cout.width(22);
137 >          cout << buffer;
138 >                  
139 >        }
140 >        cout.flush();
141 >      }
142   #ifdef IS_MPI
143      }
144   #endif

Comparing:
trunk/src/utils/ProgressBar.cpp (property svn:keywords), Revision 1420 by gezelter, Fri Mar 26 18:42:55 2010 UTC vs.
branches/development/src/utils/ProgressBar.cpp (property svn:keywords), Revision 1618 by gezelter, Mon Sep 12 17:09:26 2011 UTC

# Line 0 | Line 1
1 + Author Id Revision Date

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines