FTXUI/src/ftxui/screen/screen.cpp

270 lines
7.7 KiB
C++
Raw Normal View History

2021-05-02 02:40:35 +08:00
#include <algorithm> // for min
#include <iostream> // for operator<<, basic_ostream, wstringstream, stringstream, flush, cout, ostream
#include <sstream> // IWYU pragma: keep
2020-03-23 05:32:44 +08:00
2021-05-02 02:40:35 +08:00
#include "ftxui/dom/node.hpp" // for Element, Node
#include "ftxui/dom/requirement.hpp" // for Requirement
#include "ftxui/screen/screen.hpp"
#include "ftxui/screen/string.hpp" // for to_string, wchar_width
#include "ftxui/screen/terminal.hpp" // for Terminal::Dimensions, Terminal
#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 {
2020-02-12 04:44:55 +08:00
static const wchar_t* BOLD_SET = L"\x1B[1m";
static const wchar_t* BOLD_RESET = L"\x1B[22m"; // Can't use 21 here.
2020-02-12 04:44:55 +08:00
static const wchar_t* DIM_SET = L"\x1B[2m";
static const wchar_t* DIM_RESET = L"\x1B[22m";
2020-02-12 04:44:55 +08:00
static const wchar_t* UNDERLINED_SET = L"\x1B[4m";
static const wchar_t* UNDERLINED_RESET = L"\x1B[24m";
2020-02-12 04:44:55 +08:00
static const wchar_t* BLINK_SET = L"\x1B[5m";
static const wchar_t* BLINK_RESET = L"\x1B[25m";
2020-02-12 04:44:55 +08:00
static const wchar_t* INVERTED_SET = L"\x1B[7m";
static const wchar_t* INVERTED_RESET = L"\x1B[27m";
2019-01-19 07:20:29 +08:00
static const char* MOVE_LEFT = "\r";
2020-02-12 04:44:55 +08:00
static const char* MOVE_UP = "\x1B[1A";
2019-01-19 07:20:29 +08:00
2019-01-20 05:06:05 +08:00
bool In(const Box& stencil, int x, int y) {
return stencil.x_min <= x && x <= stencil.x_max && //
stencil.y_min <= y && y <= stencil.y_max;
}
Pixel dev_null_pixel;
#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
2020-08-16 08:24:50 +08:00
void UpdatePixelStyle(std::wstringstream& ss, Pixel& previous, Pixel& next) {
if (next.bold != previous.bold)
ss << (next.bold ? BOLD_SET : BOLD_RESET);
if (next.dim != previous.dim)
ss << (next.dim ? DIM_SET : DIM_RESET);
if (next.underlined != previous.underlined)
ss << (next.underlined ? UNDERLINED_SET : UNDERLINED_RESET);
if (next.blink != previous.blink)
ss << (next.blink ? BLINK_SET : BLINK_RESET);
if (next.inverted != previous.inverted)
ss << (next.inverted ? INVERTED_SET : INVERTED_RESET);
if (next.foreground_color != previous.foreground_color ||
next.background_color != previous.background_color) {
ss << L"\x1B[" + next.foreground_color.Print(false) + L"m";
ss << L"\x1B[" + next.background_color.Print(true) + L"m";
2020-08-16 08:24:50 +08:00
}
previous = next;
}
} // namespace
2019-01-20 05:06:05 +08:00
2020-05-25 07:34:13 +08:00
/// A fixed dimension.
2020-08-16 08:24:50 +08:00
/// @see Fit
/// @see Full
2019-01-27 04:52:55 +08:00
Dimension Dimension::Fixed(int v) {
return Dimension{v, v};
}
2020-05-25 07:34:13 +08:00
/// The minimal dimension that will fit the given element.
/// @see Fixed
2020-08-16 08:24:50 +08:00
/// @see Full
2020-05-25 07:34:13 +08:00
Dimension Dimension::Fit(Element& e) {
2019-01-27 04:52:55 +08:00
e->ComputeRequirement();
Terminal::Dimensions size = Terminal::Size();
2020-06-01 22:13:29 +08:00
return Dimension{std::min(e->requirement().min_x, size.dimx),
std::min(e->requirement().min_y, size.dimy)};
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
2019-01-27 04:52:55 +08:00
Dimension Dimension::Full() {
Terminal::Dimensions size = Terminal::Size();
return Dimension{size.dimx, size.dimy};
}
// static
2020-05-25 07:34:13 +08:00
/// Create a screen with the given dimension along the x-axis and y-axis.
2019-01-27 04:52:55 +08:00
Screen Screen::Create(Dimension width, Dimension height) {
return Screen(width.dimx, height.dimy);
}
// static
2020-05-25 07:34:13 +08:00
/// Create a screen with the given dimension.
2019-01-27 04:52:55 +08:00
Screen Screen::Create(Dimension dimension) {
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::wstringstream 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);
2021-03-22 05:54:39 +08:00
ss << L"\r\n";
2020-10-17 03:26:59 +08:00
}
for (int x = 0; x < dimx_;) {
auto& pixel = pixels_[y][x];
wchar_t c = pixel.character;
UpdatePixelStyle(ss, previous_pixel, pixel);
auto width = wchar_width(c);
if (width <= 0) {
2021-05-02 02:40:35 +08:00
// Avoid an infinite loop for non-printable characters
c = L' ';
width = 1;
}
ss << c;
x += width;
}
}
2019-01-03 05:33:59 +08:00
UpdatePixelStyle(ss, previous_pixel, final_pixel);
return to_string(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.
2019-01-27 04:52:55 +08:00
wchar_t& 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) {
2019-01-20 05:06:05 +08:00
return In(stencil, 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.
std::string Screen::ResetPosition() {
std::stringstream ss;
ss << MOVE_LEFT;
2019-01-27 04:52:55 +08:00
for (int y = 1; y < dimy_; ++y) {
ss << MOVE_UP;
}
return ss.str();
}
2020-05-25 07:34:13 +08:00
/// @brief Clear all the pixel from the screen.
void Screen::Clear() {
pixels_ = std::vector<std::vector<Pixel>>(dimy_,
std::vector<Pixel>(dimx_, 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) {
2019-01-19 07:20:29 +08:00
wchar_t& left = at(x - 1, y);
wchar_t& top = at(x, y - 1);
wchar_t& cur = at(x, y);
// Left vs current
if (cur == U'' && left == U'') cur = U'';
if (cur == U'' && left == U'') left = U'';
if (cur == U'' && left == U'') cur = U'';
if (cur == U'' && left == U'') left = U'';
2019-01-19 07:20:29 +08:00
// Top vs current
if (cur == U'' && top == U'') cur = U'';
if (cur == U'' && top == U'') top = U'';
if (cur == U'' && top == U'') cur = U'';
if (cur == U'' && top == U'') top = U'';
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.