Introduction
Welcome to the FTXUI documentation. Here, you will find the detail of every functions and classes.
Short example
main.cpp
#include <iostream>
int main(void) {
hbox({
text("left") | border,
text("middle") | border | flex,
text("right") | border,
});
auto screen = Screen::Create(
Dimension::Full(),
Dimension::Fit(document)
);
Render(screen, document);
screen.Print();
return EXIT_SUCCESS;
}
std::shared_ptr< Node > Element
output
┌────┐┌─────────────────────────────────────────────────────────────────┐┌─────┐
│left││middle ││right│
└────┘└─────────────────────────────────────────────────────────────────┘└─────┘
Build
Using CMake
CMakeLists.txt
cmake_minimum_required (VERSION 3.11)
# --- Fetch FTXUI --------------------------------------------------------------
include(FetchContent)
set(FETCHCONTENT_UPDATES_DISCONNECTED TRUE)
FetchContent_Declare(ftxui
GIT_REPOSITORY https://github.com/ArthurSonzogni/ftxui
# Specify a GIT_TAG here.
)
FetchContent_GetProperties(ftxui)
if(NOT ftxui_POPULATED)
FetchContent_Populate(ftxui)
add_subdirectory(${ftxui_SOURCE_DIR} ${ftxui_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()
# ------------------------------------------------------------------------------
project(ftxui-starter
LANGUAGES CXX
VERSION 1.0.0
)
add_executable(ftxui-starter src/main.cpp)
target_include_directories(ftxui-starter PRIVATE src)
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 split into 3 modules:
- ftxui/screen defines a
ftxui::Screen, this is a grid of ftxui::Pixel.
- ftxui/dom is the main module. It defines a hierarchical set of
ftxui::Element. An element draws something on the ftxui::Screen. It is responsive to the size of its container.
- ftxui/component The part is only needed if you need to respond to the User input. It defines a set of
ftxui::Component. The use can navigates using the arrow keys and interact with widgets like checkbox/inputbox/... You can make you own components.
screen
It defines a ftxui::Screen. This is a grid of ftxui::Pixel. A Pixel represent a Unicode character and its associated style (bold, colors, etc...). The screen can be printed as a string using ftxui::Screen::ToString().
#include <iostream>
int main(void) {
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 of Element. An element manages layout and can be responsive to the terminal dimensions.
Example:
text("The window") | bold | color(Color::Blue),
gauge(0.5)
text("The footer")
});
document = border(document);
List of elements
You only need one header: ftxui/dom/elements.hpp
#ifndef FTXUI_DOM_ELEMENTS_HPP
#define FTXUI_DOM_ELEMENTS_HPP
#include <functional>
#include <memory>
class Node;
using Element = std::shared_ptr<Node>;
namespace Dimension {
}
}
#include "ftxui/dom/take_any_args.hpp"
#endif
Dimensions Fit(Element &)
Decorator bgcolor(Color)
Decorate using a background color.
Element borderDouble(Element)
Draw a double border around the element.
Element xflex(Element)
Expand/Minimize if possible/needed on the X axis.
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.
std::function< Element(Element)> Decorator
Element separatorEmpty()
Draw a vertical or horizontal separation in between two other elements, using the EMPTY style.
Element vscroll_indicator(Element)
Add a filter that will invert the foreground and the background colors.
Element nothing(Element element)
A decoration doing absolutely nothing.
Element clear_under(Element element)
Before drawing |child|, clear the pixels below. This is useful in.
Element flex(Element)
Make a child element to expand proportionnally to the space left in a container.
Element hflow(Elements)
A container displaying elements horizontally one by one.
Decorator borderWith(Pixel)
Same as border but with a constant Pixel around the element.
Element bold(Element)
Use a bold font, for elements with more emphasis.
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".
Elements paragraph(std::wstring text)
Return a vector of ftxui::text for every word of the string. This is useful combined with ftxui::hflo...
Element borderRounded(Element)
Draw a rounded border around the element.
Element yflex(Element)
Expand/Minimize if possible/needed on the Y axis.
Element window(Element title, Element content)
Draw window with a title and a border around the element.
Element flex_shrink(Element)
Minimize if needed.
Element hbox(Elements)
A container displaying elements horizontally one by one.
Element underlined(Element)
Make the underlined element to be underlined.
Element center(Element)
Center an element horizontally and vertically.
std::vector< Element > Elements
Element borderHeavy(Element)
Draw a heavy border around the element.
Element inverted(Element)
Add a filter that will invert the foreground and the background colors.
Element text(std::wstring text)
Display a piece of unicode text.
Element align_right(Element)
Align an element on the right side.
Element operator|(Element, Decorator)
From an element, apply a decorator.
Element yflex_grow(Element)
Expand if possible on the Y axis.
Element flex_grow(Element)
Expand if possible.
Element notflex(Element)
Make the element not flexible.
Element dbox(Elements)
Stack several element on top of each other.
Element xflex_shrink(Element)
Minimize if needed on the X axis.
Element separatorCharacter(std::string)
Draw a vertical or horizontal separation in between two other elements.
Element vtext(std::wstring text)
Display a piece unicode text vertically.
Element borderLight(Element)
Draw a light border around the element.
Decorator reflect(Box &box)
std::function< std::vector< int >(int, int)> GraphFunction
Decorator borderStyled(BorderStyle)
Same as border but with different styles.
Element gridbox(std::vector< Elements > lines)
A container displaying a grid of elements.
Element separator(void)
Draw a vertical or horizontal separation in between two other elements.
Element filler()
An element that will take expand proportionnally to the space left in a container.
Element dim(Element)
Use a light font, for elements with less emphasis.
Element frame(Element)
Allow an element to be displayed inside a 'virtual' area. It size can be larger than its container....
Element blink(Element)
The text drawn alternates in between visible and hidden.
Element vcenter(Element)
Center an element vertically.
Decorator size(Direction, Constraint, int value)
Apply a constraint on the size of an element.
Element separatorDouble()
Draw a vertical or horizontal separation in between two other elements, using the DOUBLE style.
Element gauge(float ratio)
Draw a high definition progress bar.
Element graph(GraphFunction)
Draw a graph using a GraphFunction.
Element border(Element)
Draw a border around the element.
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.
Decorator color(Color)
Decorate using a foreground color.
Element yflex_shrink(Element)
Minimize if needed on the Y axis.
Element hcenter(Element)
Center an element horizontally.
Element vbox(Elements)
A container displaying elements vertically one by one.
text
The most simple widget. It displays a text.
text("I am a piece of text");
border
Add a border around an element
border(text("The element"))
┌───────────┐
│The element│
└───────────┘
window
A ftxui::window is a ftxui::border, but with some text on top of the border. Add a border around an element
window("The window", text("The element"))
┌The window─┐
│The element│
└───────────┘
separator
Display a vertical or horizontal line to visually split the content of a container in two.
border(
hbox({
text("Left"),
separator(),
text("Right")
})
)
┌────┬─────┐
│left│right│
└────┴─────┘
gauge
A gauge. It can be used to represent a progress bar.
┌────────────────────────────────────────────────────────────────────────────┐
│██████████████████████████████████████ │
└────────────────────────────────────────────────────────────────────────────┘
graph
Colors
A terminal console can usually display colored text and colored background.
A class representing terminal colors.
Palette16
On most terminal the following colors are supported:
- Default
- Black
- GrayDark
- GrayLight
- White
- Blue
- BlueLight
- Cyan
- CyanLight
- Green
- GreenLight
- Magenta
- MagentaLight
- Red
- RedLight
- Yellow
- YellowLight
Example:
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 chose the 24bit RGB color:
There are two constructors:
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.
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.
Style
A terminal console can usually display colored text and colored background. The text can also have different effects: bold, dim, underlined, inverted, blink.
Example:
underlined(bold(text("This text is bold and underlined")))
Tips: The pipe operator can be used to chain Decorator:
text("This text is bold")) | bold | underlined
Layout
These layout are similar to the HTML flexbox:
- vbox (Vertical-box)
- hbox (Horizontal-box)
- dbox (Z-axis-box) They are used to compose all the elements together. Each children are put side by side. If the container is flexible, the extra space available will be shared among the remaining flexible children.
flex(element) can be used to make a non-flexible element flexible. filler() is a flexible empty element. You can use it align children on one side of the container.
An horizontal flow layout is implemented by:
Examples
hbox({
text("left") | border ,
text("middle") | border | flex,
text("right") | border,
});
┌────┐┌─────────────────────────────────────────────────────────────────┐┌─────┐
│left││middle ││right│
└────┘└─────────────────────────────────────────────────────────────────┘└─────┘
hbox({
text("left") | border ,
text("middle") | border | flex,
text("right") | border | flex,
});
┌────┐┌───────────────────────────────────┐┌───────────────────────────────────┐
│left││middle ││right │
└────┘└───────────────────────────────────┘└───────────────────────────────────┘
component
The ftxui/component directory defines the logic to get produce interactive component responding to user's events (keyboard, mouse, etc...)
A ftxui::ScreenInteractive defines a main loop to render a component.
A ftxui::Component is a shared pointer to a ftxui::ComponentBase. The later defines
Predefined components are available in ftxui/dom/component.hpp:
#ifndef FTXUI_COMPONENT_HPP
#define FTXUI_COMPONENT_HPP
#include <functional>
#include <memory>
#include <string>
#include <vector>
struct ButtonOption;
struct CheckboxOption;
struct Event;
struct InputOption;
struct MenuOption;
struct RadioboxOption;
struct ToggleOption;
struct MenuEntryOption;
template <class T, class... Args>
std::shared_ptr<T>
Make(Args&&... args) {
return std::make_shared<T>(args...);
}
std::function<void()> on_click,
Ref<ButtonOption> = {});
bool* checked,
Ref<CheckboxOption> option = {});
ConstStringRef placeholder,
Ref<InputOption> option = {});
int* selected_,
Ref<MenuOption> = {});
int* selected_,
Ref<RadioboxOption> option = {});
int* selected,
Ref<ToggleOption> option = {});
template <class T>
Component Slider(ConstStringRef label, T* value, T min, T max, T increment);
namespace Container {
}
}
#endif
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 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 Checkbox(ConstStringRef label, bool *checked, Ref< CheckboxOption > option={})
Draw checkable element.
Component Radiobox(ConstStringListRef entries, int *selected_, Ref< RadioboxOption > option={})
A list of element, where only one can be selected.
Component Toggle(ConstStringListRef entries, int *selected, Ref< ToggleOption > option={})
An horizontal list of elements. The user can navigate through them.
Component ResizableSplitTop(Component main, Component back, int *main_size)
An vertical split in between two components, configurable using the mouse.
std::shared_ptr< T > Make(Args &&... args)
Component Input(StringRef content, ConstStringRef placeholder, Ref< InputOption > option={})
An input box for editing text.
std::vector< Component > Components
Component Renderer(Component child, std::function< Element()>)
Return a new Component, similar to |child|, but using |render| as the Component::Render() event.
Component Button(ConstStringRef label, std::function< void()> on_click, Ref< ButtonOption >={})
Draw a button. Execute a function when clicked.
Component Menu(ConstStringListRef entries, int *selected_, Ref< MenuOption >={})
A list of text. The focused element is selected.
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)
Component MenuEntry(ConstStringRef label, Ref< MenuEntryOption >={})
Component Maybe(Component, bool *show)
Component ResizableSplitBottom(Component main, Component back, int *main_size)
An vertical split in between two components, configurable using the mouse.
Component Slider(ConstStringRef label, T *value, T min, T max, T increment)
An horizontal slider.
Component ResizableSplitLeft(Component main, Component back, int *main_size)
An horizontal split in between two components, configurable using the mouse.
std::shared_ptr< ComponentBase > Component
Component CatchEvent(Component child, std::function< bool(Event)>)
Element are stateless object. On the other side, components are used when an internal state is needed. Components are used to interact with the user with its keyboard. They handle keyboard navigation, including component focus.
Input
Produced by: ftxui::Input() from "ftxui/component/component.hpp"
Menu
Produced by: ftxui::Menu() from "ftxui/component/component.hpp"
Toggle.
Produced by: ftxui::Toggle() from "ftxui/component/component.hpp"
CheckBox
Produced by: ftxui::Checkbox() from "ftxui/component/component.hpp"
RadioBox
Produced by: ftxui::Radiobox() from "ftxui/component/component.hpp"
Renderer
Produced by: ftxui::Renderer() from ftxui/component/component.hpp. This component decorate another one by using a different function to render an interface.
CatchEvent
Produced by: ftxui::CatchEvent() from ftxui/component/component.hpp. This component decorate another one and catch the events before the underlying component.
Container::Horizontal
Produced by: ftxui::Container::Horizontal() from "ftxui/component/component.hpp". It displays a list of components horizontally and handle keyboard/mouse navigation.
Container::Vertial
Produced by: ftxui::Container::Vertical() from "ftxui/component/component.hpp". It displays a list of components vertically and handles keyboard/mouse navigation.
Container::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.
ResizableSplit::{Left, Right, Top, Bottom}
Produced by:
It defines an horizontal or vertical separation in between two children component. The position of the split is variable and controllable using the mouse.
Force a frame redraw.
Whenever a new group of events have been processed: keyboard, mouse, window resize, etc..., the ftxui::ScreenInteractive::Loop() is responsible for drawing a new frame.
You might want to react to arbitrary events that are unknown to FTXUI. This can be achieve by posting events via ftxui::ScreenInteractive::PostEvent, via a thread. You can post the eventftxui::Event::Custom.
screen->PostEvent(Event::Custom);
ftxui::ScreenInteractive::PostEvent is thread safe.