-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgress.cpp
More file actions
99 lines (83 loc) · 2.86 KB
/
Progress.cpp
File metadata and controls
99 lines (83 loc) · 2.86 KB
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
//===----------------------------------------------------------------------===//
//
// Json Parser for large data set
//
//===----------------------------------------------------------------------===//
//
// Copyright (C) 2019. rollrat. All Rights Reserved.
//
//===----------------------------------------------------------------------===//
#include "Progress.h"
#include <algorithm>
///===-----------------------------------------------------------------------===
///
/// Progress Base
///
///===-----------------------------------------------------------------------===
void jsonhead::ProgressBase::Dispose() {
disposed = true;
update_text(jsonhead::String(""));
}
void jsonhead::ProgressBase::update_text(jsonhead::String&& text) {
using namespace jsonhead;
int prefix_len = 0;
int length = std::min(current_text.Length(), text.Length());
while (prefix_len < length && text[prefix_len] == current_text[prefix_len])
prefix_len++;
StringBuilder builder;
builder.Append(String('\b', current_text.Length() - prefix_len));
builder.Append(text.Substring(prefix_len));
int overlap = current_text.Length() - text.Length();
if (overlap > 0)
{
builder.Append(String(' ', overlap));
builder.Append(String('\b', overlap));
}
std::cout << builder.ToString();
current_text = text;
}
///===-----------------------------------------------------------------------===
///
/// Wait Progress
///
///===-----------------------------------------------------------------------===
jsonhead::WaitProgress::WaitProgress() {
timer = std::thread([&]() {
while (true) {
if (disposed) break;
auto delta = std::chrono::steady_clock::now() +
std::chrono::milliseconds(125);
Tick();
std::this_thread::sleep_until(delta);
}
});
}
void jsonhead::WaitProgress::Tick() {
update_text(animation[index++ % animation.Length()]);
}
void jsonhead::WaitProgress::Dispose() {
disposed = true;
timer.detach();
update_text(jsonhead::String(""));
}
///===-----------------------------------------------------------------------===
///
/// Progress Bar
///
///===-----------------------------------------------------------------------===
void jsonhead::ProgressBar::Update(double progress, String message) {
using namespace jsonhead;
if (disposed) return;
int progress_block_count = (int)(progress * block_count);
int percent = (int)(progress * 100);
StringBuilder builder;
builder.Append('[');
builder.Append(String('#', progress_block_count));
builder.Append(String('-', (size_t)block_count - progress_block_count));
builder.Append("] ");
builder.Append(percent);
builder.Append("% [");
builder.Append(message);
builder.Append(']');
update_text(builder.ToString());
}