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
17 using namespace ftxui;
18
19// Define a special style for some menu entry.
20 MenuEntryOption Colored(ftxui::Color c) {
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
34 int main() {
35 auto screen = ScreenInteractive::TerminalOutput();
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}
captured_mouse.hpp
color.hpp
component.hpp
component_base.hpp
component_options.hpp
elements.hpp
ftxui::ScreenInteractive::TerminalOutput
static ScreenInteractive TerminalOutput()
Definition screen_interactive.cpp:410
ftxui::MenuEntryOption::transform
std::function< Element(const EntryState &state)> transform
Definition component_options.hpp:80
ftxui::MenuEntry
Component MenuEntry(MenuEntryOption options)
A specific menu entry. They can be put into a Container::Vertical to form a menu.
Definition src/ftxui/component/menu.cpp:614
ftxui::Renderer
Component Renderer(Component child, std::function< Element()>)
Return a new Component, similar to |child|, but using |render| as the Component::Render() event.
Definition src/ftxui/component/renderer.cpp:61
ftxui::Container::Vertical
Component Vertical(Components children)
A list of components, drawn one by one vertically and navigated vertically using up/down arrow key or...
Definition container.cpp:317
ftxui::MenuEntryOption
Option for the MenuEntry component.
Definition component_options.hpp:78
ftxui::Node::Render
virtual void Render(Screen &screen)
Display an element on a ftxui::Screen.
Definition node.cpp:59
ftxui::size
Decorator size(WidthOrHeight, Constraint, int value)
Apply a constraint on the size of an element.
Definition src/ftxui/dom/size.cpp:89
ftxui::bold
Element bold(Element)
Use a bold font, for elements with more emphasis.
Definition bold.cpp:33
ftxui::inverted
Element inverted(Element)
Add a filter that will invert the foreground and the background colors.
Definition inverted.cpp:34
ftxui::text
Element text(std::wstring text)
Display a piece of unicode text.
Definition text.cpp:160
ftxui::separator
Element separator()
Draw a vertical or horizontal separation in between two other elements.
Definition src/ftxui/dom/separator.cpp:134
ftxui::color
Decorator color(Color)
Decorate using a foreground color.
Definition dom/color.cpp:110
ftxui::vbox
Element vbox(Elements)
A container displaying elements vertically one by one.
Definition vbox.cpp:96
ftxui::Color::Cyan
@ Cyan
Definition color.hpp:60
ftxui::Color::Yellow
@ Yellow
Definition color.hpp:57
ftxui::Color::Green
@ Green
Definition color.hpp:56
ftxui::Color::Red
@ Red
Definition color.hpp:55
ftxui::Color::Blue
@ Blue
Definition color.hpp:58
ftxui::Color
Color is a class that represents a color in the terminal user interface.
Definition color.hpp:22
Colored
MenuEntryOption Colored(ftxui::Color c)
Definition menu_entries.cpp:20
main
int main()
Definition menu_entries.cpp:34
ftxui
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
ftxui::HEIGHT
@ HEIGHT
Definition elements.hpp:161
ftxui::Element
std::shared_ptr< Node > Element
Definition elements.hpp:22
ftxui::hbox
Element hbox(Elements)
A container displaying elements horizontally one by one.
Definition hbox.cpp:94
ftxui::LESS_THAN
@ LESS_THAN
Definition elements.hpp:162
screen_interactive.hpp
ftxui::EntryState
arguments for transform from |ButtonOption|, |CheckboxOption|, |RadioboxOption|, |MenuEntryOption|,...
Definition component_options.hpp:22