FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
examples/component/input.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 <memory> // for allocator, __shared_ptr_access
5#include <string> // for char_traits, operator+, string, basic_string
6
7#include "ftxui/component/captured_mouse.hpp" // for ftxui
8#include "ftxui/component/component.hpp" // for Input, Renderer, Vertical
9#include "ftxui/component/component_base.hpp" // for ComponentBase
10#include "ftxui/component/component_options.hpp" // for InputOption
11#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
12#include "ftxui/dom/elements.hpp" // for text, hbox, separator, Element, operator|, vbox, border
13#include "ftxui/util/ref.hpp" // for Ref
14
15int main() {
16 using namespace ftxui;
17
18 // The data:
19 std::string first_name;
20 std::string last_name;
21 std::string password;
22 std::string phoneNumber;
23
24 // The basic input components:
25 Component input_first_name = Input(&first_name, "first name");
26 Component input_last_name = Input(&last_name, "last name");
27
28 // The password input component:
29 InputOption password_option;
30 password_option.password = true;
31 Component input_password = Input(&password, "password", password_option);
32
33 // The phone number input component:
34 // We are using `CatchEvent` to filter out non-digit characters.
35 Component input_phone_number = Input(&phoneNumber, "phone number");
36 input_phone_number |= CatchEvent([&](Event event) {
37 return event.is_character() && !std::isdigit(event.character()[0]);
38 });
39 input_phone_number |= CatchEvent([&](Event event) {
40 return event.is_character() && phoneNumber.size() > 10;
41 });
42
43 // The component tree:
44 auto component = Container::Vertical({
45 input_first_name,
46 input_last_name,
47 input_password,
48 input_phone_number,
49 });
50
51 // Tweak how the component tree is rendered:
52 auto renderer = Renderer(component, [&] {
53 return vbox({
54 hbox(text(" First name : "), input_first_name->Render()),
55 hbox(text(" Last name : "), input_last_name->Render()),
56 hbox(text(" Password : "), input_password->Render()),
57 hbox(text(" Phone num : "), input_phone_number->Render()),
58 separator(),
59 text("Hello " + first_name + " " + last_name),
60 text("Your password is " + password),
61 text("Your phone number is " + phoneNumber),
62 }) |
63 border;
64 });
65
66 auto screen = ScreenInteractive::TerminalOutput();
67 screen.Loop(renderer);
68}
std::string character() const
Definition event.hpp:107
Ref< bool > password
Obscure the input content using '*'.
Represent an event. It can be key press event, a terminal resize, or more ...
Definition event.hpp:29
Option for the Input component.
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
std::shared_ptr< ComponentBase > Component