Loading...
Searching...
No Matches
examples/component/menu_entries.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 <functional> // for function
#include <iostream> // for basic_ostream::operator<<, operator<<, endl, basic_ostream, basic_ostream<>::__ostream_type, cout, ostream
#include <memory> // for allocator, shared_ptr, __shared_ptr_access
#include <string> // for char_traits, to_string, operator+, string, basic_string
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for MenuEntry, Renderer, Vertical
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/component_options.hpp" // for MenuEntryOption
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/dom/elements.hpp" // for operator|, Element, separator, text, hbox, size, frame, color, vbox, HEIGHT, LESS_THAN, bold, border, inverted
#include "ftxui/screen/color.hpp" // for Color, Color::Blue, Color::Cyan, Color::Green, Color::Red, Color::Yellow
using namespace ftxui;
// Define a special style for some menu entry.
MenuEntryOption Colored(ftxui::Color c) {
MenuEntryOption option;
option.transform = [c](EntryState state) {
state.label = (state.active ? "> " : " ") + state.label;
Element e = text(state.label) | color(c);
if (state.focused)
e = e | inverted;
if (state.active)
e = e | bold;
return e;
};
return option;
}
int main() {
auto screen = ScreenInteractive::TerminalOutput();
int selected = 0;
auto menu = Container::Vertical(
{
MenuEntry(" 1. improve"),
MenuEntry(" 2. tolerant"),
MenuEntry(" 3. career"),
MenuEntry(" 4. cast"),
MenuEntry(" 5. question"),
Renderer([] { return separator(); }),
MenuEntry(" 6. rear", Colored(Color::Red)),
MenuEntry(" 7. drown", Colored(Color::Yellow)),
MenuEntry(" 8. nail", Colored(Color::Green)),
MenuEntry(" 9. quit", Colored(Color::Cyan)),
MenuEntry("10. decorative", Colored(Color::Blue)),
Renderer([] { return separator(); }),
MenuEntry("11. costume"),
MenuEntry("12. pick"),
MenuEntry("13. oral"),
MenuEntry("14. minister"),
MenuEntry("15. football"),
MenuEntry("16. welcome"),
MenuEntry("17. copper"),
MenuEntry("18. inhabitant"),
MenuEntry("19. fortune"),
},
&selected);
// Display together the menu with a border
auto renderer = Renderer(menu, [&] {
return vbox({
hbox(text("selected = "), text(std::to_string(selected))),
separator(),
menu->Render() | frame | size(HEIGHT, LESS_THAN, 10),
}) |
border;
});
screen.Loop(renderer);
std::cout << "Selected element = " << selected << std::endl;
}
captured_mouse.hpp
ftxui::Color
A class representing terminal colors.
Definition color.hpp:20
ftxui::Color::Cyan
@ Cyan
Definition color.hpp:58
ftxui::Color::Yellow
@ Yellow
Definition color.hpp:55
ftxui::Color::Green
@ Green
Definition color.hpp:54
ftxui::Color::Red
@ Red
Definition color.hpp:53
ftxui::Color::Blue
@ Blue
Definition color.hpp:56
ftxui::ScreenInteractive::TerminalOutput
static ScreenInteractive TerminalOutput()
Definition screen_interactive.cpp:406
color.hpp
component.hpp
component_base.hpp
component_options.hpp
elements.hpp
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
Definition animation.hpp:10
ftxui::HEIGHT
@ HEIGHT
Definition elements.hpp:153
ftxui::size
Decorator size(WidthOrHeight, Constraint, int value)
Apply a constraint on the size of an element.
Definition size.cpp:89
ftxui::Element
std::shared_ptr< Node > Element
Definition elements.hpp:22
ftxui::MenuEntry
Component MenuEntry(MenuEntryOption options)
A specific menu entry. They can be put into a Container::Vertical to form a menu.
Definition menu.cpp:614
ftxui::bold
Element bold(Element)
Use a bold font, for elements with more emphasis.
Definition bold.cpp:33
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 renderer.cpp:61
ftxui::hbox
Element hbox(Elements)
A container displaying elements horizontally one by one.
Definition hbox.cpp:94
ftxui::MenuEntryOption::transform
std::function< Element(const EntryState &state)> transform
Definition component_options.hpp:79
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 separator.cpp:134
ftxui::frame
Element frame(Element)
Allow an element to be displayed inside a 'virtual' area. It size can be larger than its container....
Definition frame.cpp:118
ftxui::LESS_THAN
@ LESS_THAN
Definition elements.hpp:154
ftxui::border
Element border(Element)
Draw a border around the element.
Definition border.cpp:227
ftxui::color
Decorator color(Color)
Decorate using a foreground color.
Definition color.cpp:110
ftxui::vbox
Element vbox(Elements)
A container displaying elements vertically one by one.
Definition vbox.cpp:96
ftxui::EntryState
arguments for |ButtonOptiontransform|, |CheckboxOptiontransform|, |Radioboxtransform|,...
Definition component_options.hpp:23
screen_interactive.hpp