FTXUI/src/ftxui/screen/terminal.cpp

82 lines
1.9 KiB
C++
Raw Normal View History

2021-05-02 02:40:35 +08:00
#include <cstdlib> // for getenv
#include <string> // for string, allocator
2021-05-02 02:40:35 +08:00
#include "ftxui/screen/terminal.hpp"
#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
2021-04-10 23:37:32 +08:00
#ifndef NOMINMAX
#define NOMINMAX
2021-04-10 23:37:32 +08:00
#endif
#include <Windows.h>
#else
2021-05-02 02:40:35 +08:00
#include <sys/ioctl.h> // for winsize, ioctl, TIOCGWINSZ
#include <unistd.h> // for STDOUT_FILENO
#endif
2018-09-18 14:48:40 +08:00
namespace ftxui {
Terminal::Dimensions Terminal::Size() {
#if defined(__EMSCRIPTEN__)
2021-03-22 05:54:39 +08:00
return Dimensions{140, 43};
#elif defined(_WIN32)
CONSOLE_SCREEN_BUFFER_INFO csbi;
int columns, rows;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
return Dimensions{columns, rows};
#else
2018-09-18 14:48:40 +08:00
winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
return Dimensions{w.ws_col, w.ws_row};
#endif
2018-09-18 14:48:40 +08:00
}
2020-10-17 04:31:24 +08:00
namespace {
const char* Safe(const char* c) {
return c ? c : "";
}
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"));
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"));
if (Contains(COLORTERM, "256") || Contains(TERM, "256"))
2020-10-17 04:31:24 +08:00
return Terminal::Color::Palette256;
return Terminal::Color::Palette16;
}
} // namespace
Terminal::Color Terminal::ColorSupport() {
if (!cached) {
cached = true;
cached_supported_color = ComputeColorSupport();
}
2020-10-17 04:31:24 +08:00
return cached_supported_color;
}
} // namespace ftxui
// 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.