File: progressbar.cpp

package info (click to toggle)
mrtrix3 3.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,712 kB
  • sloc: cpp: 129,776; python: 9,494; sh: 593; makefile: 234; xml: 47
file content (211 lines) | stat: -rw-r--r-- 5,949 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* Copyright (c) 2008-2022 the MRtrix3 contributors.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Covered Software is provided under this License on an "as is"
 * basis, without warranty of any kind, either expressed, implied, or
 * statutory, including, without limitation, warranties that the
 * Covered Software is free of defects, merchantable, fit for a
 * particular purpose or non-infringing.
 * See the Mozilla Public License v. 2.0 for more details.
 *
 * For more details, see http://www.mrtrix.org/.
 */


#include "app.h"
#include "progressbar.h"

// MSYS2 supports VT100, and file redirection is handled explicitly so this can be used globally
#define CLEAR_LINE_CODE "\033[0K"
#define WRAP_ON_CODE "\033[?7h"
#define WRAP_OFF_CODE "\033[?7l"

namespace MR
{

  extern bool __need_newline;

  namespace
  {

    const char* busy[] = {
      ".   ",
      " .  ",
      "  . ",
      "   .",
      "  . ",
      " .  "
    };



    void display_func_multithreaded (const ProgressBar& p)
    {
      ProgressBar::notification_is_genuine = true;
      ProgressBar::notifier.notify_all();
    }


    void display_func_terminal (const ProgressBar& p)
    {
      __need_newline = true;
      if (p.show_percent())
        __print_stderr (printf (WRAP_OFF_CODE "\r%s: [%3" PRI_SIZET "%%] %s%s" CLEAR_LINE_CODE WRAP_ON_CODE,
              App::NAME.c_str(), p.value(), p.text().c_str(), p.ellipsis().c_str()));
      else
        __print_stderr (printf (WRAP_OFF_CODE "\r%s: [%s] %s%s" CLEAR_LINE_CODE WRAP_ON_CODE,
              App::NAME.c_str(), busy[p.value()%6], p.text().c_str(), p.ellipsis().c_str()));
    }


    void done_func_terminal (const ProgressBar& p)
    {
      if (p.show_percent())
        __print_stderr (printf ("\r%s: [100%%] %s" CLEAR_LINE_CODE "\n",
              App::NAME.c_str(), p.text().c_str()));
      else
        __print_stderr (printf ("\r%s: [done] %s" CLEAR_LINE_CODE "\n",
              App::NAME.c_str(), p.text().c_str()));
      __need_newline = false;
    }







    void display_func_redirect (const ProgressBar& p)
    {
      static size_t count = 0;
      static size_t next_update_at = 0;
      // need to update whole line since text may have changed:
      if (p.text_has_been_modified()) {
        __need_newline = false;
        if (p.value() == 0 && p.count() == 0)
          count = next_update_at = 0;
        if (count++ == next_update_at) {
          if (p.show_percent()) {
            __print_stderr (printf ("%s: [%3" PRI_SIZET "%%] %s%s\n",
                  App::NAME.c_str(), p.value(), p.text().c_str(), p.ellipsis().c_str()));;
          }
          else {
            __print_stderr (printf ("%s: [%s] %s%s\n",
                  App::NAME.c_str(), busy[p.value()%6], p.text().c_str(), p.ellipsis().c_str()));
          }
          if (next_update_at)
            next_update_at *= 2;
          else
            next_update_at = 1;
        }
      }
      // text is static - can simply append to the current line:
      else {
        __need_newline = true;
        if (p.show_percent()) {
          if (p.first_time) {
             p.first_time = false;
            __print_stderr (printf ("%s: %s%s [",
                  App::NAME.c_str(), p.text().c_str(), p.ellipsis().c_str()));;
          }
          else while (p.last_value < p.value()) {
            __print_stderr (printf ("="));
            p.last_value += 2;
          }
        }
        else {
          if (p.value() == 0) {
            __print_stderr (printf ("%s: %s%s ",
                  App::NAME.c_str(), p.text().c_str(), p.ellipsis().c_str()));;
          }
          else if (!(p.value() & (p.value()-1))) {
            __print_stderr (".");
          }
        }
      }
    }


    void done_func_redirect (const ProgressBar& p)
    {
      if (p.text_has_been_modified()) {
        if (p.show_percent()) {
          __print_stderr (printf ("%s: [100%%] %s\n", App::NAME.c_str(), p.text().c_str()));;
        }
        else {
          __print_stderr (printf ("%s: [done] %s\n", App::NAME.c_str(), p.text().c_str()));
        }
      }
      else {
        if (p.show_percent())
          __print_stderr (printf ("]\n"));
        else
          __print_stderr (printf ("done\n"));
      }
      __need_newline = false;
    }

  }


  void (*ProgressBar::display_func) (const ProgressBar& p) = display_func_terminal;
  void (*ProgressBar::done_func) (const ProgressBar& p) = done_func_terminal;
  void (*ProgressBar::previous_display_func) (const ProgressBar& p) = nullptr;

  std::condition_variable ProgressBar::notifier;
  bool ProgressBar::notification_is_genuine = false;
  std::mutex ProgressBar::mutex;
  void* ProgressBar::data = nullptr;
  bool ProgressBar::progressbar_active = false;

  ProgressBar::SwitchToMultiThreaded::SwitchToMultiThreaded () {
    ProgressBar::previous_display_func = ProgressBar::display_func;
    ProgressBar::display_func = display_func_multithreaded;
  }


  ProgressBar::SwitchToMultiThreaded::~SwitchToMultiThreaded () {
    ProgressBar::display_func = ProgressBar::previous_display_func;
    ProgressBar::previous_display_func = nullptr;
  }




  void (*previous_display_func) (ProgressBar& p);



  bool ProgressBar::set_update_method ()
  {
    bool stderr_to_file = false;

    struct stat buf;
    if (fstat (STDERR_FILENO, &buf))
      // unable to determine nature of stderr; assuming socket
      stderr_to_file = false;
    else
      stderr_to_file = S_ISREG (buf.st_mode);



    if (stderr_to_file) {
      ProgressBar::display_func = display_func_redirect;
      ProgressBar::done_func = done_func_redirect;
    }
    else {
      ProgressBar::display_func = display_func_terminal;
      ProgressBar::done_func = done_func_terminal;
    }

    return stderr_to_file;
  }




}