Skip to content

Commit 7cc5045

Browse files
committed
Terminal: ProgressBar
1 parent 4d56a87 commit 7cc5045

3 files changed

Lines changed: 22 additions & 10 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ The [unit tests](tests) serve as examples of how to use these functions.
4646

4747
## Getting Started
4848

49-
A C++23 compliant compiler with std module support and XMake is needed to build CppUtils
49+
A C++26 compliant compiler with std module support and XMake is needed to build CppUtils
5050

5151
This library is used in my C++ projects, but you can also use it in your projects.
5252
Just follow the installation steps and consult the documentation for each feature you need.

modules/Terminal/ProgressBar.mpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,24 @@ export namespace CppUtils::Terminal
1010
class ProgressBar final
1111
{
1212
public:
13-
inline ProgressBar():
14-
m_canvas{Container::Size2d<>{getTerminalSize().x, 3}}
13+
inline ProgressBar(std::string name = ""):
14+
m_name{std::move(name)},
15+
m_canvas{Container::Size2d<>{getTerminalSize().x, 1}}
1516
{
1617
setPercent(0);
1718
}
1819

19-
inline auto setPercent([[maybe_unused]] std::size_t percent) -> void
20+
inline auto setPercent(std::size_t percent) -> void
2021
{
22+
auto width = m_canvas.getSize().x - std::size(m_name) - 9;
23+
auto progress = percent * width / 100;
24+
auto string = std::format("{} [{}{}] {:>3}% ", m_name, std::string(progress, '#'), std::string(width - progress, '.'), percent);
25+
m_canvas.print(Container::Size2d<>{0, 0}, string);
26+
m_canvas.update();
2127
}
2228

2329
private:
30+
std::string m_name;
2431
Canvas m_canvas;
2532
};
2633
}

tests/Terminal/Canvas.mpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,29 @@ export namespace CppUtils::UnitTest::Terminal::Canvas
1414
auto canvas = CppUtils::Terminal::Canvas{CppUtils::Container::Size2d<>{10, 5}};
1515
canvas.fill('/');
1616
canvas.update();
17-
std::this_thread::sleep_for(500ms);
17+
std::this_thread::sleep_for(300ms);
1818
}
1919
{
2020
auto canvas = CppUtils::Terminal::Canvas{CppUtils::Container::Size2d<>{16, 9}};
2121
canvas.fill('.');
2222
canvas.update();
23-
std::this_thread::sleep_for(500ms);
23+
std::this_thread::sleep_for(300ms);
2424
}
2525
{
2626
auto canvas = CppUtils::Terminal::Canvas{};
2727
canvas.fill('#');
2828
canvas.update();
29-
std::this_thread::sleep_for(500ms);
29+
std::this_thread::sleep_for(300ms);
3030
}
3131
});
3232

3333
suite.addTest("Fill", [&] {
3434
auto canvas = CppUtils::Terminal::Canvas{CppUtils::Container::Size2d<>{10, 5}};
35-
const auto chars = "-\\||/"sv;
35+
const auto chars = "-\\|/"sv;
3636
for (auto i = 0uz; i < 5; ++i)
3737
for (auto c : chars)
3838
{
39-
std::this_thread::sleep_for(75ms);
39+
std::this_thread::sleep_for(100ms);
4040
canvas.fill(c);
4141
canvas.update();
4242
}
@@ -50,7 +50,12 @@ export namespace CppUtils::UnitTest::Terminal::Canvas
5050
});
5151

5252
suite.addTest("Progress bar", [&] {
53-
std::this_thread::sleep_for(3s);
53+
auto progressBar = CppUtils::Terminal::ProgressBar{"Progress bar"};
54+
for (auto i = 0uz; i <= 100; ++i)
55+
{
56+
progressBar.setPercent(i);
57+
std::this_thread::sleep_for(20ms);
58+
}
5459
});
5560

5661
suite.addTest("Bouncing ball", [&] {

0 commit comments

Comments
 (0)