FTXUI 7.0.3
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/app.hpp" // for App
10#include "ftxui/component/captured_mouse.hpp" // for ftxui
11#include "ftxui/component/component.hpp" // for MenuEntry, Renderer, Vertical
12#include "ftxui/component/component_base.hpp" // for ComponentBase
13#include "ftxui/component/component_options.hpp" // for MenuEntryOption
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 un estilo especial para alguna entrada del menú.
20MenuEntryOption 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 }
28 if (state.active) {
29 e = e | bold;
30 }
31 return e;
32 };
33 return option;
34}
35
36int main() {
37 auto screen = App::TerminalOutput();
38
39 int selected = 0;
40 auto menu = Container::Vertical(
41 {
42 MenuEntry(" 1. improve"),
43 MenuEntry(" 2. tolerant"),
44 MenuEntry(" 3. career"),
45 MenuEntry(" 4. cast"),
46 MenuEntry(" 5. question"),
47
48 Renderer([] { return separator(); }),
49
50 MenuEntry(" 6. rear", Colored(Color::Red)),
51 MenuEntry(" 7. drown", Colored(Color::Yellow)),
52 MenuEntry(" 8. nail", Colored(Color::Green)),
53 MenuEntry(" 9. quit", Colored(Color::Cyan)),
54 MenuEntry("10. decorative", Colored(Color::Blue)),
55
56 Renderer([] { return separator(); }),
57
58 MenuEntry("11. costume"),
59 MenuEntry("12. pick"),
60 MenuEntry("13. oral"),
61 MenuEntry("14. minister"),
62 MenuEntry("15. football"),
63 MenuEntry("16. welcome"),
64 MenuEntry("17. copper"),
65 MenuEntry("18. inhabitant"),
66 MenuEntry("19. fortune"),
67 },
68 &selected);
69
70 // Muestra el menú junto con un borde
71 auto renderer = Renderer(menu, [&] {
72 return vbox({
73 hbox(text("selected = "), text(std::to_string(selected))),
74 separator(),
75 menu->Render() | frame | size(HEIGHT, LESS_THAN, 10),
76 }) |
77 border;
78 });
79
80 screen.Loop(renderer);
81
82 std::cout << "Selected element = " << selected << std::endl;
83}
Component Renderer(Component child, std::function< Element()>)
Devuelve un nuevo Component, similar a |child|, pero usando |render| como el evento Component::Render...
Component MenuEntry(ConstStringRef label, MenuEntryOption options={})
Una entrada de menú específica. Se pueden colocar en un Container::Vertical para formar un menú.
Element text(std::string_view text)
Muestra un fragmento de texto Unicode codificado en UTF8.
Decorator size(WidthOrHeight direction, Constraint constraint, int value)
Aplica una restricción al tamaño de un elemento.
Element bold(Element child)
Utiliza una fuente en negrita, para elementos con más énfasis.
Definition bold.cpp:33
Element inverted(Element child)
Agrega un filtro que invertirá los colores de primer plano y de fondo. colores.
Definition inverted.cpp:34
Element separator()
Dibuja una separación vertical u horizontal entre otros dos elementos.
Element color(Color color, Element child)
Establece el color de primer plano de un elemento.
Definition dom/color.cpp:81
Element vbox(Elements children)
Un contenedor que muestra elementos verticalmente uno por uno.
Definition vbox.cpp:96
MenuEntryOption Colored(ftxui::Color c)
int main()
El espacio de nombres ftxui:: de FTXUI.
Definition animation.hpp:11
@ LESS_THAN
Definition elements.hpp:194
std::shared_ptr< Node > Element
Definition elements.hpp:25
Element hbox(Elements children)
Un contenedor que muestra elementos horizontalmente uno por uno.
Definition hbox.cpp:94