FTXUI 7.0.3
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 // On applique les quirks uniquement pour le panneau de démo, afin d'en
80 // voir l'effet.
81 // Remarque : Dans FTXUI, les paramètres du terminal sont globaux. Les
82 // appliquer ici affectera le rendu réel de l'arbre d'éléments retourné.
83 quirks.SetBlockCharacters(block_characters);
84 quirks.SetCursorHiding(cursor_hiding);
85 quirks.SetComponentAscii(component_ascii);
86 quirks.SetColorSupport((Terminal::Color)color_support);
87 Terminal::SetQuirks(quirks);
88
89 auto current_time = std::chrono::steady_clock::now();
90 std::chrono::duration<float> elapsed = current_time - start_time;
91 // Sinusoïde plus lente (0.5f au lieu de 2.0f)
92 // Plage 20%-80% -> 0.5f + 0.3f * sin (0.2 à 0.8)
93 float gauge_value = 0.5f + 0.3f * std::sin(elapsed.count() * 0.5f);
94 screen.RequestAnimationFrame();
95
96 Elements hsv_rows;
97 const int saturation = 255;
98 for (int value = 0; value < 255; value += 20) {
99 Elements line;
100 for (int hue = 0; hue < 255; hue += 6) {
101 line.push_back(text("▀") //
102 | color(Color::HSV(hue, saturation, value)) //
103 | bgcolor(Color::HSV(hue, saturation, value + 10)));
104 }
105 hsv_rows.push_back(hbox(std::move(line)));
106 }
107
108 auto overrides_pane = vbox({
109 cb_block->Render(),
110 hbox(text("Verification: "), gauge(gauge_value) | flex),
111 separator(),
112 cb_cursor->Render(),
113 text("(Hides terminal cursor if supported/enabled)") | dim,
114 separator(),
115 cb_ascii->Render(),
116 hbox({
117 text("Verification: "),
118 dummy_checkbox->Render(),
119 text(" "),
120 dummy_radiobox->Render(),
121 }),
122 separator(),
123 text("Color Support:"),
124 rb_color->Render(),
125 text("Verification (HSV Square):"),
126 vbox(std::move(hsv_rows)),
127 });
128
129 return hbox({
130 vbox({
131 window(text(" Terminal Info "), info_pane),
132 window(text(" Exit "),
133 vbox({
134 text("Press 'q' or click the button to exit."),
135 btn_quit->Render(),
136 })),
137 }) | size(WIDTH, EQUAL, 40),
138 window(text(" Runtime Quirk Overrides & Visual Verification "),
139 overrides_pane) |
140 flex,
141 });
142 });
143
144 auto component = CatchEvent(renderer, [&](Event event) {
145 if (event == Event::Character('q')) {
146 screen.Exit();
147 return true;
148 }
149 return false;
150 });
151
152 screen.Loop(component);
153}
Color
Color est une énumération qui représente le support des couleurs du terminal.
Definition terminal.hpp:31
L'espace de noms FTXUI ftxui::
Definition animation.hpp:11
std::vector< Element > Elements
Definition elements.hpp:26
int main()