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