-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgressBar.mpp
More file actions
156 lines (132 loc) · 5.42 KB
/
ProgressBar.mpp
File metadata and controls
156 lines (132 loc) · 5.42 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
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
export module CppUtils.Terminal.ProgressBar;
import std;
import CppUtils.Terminal.Area;
import CppUtils.Container.Size;
import CppUtils.String.Encoding;
export namespace CppUtils::Terminal
{
struct BarRenderer final
{
float percent;
std::size_t width;
};
}
export template<>
struct std::formatter<CppUtils::Terminal::BarRenderer>
{
std::string filled = "#";
std::string empty = ".";
std::string head = "";
constexpr auto parse(std::format_parse_context& context)
{
auto iterator = std::begin(context);
if (iterator == std::end(context) or *iterator == '}')
return iterator;
auto content = std::string{};
while (iterator != std::end(context) and *iterator != '}')
content += *iterator++;
auto filledCharacter = CppUtils::String::utf32Substr(content, 0uz, 1uz);
auto emptyCharacter = CppUtils::String::utf32Substr(content, 1uz, 1uz);
if (not std::empty(filledCharacter) and not std::empty(emptyCharacter))
{
filled = filledCharacter;
empty = emptyCharacter;
head = CppUtils::String::utf32Substr(content, 2uz, 1uz);
}
return iterator;
}
auto format(const CppUtils::Terminal::BarRenderer& barRenderer, std::format_context& context) const
{
using namespace std::literals;
if (barRenderer.width == 0uz)
return context.out();
static constexpr auto doneText = "--- Done ---"sv;
if (barRenderer.percent >= 100.f and barRenderer.width >= std::size(doneText))
return std::format_to(context.out(), "{:^{}}", doneText, barRenderer.width);
const auto filledLength = static_cast<std::size_t>(static_cast<float>(barRenderer.width) * barRenderer.percent / 100.f);
const auto emptyLength = barRenderer.width - filledLength;
const auto hasHead = not std::empty(head) and filledLength > 0uz;
const auto filledLengthWithoutHead = filledLength - (hasHead ? 1uz : 0uz);
auto result = std::string{};
result.reserve(barRenderer.width * 4uz);
std::ranges::copy(std::views::repeat(filled) | std::views::take(filledLengthWithoutHead) | std::views::join, std::back_inserter(result));
if (hasHead)
result += head;
std::ranges::copy(std::views::repeat(empty) | std::views::take(emptyLength) | std::views::join, std::back_inserter(result));
return std::format_to(context.out(), "{}", result);
}
};
export namespace CppUtils::Terminal
{
using ProgressBarStyle = std::string;
namespace ProgressBarStyles
{
// [██████ ]
inline const auto Block = ProgressBarStyle{"{0} [{1:\N{FULL BLOCK} }] {2:>3}% "};
// [██████······]
inline const auto Clean = ProgressBarStyle{"{0} [{1:\N{FULL BLOCK}\N{MIDDLE DOT}}] {2:>3}% "};
// [━━━━━━──────]
inline const auto Line = ProgressBarStyle{"{0} [{1:\N{BOX DRAWINGS HEAVY HORIZONTAL}\N{BOX DRAWINGS LIGHT HORIZONTAL}}] {2:>3}% "};
// [━━━━━━▶─────]
inline const auto Arrowhead = ProgressBarStyle{"{0} [{1:\N{BOX DRAWINGS HEAVY HORIZONTAL}\N{BOX DRAWINGS LIGHT HORIZONTAL}\N{BLACK RIGHT-POINTING TRIANGLE}}] {2:>3}% "};
// [══════──────]
inline const auto Double = ProgressBarStyle{"{0} [{1:\N{BOX DRAWINGS DOUBLE HORIZONTAL}\N{BOX DRAWINGS LIGHT HORIZONTAL}}] {2:>3}% "};
// [●●●●●●○○○○○○]
inline const auto Round = ProgressBarStyle{"{0} [{1:\N{BLACK CIRCLE}\N{WHITE CIRCLE}}] {2:>3}% "};
// [▓▓▓▓▓▓░░░░░░]
inline const auto Shaded = ProgressBarStyle{"{0} [{1:\N{DARK SHADE}\N{LIGHT SHADE}}] {2:>3}% "};
// [⣿⣿⣿⣿⣿ ]
inline const auto Braille = ProgressBarStyle{"{0} [{1:\N{BRAILLE PATTERN DOTS-12345678}\N{BRAILLE PATTERN BLANK}}] {2:>3}% "};
// [>>>>>>------]
inline const auto Arrow = ProgressBarStyle{"{0} [{1:>-}] {2:>3}% "};
// [######......]
inline const auto Retro = ProgressBarStyle{"{0} [{1:#.}] {2:>3}%"};
}
class ProgressBar final: public Widget
{
public:
inline ProgressBar(std::string title = "", ProgressBarStyle style = ProgressBarStyles::Clean):
m_title{std::move(title)},
m_style{std::move(style)}
{}
inline auto setPercent(float percent) noexcept -> void
{
using namespace std::chrono_literals;
m_percent = std::clamp(percent, 0.f, 100.f);
if (hasWidgetManager())
requestUpdate(10ms);
}
inline auto setStyle(ProgressBarStyle style) noexcept -> void
{
using namespace std::chrono_literals;
m_style = std::move(style);
if (hasWidgetManager())
requestUpdate(10ms);
}
[[nodiscard]] inline auto getSize() const noexcept -> Container::Size2 final
{
return Container::Size2{std::numeric_limits<std::size_t>::max(), 1};
}
inline auto draw(WritableAreaView& view) noexcept -> void final
{
using namespace std::literals;
const auto size = view.getViewport().getSize();
const auto percent = static_cast<float>(m_percent);
const auto percentInt = static_cast<std::size_t>(percent);
auto dummyBarRenderer = BarRenderer{percent, 0uz};
const auto overheadText = std::vformat(m_style, std::make_format_args(m_title, dummyBarRenderer, percentInt));
const auto overheadWidth = std::size(overheadText);
if (size.width() <= overheadWidth + 2)
return;
const auto availableBarWidth = size.width() - overheadWidth;
auto barRenderer = BarRenderer{percent, availableBarWidth};
const auto text = std::vformat(m_style, std::make_format_args(m_title, barRenderer, percentInt));
view.printText({0, 0}, text);
drawFinished();
}
private:
std::string m_title;
std::string m_style;
std::atomic<float> m_percent = 0.f;
};
}