displayLayout and Rendering TUI library |
git clone git://git.dimitrijedobrota.com/display.git |
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING | |
commit | ec54978c5e15a73b65f884a504716c5ea08018a7 |
parent | ed9c35afa50572c55d4bd8765a3c90c5018faed0 |
author | Dimitrije Dobrota <mail@dimitrijedobrota.com> |
date | Fri, 7 Feb 2025 11:34:00 +0100 |
Better type consistency
Diffstat:M | .clang-tidy | | | ++ |
M | CMakeLists.txt | | | +- |
M | example/example.cpp | | | +++++++++++++++++++++---------------- |
M | include/display/layout.hpp | | | ++++++++++++++++++++++++++++++++++++---------------------- |
M | source/layout.cpp | | | ++++++++---------- |
5 files changed, 68 insertions(+), 49 deletions(-)
diff --git a/.clang-tidy b/.clang-tidy
@@ -5,6 +5,7 @@
Checks: "*,\
-google-readability-todo,\
-altera-*,\
-boost-*,\
-cppcoreguidelines-avoid-magic-numbers,\
-abseil-string-find-str-contains,\
-fuchsia-*,\
@@ -14,6 +15,7 @@ Checks: "*,\
-llvmlibc-*,\
-modernize-use-nodiscard,\
-modernize-use-trailing-return-type,\
-modernize-use-ranges,\
-misc-include-cleaner,\
-misc-non-private-member-variables-in-classes,\
-readability-magic-numbers
diff --git a/CMakeLists.txt b/CMakeLists.txt
@@ -4,7 +4,7 @@ include(cmake/prelude.cmake)
project(
display
VERSION 0.1.4
VERSION 0.1.5
DESCRIPTION "TUI library"
HOMEPAGE_URL "https://example.com/"
LANGUAGES CXX
diff --git a/example/example.cpp b/example/example.cpp
@@ -13,9 +13,9 @@ int renderer(display::Window& win)
static int color_red = 0;
std::cout << alec::background(color_red += 25, 65, 65);
for (auto ypos = win.ypos(); ypos < win.ypos() + win.height(); ypos++) {
std::cout << alec::cursor_position(ypos, win.xpos());
std::cout << std::string(win.width(), ' ');
for (auto ypos = win.pos().y; ypos < win.pos().y + win.dim().height; ypos++) {
std::cout << alec::cursor_position(ypos, win.pos().x);
std::cout << std::string(win.dim().width, ' ');
}
std::cout << alec::background_v<alec::Color::DEFAULT>;
std::cout << std::flush;
@@ -27,20 +27,25 @@ int renderer(display::Window& win)
int main()
{
display::start();
display::LayoutFree layout;
layout.append({{3, 3}, {15, 15}});
layout.append({{0, 0}, {10, 10}}, 1);
layout.append({{5, 5}, {5, 10}}, 1);
layout.append({{15, 15}, {5, 10}}, 1);
layout.render(renderer);
while (true) {
const auto event = alec::get_event();
if (event.type() == alec::event::Type::KEY && event.key() == 'q') {
break;
try {
display::start();
display::LayoutFree layout;
layout.append({{3, 3}, {15, 15}});
layout.append({{0, 0, 1}, {10, 10}});
layout.append({{5, 5, 1}, {5, 10}});
layout.append({{15, 15, 1}, {5, 10}});
layout.render(renderer);
while (true) {
const auto event = alec::get_event();
if (event.type() == alec::event::Type::KEY && event.key() == 'q') {
break;
}
}
} catch (std::exception& err) {
std::cout << err.what() << '\n' << std::flush;
}
display::stop();
diff --git a/include/display/layout.hpp b/include/display/layout.hpp
@@ -8,33 +8,47 @@ namespace display
{
using sz_t = std::uint16_t;
using pos_t = std::pair<sz_t, sz_t>;
using dim_t = std::pair<sz_t, sz_t>;
struct dim_t
{
dim_t(sz_t wdth = 0, sz_t hght = 0) // NOLINT
: width(wdth)
, height(hght)
{
}
sz_t width;
sz_t height;
};
struct pos_t
{
pos_t(sz_t xpos = 0, sz_t ypos = 0, sz_t zpos = 0) // NOLINT
: x(xpos)
, y(ypos)
, z(zpos)
{
}
sz_t x;
sz_t y;
sz_t z;
};
class Window
{
public:
Window(pos_t pos, dim_t dim)
: m_pos(std::move(pos))
, m_dim(std::move(dim))
: m_pos(pos)
, m_dim(dim)
{
}
auto& pos() const { return m_pos; }
auto xpos() const { return m_pos.first; }
auto ypos() const { return m_pos.second; }
auto& dim() const { return m_dim; }
auto width() const { return m_dim.first; }
auto height() const { return m_dim.second; }
auto set_pos(pos_t pos) { m_pos = std::move(pos); }
auto set_xpos(sz_t val) { m_pos.first = val; }
auto set_ypos(sz_t val) { m_pos.second = val; }
const auto& pos() const { return m_pos; }
auto& pos() { return m_pos; }
auto set_dim(dim_t dim) { m_dim = std::move(dim); }
auto set_width(sz_t val) { m_dim.first = val; }
auto set_height(sz_t val) { m_dim.second = val; }
const auto& dim() const { return m_dim; }
auto& dim() { return m_dim; }
private:
pos_t m_pos;
@@ -46,15 +60,15 @@ class LayoutFree
public:
using render_f = int(Window&);
auto operator[](std::size_t idx) const { return std::get<1>(m_windows[idx]); }
auto operator[](std::size_t idx) { return std::get<1>(m_windows[idx]); }
auto operator[](std::size_t idx) const { return m_windows[idx]; }
auto operator[](std::size_t idx) { return m_windows[idx]; }
void append(Window window, int zidx = -1);
void append(Window window);
int render(render_f renderer);
private:
std::vector<std::pair<int, Window>> m_windows;
std::vector<Window> m_windows;
bool m_is_sorted = true;
};
diff --git a/source/layout.cpp b/source/layout.cpp
@@ -6,9 +6,9 @@
namespace display
{
void LayoutFree::append(Window window, int zidx)
void LayoutFree::append(Window window)
{
m_windows.emplace_back(zidx, std::move(window));
m_windows.push_back(window);
m_is_sorted = false;
}
@@ -19,18 +19,16 @@ int LayoutFree::render(render_f renderer)
if (!m_is_sorted) {
idxs.resize(m_windows.size());
std::iota(idxs.begin(), idxs.end(), 0);
std::stable_sort(idxs.begin(), // NOLINT
idxs.end(),
[&](auto left, auto right)
{
return std::get<0>(m_windows[left])
< std::get<0>(m_windows[right]);
});
std::stable_sort(
idxs.begin(),
idxs.end(),
[&](auto left, auto right)
{ return m_windows[left].pos().z < m_windows[right].pos().z; });
m_is_sorted = true;
}
for (const auto idx : idxs) {
const auto res = renderer(std::get<1>(m_windows[idx]));
const auto res = renderer(m_windows[idx]);
if (res != 0) {
return res;
}