displayLayout and Rendering TUI library |
git clone git://git.dimitrijedobrota.com/display.git |
Log | Files | Refs | README | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING | |
commit | 7ab20e8f93307b6e9e69366997d3f776fc37072c |
parent | 02cc734123e95d7ed303a9a7537fa7fc746719b1 |
author | Dimitrije Dobrota <mail@dimitrijedobrota.com> |
date | Sat, 8 Feb 2025 14:40:48 +0100 |
Add support for pivoting windows
Diffstat:M | .clang-tidy | | | +++--- |
M | CMakeLists.txt | | | +- |
M | example/example.cpp | | | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ |
M | include/display/layout.hpp | | | ++++++++++++++++++++++++++++++++- |
4 files changed, 92 insertions(+), 11 deletions(-)
diff --git a/.clang-tidy b/.clang-tidy
@@ -79,9 +79,9 @@ CheckOptions:
- key: 'readability-identifier-naming.ConstexprVariableCase'
value: 'lower_case'
- key: 'readability-identifier-naming.EnumCase'
value: 'lower_case'
value: 'CamelCase'
- key: 'readability-identifier-naming.EnumConstantCase'
value: 'lower_case'
value: 'CamelCase'
- key: 'readability-identifier-naming.FunctionCase'
value: 'lower_case'
- key: 'readability-identifier-naming.GlobalConstantCase'
@@ -135,7 +135,7 @@ CheckOptions:
- key: 'readability-identifier-naming.PublicMethodCase'
value: 'lower_case'
- key: 'readability-identifier-naming.ScopedEnumConstantCase'
value: 'lower_case'
value: 'CamelCase'
- key: 'readability-identifier-naming.StaticConstantCase'
value: 'lower_case'
- key: 'readability-identifier-naming.StaticVariableCase'
diff --git a/CMakeLists.txt b/CMakeLists.txt
@@ -4,7 +4,7 @@ include(cmake/prelude.cmake)
project(
display
VERSION 0.1.5
VERSION 0.1.6
DESCRIPTION "TUI library"
HOMEPAGE_URL "https://example.com/"
LANGUAGES CXX
diff --git a/example/example.cpp b/example/example.cpp
@@ -10,11 +10,49 @@ namespace
int renderer(display::Window& win)
{
using display::PvtX;
using display::PvtY;
static int color_red = 0;
display::sz_t start_x = 0;
display::sz_t end_x = 0;
display::sz_t start_y = 0;
display::sz_t end_y = 0;
switch (win.piv().x) {
case PvtX::Left:
start_x = win.pos().x;
end_x = start_x + win.dim().width;
break;
case PvtX::Center:
start_x = win.pos().x - win.dim().width / 2;
end_x = start_x + win.dim().width;
break;
case PvtX::Right:
end_x = win.pos().x;
start_x = end_x - win.dim().width;
break;
}
switch (win.piv().y) {
case PvtY::Top:
start_y = win.pos().y;
end_y = start_y + win.dim().height;
break;
case PvtY::Center:
start_y = win.pos().y - win.dim().height / 2;
end_y = start_y + win.dim().height;
break;
case PvtY::Bottom:
end_y = win.pos().y;
start_y = end_y - win.dim().height;
break;
}
std::cout << alec::background(color_red += 25, 65, 65);
for (auto ypos = win.pos().y; ypos < win.pos().y + win.dim().height; ypos++) {
std::cout << alec::cursor_position(ypos, win.pos().x);
for (auto ypos = start_y; ypos <= end_y; ypos++) {
std::cout << alec::cursor_position(ypos, start_x);
std::cout << std::string(win.dim().width, ' ');
}
std::cout << alec::background_v<alec::Color::DEFAULT>;
@@ -30,11 +68,23 @@ int main()
try {
display::start();
const auto [cols, rows] = alec::get_screen_size();
const std::uint16_t colsm = cols / 2;
const std::uint16_t rowm = rows / 2;
using display::PvtX;
using display::PvtY;
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.append({{0, 0}, {20, 10}, {PvtX::Left, PvtY::Top}});
layout.append({{colsm, 0}, {20, 10}, {PvtX::Center, PvtY::Top}});
layout.append({{cols, 0}, {20, 10}, {PvtX::Right, PvtY::Top}});
layout.append({{cols, rowm}, {20, 10}, {PvtX::Right, PvtY::Center}});
layout.append({{cols, rows}, {20, 10}, {PvtX::Right, PvtY::Bottom}});
layout.append({{colsm, rows}, {20, 10}, {PvtX::Center, PvtY::Bottom}});
layout.append({{0, rows}, {20, 10}, {PvtX::Left, PvtY::Bottom}});
layout.append({{0, rowm}, {20, 10}, {PvtX::Left, PvtY::Center}});
layout.append({{colsm, rowm}, {20, 10}, {PvtX::Center, PvtY::Center}});
layout.render(renderer);
while (true) {
diff --git a/include/display/layout.hpp b/include/display/layout.hpp
@@ -35,12 +35,39 @@ struct pos_t
sz_t z;
};
enum class PvtX : std::uint8_t
{
Left,
Center,
Right
};
enum class PvtY : std::uint8_t
{
Top,
Center,
Bottom
};
struct piv_t
{
piv_t(PvtX pvtx = PvtX::Left, PvtY pvty = PvtY::Top) // NOLINT
: x(pvtx)
, y(pvty)
{
}
PvtX x;
PvtY y;
};
class Window
{
public:
Window(pos_t pos, dim_t dim)
Window(pos_t pos, dim_t dim, piv_t piv = {})
: m_pos(pos)
, m_dim(dim)
, m_piv(piv)
{
}
@@ -50,9 +77,13 @@ public:
const auto& dim() const { return m_dim; }
auto& dim() { return m_dim; }
const auto& piv() const { return m_piv; }
auto& piv() { return m_piv; }
private:
pos_t m_pos;
dim_t m_dim;
piv_t m_piv;
};
class LayoutFree