Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / samples / common / samples / console_progress.hpp
1 // Copyright (C) 2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #pragma once
6
7 #include <iostream>
8 #include <iomanip>
9
10 /**
11  * @class ConsoleProgress
12  * @brief A ConsoleProgress class provides functionality for printing progress dynamics
13  */
14 class ConsoleProgress {
15     static const int DEFAULT_DETALIZATION = 20;
16
17     size_t total;
18     size_t current = 0;
19     bool stream_output;
20     size_t detalization;
21
22 public:
23     /**
24     * @brief A constructor of ConsoleProgress class
25     * @param _total - maximum value that is correspondent to 100%
26     * @param _detalization - number of symbols(.) to use to represent progress
27     */
28     explicit ConsoleProgress(size_t _total, bool _stream_output = false, size_t _detalization = DEFAULT_DETALIZATION) :
29             total(_total), detalization(_detalization) {
30         stream_output = _stream_output;
31         if (total == 0) {
32             total = 1;
33         }
34     }
35
36     /**
37      * @brief Shows progress with current data. Progress is shown from the beginning of the current line.
38      * @return
39      */
40     void showProgress() const {
41         std::cout << "\rProgress: [";
42         size_t i = 0;
43         for (; i < detalization * current / total; i++) {
44             std::cout << ".";
45         }
46         for (; i < detalization; i++) {
47             std::cout << " ";
48         }
49         std::cout << "] " << std::fixed << std::setprecision(2) << 100 * static_cast<float>(current) / total << "% done";
50         if (stream_output) {
51             std::cout << std::endl;
52         } else {
53             std::flush(std::cout);
54         }
55     }
56
57     /**
58      * @brief Updates current value and progressbar
59      * @param newProgress - new value to represent
60      */
61     void updateProgress(size_t newProgress) {
62         current = newProgress;
63         if (current > total) current = total;
64         showProgress();
65     }
66
67     /**
68      * @brief Adds value to currently represented and redraw progressbar
69      * @param add - value to add
70      */
71     void addProgress(int add) {
72         if (add < 0 && -add > static_cast<int>(current)) {
73             add = -static_cast<int>(current);
74         }
75         updateProgress(current + add);
76     }
77
78     /**
79      * @brief Output end line.
80      * @return
81      */
82     void finish() {
83         std::cout << "\n";
84     }
85 };