display

Layout and Rendering TUI library
git clone git://git.dimitrijedobrota.com/display.git
Log | Files | Refs | README | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING |

commit7a98d5f4741e6dbd6eb4a8c44e63a283fbbbc508
parentf025ceaf680e36c28cef477714c94ee94e52f48f
authorDimitrije Dobrota <mail@dimitrijedobrota.com>
dateTue, 4 Feb 2025 15:09:30 +0100

Outsource work to alec

Diffstat:
MCMakeLists.txt|+-
Mexample/example.cpp|+++++++++++-
Minclude/display/display.hpp|++
Msource/display.cpp|++---------------------------

4 files changed, 16 insertions(+), 29 deletions(-)


diff --git a/CMakeLists.txt b/CMakeLists.txt

@@ -13,7 +13,7 @@ project(

include(cmake/project-is-top-level.cmake)
include(cmake/variables.cmake)
find_package(alec 0.1 CONFIG REQUIRED)
find_package(alec 0.1.13 CONFIG REQUIRED)
# ---- Declare library ----

diff --git a/example/example.cpp b/example/example.cpp

@@ -1,5 +1,6 @@

#include <cstdio>
#include <iostream>
#include "alec/alec.hpp"
#include "display/display.hpp"
int main()

@@ -7,6 +8,15 @@ int main()

display::start();
while (true) {
const auto event = alec::get_event();
if (event.type() == alec::event::Type::NONE) {
continue;
}
if (event.type() == alec::event::Type::KEY && event.key() == 'q') {
break;
}
std::cout << char(event.key()) << std::flush;
}
display::stop();

diff --git a/include/display/display.hpp b/include/display/display.hpp

@@ -9,4 +9,6 @@ void exit();

void start(exit_f f_exit = exit);
void stop(bool force = false);
int get_key();
} // namespace display

diff --git a/source/display.cpp b/source/display.cpp

@@ -22,7 +22,6 @@ namespace display

{
static int start_count = 0; // NOLINT
static struct termios orig_termios; // NOLINT
void exit()
{

@@ -36,34 +35,12 @@ void start(exit_f f_exit)

return;
}
if (isatty(STDIN_FILENO) == 0) {
throw std::runtime_error("STDIN_FILENO is not associated with a terminal");
}
if (atexit(f_exit) != 0) {
throw std::runtime_error("Can't register new exit function");
}
if (tcgetattr(STDIN_FILENO, &orig_termios) == -1) {
throw std::runtime_error("Can't read termios");
}
struct termios raw = orig_termios;
// NOLINTBEGIN
raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
raw.c_oflag &= ~(OPOST);
raw.c_cflag |= (CS8);
raw.c_lflag &= ~(ECHO | ICANON | IEXTEN); // | ISIG
raw.c_cc[VMIN] = 0;
raw.c_cc[VTIME] = 1;
// NOLINTEND
/* put terminal in raw mode after flushing */
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) < 0) {
throw std::runtime_error("Can't write termios");
}
alec::init_buffer(2);
write<alec::abuf_enable_v>();
write<alec::cursor_hide_v>();
}

@@ -75,10 +52,8 @@ void stop(bool force)

return;
}
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios) < 0) {
throw std::runtime_error("Can't write termios");
}
alec::dest_buffer();
write<alec::cursor_show_v>();
}