2020-03-22 22:32:44 +01:00
|
|
|
#include "ftxui/screen/terminal.hpp"
|
|
|
|
|
2018-09-18 08:48:40 +02:00
|
|
|
#include <stdio.h>
|
2020-03-21 16:18:15 +01:00
|
|
|
|
2020-09-02 11:32:22 +02:00
|
|
|
#include <cstdlib>
|
|
|
|
|
2020-03-25 08:54:03 +01:00
|
|
|
#if defined(_WIN32)
|
2020-08-16 00:24:18 +02:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#define NOMINMAX
|
|
|
|
#include <Windows.h>
|
2020-03-21 16:18:15 +01:00
|
|
|
#else
|
2020-08-16 00:24:18 +02:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <unistd.h>
|
2020-03-21 16:18:15 +01:00
|
|
|
#endif
|
2018-09-18 08:48:40 +02:00
|
|
|
|
2020-03-22 22:32:44 +01:00
|
|
|
#include <iostream>
|
2018-09-18 08:48:40 +02:00
|
|
|
|
|
|
|
namespace ftxui {
|
|
|
|
|
|
|
|
Terminal::Dimensions Terminal::Size() {
|
2020-03-21 16:18:15 +01:00
|
|
|
#if defined(__EMSCRIPTEN__)
|
2021-03-21 22:54:39 +01:00
|
|
|
return Dimensions{140, 43};
|
2020-03-25 08:54:03 +01:00
|
|
|
#elif defined(_WIN32)
|
2020-03-21 16:18:15 +01:00
|
|
|
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};
|
2019-02-02 01:59:48 +01:00
|
|
|
#else
|
2018-09-18 08:48:40 +02:00
|
|
|
winsize w;
|
|
|
|
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
|
|
|
|
return Dimensions{w.ws_col, w.ws_row};
|
2019-02-02 01:59:48 +01:00
|
|
|
#endif
|
2018-09-18 08:48:40 +02:00
|
|
|
}
|
|
|
|
|
2020-10-16 22:31:24 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
const char* Safe(const char* c) {
|
|
|
|
return c ? c : "";
|
|
|
|
}
|
|
|
|
|
2020-12-23 12:20:01 +01:00
|
|
|
bool Contains(const std::string& s, const char* key) {
|
|
|
|
return s.find(key) != std::string::npos;
|
|
|
|
}
|
|
|
|
|
2020-10-16 22:31:24 +02:00
|
|
|
static bool cached = false;
|
|
|
|
Terminal::Color cached_supported_color;
|
|
|
|
Terminal::Color ComputeColorSupport() {
|
2021-03-21 22:54:39 +01:00
|
|
|
#if defined(__EMSCRIPTEN__)
|
|
|
|
return Terminal::Color::TrueColor;
|
|
|
|
#endif
|
|
|
|
|
2020-10-16 22:31:24 +02:00
|
|
|
std::string COLORTERM = Safe(std::getenv("COLORTERM"));
|
2020-12-23 12:20:01 +01:00
|
|
|
if (Contains(COLORTERM, "24bit") || Contains(COLORTERM, "truecolor"))
|
2020-10-16 22:31:24 +02:00
|
|
|
return Terminal::Color::TrueColor;
|
|
|
|
|
|
|
|
std::string TERM = Safe(std::getenv("TERM"));
|
2020-12-23 12:20:01 +01:00
|
|
|
if (Contains(COLORTERM, "256") || Contains(TERM, "256"))
|
2020-10-16 22:31:24 +02:00
|
|
|
return Terminal::Color::Palette256;
|
|
|
|
|
|
|
|
return Terminal::Color::Palette16;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
Terminal::Color Terminal::ColorSupport() {
|
|
|
|
if (!cached) {
|
|
|
|
cached = true;
|
|
|
|
cached_supported_color = ComputeColorSupport();
|
2020-10-15 21:17:26 +02:00
|
|
|
}
|
2020-10-16 22:31:24 +02:00
|
|
|
return cached_supported_color;
|
2020-09-02 11:32:22 +02:00
|
|
|
}
|
|
|
|
|
2019-06-23 17:47:33 +02:00
|
|
|
} // namespace ftxui
|
2020-08-16 00:24:18 +02: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.
|