FTXUI  0.10.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
terminal.cpp
Go to the documentation of this file.
1#include <cstdlib> // for getenv
2#include <string> // for string, allocator
3
5
6#if defined(_WIN32)
7#define WIN32_LEAN_AND_MEAN
8
9#ifndef NOMINMAX
10#define NOMINMAX
11#endif
12
13#include <Windows.h>
14#else
15#include <sys/ioctl.h> // for winsize, ioctl, TIOCGWINSZ
16#include <unistd.h> // for STDOUT_FILENO
17#endif
18
19namespace ftxui {
20
22#if defined(__EMSCRIPTEN__)
23 return Dimensions{140, 43};
24#elif defined(_WIN32)
25 CONSOLE_SCREEN_BUFFER_INFO csbi;
26
27 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
28 return Dimensions{csbi.srWindow.Right - csbi.srWindow.Left + 1,
29 csbi.srWindow.Bottom - csbi.srWindow.Top + 1};
30 }
31
32 // The Microsoft default "cmd" returns errors above.
33 return Dimensions{80, 80};
34
35#else
36 winsize w;
37 ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
38 return Dimensions{w.ws_col, w.ws_row};
39#endif
40}
41
42namespace {
43
44const char* Safe(const char* c) {
45 return c ? c : "";
46}
47
48bool Contains(const std::string& s, const char* key) {
49 return s.find(key) != std::string::npos;
50}
51
52static bool cached = false;
53Terminal::Color cached_supported_color;
54Terminal::Color ComputeColorSupport() {
55#if defined(__EMSCRIPTEN__)
57#endif
58
59 std::string COLORTERM = Safe(std::getenv("COLORTERM"));
60 if (Contains(COLORTERM, "24bit") || Contains(COLORTERM, "truecolor"))
62
63 std::string TERM = Safe(std::getenv("TERM"));
64 if (Contains(COLORTERM, "256") || Contains(TERM, "256"))
66
67#if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
68 // Microsoft terminals do not properly declare themselve supporting true
69 // colors: https://github.com/microsoft/terminal/issues/1040
70 // As a fallback, assume microsoft terminal are the ones not setting those
71 // variables, and enable true colors.
72 if (TERM == "" && COLORTERM == "")
74#endif
75
77}
78
79} // namespace
80
82 if (!cached) {
83 cached = true;
84 cached_supported_color = ComputeColorSupport();
85 }
86 return cached_supported_color;
87}
88
89} // namespace ftxui
90
91// Copyright 2020 Arthur Sonzogni. All rights reserved.
92// Use of this source code is governed by the MIT license that can be found in
93// the LICENSE file.
Color ColorSupport()
Definition terminal.cpp:81
Dimensions Size()
Definition terminal.cpp:21