FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
terminal_info.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 <chrono> // for chrono
5#include <cmath> // for sin
6#include <memory> // for shared_ptr, __shared_ptr_access
7#include <string> // for string, to_string
8#include <utility> // for move
9#include <vector> // for vector
10
11#include "ftxui/component/app.hpp" // for App
12#include "ftxui/component/component.hpp" // for Renderer, Button, Vertical, Checkbox, Radiobox
13#include "ftxui/component/component_base.hpp" // for ComponentBase
14#include "ftxui/dom/elements.hpp" // for operator|, Element, text, vbox, border, window, separator, hbox
15#include "ftxui/screen/color.hpp" // for Color
16#include "ftxui/screen/terminal.hpp" // for Quirks, GetQuirks, SetQuirks
17
18int main() {
19 using namespace ftxui;
20 auto screen = App::FitComponent();
21
22 auto quirks = Terminal::GetQuirks();
23
24 auto cb_block =
25 Checkbox("Support 8 Unicode block characters", &quirks.block_characters);
26 auto cb_cursor = Checkbox("Support cursor hiding", &quirks.cursor_hiding);
27 auto cb_ascii = Checkbox("Use ASCII for components", &quirks.component_ascii);
28
29 std::vector<std::string> color_names = {
30 "Palette1",
31 "Palette16",
32 "Palette256",
33 "TrueColor",
34 };
35 auto rb_color = Radiobox(&color_names, (int*)&quirks.color_support);
36
37 auto btn_quit =
38 Button("Quit", screen.ExitLoopClosure(), ButtonOption::Animated());
39
40 bool dummy_checked = true;
41 int dummy_selected = 0;
42 std::vector<std::string> dummy_options = {"Option"};
43 auto dummy_checkbox = Checkbox("Tested", &dummy_checked);
44 auto dummy_radiobox = Radiobox(&dummy_options, &dummy_selected);
45
46 auto layout = Container::Vertical({
47 cb_block,
48 cb_cursor,
49 cb_ascii,
50 rb_color,
51 dummy_checkbox,
52 dummy_radiobox,
53 btn_quit,
54 });
55
56 auto start_time = std::chrono::steady_clock::now();
57
58 auto renderer = Renderer(layout, [&] {
59 auto capabilities = screen.TerminalCapabilityNames();
60 Elements capability_elements;
61 for (const auto& cap : capabilities) {
62 capability_elements.push_back(text(" - " + cap));
63 }
64
65 auto info_pane = vbox({
66 text("Terminal Name: " + screen.TerminalName()),
67 text("Terminal Version: " + std::to_string(screen.TerminalVersion())),
68 text("Emulator Name: " + screen.TerminalEmulatorName()),
69 text("Emulator Version: " + screen.TerminalEmulatorVersion()),
70 text("Detected Capabilities:"),
71 vbox(std::move(capability_elements)),
72 });
73
74 // We only apply the quirks for the demo pane to see the effect.
75 // Note: In FTXUI, Terminal settings are global. Applying them here
76 // will affect the actual rendering of the returned element tree.
77 Terminal::SetQuirks(quirks);
78
79 auto current_time = std::chrono::steady_clock::now();
80 std::chrono::duration<float> elapsed = current_time - start_time;
81 // Slower sinusoide (0.5f instead of 2.0f)
82 // Range 20%-80% -> 0.5f + 0.3f * sin (0.2 to 0.8)
83 float gauge_value = 0.5f + 0.3f * std::sin(elapsed.count() * 0.5f);
84 screen.RequestAnimationFrame();
85
86 Elements hsv_rows;
87 const int saturation = 255;
88 for (int value = 0; value < 255; value += 20) {
89 Elements line;
90 for (int hue = 0; hue < 255; hue += 6) {
91 line.push_back(text("▀") //
92 | color(Color::HSV(hue, saturation, value)) //
93 | bgcolor(Color::HSV(hue, saturation, value + 10)));
94 }
95 hsv_rows.push_back(hbox(std::move(line)));
96 }
97
98 auto overrides_pane = vbox({
99 cb_block->Render(),
100 hbox(text("Verification: "), gauge(gauge_value) | flex),
101 separator(),
102 cb_cursor->Render(),
103 text("(Hides terminal cursor if supported/enabled)") | dim,
104 separator(),
105 cb_ascii->Render(),
106 hbox({
107 text("Verification: "),
108 dummy_checkbox->Render(),
109 text(" "),
110 dummy_radiobox->Render(),
111 }),
112 separator(),
113 text("Color Support:"),
114 rb_color->Render(),
115 text("Verification (HSV Square):"),
116 vbox(std::move(hsv_rows)),
117 });
118
119 return hbox({
120 vbox({
121 window(text(" Terminal Info "), info_pane),
122 window(text(" Exit "),
123 vbox({
124 text("Press 'q' or click the button to exit."),
125 btn_quit->Render(),
126 })),
127 }) | size(WIDTH, EQUAL, 40),
128 window(text(" Runtime Quirk Overrides & Visual Verification "),
129 overrides_pane) |
130 flex,
131 });
132 });
133
134 auto component = CatchEvent(renderer, [&](Event event) {
135 if (event == Event::Character('q')) {
136 screen.Exit();
137 return true;
138 }
139 return false;
140 });
141
142 screen.Loop(component);
143}
Represent an event. It can be key press event, a terminal resize, or more ...
Definition event.hpp:32
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
std::vector< Element > Elements
Definition elements.hpp:24
int main()