TPIE

2362a60
progress_indicator_arrow.h
Go to the documentation of this file.
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; c-file-style: "stroustrup"; -*-
2 // vi:set ts=4 sts=4 sw=4 noet :
3 // Copyright 2008, The TPIE development team
4 //
5 // This file is part of TPIE.
6 //
7 // TPIE is free software: you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the
9 // Free Software Foundation, either version 3 of the License, or (at your
10 // option) any later version.
11 //
12 // TPIE is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with TPIE. If not, see <http://www.gnu.org/licenses/>
19 
23 
24 #ifndef _TPIE_PROGRESS_INDICATOR_ARROW_H
25 #define _TPIE_PROGRESS_INDICATOR_ARROW_H
26 
27 #include <algorithm>
28 #include <iostream>
30 
31 namespace tpie {
32 
38  private:
40  public:
41 
48  progress_indicator_arrow(const char * title, stream_size_type range, std::ostream & os = std::cout) :
49  progress_indicator_terminal(title, range, os) , m_indicatorLength(0), m_os(os) {
50  m_indicatorLength = 110;
51  }
52 
59  void set_indicator_length(int indicatorLength) {
60  m_indicatorLength = std::max(2, std::min(60, indicatorLength));
61  }
62 
66  virtual void reset() {
67  m_current = 0;
68  }
69 
70  void push_breadcrumb(const char * crumb, description_importance /*importance*/) {
71  m_crumbs.push_back(crumb);
72  }
73 
74  void pop_breadcrumb() {
75  m_crumbs.pop_back();
76  }
77 
81  virtual void refresh() {
82  // Compute the relative length of the arrow.
83  //std::cout << "refresh " << m_description << std::endl;
84 
85  memory_size_type l = m_indicatorLength - 12 - m_title.size();
86  memory_size_type progress = (m_range != 0) ?
87  static_cast<memory_size_type>(l * m_current / m_range) : 0;
88 
89  std::string newStatus;
90  {
91  std::stringstream status;
92 
93  // Don't print the last item.
94  if (progress >= l) progress = l -1;
95 
96  // Go to the beginning of the line and print the description.
97  status << m_title << " [";
98 
99  // Extend the arrow.
100 
101  status << std::string(progress, '=');
102  status << '>';
103 
104  // Print blank space.
105  status << std::string(l-progress-1, ' ');
106  status << "] ";
107 
108  status << m_current * 100 / m_range << '%';
109 
110  for (std::deque<std::string>::iterator i = m_crumbs.begin(); i != m_crumbs.end(); ++i) {
111  if (i == m_crumbs.begin()) status << ' ';
112  else status << " > ";
113  status << *i;
114  }
115 
116  status << ' ' << estimated_remaining_time();
117 
118  newStatus = status.str();
119  }
120 
121  if (newStatus != m_status) {
122 #ifdef WIN32
123  m_os << '\r' << std::string(m_status.size(), ' ') << '\r'
124  << newStatus << std::flush;
125 #else
126  m_os << "\r\x1B[K" << newStatus << std::flush;
127 #endif
128  std::swap(newStatus, m_status);
129  }
130  }
131 
132  protected:
133 
135  memory_size_type m_indicatorLength;
136 
138  std::string m_status;
139 
141  std::ostream & m_os;
142 
143  std::deque<std::string> m_crumbs;
144 
145  private:
146 
147  progress_indicator_arrow();
148  };
149 
150 }
151 
152 #endif // _TPIE_PROGRESS_INDICATOR_ARROW
A class that indicates the progress by expanding an arrow.
stream_size_type m_current
The current progress count [m_minRange...m_maxRange].
memory_size_type m_indicatorLength
The maximal length of the indicator.
stream_size_type m_range
The upper bound of the counting range.
A class that indicates the progress by a simple counter that is printed to the terminal.
void set_indicator_length(int indicatorLength)
Set the maximum length of the indicator.
std::ostream & m_os
ostream on which to display the progress indicator
progress_indicator_arrow(const char *title, stream_size_type range, std::ostream &os=std::cout)
Initializes the indicator.
virtual void refresh()
Display the indicator.
Indicate progress by a simple counter.
std::string m_title
A string holding the description of the title.
virtual void reset()
Reset the current state of the indicator and its current length.