FTXUI/src/ftxui/screen/screen.cpp

532 lines
14 KiB
C++
Raw Normal View History

2022-04-17 21:47:20 +08:00
#include <cstdint> // for uint8_t
#include <iostream> // for operator<<, stringstream, basic_ostream, flush, cout, ostream
2021-09-17 02:45:26 +08:00
#include <map> // for _Rb_tree_const_iterator, map, operator!=, operator==
2022-03-31 08:17:43 +08:00
#include <memory> // for allocator
#include <sstream> // IWYU pragma: keep
2021-09-17 02:45:26 +08:00
#include <utility> // for pair
2020-03-23 05:32:44 +08:00
2021-05-02 02:40:35 +08:00
#include "ftxui/screen/screen.hpp"
#include "ftxui/screen/string.hpp" // for string_width
2021-08-07 02:32:33 +08:00
#include "ftxui/screen/terminal.hpp" // for Dimensions, Size
#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <Windows.h>
#endif
namespace ftxui {
2019-01-20 05:06:05 +08:00
namespace {
2022-03-31 08:17:43 +08:00
Pixel& dev_null_pixel() {
static Pixel pixel;
return pixel;
}
2019-01-20 05:06:05 +08:00
#if defined(_WIN32)
void WindowsEmulateVT100Terminal() {
static bool done = false;
if (done)
return;
done = true;
// Enable VT processing on stdout and stdin
auto stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD out_mode = 0;
GetConsoleMode(stdout_handle, &out_mode);
// https://docs.microsoft.com/en-us/windows/console/setconsolemode
const int enable_virtual_terminal_processing = 0x0004;
const int disable_newline_auto_return = 0x0008;
out_mode |= enable_virtual_terminal_processing;
out_mode |= disable_newline_auto_return;
SetConsoleMode(stdout_handle, out_mode);
}
#endif
void UpdatePixelStyle(std::stringstream& ss,
Pixel& previous,
const Pixel& next) {
2022-03-31 08:17:43 +08:00
if (next == previous) {
return;
}
if ((!next.bold && previous.bold) || //
(!next.dim && previous.dim)) {
ss << "\x1B[22m"; // BOLD_RESET and DIM_RESET
// We might have wrongfully reset dim or bold because they share the same
// resetter. Take it into account so that the side effect will cause it to
// be set again below.
previous.bold = false;
previous.dim = false;
}
2022-03-31 08:17:43 +08:00
if (next.bold && !previous.bold) {
ss << "\x1B[1m"; // BOLD_SET
}
if (next.dim && !previous.dim) {
ss << "\x1B[2m"; // DIM_SET
}
if (next.underlined && !previous.underlined) {
ss << "\x1B[4m"; // UNDERLINED_SET
}
if (!next.underlined && previous.underlined) {
ss << "\x1B[24m"; // UNDERLINED_RESET
}
2020-08-16 08:24:50 +08:00
2022-03-31 08:17:43 +08:00
if (next.blink && !previous.blink) {
ss << "\x1B[5m"; // BLINK_SET
}
2020-08-16 08:24:50 +08:00
2022-03-31 08:17:43 +08:00
if (!next.blink && previous.blink) {
ss << "\x1B[25m"; // BLINK_RESET
}
2020-08-16 08:24:50 +08:00
2022-03-31 08:17:43 +08:00
if (next.inverted && !previous.inverted) {
ss << "\x1B[7m"; // INVERTED_SET
}
2020-08-16 08:24:50 +08:00
2022-03-31 08:17:43 +08:00
if (!next.inverted && previous.inverted) {
ss << "\x1B[27m"; // INVERTED_RESET
}
2020-08-16 08:24:50 +08:00
if (next.foreground_color != previous.foreground_color ||
next.background_color != previous.background_color) {
ss << "\x1B[" + next.foreground_color.Print(false) + "m";
ss << "\x1B[" + next.background_color.Print(true) + "m";
2020-08-16 08:24:50 +08:00
}
previous = next;
}
2021-09-12 06:36:59 +08:00
struct TileEncoding {
2022-03-31 08:17:43 +08:00
uint8_t left : 2;
uint8_t top : 2;
uint8_t right : 2;
uint8_t down : 2;
uint8_t round : 1;
2021-09-12 06:36:59 +08:00
// clang-format off
2021-09-12 06:36:59 +08:00
bool operator<(const TileEncoding& other) const {
2022-03-31 08:17:43 +08:00
if (left < other.left) { return true; }
if (left > other.left) { return false; }
if (top < other.top) { return true; }
if (top > other.top) { return false; }
if (right < other.right) { return true; }
if (right > other.right) { return false; }
if (down < other.down) { return true; }
if (down > other.down) { return false; }
if (round < other.round) { return true; }
if (round > other.round) { return false; }
return false;
2021-09-12 06:36:59 +08:00
}
// clang-format on
2021-09-12 06:36:59 +08:00
};
// clang-format off
2022-03-31 08:17:43 +08:00
const std::map<std::string, TileEncoding> tile_encoding = { // NOLINT
2021-09-12 06:36:59 +08:00
{"", {1, 0, 1, 0, 0}},
{"", {2, 0, 2, 0, 0}},
{"", {0, 1, 0, 1, 0}},
{"", {0, 2, 0, 2, 0}},
{"", {0, 0, 1, 1, 0}},
{"", {0, 0, 2, 1, 0}},
{"", {0, 0, 1, 2, 0}},
{"", {0, 0, 2, 2, 0}},
{"", {1, 0, 0, 1, 0}},
{"", {2, 0, 0, 1, 0}},
{"", {1, 0, 0, 2, 0}},
{"", {2, 0, 0, 2, 0}},
{"", {0, 1, 1, 0, 0}},
{"", {0, 1, 2, 0, 0}},
{"", {0, 2, 1, 0, 0}},
{"", {0, 2, 2, 0, 0}},
{"", {1, 1, 0, 0, 0}},
{"", {2, 1, 0, 0, 0}},
{"", {1, 2, 0, 0, 0}},
{"", {2, 2, 0, 0, 0}},
{"", {0, 1, 1, 1, 0}},
{"", {0, 1, 2, 1, 0}},
{"", {0, 2, 1, 1, 0}},
{"", {0, 1, 1, 2, 0}},
{"", {0, 2, 1, 2, 0}},
{"", {0, 2, 2, 1, 0}},
{"", {0, 1, 2, 2, 0}},
{"", {0, 2, 2, 2, 0}},
{"", {1, 1, 0, 1, 0}},
{"", {2, 1, 0, 1, 0}},
{"", {1, 2, 0, 1, 0}},
{"", {1, 1, 0, 2, 0}},
{"", {1, 2, 0, 2, 0}},
{"", {2, 2, 0, 1, 0}},
{"", {2, 1, 0, 2, 0}},
{"", {2, 2, 0, 2, 0}},
{"", {1, 0, 1, 1, 0}},
{"", {2, 0, 1, 1, 0}},
{"", {1, 0, 2, 1, 0}},
{"", {2, 0, 2, 1, 0}},
{"", {1, 0, 1, 2, 0}},
{"", {2, 0, 1, 2, 0}},
{"", {1, 0, 2, 2, 0}},
{"", {2, 0, 2, 2, 0}},
{"", {1, 1, 1, 0, 0}},
{"", {2, 1, 1, 0, 0}},
{"", {1, 1, 2, 0, 0}},
{"", {2, 1, 2, 0, 0}},
{"", {1, 2, 1, 0, 0}},
{"", {2, 2, 1, 0, 0}},
{"", {1, 2, 2, 0, 0}},
{"", {2, 2, 2, 0, 0}},
{"", {1, 1, 1, 1, 0}},
{"", {2, 1, 1, 1, 0}},
{"", {1, 1, 2, 1, 0}},
{"", {2, 1, 2, 1, 0}},
{"", {1, 2, 1, 1, 0}},
{"", {1, 1, 1, 2, 0}},
{"", {1, 2, 1, 2, 0}},
{"", {2, 2, 1, 1, 0}},
{"", {1, 2, 2, 1, 0}},
{"", {2, 1, 1, 2, 0}},
{"", {1, 1, 2, 2, 0}},
{"", {2, 2, 2, 1, 0}},
{"", {2, 1, 2, 2, 0}},
{"", {2, 2, 1, 2, 0}},
{"", {1, 2, 2, 2, 0}},
{"", {2, 2, 2, 2, 0}},
{"", {3, 0, 3, 0, 0}},
{"", {0, 3, 0, 3, 0}},
{"", {0, 0, 3, 1, 0}},
{"", {0, 0, 1, 3, 0}},
{"", {0, 0, 3, 3, 0}},
{"", {3, 0, 0, 1, 0}},
{"", {1, 0, 0, 3, 0}},
{"", {3, 0, 0, 3, 0}},
{"", {0, 1, 3, 0, 0}},
{"", {0, 3, 1, 0, 0}},
{"", {0, 3, 3, 0, 0}},
{"", {3, 1, 0, 0, 0}},
{"", {1, 3, 0, 0, 0}},
{"", {3, 3, 0, 0, 0}},
{"", {0, 1, 3, 1, 0}},
{"", {0, 3, 1, 3, 0}},
{"", {0, 3, 3, 3, 0}},
{"", {3, 1, 0, 1, 0}},
{"", {1, 3, 0, 3, 0}},
{"", {3, 3, 0, 3, 0}},
{"", {3, 0, 3, 1, 0}},
{"", {1, 0, 1, 3, 0}},
{"", {3, 0, 3, 3, 0}},
{"", {3, 1, 3, 0, 0}},
{"", {1, 3, 1, 0, 0}},
{"", {3, 3, 3, 0, 0}},
{"", {3, 1, 3, 1, 0}},
{"", {1, 3, 1, 3, 0}},
{"", {3, 3, 3, 3, 0}},
{"", {0, 0, 1, 1, 1}},
{"", {1, 0, 0, 1, 1}},
{"", {1, 1, 0, 0, 1}},
{"", {0, 1, 1, 0, 1}},
{"", {1, 0, 0, 0, 0}},
{"", {0, 1, 0, 0, 0}},
{"", {0, 0, 1, 0, 0}},
{"", {0, 0, 0, 1, 0}},
{"", {2, 0, 0, 0, 0}},
{"", {0, 2, 0, 0, 0}},
{"", {0, 0, 2, 0, 0}},
{"", {0, 0, 0, 2, 0}},
{"", {1, 0, 2, 0, 0}},
{"", {0, 1, 0, 2, 0}},
{"", {2, 0, 1, 0, 0}},
{"", {0, 2, 0, 1, 0}},
};
// clang-format on
template <class A, class B>
2022-03-31 08:17:43 +08:00
std::map<B, A> InvertMap(const std::map<A, B> input) {
2021-09-12 06:36:59 +08:00
std::map<B, A> output;
2022-03-31 08:17:43 +08:00
for (const auto& it : input) {
2021-09-12 06:36:59 +08:00
output[it.second] = it.first;
2022-03-31 08:17:43 +08:00
}
2021-09-12 06:36:59 +08:00
return output;
}
2022-03-31 08:17:43 +08:00
const std::map<TileEncoding, std::string> tile_encoding_inverse = // NOLINT
2021-09-12 06:36:59 +08:00
InvertMap(tile_encoding);
void UpgradeLeftRight(std::string& left, std::string& right) {
const auto it_left = tile_encoding.find(left);
2022-03-31 08:17:43 +08:00
if (it_left == tile_encoding.end()) {
2021-09-12 06:36:59 +08:00
return;
2022-03-31 08:17:43 +08:00
}
2021-09-12 06:36:59 +08:00
const auto it_right = tile_encoding.find(right);
2022-03-31 08:17:43 +08:00
if (it_right == tile_encoding.end()) {
2021-09-12 06:36:59 +08:00
return;
2022-03-31 08:17:43 +08:00
}
2021-09-12 06:36:59 +08:00
if (it_left->second.right == 0 && it_right->second.left != 0) {
TileEncoding encoding_left = it_left->second;
encoding_left.right = it_right->second.left;
const auto it_left_upgrade = tile_encoding_inverse.find(encoding_left);
2022-03-31 08:17:43 +08:00
if (it_left_upgrade != tile_encoding_inverse.end()) {
2021-09-12 06:36:59 +08:00
left = it_left_upgrade->second;
2022-03-31 08:17:43 +08:00
}
2021-09-12 06:36:59 +08:00
}
if (it_right->second.left == 0 && it_left->second.right != 0) {
TileEncoding encoding_right = it_right->second;
encoding_right.left = it_left->second.right;
const auto it_right_upgrade = tile_encoding_inverse.find(encoding_right);
2022-03-31 08:17:43 +08:00
if (it_right_upgrade != tile_encoding_inverse.end()) {
2021-09-12 06:36:59 +08:00
right = it_right_upgrade->second;
2022-03-31 08:17:43 +08:00
}
2021-09-12 06:36:59 +08:00
}
}
void UpgradeTopDown(std::string& top, std::string& down) {
const auto it_top = tile_encoding.find(top);
2022-03-31 08:17:43 +08:00
if (it_top == tile_encoding.end()) {
2021-09-12 06:36:59 +08:00
return;
2022-03-31 08:17:43 +08:00
}
2021-09-12 06:36:59 +08:00
const auto it_down = tile_encoding.find(down);
2022-03-31 08:17:43 +08:00
if (it_down == tile_encoding.end()) {
2021-09-12 06:36:59 +08:00
return;
2022-03-31 08:17:43 +08:00
}
2021-09-12 06:36:59 +08:00
if (it_top->second.down == 0 && it_down->second.top != 0) {
TileEncoding encoding_top = it_top->second;
encoding_top.down = it_down->second.top;
const auto it_top_down = tile_encoding_inverse.find(encoding_top);
2022-03-31 08:17:43 +08:00
if (it_top_down != tile_encoding_inverse.end()) {
2021-09-12 06:36:59 +08:00
top = it_top_down->second;
2022-03-31 08:17:43 +08:00
}
2021-09-12 06:36:59 +08:00
}
if (it_down->second.top == 0 && it_top->second.down != 0) {
TileEncoding encoding_down = it_down->second;
encoding_down.top = it_top->second.down;
const auto it_down_top = tile_encoding_inverse.find(encoding_down);
2022-03-31 08:17:43 +08:00
if (it_down_top != tile_encoding_inverse.end()) {
2021-09-12 06:36:59 +08:00
down = it_down_top->second;
2022-03-31 08:17:43 +08:00
}
2021-09-12 06:36:59 +08:00
}
}
bool ShouldAttemptAutoMerge(Pixel& pixel) {
return pixel.automerge && pixel.character.size() == 3;
}
} // namespace
2019-01-20 05:06:05 +08:00
2022-03-31 08:17:43 +08:00
bool Pixel::operator==(const Pixel& other) const {
return character == other.character && //
background_color == other.background_color && //
foreground_color == other.foreground_color && //
blink == other.blink && //
bold == other.bold && //
dim == other.dim && //
inverted == other.inverted && //
underlined == other.underlined && //
automerge == other.automerge; //
}
2020-05-25 07:34:13 +08:00
/// A fixed dimension.
2020-08-16 08:24:50 +08:00
/// @see Fit
/// @see Full
Dimensions Dimension::Fixed(int v) {
return {v, v};
2019-01-27 04:52:55 +08:00
}
2020-05-25 07:34:13 +08:00
/// Use the terminal dimensions.
/// @see Fixed
2020-08-16 08:24:50 +08:00
/// @see Fit
Dimensions Dimension::Full() {
return Terminal::Size();
2019-01-27 04:52:55 +08:00
}
// static
2020-05-25 07:34:13 +08:00
/// Create a screen with the given dimension along the x-axis and y-axis.
Screen Screen::Create(Dimensions width, Dimensions height) {
2019-01-27 04:52:55 +08:00
return Screen(width.dimx, height.dimy);
}
// static
2020-05-25 07:34:13 +08:00
/// Create a screen with the given dimension.
Screen Screen::Create(Dimensions dimension) {
2019-01-27 04:52:55 +08:00
return Screen(dimension.dimx, dimension.dimy);
}
Screen::Screen(int dimx, int dimy)
: stencil{0, dimx - 1, 0, dimy - 1},
2019-01-20 05:06:05 +08:00
dimx_(dimx),
dimy_(dimy),
pixels_(dimy, std::vector<Pixel>(dimx)) {
#if defined(_WIN32)
// The placement of this call is a bit weird, however we can assume that
// anybody who instantiates a Screen object eventually wants to output
// something to the console.
// As we require UTF8 for all input/output operations we will just switch to
// UTF8 encoding here
SetConsoleOutputCP(CP_UTF8);
SetConsoleCP(CP_UTF8);
WindowsEmulateVT100Terminal();
#endif
}
2020-05-25 07:34:13 +08:00
/// Produce a std::string that can be used to print the Screen on the terminal.
2021-03-22 05:54:39 +08:00
/// Don't forget to flush stdout. Alternatively, you can use Screen::Print();
std::string Screen::ToString() {
std::stringstream ss;
2018-10-12 15:23:37 +08:00
Pixel previous_pixel;
2020-10-17 03:26:59 +08:00
Pixel final_pixel;
2019-01-27 04:52:55 +08:00
for (int y = 0; y < dimy_; ++y) {
2020-10-17 03:26:59 +08:00
if (y != 0) {
UpdatePixelStyle(ss, previous_pixel, final_pixel);
ss << "\r\n";
2020-10-17 03:26:59 +08:00
}
bool previous_fullwidth = false;
for (const auto& pixel : pixels_[y]) {
if (!previous_fullwidth) {
UpdatePixelStyle(ss, previous_pixel, pixel);
ss << pixel.character;
}
previous_fullwidth = (string_width(pixel.character) == 2);
}
}
2019-01-03 05:33:59 +08:00
UpdatePixelStyle(ss, previous_pixel, final_pixel);
return ss.str();
}
2021-03-22 05:54:39 +08:00
void Screen::Print() {
std::cout << ToString() << '\0' << std::flush;
2021-03-22 05:54:39 +08:00
}
2020-05-25 07:34:13 +08:00
/// @brief Access a character a given position.
/// @param x The character position along the x-axis.
/// @param y The character position along the y-axis.
std::string& Screen::at(int x, int y) {
return PixelAt(x, y).character;
}
2020-05-25 07:34:13 +08:00
/// @brief Access a Pixel at a given position.
/// @param x The pixel position along the x-axis.
/// @param y The pixel position along the y-axis.
2019-01-27 04:52:55 +08:00
Pixel& Screen::PixelAt(int x, int y) {
2022-03-31 08:17:43 +08:00
return stencil.Contain(x, y) ? pixels_[y][x] : dev_null_pixel();
}
2020-05-25 07:34:13 +08:00
/// @brief Return a string to be printed in order to reset the cursor position
/// to the beginning of the screen.
///
/// ```cpp
/// std::string reset_position;
/// while(true) {
/// auto document = render();
/// auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document));
/// Render(screen, document);
/// std::cout << reset_position << screen.ToString() << std::flush;
/// reset_position = screen.ResetPosition();
///
/// using namespace std::chrono_literals;
/// std::this_thread::sleep_for(0.01s);
/// }
/// ```
///
/// @return The string to print in order to reset the cursor position to the
/// beginning.
2022-03-31 08:17:43 +08:00
std::string Screen::ResetPosition(bool clear) const {
std::stringstream ss;
2021-05-17 06:44:37 +08:00
if (clear) {
2022-03-31 08:17:43 +08:00
ss << "\r"; // MOVE_LEFT;
ss << "\x1b[2K"; // CLEAR_SCREEN;
2021-05-17 06:44:37 +08:00
for (int y = 1; y < dimy_; ++y) {
2022-03-31 08:17:43 +08:00
ss << "\x1B[1A"; // MOVE_UP;
ss << "\x1B[2K"; // CLEAR_LINE;
2021-05-17 06:44:37 +08:00
}
} else {
2022-03-31 08:17:43 +08:00
ss << "\r"; // MOVE_LEFT;
2021-05-17 06:44:37 +08:00
for (int y = 1; y < dimy_; ++y) {
2022-03-31 08:17:43 +08:00
ss << "\x1B[1A"; // MOVE_UP;
2021-05-17 06:44:37 +08:00
}
}
return ss.str();
}
2020-05-25 07:34:13 +08:00
/// @brief Clear all the pixel from the screen.
void Screen::Clear() {
for (auto& line : pixels_) {
for (auto& cell : line) {
cell = Pixel();
}
}
cursor_.x = dimx_ - 1;
cursor_.y = dimy_ - 1;
}
// clang-format off
2019-01-19 07:20:29 +08:00
void Screen::ApplyShader() {
// Merge box characters togethers.
for (int y = 1; y < dimy_; ++y) {
for (int x = 1; x < dimx_; ++x) {
// Box drawing character uses exactly 3 byte.
Pixel& cur = pixels_[y][x];
2022-03-31 08:17:43 +08:00
if (!ShouldAttemptAutoMerge(cur)) {
continue;
2022-03-31 08:17:43 +08:00
}
Pixel& left = pixels_[y][x-1];
Pixel& top = pixels_[y-1][x];
2022-03-31 08:17:43 +08:00
if (ShouldAttemptAutoMerge(left)) {
UpgradeLeftRight(left.character, cur.character);
2022-03-31 08:17:43 +08:00
}
if (ShouldAttemptAutoMerge(top)) {
UpgradeTopDown(top.character, cur.character);
2022-03-31 08:17:43 +08:00
}
2019-01-19 07:20:29 +08:00
}
}
}
2021-03-22 05:54:39 +08:00
// clang-format on
2019-01-19 07:20:29 +08:00
2020-02-12 04:44:55 +08:00
} // 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.