Skip to content

Commit a85c21a

Browse files
committed
Code cleaning
1 parent 3e8a5be commit a85c21a

11 files changed

Lines changed: 147 additions & 55 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
- [`Title`](modules/Terminal/Title.mpp) - Terminal title utilities
8282

8383
### 🚦 Multithreading & Synchronization
84-
- [`Scheduler`](modules/Thread/Scheduler.mpp) - Simple scheduler to run delayed functions on separate threads
84+
- [`Scheduler`](modules/Thread/Scheduler.mpp) - Simple scheduler to run delayed functions on separate thread
8585
- [`ThreadLoop`](modules/Thread/ThreadLoop.mpp) - Thread loop with exception handling
8686
- [`ThreadPool`](modules/Thread/ThreadPool.mpp) - Fixed-size thread pool for parallel task execution
8787
- [`TryAsync`](modules/Thread/TryAsync.mpp) - Launches a function asynchronously, forwards exception to caller

modules/Log/Logger.mpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ export module CppUtils.Logger;
66

77
import std;
88
import CppUtils.String;
9-
import CppUtils.Terminal;
9+
import CppUtils.Terminal.Size;
10+
import CppUtils.Terminal.TextColor;
11+
import CppUtils.Terminal.TextModifier;
1012

1113
// Todo: log le datetime
1214
// Todo: log le stacktrace ( https://en.cppreference.com/w/cpp/utility/stacktrace_entry )

modules/Terminal/Area.mpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export module CppUtils.Terminal.Area;
22

33
import std;
44
import CppUtils.Container.Size;
5-
import :AreaBuffer;
5+
export import :AreaBuffer;
66
export import CppUtils.Terminal.CharAttributes;
77
export import :Widget;
88
export import :Viewport;

modules/Terminal/AreaBuffer.mpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,18 @@ export namespace CppUtils::Terminal
1212
using Line = std::vector<CharAttributes>;
1313
using Buffer = std::vector<Line>;
1414

15-
inline AreaBuffer(const Container::Size2& size)
16-
{
17-
setSize(size);
18-
}
15+
inline AreaBuffer(const Container::Size2& size, CharAttributes defaultCharAttributes = {}):
16+
AreaBuffer{size, Buffer{size.height(), Line{size.width(), defaultCharAttributes}}}
17+
{}
18+
19+
inline AreaBuffer(const Container::Size2& size, char defaultChar):
20+
AreaBuffer{size, CharAttributes{defaultChar}}
21+
{}
22+
23+
inline AreaBuffer(const Container::Size2& size, Buffer&& buffer):
24+
m_size{size},
25+
m_buffer{std::move(buffer)}
26+
{}
1927

2028
inline auto setSize(const Container::Size2& size) noexcept -> void
2129
{
@@ -46,7 +54,7 @@ export namespace CppUtils::Terminal
4654
}
4755

4856
private:
49-
Buffer m_buffer;
5057
Container::Size2 m_size;
58+
Buffer m_buffer;
5159
};
5260
}

modules/Terminal/Canvas.mpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import CppUtils.Terminal.TextStyle;
1414
import CppUtils.Terminal.TextColor;
1515
import CppUtils.Terminal.BackgroundColor;
1616
import CppUtils.Execution.Event;
17+
import CppUtils.Thread.Scheduler;
18+
import CppUtils.Execution.EventSystem;
1719

1820
export namespace CppUtils::Terminal
1921
{
@@ -202,6 +204,8 @@ export namespace CppUtils::Terminal
202204
bool m_forceReprint = true;
203205
AreaBuffer m_previousBuffer;
204206
Execution::Event m_stopEvent;
207+
Thread::Scheduler m_scheduler;
208+
Execution::EventSystem m_eventSystem;
205209
};
206210
}
207211
}

modules/Terminal/Spinner.mpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
export module CppUtils.Terminal.Spinner;
2+
3+
import std;
4+
import CppUtils.Terminal.Area;
5+
import CppUtils.Container.Size;
6+
import CppUtils.Chrono.Concept;
7+
8+
export namespace CppUtils::Terminal
9+
{
10+
template<Chrono::Duration Duration = std::chrono::milliseconds>
11+
class Spinner final: public Widget
12+
{
13+
public:
14+
inline Spinner(const Container::Size2& size, std::vector<AreaBuffer>&& frames, const Duration& duration = std::chrono::milliseconds{100}):
15+
m_size{size},
16+
m_frames{std::move(frames)},
17+
m_duration{duration}
18+
{}
19+
20+
inline auto editFrame(std::size_t frame, AreaBuffer&& buffer) noexcept -> void
21+
{
22+
if (frame >= std::size(m_frames))
23+
return;
24+
m_frames[frame] = std::move(buffer);
25+
}
26+
27+
inline auto draw(WritableAreaView& view) noexcept -> void
28+
{
29+
if (std::empty(m_frames))
30+
return;
31+
view.applyArea(m_frames[m_frame]);
32+
if (++m_frame == std::size(m_frames))
33+
m_frame = 0;
34+
}
35+
36+
[[nodiscard]] inline constexpr auto getSize() const noexcept -> const auto&
37+
{
38+
return m_size;
39+
}
40+
41+
private:
42+
Container::Size2 m_size;
43+
std::size_t m_frame = 0;
44+
std::vector<AreaBuffer> m_frames;
45+
Duration m_duration;
46+
};
47+
48+
template<Chrono::Duration Duration>
49+
Spinner(const Container::Size2& size, std::vector<AreaBuffer::Buffer>&& frames, const Duration& duration) -> Spinner<Duration>;
50+
}

modules/Terminal/Terminal.mpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
export module CppUtils.Terminal;
22

3+
export import CppUtils.Terminal.Area;
34
export import CppUtils.Terminal.BackgroundColor;
45
export import CppUtils.Terminal.Canvas;
6+
export import CppUtils.Terminal.CharAttributes;
57
export import CppUtils.Terminal.Cursor;
68
export import CppUtils.Terminal.Handle;
9+
export import CppUtils.Terminal.Layout;
710
export import CppUtils.Terminal.ProgressBar;
811
export import CppUtils.Terminal.RawTerminal;
912
export import CppUtils.Terminal.Size;
13+
export import CppUtils.Terminal.Spinner;
1014
export import CppUtils.Terminal.TextColor;
1115
export import CppUtils.Terminal.TextModifier;
1216
export import CppUtils.Terminal.TextStyle;

modules/Terminal/WritableAreaView.mpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export namespace CppUtils::Terminal
1111
public:
1212
WritableAreaView(AreaBuffer& areaBuffer, Viewport viewport):
1313
m_areaBuffer{areaBuffer},
14-
m_viewport{viewport}
14+
m_viewport{std::move(viewport)}
1515
{}
1616

1717
[[nodiscard]] inline auto getSubView(Viewport viewport) noexcept -> std::expected<WritableAreaView, std::string>
@@ -31,6 +31,11 @@ export namespace CppUtils::Terminal
3131
});
3232
}
3333

34+
inline auto applyArea(const AreaBuffer& areaBuffer) noexcept -> void
35+
{
36+
applyArea(areaBuffer, m_viewport);
37+
}
38+
3439
inline auto setChar(const Container::Size2& position, const CharAttributes& c) noexcept -> void
3540
{
3641
if (not m_viewport.contains(position))

modules/Type/Utility.mpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export namespace CppUtils::Type
2929
inline constexpr auto preferPassByValue = not preferPassByRef<T>;
3030

3131
template<class T>
32-
using In = std::conditional_t<preferPassByValue<T>, T, const T&>;
32+
using In = std::conditional_t<preferPassByRef<T>, const T&, T>;
3333

3434
static_assert(std::is_same_v<In<int>, int>);
3535
static_assert(std::is_same_v<In<std::string>, const std::string&>);

tests/Terminal/Canvas.mpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export namespace CppUtils::UnitTest::Terminal::Canvas
4242
}
4343
});
4444

45+
/*
4546
suite.addTest("Fill with scheduler", [&] {
4647
auto canvas = CppUtils::Terminal::v2::Canvas{CppUtils::Container::Size2{10, 5}};
4748
auto scheduler = CppUtils::Thread::Scheduler{};
@@ -62,6 +63,24 @@ export namespace CppUtils::UnitTest::Terminal::Canvas
6263
scheduler.schedule(self, 50ms);
6364
};
6465
updateFrame();
66+
scheduler.waitUntilFinished();
67+
canvas.wait();
68+
});
69+
*/
70+
71+
suite.addTest("Spinner", [&] {
72+
auto canvas = CppUtils::Terminal::v2::Canvas{CppUtils::Container::Size2{1, 1}};
73+
auto frames = std::vector<CppUtils::Terminal::AreaBuffer>{
74+
{CppUtils::Container::Size2{1, 1}, '-'},
75+
{CppUtils::Container::Size2{1, 1}, '\\'},
76+
{CppUtils::Container::Size2{1, 1}, '|'},
77+
{CppUtils::Container::Size2{1, 1}, '/'}};
78+
canvas.addWidget(std::make_unique<CppUtils::Terminal::Spinner<>>(CppUtils::Container::Size2{1, 1}, std::move(frames), 100ms));
79+
auto scheduler = CppUtils::Thread::Scheduler{};
80+
scheduler.schedule([&canvas]() mutable {
81+
canvas.close();
82+
}, 250ms);
83+
scheduler.waitUntilFinished();
6584
canvas.wait();
6685
});
6786

0 commit comments

Comments
 (0)