FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
examples/component/input.cpp
Go to the documentation of this file.
1// 版權所有 2020 Arthur Sonzogni. 保留所有權利。
2// 本原始碼的使用受 MIT 授權約束,該授權可在 LICENSE 檔案中找到。
3#include <memory> // for allocator, __shared_ptr_access
4#include <string> // for char_traits, operator+, string, basic_string
5
6#include "ftxui/component/captured_mouse.hpp" // for ftxui
7#include "ftxui/component/component.hpp" // for Input, Renderer, Vertical
8#include "ftxui/component/component_base.hpp" // for ComponentBase
9#include "ftxui/component/component_options.hpp" // for InputOption
10#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
11#include "ftxui/dom/elements.hpp" // for text, hbox, separator, Element, operator|, vbox, border
12#include "ftxui/util/ref.hpp" // for Ref
13
14int main() {
15 using namespace ftxui;
16
17 // 資料:
18 std::string first_name;
19 std::string last_name;
20 std::string password;
21 std::string phoneNumber;
22
23 // 基本輸入元件:
24 Component input_first_name = Input(&first_name, "first name");
25 Component input_last_name = Input(&last_name, "last name");
26
27 // 密碼輸入元件:
28 InputOption password_option;
29 password_option.password = true;
30 Component input_password = Input(&password, "password", password_option);
31
32 // 電話號碼輸入元件:
33 // 我們使用 `CatchEvent` 來過濾非數字字元。
34 Component input_phone_number = Input(&phoneNumber, "phone number");
35 input_phone_number |= CatchEvent([&](Event event) {
36 return event.is_character() && !std::isdigit(event.character()[0]);
37 });
38 input_phone_number |= CatchEvent([&](Event event) {
39 return event.is_character() && phoneNumber.size() > 10;
40 });
41
42 // 元件樹:
43 auto component = Container::Vertical({
44 input_first_name,
45 input_last_name,
46 input_password,
47 input_phone_number,
48 });
49
50 // 調整元件樹的渲染方式:
51 auto renderer = Renderer(component, [&] {
52 return vbox({
53 hbox(text(" First name : "), input_first_name->Render()),
54 hbox(text(" Last name : "), input_last_name->Render()),
55 hbox(text(" Password : "), input_password->Render()),
56 hbox(text(" Phone num : "), input_phone_number->Render()),
57 separator(),
58 text("Hello " + first_name + " " + last_name),
59 text("Your password is " + password),
60 text("Your phone number is " + phoneNumber),
61 }) |
62 border;
63 });
64
65 auto screen = ScreenInteractive::TerminalOutput();
66 screen.Loop(renderer);
67}
auto component
Definition gallery.cpp:127
std::string character() const
Definition event.hpp:105
Ref< bool > password
使用 '*' 隱藏輸入內容。
代表一個事件。它可以是按鍵事件、終端機大小調整,或更多...
Definition event.hpp:27
Input 元件的選項。
FTXUI 的 ftxui:: 命名空間
Definition animation.hpp:10
std::shared_ptr< ComponentBase > Component