FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
menu_entries.cpp
Go to the documentation of this file.
1// Copyright 2020 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4#include <functional> // for function
5#include <iostream> // for basic_ostream::operator<<, operator<<, endl, basic_ostream, basic_ostream<>::__ostream_type, cout, ostream
6#include <memory> // for allocator, shared_ptr, __shared_ptr_access
7#include <string> // for char_traits, to_string, operator+, string, basic_string
8
9#include "ftxui/component/captured_mouse.hpp" // for ftxui
10#include "ftxui/component/component.hpp" // for MenuEntry, Renderer, Vertical
11#include "ftxui/component/component_base.hpp" // for ComponentBase
12#include "ftxui/component/component_options.hpp" // for MenuEntryOption
13#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
14#include "ftxui/dom/elements.hpp" // for operator|, Element, separator, text, hbox, size, frame, color, vbox, HEIGHT, LESS_THAN, bold, border, inverted
15#include "ftxui/screen/color.hpp" // for Color, Color::Blue, Color::Cyan, Color::Green, Color::Red, Color::Yellow
16
17using namespace ftxui;
18
19// Define a special style for some menu entry.
21 MenuEntryOption option;
22 option.transform = [c](EntryState state) {
23 state.label = (state.active ? "> " : " ") + state.label;
24 Element e = text(state.label) | color(c);
25 if (state.focused)
26 e = e | inverted;
27 if (state.active)
28 e = e | bold;
29 return e;
30 };
31 return option;
32}
33
34int main() {
36
37 int selected = 0;
38 auto menu = Container::Vertical(
39 {
40 MenuEntry(" 1. improve"),
41 MenuEntry(" 2. tolerant"),
42 MenuEntry(" 3. career"),
43 MenuEntry(" 4. cast"),
44 MenuEntry(" 5. question"),
45
46 Renderer([] { return separator(); }),
47
48 MenuEntry(" 6. rear", Colored(Color::Red)),
49 MenuEntry(" 7. drown", Colored(Color::Yellow)),
50 MenuEntry(" 8. nail", Colored(Color::Green)),
51 MenuEntry(" 9. quit", Colored(Color::Cyan)),
52 MenuEntry("10. decorative", Colored(Color::Blue)),
53
54 Renderer([] { return separator(); }),
55
56 MenuEntry("11. costume"),
57 MenuEntry("12. pick"),
58 MenuEntry("13. oral"),
59 MenuEntry("14. minister"),
60 MenuEntry("15. football"),
61 MenuEntry("16. welcome"),
62 MenuEntry("17. copper"),
63 MenuEntry("18. inhabitant"),
64 MenuEntry("19. fortune"),
65 },
66 &selected);
67
68 // Display together the menu with a border
69 auto renderer = Renderer(menu, [&] {
70 return vbox({
71 hbox(text("selected = "), text(std::to_string(selected))),
72 separator(),
73 menu->Render() | frame | size(HEIGHT, LESS_THAN, 10),
74 }) |
75 border;
76 });
77
78 screen.Loop(renderer);
79
80 std::cout << "Selected element = " << selected << std::endl;
81}
static ScreenInteractive TerminalOutput()
std::function< Element(const EntryState &state)> transform
Component MenuEntry(MenuEntryOption options)
A specific menu entry. They can be put into a Container::Vertical to form a menu.
Component Renderer(Component child, std::function< Element()>)
Return a new Component, similar to |child|, but using |render| as the Component::Render() event.
Component Vertical(Components children)
A list of components, drawn one by one vertically and navigated vertically using up/down arrow key or...
Option for the MenuEntry component.
virtual void Render(Screen &screen)
Display an element on a ftxui::Screen.
Definition node.cpp:59
Decorator size(WidthOrHeight, Constraint, int value)
Apply a constraint on the size of an element.
Element bold(Element)
Use a bold font, for elements with more emphasis.
Definition bold.cpp:33
Element inverted(Element)
Add a filter that will invert the foreground and the background colors.
Definition inverted.cpp:34
Element text(std::wstring text)
Display a piece of unicode text.
Definition text.cpp:160
Element separator()
Draw a vertical or horizontal separation in between two other elements.
Decorator color(Color)
Decorate using a foreground color.
Element vbox(Elements)
A container displaying elements vertically one by one.
Definition vbox.cpp:96
Color is a class that represents a color in the terminal user interface.
Definition color.hpp:22
MenuEntryOption Colored(ftxui::Color c)
int main()
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
std::shared_ptr< Node > Element
Definition elements.hpp:22
Element hbox(Elements)
A container displaying elements horizontally one by one.
Definition hbox.cpp:94
@ LESS_THAN
Definition elements.hpp:162
arguments for transform from |ButtonOption|, |CheckboxOption|, |RadioboxOption|, |MenuEntryOption|,...