FTXUI  5.0.0
C++ functional terminal UI.
examples/component/print_key_press.cpp
// Copyright 2020 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
#include <stddef.h> // for size_t
#include <algorithm> // for max
#include <memory> // for allocator, shared_ptr
#include <string> // for char_traits, operator+, string, basic_string, to_string
#include <utility> // for move
#include <vector> // for vector
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for CatchEvent, Renderer
#include "ftxui/component/event.hpp" // for Event
#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Middle, Mouse::None, Mouse::Pressed, Mouse::Released, Mouse::Right, Mouse::WheelDown, Mouse::WheelUp
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/dom/elements.hpp" // for text, vbox, window, Element, Elements
using namespace ftxui;
std::string Code(Event event) {
std::string codes;
for (auto& it : event.input()) {
codes += " " + std::to_string((unsigned int)it);
}
return codes;
}
int main() {
std::vector<Event> keys;
auto left_column = Renderer([&] {
Elements children = {
text("Codes"),
};
for (size_t i = std::max(0, (int)keys.size() - 20); i < keys.size(); ++i) {
children.push_back(text(Code(keys[i])));
}
return vbox(children);
});
auto right_column = Renderer([&] {
Elements children = {
text("Event"),
};
for (size_t i = std::max(0, (int)keys.size() - 20); i < keys.size(); ++i) {
children.push_back(text(keys[i].DebugString()));
}
return vbox(children);
});
int split_size = 40;
auto component = ResizableSplitLeft(left_column, right_column, &split_size);
component |= border;
component |= CatchEvent([&](Event event) {
keys.push_back(event);
return false;
});
screen.Loop(component);
}
static ScreenInteractive TerminalOutput()
Component Renderer(Component child, std::function< Element()>)
Return a new Component, similar to |child|, but using |render| as the Component::Render() event.
Definition: renderer.cpp:61
std::string to_string(const std::wstring &s)
Convert a UTF8 std::string into a std::wstring.
Definition: string.cpp:1565
Element text(std::wstring text)
Display a piece of unicode text.
Definition: text.cpp:119
std::vector< Element > Elements
Definition: elements.hpp:23
Component CatchEvent(Component child, std::function< bool(Event)>)
Element separator()
Draw a vertical or horizontal separation in between two other elements.
Definition: separator.cpp:134
Component ResizableSplitLeft(Component main, Component back, int *main_size)
An horizontal split in between two components, configurable using the mouse.
Element border(Element)
Draw a border around the element.
Definition: border.cpp:228
Element vbox(Elements)
A container displaying elements vertically one by one.
Definition: vbox.cpp:83