2021-05-02 02:40:35 +08:00
|
|
|
#include <cstdlib> // for getenv
|
|
|
|
#include <string> // for string, allocator
|
2020-03-21 23:18:15 +08:00
|
|
|
|
2021-05-02 02:40:35 +08:00
|
|
|
#include "ftxui/screen/terminal.hpp"
|
2020-09-02 17:32:22 +08:00
|
|
|
|
2020-03-25 15:54:03 +08:00
|
|
|
#if defined(_WIN32)
|
2020-08-16 06:24:18 +08:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2021-04-10 23:37:32 +08:00
|
|
|
|
|
|
|
#ifndef NOMINMAX
|
2020-08-16 06:24:18 +08:00
|
|
|
#define NOMINMAX
|
2021-04-10 23:37:32 +08:00
|
|
|
#endif
|
|
|
|
|
2020-08-16 06:24:18 +08:00
|
|
|
#include <Windows.h>
|
2020-03-21 23:18:15 +08:00
|
|
|
#else
|
2021-05-02 02:40:35 +08:00
|
|
|
#include <sys/ioctl.h> // for winsize, ioctl, TIOCGWINSZ
|
|
|
|
#include <unistd.h> // for STDOUT_FILENO
|
2020-03-21 23:18:15 +08:00
|
|
|
#endif
|
2018-09-18 14:48:40 +08:00
|
|
|
|
|
|
|
namespace ftxui {
|
|
|
|
|
2021-10-13 19:44:30 +08:00
|
|
|
#if defined(__EMSCRIPTEN__)
|
2022-01-02 02:24:37 +08:00
|
|
|
// This dimension was chosen arbitrarily to be able to display:
|
|
|
|
// https://arthursonzogni.com/FTXUI/examples
|
|
|
|
// This will have to be improved when someone has time to implement and need
|
|
|
|
// it.
|
|
|
|
static Dimensions fallback_size {140, 43};
|
2021-08-03 05:19:29 +08:00
|
|
|
Dimensions Terminal::Size() {
|
2021-10-13 19:44:30 +08:00
|
|
|
return fallback_size;
|
2022-01-02 02:24:37 +08:00
|
|
|
}
|
|
|
|
|
2020-03-25 15:54:03 +08:00
|
|
|
#elif defined(_WIN32)
|
2022-01-02 02:24:37 +08:00
|
|
|
|
|
|
|
// The terminal size in VT100 was 80x24. It is still used nowadays by
|
|
|
|
// default in many terminal emulator. That's a good choice for a fallback
|
|
|
|
// value.
|
|
|
|
static Dimensions fallback_size {80, 24};
|
|
|
|
Dimensions Terminal::Size() {
|
2020-03-21 23:18:15 +08:00
|
|
|
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
|
|
|
|
2021-07-04 23:38:31 +08:00
|
|
|
if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
|
|
|
|
return Dimensions{csbi.srWindow.Right - csbi.srWindow.Left + 1,
|
|
|
|
csbi.srWindow.Bottom - csbi.srWindow.Top + 1};
|
|
|
|
}
|
|
|
|
|
2021-10-13 19:44:30 +08:00
|
|
|
return fallback_size;
|
2022-01-02 02:24:37 +08:00
|
|
|
}
|
2021-07-04 23:38:31 +08:00
|
|
|
|
2019-02-02 08:59:48 +08:00
|
|
|
#else
|
2022-01-02 02:24:37 +08:00
|
|
|
// The terminal size in VT100 was 80x24. It is still used nowadays by
|
|
|
|
// default in many terminal emulator. That's a good choice for a fallback
|
|
|
|
// value.
|
|
|
|
static Dimensions fallback_size {80, 24};
|
|
|
|
Dimensions Terminal::Size() {
|
2021-10-09 17:51:00 +08:00
|
|
|
winsize w{};
|
2022-01-02 02:24:37 +08:00
|
|
|
const int status = ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
|
|
|
|
// The ioctl return value result should be checked. Some operating systems
|
|
|
|
// don't support TIOCGWINSZ.
|
|
|
|
if (w.ws_col == 0 || w.ws_row == 0 || status < 0) {
|
2021-10-13 19:44:30 +08:00
|
|
|
return fallback_size;
|
2021-10-09 17:51:00 +08:00
|
|
|
}
|
2018-09-18 14:48:40 +08:00
|
|
|
return Dimensions{w.ws_col, w.ws_row};
|
|
|
|
}
|
|
|
|
|
2022-01-02 02:24:37 +08:00
|
|
|
#endif
|
|
|
|
|
2021-10-13 19:44:30 +08:00
|
|
|
/// @brief Override terminal size in case auto-detection fails
|
|
|
|
/// @param fallbackSize Terminal dimensions to fallback to
|
|
|
|
void Terminal::SetFallbackSize(const Dimensions& fallbackSize) {
|
|
|
|
fallback_size = fallbackSize;
|
|
|
|
}
|
|
|
|
|
2020-10-17 04:31:24 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
const char* Safe(const char* c) {
|
|
|
|
return c ? c : "";
|
|
|
|
}
|
|
|
|
|
2020-12-23 19:20:01 +08:00
|
|
|
bool Contains(const std::string& s, const char* key) {
|
|
|
|
return s.find(key) != std::string::npos;
|
|
|
|
}
|
|
|
|
|
2020-10-17 04:31:24 +08:00
|
|
|
static bool cached = false;
|
|
|
|
Terminal::Color cached_supported_color;
|
|
|
|
Terminal::Color ComputeColorSupport() {
|
2021-03-22 05:54:39 +08:00
|
|
|
#if defined(__EMSCRIPTEN__)
|
|
|
|
return Terminal::Color::TrueColor;
|
|
|
|
#endif
|
|
|
|
|
2020-10-17 04:31:24 +08:00
|
|
|
std::string COLORTERM = Safe(std::getenv("COLORTERM"));
|
2020-12-23 19:20:01 +08:00
|
|
|
if (Contains(COLORTERM, "24bit") || Contains(COLORTERM, "truecolor"))
|
2020-10-17 04:31:24 +08:00
|
|
|
return Terminal::Color::TrueColor;
|
|
|
|
|
|
|
|
std::string TERM = Safe(std::getenv("TERM"));
|
2020-12-23 19:20:01 +08:00
|
|
|
if (Contains(COLORTERM, "256") || Contains(TERM, "256"))
|
2020-10-17 04:31:24 +08:00
|
|
|
return Terminal::Color::Palette256;
|
|
|
|
|
2021-07-04 23:38:31 +08:00
|
|
|
#if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
|
|
|
|
// Microsoft terminals do not properly declare themselve supporting true
|
|
|
|
// colors: https://github.com/microsoft/terminal/issues/1040
|
|
|
|
// As a fallback, assume microsoft terminal are the ones not setting those
|
|
|
|
// variables, and enable true colors.
|
|
|
|
if (TERM == "" && COLORTERM == "")
|
|
|
|
return Terminal::Color::TrueColor;
|
|
|
|
#endif
|
|
|
|
|
2020-10-17 04:31:24 +08:00
|
|
|
return Terminal::Color::Palette16;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
Terminal::Color Terminal::ColorSupport() {
|
|
|
|
if (!cached) {
|
|
|
|
cached = true;
|
|
|
|
cached_supported_color = ComputeColorSupport();
|
2020-10-16 03:17:26 +08:00
|
|
|
}
|
2020-10-17 04:31:24 +08:00
|
|
|
return cached_supported_color;
|
2020-09-02 17:32:22 +08:00
|
|
|
}
|
|
|
|
|
2019-06-23 23:47:33 +08:00
|
|
|
} // namespace ftxui
|
2020-08-16 06:24:18 +08:00
|
|
|
|
|
|
|
// Copyright 2020 Arthur Sonzogni. All rights reserved.
|
|
|
|
// Use of this source code is governed by the MIT license that can be found in
|
|
|
|
// the LICENSE file.
|