ftxui_template/README.md
2025-05-09 11:31:50 +02:00

2.5 KiB

FTXUI Template

Static Badge Static Badge

Template example using the FTXUI library with a proper logger, thread-safe event handler and multiple pages paradigm. This template can be used in any project and is a good way to start a project using a simple and not-so-verbose approach.

Build the project

You can simply use CMake with the following commands:

mkdir build && cd build
cmake ..
cmake --build . --parallel

Using the Interface

Event

Main thread-safe event handler. You can set a custom function in it that will be called once the loop is starting. It is recommended to run the eventhandler in a separated thread.

Each event that is added or handled is thread safe.

main.cpp


static inline void loop_event(EventHandler &handler, const EventPayload &payload)
{
    // Called each time there is a new event in the event buffer...
}

int main(void)
{
    // Basic usage
    EventHandler handler;
    handler.set_handler(loop_event);
    Page1 page1(handler);
    // Starting the event loop in a separated thread.
    std::thread t_event(&EventHandler::loop, &handler);
    t_event.join();
    return 0;
}

page1.cpp

void Page1::change_screen()
{
    this->send_event(EventType::SWITCH_SCREEN, static_cast<size_t>(1));
}

Logger

'Logger' is a general Logger that can be instanciated anywhere as a singleton. It can have a maximum size for its buffer that can be changed on the go.Each LoggerType can be gathered from each data which can lead to multiple ways to print out a log.

Logger& logger = Logger::GetInstance();
logger.push_back("Hello, world!", LoggerType::PRINT);
// std::vector<std::string>& buffer = logger.get_buffer();

UserInterface

'UserInterface' is the main object that can be instanciated anywhere as a singleton you can add any pages to it and cycle through them using the select_screen function.

UserInterface &ui = UserInterface::GetInstance();

int main(void)
{
    // Event declaration
    EventHandler handler;

    // Page declaration
    PageX pagex(handler);
    // other pages...

    ui.add_screen(&pagex);
    // other pages...

    ui.start();
    return 0;
}

Creating a new page

To create a new page, just create a parent class from Page and create a ftxui::Component named _page in the constructor. You can follow the examples given in the pages folder.