Movatterモバイル変換


[0]ホーム

URL:


FTXUI  5.0.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
FTXUI

Table of Contents

Introduction

Welcome to the FTXUI documentation!

This is a brief tutorial. You are also encouraged to self-learn by reading theexamples.

Short example

To build a single frame, you need create anftxui::Element, and display it on aftxui::Screen.

main.cpp

#include <iostream>
int main(void) {
using namespaceftxui;
// Define the document
Element document =
hbox({
text("left") | border,
text("middle") | border | flex,
text("right") | border,
});
auto screen = Screen::Create(
Dimension::Full(),// Width
Dimension::Fit(document)// Height
);
Render(screen, document);
screen.Print();
return EXIT_SUCCESS;
}
std::shared_ptr< Node > Element
Definitionelements.hpp:22

output

┌────┐┌────────────────────────────────────┐┌─────┐
│left││middle ││right│
└────┘└────────────────────────────────────┘└─────┘

Configure

Using CMake and find_package

Assuming FTXUI is available or installed on the system.

CMakeLists.txt

cmake_minimum_required (VERSION 3.11)
find_package(ftxui 5 REQUIRED)
project(ftxui-starter LANGUAGES CXX VERSION 1.0.0)
add_executable(ftxui-starter src/main.cpp)
target_link_libraries(ftxui-starter
PRIVATE ftxui::screen
PRIVATE ftxui::dom
PRIVATE ftxui::component # Not needed for this example.
)

Using CMake and FetchContent

If you want to fetch FTXUI using cmake:

CMakeLists.txt

cmake_minimum_required (VERSION 3.11)
include(FetchContent)
set(FETCHCONTENT_UPDATES_DISCONNECTED TRUE)
FetchContent_Declare(ftxui
GIT_REPOSITORY https://github.com/ArthurSonzogni/ftxui
GIT_TAG main # Important: Specify a version or a commit hash here.
)
FetchContent_MakeAvailable(ftxui)
project(ftxui-starter LANGUAGES CXX VERSION 1.0.0)
add_executable(ftxui-starter src/main.cpp)
target_link_libraries(ftxui-starter
PRIVATE ftxui::screen
PRIVATE ftxui::dom
PRIVATE ftxui::component # Not needed for this example.
)

Build

mkdir build && cd build
cmake ..
make
./main

List of modules.

The project is comprised of 3 modules:

  1. ftxui/screen defines aftxui::Screen, a grid offtxui::Pixel.
  2. ftxui/dom is the main module. It defines a hierarchical set offtxui::Element. An element draws something on theftxui::Screen. It is responsive to the size of its container.
  3. ftxui/component The module is required if your program needs to respond to user input. It defines a set offtxui::Component. These components can be utilized to navigate using the arrow keysand/or cursor. There are several builtin widgets like checkbox/inputbox/etc to interact with. You can combine them, or even define your own custom components.

screen

This is the visual element of the program. It defines aftxui::Screen, which is a grid offtxui::Pixel. A Pixel represents a Unicode character and its associated style (bold, italic, colors, etc.). The screen can be printed as a string usingftxui::Screen::ToString(). The following example highlights this process:

#include <iostream>
int main(void) {
using namespaceftxui;
auto screen = Screen::Create(Dimension::Fixed(32), Dimension::Fixed(10));
auto& pixel = screen.PixelAt(9,9);
pixel.character = U'A';
pixel.bold =true;
pixel.foreground_color = Color::Blue;
std::cout << screen.ToString();
return EXIT_SUCCESS;
}

dom

This module defines a hierarchical set offtxui::Element. An element manages the layout and can be responsive to the terminal dimension changes. Note the following example where this module is used to create a simple layout with a number of operators:

Example:

// Define the document
Element document = vbox({
text("The window") | bold | color(Color::Blue),
gauge(0.5)
text("The footer")
});
// Add a border, by calling the `ftxui::border` decorator function.
document = border(document);
// Add another border, using the pipe operator.
document = document | border.
// Add another border, using the |= operator.
document |= border

List of elements

The list of all elements are included and can be accessed by including the corresponding header file:

// 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.
#ifndef FTXUI_DOM_ELEMENTS_HPP
#define FTXUI_DOM_ELEMENTS_HPP
#include <functional>
#include <memory>
namespaceftxui {
classNode;
usingElement = std::shared_ptr<Node>;
usingElements = std::vector<Element>;
usingDecorator = std::function<Element(Element)>;
usingGraphFunction = std::function<std::vector<int>(int,int)>;
};
// Pipe elements into decorator togethers.
// For instance the next lines are equivalents:
// -> text("ftxui") | bold | underlined
// -> underlined(bold(text("FTXUI")))
// --- Widget ---
Elementtext(std::stringtext);
Elementvtext(std::stringtext);
float right,
float down,
DecoratorborderWith(const Pixel&);
Elementparagraph(const std::string&text);
ElementparagraphAlignLeft(const std::string&text);
ElementparagraphAlignRight(const std::string&text);
ElementparagraphAlignCenter(const std::string&text);
Elementcanvas(int width,int height, std::function<void(Canvas&)>);
Elementcanvas(std::function<void(Canvas&)>);
// -- Decorator ---
Decoratorcolor(const LinearGradient&);
Decoratorbgcolor(const LinearGradient&);
Elementcolor(const LinearGradient&,Element);
Elementbgcolor(const LinearGradient&,Element);
DecoratorselectionColor(Color foreground);
DecoratorselectionStyle(std::function<void(Pixel&)>style);
// --- Layout is
// Horizontal, Vertical or stacked set of elements.
Elementflexbox(Elements, FlexboxConfig config = FlexboxConfig());
Elementgridbox(std::vector<Elements> lines);
Elementhflow(Elements);// Helper: default flexbox with row direction.
Elementvflow(Elements);// Helper: default flexbox with column direction.
// -- Flexibility ---
// Define how to share the remaining space when not all of it is used inside a
// container.
Elementflex(Element);// Expand/Minimize if possible/needed.
Elementflex_grow(Element);// Expand element if possible.
Elementflex_shrink(Element);// Minimize element if needed.
Elementxflex(Element);// Expand/Minimize if possible/needed on X axis.
Elementxflex_grow(Element);// Expand element if possible on X axis.
Elementxflex_shrink(Element);// Minimize element if needed on X axis.
Elementyflex(Element);// Expand/Minimize if possible/needed on Y axis.
Elementyflex_grow(Element);// Expand element if possible on Y axis.
Elementyflex_shrink(Element);// Minimize element if needed on Y axis.
Elementnotflex(Element);// Reset the flex attribute.
Elementfiller();// A blank expandable element.
// -- Size override;
// --- Frame ---
// A frame is a scrollable area. The internal area is potentially larger than
// the external one. The internal area is scrolled in order to make visible the
// focused element.
Elementselect(Element e);// Deprecated - Alias for focus.
// --- Cursor ---
// Those are similar to `focus`, but also change the shape of the cursor.
// --- Misc ---
Decoratorreflect(Box& box);
// Before drawing the |element| clear the pixel below. This is useful in
// combinaison with dbox.
// --- Util --------------------------------------------------------------------
namespaceDimension {
DimensionsFit(Element&,boolextend_beyond_screen =false);
}// namespace Dimension
}// namespace ftxui
// Make container able to take any number of children as input.
#include "ftxui/dom/take_any_args.hpp"
// Include old definitions using wstring.
#endif// FTXUI_DOM_ELEMENTS_HPP
Dimensions Fit(Element &, bool extend_beyond_screen=false)
Definitionutil.cpp:93
Decorator bgcolor(Color)
Decorate using a background color.
Definitioncolor.cpp:124
Element window(Element title, Element content, BorderStyle border=ROUNDED)
Draw window with a title and a border around the element.
Definitionborder.cpp:507
Element borderDouble(Element)
Draw a double border around the element.
Definitionborder.cpp:405
Element focusCursorBarBlinking(Element)
Same as focus, but set the cursor shape to be a blinking bar.
Definitionframe.cpp:189
Element xflex(Element)
Expand/Minimize if possible/needed on the X axis.
Definitionflex.cpp:129
Element gaugeDirection(float progress, Direction direction)
Draw a high definition progress bar progressing in specified direction.
Definitiongauge.cpp:169
Decorator focusPositionRelative(float x, float y)
Used inside a frame, this force the view to be scrolled toward a a given position....
Definitionfocus.cpp:31
Element separatorStyled(BorderStyle)
Draw a vertical or horizontal separation in between two other elements.
Element xflex_grow(Element)
Expand if possible on the X axis.
Definitionflex.cpp:147
std::function< Element(Element)> Decorator
Definitionelements.hpp:24
Element underlinedDouble(Element)
Apply a underlinedDouble to text.
Element clear_under(Element element)
Before drawing |child|, clear the pixels below. This is useful in.
Element borderDashed(Element)
Draw a dashed border around the element.
Definitionborder.cpp:300
Element separatorEmpty()
Draw a vertical or horizontal separation in between two other elements, using the EMPTY style.
Element vscroll_indicator(Element)
Display a vertical scrollbar to the right. colors.
Element separatorVSelector(float up, float down, Color unselected_color, Color selected_color)
Draw an vertical bar, with the area in between up/downcolored differently.
Element flexbox(Elements, FlexboxConfig config=FlexboxConfig())
Element nothing(Element element)
A decoration doing absolutely nothing.
Definitionutil.cpp:28
Decorator size(WidthOrHeight, Constraint, int value)
Apply a constraint on the size of an element.
Definitionsize.cpp:89
Element flex(Element)
Make a child element to expand proportionally to the space left in a container.
Definitionflex.cpp:123
Element paragraphAlignRight(const std::string &text)
Return an element drawing the paragraph on multiple lines, aligned on the right.
std::shared_ptr< T > Make(Args &&... args)
Element xframe(Element)
Same as frame, but only on the x-axis.
Definitionframe.cpp:126
Element gaugeRight(float progress)
Draw a high definition progress bar progressing from left to right.
Definitiongauge.cpp:191
Element focusCursorUnderlineBlinking(Element)
Same as focus, but set the cursor shape to be a blinking underline.
Definitionframe.cpp:217
Element bold(Element)
Use a bold font, for elements with more emphasis.
Definitionbold.cpp:33
Element separatorLight()
Draw a vertical or horizontal separation in between two other elements, using the LIGHT style.
Element spinner(int charset_index, size_t image_index)
Useful to represent the effect of time and/or events. This display an ASCII art "video".
Definitionspinner.cpp:282
Element borderRounded(Element)
Draw a rounded border around the element.
Definitionborder.cpp:440
Element emptyElement()
Definitionutil.cpp:140
Decorator selectionStyle(std::function< void(Pixel &)> style)
Set the style of an element when selected.
Element yflex(Element)
Expand/Minimize if possible/needed on the Y axis.
Definitionflex.cpp:135
Element flex_shrink(Element)
Minimize if needed.
Definitionflex.cpp:159
Element hflow(Elements)
Element focusCursorBar(Element)
Same as focus, but set the cursor shape to be a still block.
Definitionframe.cpp:175
Element separatorHSelector(float left, float right, Color unselected_color, Color selected_color)
Draw an horizontal bar, with the area in between left/right colored differently.
Element focusCursorBlock(Element)
Same as focus, but set the cursor shape to be a still block.
Definitionframe.cpp:147
Element hbox(Elements)
A container displaying elements horizontally one by one.
Definitionhbox.cpp:94
Element canvas(ConstRef< Canvas >)
Produce an element from a Canvas, or a reference to a Canvas.
Definitioncanvas.cpp:891
Element underlined(Element)
Make the underlined element to be underlined.
Element center(Element)
Center an element horizontally and vertically.
Decorator selectionForegroundColor(Color foreground)
Set the foreground color of an element when selected. Note that the style is applied on top of the ex...
Element focusCursorUnderline(Element)
Same as focus, but set the cursor shape to be a still underline.
Definitionframe.cpp:203
Component operator|(Component component, ComponentDecorator decorator)
Definitionutil.cpp:12
Element borderHeavy(Element)
Draw a heavy border around the element.
Definitionborder.cpp:370
Element inverted(Element)
Add a filter that will invert the foreground and the background colors.
Definitioninverted.cpp:34
Element paragraphAlignCenter(const std::string &text)
Return an element drawing the paragraph on multiple lines, aligned on the center.
Decorator selectionBackgroundColor(Color foreground)
Set the background color of an element when selected. Note that the style is applied on top of the ex...
Element gaugeUp(float progress)
Draw a high definition progress bar progressing from bottom to top.
Definitiongauge.cpp:242
Element text(std::wstring text)
Display a piece of unicode text.
Definitiontext.cpp:160
Element align_right(Element)
Align an element on the right side.
Decorator focusPosition(int x, int y)
Used inside a frame, this force the view to be scrolled toward a a given position....
Definitionfocus.cpp:69
std::vector< Element > Elements
Definitionelements.hpp:23
Element yframe(Element)
Same as frame, but only on the y-axis.
Definitionframe.cpp:134
Element yflex_grow(Element)
Expand if possible on the Y axis.
Definitionflex.cpp:153
Element hscroll_indicator(Element)
Display an horizontal scrollbar to the bottom. colors.
Element flex_grow(Element)
Expand if possible.
Definitionflex.cpp:141
Element separatorDashed()
Draw a vertical or horizontal separation in between two other elements, using the DASHED style.
Element notflex(Element)
Make the element not flexible.
Definitionflex.cpp:177
Element strikethrough(Element)
Apply a strikethrough to text.
Element italic(Element)
Apply a underlinedDouble to text.
Definitionitalic.cpp:17
Element dbox(Elements)
Stack several element on top of each other.
Definitiondbox.cpp:108
Decorator selectionColor(Color foreground)
Set the color of an element when selected.
Element xflex_shrink(Element)
Minimize if needed on the X axis.
Definitionflex.cpp:165
Element gaugeLeft(float progress)
Draw a high definition progress bar progressing from right to left.
Definitiongauge.cpp:213
Element separatorCharacter(std::string)
Draw a vertical or horizontal separation in between two other elements.
Element vflow(Elements)
Element select(Element e)
Set the child to be the one focused among its siblings.
Definitionframe.cpp:108
Element vtext(std::wstring text)
Display a piece unicode text vertically.
Definitiontext.cpp:220
Element borderLight(Element)
Draw a light border around the element.
Definitionborder.cpp:335
Element focus(Element)
Set the child to be the one focused among its siblings.
Definitionframe.cpp:101
Element paragraphAlignLeft(const std::string &text)
Return an element drawing the paragraph on multiple lines, aligned on the left.
Element selectionStyleReset(Element)
Reset the selection style of an element.
Decorator borderWith(const Pixel &)
Same as border but with a constant Pixel around the element.
Definitionborder.cpp:234
Decorator reflect(Box &box)
Definitionreflect.cpp:43
std::function< std::vector< int >(int, int)> GraphFunction
Definitionelements.hpp:25
Decorator borderStyled(BorderStyle)
Same as border but with different styles.
Definitionborder.cpp:243
Element gridbox(std::vector< Elements > lines)
A container displaying a grid of elements.
Definitiongridbox.cpp:173
Element separator()
Draw a vertical or horizontal separation in between two other elements.
Element filler()
An element that will take expand proportionally to the space left in a container.
Definitionflex.cpp:98
Elements paragraph(std::wstring text)
Element dim(Element)
Use a light font, for elements with less emphasis.
Definitiondim.cpp:33
Element automerge(Element child)
Enable character to be automatically merged with others nearby.
Element frame(Element)
Allow an element to be displayed inside a 'virtual' area. It size can be larger than its container....
Definitionframe.cpp:118
Decorator hyperlink(std::string link)
Decorate using an hyperlink. The link will be opened when the user click on it. This is supported onl...
Element blink(Element)
The text drawn alternates in between visible and hidden.
Definitionblink.cpp:33
Element vcenter(Element)
Center an element vertically.
Element separatorDouble()
Draw a vertical or horizontal separation in between two other elements, using the DOUBLE style.
Element focusCursorBlockBlinking(Element)
Same as focus, but set the cursor shape to be a blinking block.
Definitionframe.cpp:161
Component & operator|=(Component &component, ComponentDecorator decorator)
Definitionutil.cpp:22
@ GREATER_THAN
Element gauge(float progress)
Draw a high definition progress bar.
Definitiongauge.cpp:293
Element paragraphAlignJustify(const std::string &text)
Return an element drawing the paragraph on multiple lines, aligned using a justified alignment....
Element graph(GraphFunction)
Draw a graph using a GraphFunction.
Definitiongraph.cpp:71
Element border(Element)
Draw a border around the element.
Definitionborder.cpp:227
Element separatorHeavy()
Draw a vertical or horizontal separation in between two other elements, using the HEAVY style.
Element borderEmpty(Element)
Draw an empty border around the element.
Definitionborder.cpp:475
Decorator color(Color)
Decorate using a foreground color.
Definitioncolor.cpp:110
Element yflex_shrink(Element)
Minimize if needed on the Y axis.
Definitionflex.cpp:171
Element hcenter(Element)
Center an element horizontally.
Element vbox(Elements)
A container displaying elements vertically one by one.
Definitionvbox.cpp:96
Element gaugeDown(float progress)
Draw a high definition progress bar progressing from top to bottom.
Definitiongauge.cpp:271

text

The most simple widget. It displays a text.

text("I am a piece of text");
I am a piece of text.

vtext

Identical toftxui::text, but displayed vertically.

Code:

vtext("HELLO");

Terminal output:

H
E
L
L
O

paragraph 

Similar toftxui::text, but the individual word are wrapped along multiple lines, depending on the width of its container.

Sample Code:

paragraph("A very long text")

ezgif com-gif-maker (4)

For a more detailed example refer todetailed example. Paragraph also includes a number of other variants as shown below:

Element paragraph(std::string text);
Element paragraphAlignLeft(std::string text);
Element paragraphAlignRight(std::string text);
Element paragraphAlignCenter(std::string text);
Element paragraphAlignJustify(std::string text);

border

Adds a border around an element.

Code:

border(text("The element"))

Terminal output:

┌───────────┐
│The element│
└───────────┘

You can achieve the same behavior by using the pipe operator.

Code:

text("The element") | border

Border also comes in a variety of styles as shown below:

Element border(Element);
Element borderLight(Element);
Element borderHeavy(Element);
Element borderDouble(Element);
Element borderRounded(Element);
Element borderEmpty(Element);
Decorator borderStyled(BorderStyle);
Decorator borderWith(Pixel);
A Unicode character and its associated style.
Definitionpixel.hpp:15

window

Aftxui::window is aftxui::border, but with an additional header. To add a window around an element, wrap it and specify a string as the header. Code:

window("The window", text("The element"))

Terminal output:

┌The window─┐
│The element│
└───────────┘

separator

Displays a vertical/horizontal line to visually split the content of a container in two.

Code:

border(
hbox({
text("Left"),
separator(),
text("Right")
})
)

Terminal output:

┌────┬─────┐
│left│right│
└────┴─────┘

Separators come in a variety of flavors as shown below:

Element separator(void);
Element separatorLight();
Element separatorHeavy();
Element separatorDouble();
Element separatorEmpty();
Element separatorStyled(BorderStyle);
Element separator(Pixel);
Element separatorCharacter(std::string);
Element separatorHSelector(float left,
float right,
Color background,
Color foreground);
Element separatorVSelector(float up,
float down,
Color background,
Color foreground);
A class representing terminal colors.
Definitioncolor.hpp:20

gauge

It constitutes a gauge. It can be used to represent a progress bar.

Code:

border(gauge(0.5))

Teminal output:

┌────────────────────────────────────────────────────────────────────────────┐
│██████████████████████████████████████ │
└────────────────────────────────────────────────────────────────────────────┘

Gauges can be displayed in many orientations as shown below:

Element gauge(float ratio);
Element gaugeLeft(float ratio);
Element gaugeRight(float ratio);
Element gaugeUp(float ratio);
Element gaugeDown(float ratio);
Element gaugeDirection(float ratio, GaugeDirection);

graph

See:

Colors

Most terminal consoles can display colored text and colored backgrounds. FTXUI supports every color palette:

Decorator bgcolor(Color);

Colorgallery:image

Palette16 

On most terminals the following colors are supported:

  • Default
  • Black
  • GrayDark
  • GrayLight
  • White
  • Blue
  • BlueLight
  • Cyan
  • CyanLight
  • Green
  • GreenLight
  • Magenta
  • MagentaLight
  • Red
  • RedLight
  • Yellow
  • YellowLight

Example use of the above colors using the pipe operator:

text("Blue foreground") | color(Color::Blue);
text("Blue background") | bgcolor(Color::Blue);
text("Black on white") | color(Color::Black) | bgcolor(Color::White);

Palette256 

On terminal supporting 256 colors.

text("HotPink") | color(Color::HotPink);

TrueColor

On terminal supporting trueColor, you can directly use the 24bit RGB color space:

Use the constructors below to specify theRGB orHSV values for your color:

There are two constructors:

ftxui::Color::RGB(uint8_t red, uint8_t green, uint8_t blue);
ftxui::Color::HSV(uint8_t hue, uint8_t saturation, uint8_t value);
static Color HSV(uint8_t hue, uint8_t saturation, uint8_t value)
Build a Color from its HSV representation. https://en.wikipedia.org/wiki/HSL_and_HSV.
Definitioncolor.cpp:207
static Color RGB(uint8_t red, uint8_t green, uint8_t blue)
Build a Color from its RGB representation. https://en.wikipedia.org/wiki/RGB_color_model.
Definitioncolor.cpp:153

LinearGradient

FTXUI supports linear gradient. Either on the foreground or the background.

Decorator bgcolor(constLinearGradient&);
A class representing the settings for linear-gradient color effect.

Aftxui::LinearGradient is defined by an angle in degree, and a list of color stops.

auto gradient =LinearGradient()
.Angle(45)
.AddStop(0.0, Color::Red)
.AddStop(0.5, Color::Green)
.AddStop(1.0, Color::Blue);
LinearGradient & Angle(float angle)
Set the angle of the gradient.

You can also use simplified constructors:

LinearGradient(Color::Red, Color::Blue);
LinearGradient(45, Color::Red, Color::Blue);

Seedemo.

Style

In addition to colored text and colored backgrounds. Many terminals support text effects such as:bold,italic,dim,underlined,inverted,blink.

Element italic(Element);
Element inverted(Element);
Element underlined(Element);
Element underlinedDouble(Element);
Element strikethrough(Element);
Decorator bgcolor(Color);

Example

image

To use these effects, simply wrap your elements with your desired effect:

underlined(bold(text("This text is bold and underlined")))

Alternatively, use the pipe operator to chain it on your element:

text("This text is bold") | bold | underlined

Layout

Enables elements to be arranged in the following ways:

Example usingftxui::hbox,ftxui::vbox andftxui::filler.

image

Example usingftxui::gridbox:

image

Example using flexbox:

image

Checkout thisexample and the associateddemo.

Element can also become flexible using the theftxui::flex decorator.

Code:

hbox({
text("left") | border ,
text("middle") | border | flex,
text("right") | border,
});

Terminal output:

┌────┐┌─────────────────────────────────────────────────────┐┌─────┐
│left││middle ││right│
└────┘└─────────────────────────────────────────────────────┘└─────┘

Code:

hbox({
text("left") | border ,
text("middle") | border | flex,
text("right") | border | flex,
});

Terminal output:

┌────┐┌───────────────────────────────┐┌───────────────────────────────┐
│left││middle ││right │
└────┘└───────────────────────────────┘└───────────────────────────────┘

Table

Enables easy formatting of data into a neat table like visual form.

Code example:

image

Canvas

See the API<ftxui/dom/canvas.hpp>

auto c =Canvas(100, 100);
c.DrawPointLine(10, 10, 80, 10, Color::Red);
auto element = canvas(c);

Drawing can be performed on aftxui::Canvas, using braille, block, or simple characters:

Simpleexample:

image

Complexexample:

ezgif com-gif-maker (3)

component

Theftxui::component module defines the logic that produces interactive components that respond to user events (keyboard, mouse, etc.).

Aftxui::ScreenInteractive defines a main loop that renders a component.

Aftxui::Component is a shared pointer to aftxui::ComponentBase. The latter defines:

ftxui::Element are used to render a single frame.

ftxui::Component are used to render dynamic user interface, producing multiple frame, and updating its state on events.

Gallery of multiple components. (demo)

image

All predefined components are available in"ftxui/dom/component.hpp"

// Copyright 2021 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
#ifndef FTXUI_COMPONENT_HPP
#define FTXUI_COMPONENT_HPP
#include <functional>// for function
#include <memory>// for make_shared, shared_ptr
#include <utility>// for forward
#include "ftxui/component/component_base.hpp"// for Component, Components
#include "ftxui/component/component_options.hpp"// for ButtonOption, CheckboxOption, MenuOption
#include "ftxui/dom/elements.hpp"// for Element
#include "ftxui/util/ref.hpp"// for ConstRef, Ref, ConstStringRef, ConstStringListRef, StringRef
namespaceftxui {
structButtonOption;
structCheckboxOption;
structEvent;
structInputOption;
structMenuOption;
structRadioboxOption;
structMenuEntryOption;
template <classT,class...Args>
std::shared_ptr<T>Make(Args&&...args) {
return std::make_shared<T>(std::forward<Args>(args)...);
}
// Pipe operator to decorate components.
usingComponentDecorator = std::function<Component(Component)>;
usingElementDecorator = std::function<Element(Element)>;
namespaceContainer {
}// namespace Container
ComponentButton(ButtonOptionoptions);
ComponentButton(ConstStringRef label,
std::function<void()> on_click,
ButtonOptionoptions =ButtonOption::Simple());
ComponentCheckbox(CheckboxOptionoptions);
ComponentCheckbox(ConstStringRef label,
bool* checked,
CheckboxOptionoptions =CheckboxOption::Simple());
ComponentInput(InputOptionoptions = {});
ComponentInput(StringRef content, InputOptionoptions = {});
ComponentInput(StringRef content,
StringRef placeholder,
InputOptionoptions = {});
ComponentMenu(ConstStringListRef entries,
int* selected_,
ComponentMenuEntry(MenuEntryOptionoptions);
ComponentMenuEntry(ConstStringRef label, MenuEntryOptionoptions = {});
ComponentRadiobox(RadioboxOptionoptions);
ComponentRadiobox(ConstStringListRef entries,
int* selected_,
RadioboxOptionoptions = {});
ComponentDropdown(ConstStringListRef entries,int* selected);
ComponentDropdown(DropdownOptionoptions);
ComponentToggle(ConstStringListRef entries,int* selected);
// General slider constructor:
template <typename T>
// Shorthand without the `SliderOption` constructor:
ComponentSlider(ConstStringRef label,
Ref<int> value,
ConstRef<int> min = 0,
ConstRef<int> max = 100,
ConstRef<int> increment = 5);
ComponentSlider(ConstStringRef label,
Ref<float> value,
ConstRef<float> min = 0.f,
ConstRef<float> max = 100.f,
ConstRef<float> increment = 5.f);
ComponentSlider(ConstStringRef label,
Ref<long> value,
ConstRef<long> min = 0L,
ConstRef<long> max = 100L,
ConstRef<long> increment = 5L);
ComponentResizableSplit(ResizableSplitOptionoptions);
ComponentRenderer(std::function<Element()>);
ComponentRenderer(std::function<Element(bool/* focused */)>);
ComponentCatchEvent(Componentchild, std::function<bool(Event)>);
ComponentDecoratorCatchEvent(std::function<bool(Event)>on_event);
ComponentMaybe(Component, std::function<bool()>);
ComponentDecoratorMaybe(std::function<bool()>);
ComponentCollapsible(ConstStringRef label,
Ref<bool>show =false);
std::function<void()> on_enter,
std::function<void()>on_leave);
std::function<void(bool)> on_change);
ComponentDecoratorHoverable(std::function<void()> on_enter,
std::function<void()>on_leave);
ComponentDecoratorHoverable(std::function<void(bool)> on_change);
ComponentWindow(WindowOptionsoption);
}// namespace ftxui
#endif/* end of include guard: FTXUI_COMPONENT_HPP */
Component Horizontal(Components children)
A list of components, drawn one by one horizontally and navigated horizontally using left/right arrow...
Component Vertical(Components children)
A list of components, drawn one by one vertically and navigated vertically using up/down arrow key or...
Component Stacked(Components children)
A list of components to be stacked on top of each other. Events are propagated to the first component...
Component Tab(Components children, int *selector)
A list of components, where only one is drawn and interacted with at a time. The |selector| gives the...
Component Maybe(Component, const bool *show)
Decorate a component |child|. It is shown only when |show| is true.
Definitionmaybe.cpp:74
Component ResizableSplitTop(Component main, Component back, int *main_size)
An vertical split in between two components, configurable using the mouse.
Component Menu(MenuOption options)
A list of text. The focused element is selected.
Definitionmenu.cpp:512
Component MenuEntry(MenuEntryOption options)
A specific menu entry. They can be put into a Container::Vertical to form a menu.
Definitionmenu.cpp:614
std::function< Element(Element)> ElementDecorator
std::shared_ptr< ComponentBase > Component
Component Toggle(ConstStringListRef entries, int *selected)
An horizontal list of elements. The user can navigate through them.
Definitionmenu.cpp:554
std::vector< Component > Components
Component Radiobox(RadioboxOption options)
A list of element, where only one can be selected.
Component Button(ButtonOption options)
Draw a button. Execute a function when clicked.
Definitionbutton.cpp:176
Component Modal(Component main, Component modal, const bool *show_modal)
Definitionmodal.cpp:18
Component Renderer(Component child, std::function< Element()>)
Return a new Component, similar to |child|, but using |render| as the Component::Render() event.
Definitionrenderer.cpp:61
Component Hoverable(Component component, bool *hover)
Wrap a component. Gives the ability to know if it is hovered by the mouse.
Component ResizableSplit(ResizableSplitOption options)
A split in between two components.
Component Window(WindowOptions option)
A draggeable / resizeable window. To use multiple of them, they must be stacked using Container::Stac...
Definitionwindow.cpp:312
Component Input(InputOption options={})
An input box for editing text.
Definitioninput.cpp:571
Component ResizableSplitRight(Component main, Component back, int *main_size)
An horizontal split in between two components, configurable using the mouse.
Component Dropdown(ConstStringListRef entries, int *selected)
A dropdown menu.
Definitiondropdown.cpp:22
Component Slider(SliderOption< T > options)
A slider in any direction.
Definitionslider.cpp:346
Component ResizableSplitBottom(Component main, Component back, int *main_size)
An vertical split in between two components, configurable using the mouse.
Component Checkbox(CheckboxOption options)
Draw checkable element.
Component ResizableSplitLeft(Component main, Component back, int *main_size)
An horizontal split in between two components, configurable using the mouse.
std::function< Component(Component)> ComponentDecorator
Component Collapsible(ConstStringRef label, Component child, Ref< bool > show=false)
Component CatchEvent(Component child, std::function< bool(Event)>)
static ButtonOption Simple()
Create a ButtonOption, inverted when focused.
static CheckboxOption Simple()
Option for standard Checkbox.
static MenuOption Vertical()
Standard options for a vertical menu. This can be useful to implement a list of selectable items.

Input

Example:

image

Produced by:ftxui::Input() from "ftxui/component/component.hpp"

Filtered input

On can filter out the characters received by the input component, usingftxui::CatchEvent.

std::string phone_number;
Component input = Input(&phone_number,"phone number");
// Filter out non-digit characters.
input |= CatchEvent([&](Event event) {
returnevent.is_character() && !std::isdigit(event.character()[0]);
});
// Filter out characters past the 10th one.
input |= CatchEvent([&](Event event) {
returnevent.is_character() && phone_number.size() >= 10;
});
Represent an event. It can be key press event, a terminal resize, or more ...
Definitionevent.hpp:27
std::string character() const
Definitionevent.hpp:105

Menu

Defines a menu object. It contains a list of entries, one of them is selected.

Example:

image

Produced by:ftxui::Menu() from "ftxui/component/component.hpp"

Toggle 

A special kind of menu. The entries are displayed horizontally.

Example:

image

Produced by:ftxui::Toggle() from "ftxui/component/component.hpp"

CheckBox

This component defines a checkbox. It is a single entry that can be turned on/off.

Example:

image

Produced by:ftxui::Checkbox() from "ftxui/component/component.hpp"

RadioBox

A radiobutton component. This is a list of entries, where one can be turned on.

Example:

image

Produced by:ftxui::Radiobox() from "ftxui/component/component.hpp"

Dropdown

A drop down menu is a component that when checked display a list of element for the user to select one.

Example:

youtube-video-gif (3)

Produced by:ftxui::Dropdown() from "ftxui/component/component.hpp"

Slider

Represents a slider object that consists of a range with binned intermediate intervals. It can be created byftxui::Slider().

Example:

image

Produced by:ftxui::Slider() from "ftxui/component/component.hpp"

Renderer

Produced by:ftxui::Renderer() fromftxui/component/component.hpp. This component decorate another one by using a different function to render an interface.

Example:

auto inner = [...]
auto renderer = Renderer(inner, [&] {
return inner->Render() | border
});

ftxui::Renderer also supports the component decorator pattern:

auto component = [...]
component = component
| Renderer([](Element e) {return e | border))
| Renderer(bold)

As a short hand, you can also compose a component with an element decorator:

auto component = [...]
component = component | border | bold;

CatchEvent

Produced by:ftxui::CatchEvent() fromftxui/component/component.hpp. This component decorate others, catching events before the underlying component.

Examples:

auto screen = ScreenInteractive::TerminalOutput();
auto renderer = Renderer([] {
return text("My interface");
});
auto component = CatchEvent(renderer, [&](Event event) {
if (event == Event::Character('q')) {
screen.ExitLoopClosure()();
returntrue;
}
returnfalse;
});
screen.Loop(component);

Theftxui::CatchEvent can also be used as a decorator:

component = component
| CatchEvent(handler_1)
| CatchEvent(handler_2)
| CatchEvent(handler_3)
;

Collapsible

Useful for visual elements whose visibility can be toggle on/off by the user. Essentially, this the combination of theftxui::Checkbox() andftxui::Maybe() components.

auto collabsible = Collapsible("Show more", inner_element);

Maybe

Produced by:ftxui::Maybe() fromftxui/component/component.hpp. This component can be utilized to show/hide any other component via a boolean or a predicate.

Example with a boolean:

bool show =true;
auto component = Renderer([]{return"Hello World!"; });
auto maybe_component = Maybe(component, &show)

Example with a predicate:

auto component = Renderer([]{return"Hello World!"; });
auto maybe_component = Maybe(component, [&] {return time > 10; })

As usual,ftxui::Maybe can also be used as a decorator:

component = component
| Maybe(&a_boolean)
| Maybe([&] {return time > 10; })
;

Container

Horizontal

Produced by:ftxui::Container::Horizontal() from "ftxui/component/component.hpp". It displays a list of components horizontally and handle keyboard/mouse navigation.

Vertical

Produced by:ftxui::Container::Vertical() from "ftxui/component/component.hpp". It displays a list of components vertically and handles keyboard/mouse navigation.

Tab

Produced by:ftxui::Container::Tab() from "ftxui/component/component.hpp". It take a list of component and display only one of them. This is useful for implementing a tab bar.

Vertical:

ezgif com-gif-maker (1)

Horizontal:

ezgif com-gif-maker (2)

ResizableSplit

It defines a horizontal or vertical separation between two children components. The position of the split is variable and controllable using the mouse. There are four possible splits:

Example:

ezgif com-gif-maker

Force a frame redraw.

Typically,ftxui::ScreenInteractive::Loop() is responsible for drawing a new frame whenever a new group of events (e.g keyboard, mouse, window resize, etc.) has been processed. However, you might want to react to arbitrary events that are unknown to FTXUI. To accomplish this, you must post events usingftxui::ScreenInteractive::PostEvent (this is thread safe) via a thread. You will have to post the eventftxui::Event::Custom.

Example:

screen->PostEvent(Event::Custom);

[8]ページ先頭

©2009-2025 Movatter.jp