FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
terminal.cpp
Go to the documentation of this file.
1// Copyright 2020 Arthur Sonzogni. Todos los derechos reservados.
2// El uso de este código fuente se rige por la licencia MIT que se puede encontrar en
3// el archivo LICENSE.
4#include <cstdlib> // for getenv
5#include <string> // for string, allocator
6
8
9#if defined(_WIN32)
10#define WIN32_LEAN_AND_MEAN
11
12#ifndef NOMINMAX
13#define NOMINMAX
14#endif
15
16#include <windows.h>
17#else
18#include <sys/ioctl.h> // for winsize, ioctl, TIOCGWINSZ
19#include <unistd.h> // for STDOUT_FILENO
20#endif
21
22namespace ftxui {
23
24namespace {
25
26bool g_cached = false; // NOLINT
27Terminal::Color g_cached_supported_color; // NOLINT
28
29Dimensions& FallbackSize() {
30#if defined(__EMSCRIPTEN__)
31 // Esta dimensión fue elegida arbitrariamente para poder mostrar:
32 // https://arthursonzogni.com/FTXUI/examples
33 // Esto deberá mejorarse cuando alguien tenga tiempo de implementarlo y lo necesite.
34 constexpr int fallback_width = 140;
35 constexpr int fallback_height = 43;
36#else
37 // El tamaño del terminal en VT100 era 80x24. Todavía se utiliza hoy en día por
38 // defecto en muchos emuladores de terminal. Esa es una buena opción como valor de
39 // reserva.
40 constexpr int fallback_width = 80;
41 constexpr int fallback_height = 24;
42#endif
43 static Dimensions g_fallback_size{
44 fallback_width,
45 fallback_height,
46 };
47 return g_fallback_size;
48}
49
50const char* Safe(const char* c) {
51 return (c != nullptr) ? c : "";
52}
53
54bool Contains(const std::string& s, const char* key) {
55 return s.find(key) != std::string::npos;
56}
57
58Terminal::Color ComputeColorSupport() {
59#if defined(__EMSCRIPTEN__)
61#endif
62
63 std::string COLORTERM = Safe(std::getenv("COLORTERM")); // NOLINT
64 if (Contains(COLORTERM, "24bit") || Contains(COLORTERM, "truecolor")) {
66 }
67
68 std::string TERM = Safe(std::getenv("TERM")); // NOLINT
69 if (Contains(COLORTERM, "256") || Contains(TERM, "256")) {
71 }
72
73#if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
74 // Los terminales de Microsoft no se declaran correctamente como compatibles con los colores
75 // verdaderos: https://github.com/microsoft/terminal/issues/1040
76 // Como alternativa, se asume que los terminales de Microsoft son los que no establecen esas
77 // variables y se habilitan los colores verdaderos.
78 if (TERM.empty() && COLORTERM.empty()) {
80 }
81#endif
82
84}
85
86} // namespace
87
88namespace Terminal {
89
90/// @brief Obtiene el tamaño del terminal.
91/// @return El tamaño del terminal.
92/// @ingroup screen
94#if defined(__EMSCRIPTEN__)
95 // This dimension was chosen arbitrarily to be able to display:
96 // https://arthursonzogni.com/FTXUI/examples
97 // This will have to be improved when someone has time to implement and need
98 // it.
99 return FallbackSize();
100#elif defined(_WIN32)
101 CONSOLE_SCREEN_BUFFER_INFO csbi;
102
103 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
104 return Dimensions{csbi.srWindow.Right - csbi.srWindow.Left + 1,
105 csbi.srWindow.Bottom - csbi.srWindow.Top + 1};
106 }
107
108 return FallbackSize();
109#else
110 winsize w{};
111 const int status = ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); // NOLINT
112 // El resultado del valor de retorno de ioctl debe ser verificado. Algunos sistemas
113 // operativos no soportan TIOCGWINSZ.
114 if (w.ws_col == 0 || w.ws_row == 0 || status < 0) {
115 return FallbackSize();
116 }
117 return Dimensions{w.ws_col, w.ws_row};
118#endif
119}
120
121/// @brief Anula el tamaño del terminal en caso de que la autodetección falle
122/// @param fallbackSize Dimensiones del terminal a las que recurrir
123void SetFallbackSize(const Dimensions& fallbackSize) {
124 FallbackSize() = fallbackSize;
125}
126
127/// @brief Obtiene el soporte de color del terminal.
128/// @ingroup screen
130 if (!g_cached) {
131 g_cached = true;
132 g_cached_supported_color = ComputeColorSupport();
133 }
134 return g_cached_supported_color;
135}
136
137/// @brief Anula el soporte de color del terminal en caso de que la autodetección falle
138/// @ingroup dom
140 g_cached = true;
141 g_cached_supported_color = color;
142}
143
144} // namespace Terminal
145} // namespace ftxui
void SetColorSupport(Color color)
Anula el soporte de color del terminal en caso de que la autodetección falle.
Definition terminal.cpp:139
Decorator color(Color)
Decora usando un color de primer plano.
Color
Color es una enumeración que representa el soporte de color de la terminal.
Definition terminal.hpp:23
Dimensions Size()
Obtiene el tamaño del terminal.
Definition terminal.cpp:93
Color ColorSupport()
Obtiene el soporte de color del terminal.
Definition terminal.cpp:129
Dimensions es una estructura que representa el tamaño de la terminal.
Definition terminal.hpp:11
El espacio de nombres ftxui::Terminal:: de FTXUI.
void SetFallbackSize(const Dimensions &fallbackSize)
Anula el tamaño del terminal en caso de que la autodetección falle.
Definition terminal.cpp:123
El espacio de nombres ftxui:: de FTXUI.
Definition animation.hpp:10