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
|
2018-10-10 01:06:03 +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
|
|
|
|
#ifndef NOMINMAX
|
|
|
|
#define NOMINMAX
|
|
|
|
#endif
|
|
|
|
#include <Windows.h>
|
2020-03-22 18:29:33 +08:00
|
|
|
#endif
|
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
namespace ftxui {
|
2019-01-07 00:10:35 +08:00
|
|
|
|
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.
|
2019-01-07 00:10:35 +08:00
|
|
|
|
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";
|
2019-01-07 00:10:35 +08:00
|
|
|
|
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";
|
2019-01-07 00:10:35 +08:00
|
|
|
|
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";
|
2019-01-07 00:10:35 +08:00
|
|
|
|
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";
|
2018-10-10 01:06:03 +08:00
|
|
|
|
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";
|
2021-05-17 06:44:37 +08:00
|
|
|
static const char* CLEAR_LINE = "\x1B[2K";
|
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;
|
|
|
|
|
2020-07-17 03:18:09 +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
|
|
|
|
|
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) {
|
2020-09-02 17:32:22 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-06-23 23:47:33 +08:00
|
|
|
} // 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.
|
2020-09-02 17:32:22 +08:00
|
|
|
/// @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.
|
2020-09-02 17:32:22 +08:00
|
|
|
/// @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),
|
2020-03-22 18:29:33 +08:00
|
|
|
pixels_(dimy, std::vector<Pixel>(dimx)) {
|
2020-03-25 15:54:03 +08:00
|
|
|
#if defined(_WIN32)
|
2020-03-22 18:29:33 +08:00
|
|
|
// 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);
|
2020-07-17 03:18:09 +08:00
|
|
|
WindowsEmulateVT100Terminal();
|
2020-03-22 18:29:33 +08:00
|
|
|
#endif
|
|
|
|
}
|
2018-10-10 01:06:03 +08:00
|
|
|
|
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();
|
2018-10-10 01:06:03 +08:00
|
|
|
std::string Screen::ToString() {
|
|
|
|
std::wstringstream ss;
|
2018-10-12 15:23:37 +08:00
|
|
|
|
2018-10-10 01:06:03 +08:00
|
|
|
Pixel previous_pixel;
|
2020-10-17 03:26:59 +08:00
|
|
|
Pixel final_pixel;
|
2018-10-10 01:06:03 +08:00
|
|
|
|
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
|
|
|
}
|
2019-06-25 05:39:37 +08:00
|
|
|
for (int x = 0; x < dimx_;) {
|
|
|
|
auto& pixel = pixels_[y][x];
|
|
|
|
UpdatePixelStyle(ss, previous_pixel, pixel);
|
2021-02-04 16:32:05 +08:00
|
|
|
|
2021-06-26 07:32:27 +08:00
|
|
|
int x_inc = 0;
|
|
|
|
for (auto& c : pixel.character) {
|
|
|
|
ss << c;
|
|
|
|
x_inc += wchar_width(c);
|
2021-02-04 16:32:05 +08:00
|
|
|
}
|
2021-06-26 07:32:27 +08:00
|
|
|
x += std::max(x_inc, 1);
|
2018-10-10 01:06:03 +08:00
|
|
|
}
|
|
|
|
}
|
2019-01-03 05:33:59 +08:00
|
|
|
|
2019-01-07 06:24:58 +08:00
|
|
|
UpdatePixelStyle(ss, previous_pixel, final_pixel);
|
|
|
|
|
2018-10-10 01:06:03 +08:00
|
|
|
return to_string(ss.str());
|
|
|
|
}
|
|
|
|
|
2021-03-22 05:54:39 +08:00
|
|
|
void Screen::Print() {
|
2021-04-25 22:58:16 +08:00
|
|
|
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) {
|
2021-06-26 07:32:27 +08:00
|
|
|
return PixelAt(x, y).character[0];
|
2018-10-10 01:06:03 +08:00
|
|
|
}
|
|
|
|
|
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;
|
2018-10-10 01:06:03 +08:00
|
|
|
}
|
|
|
|
|
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.
|
2021-05-17 06:44:37 +08:00
|
|
|
std::string Screen::ResetPosition(bool clear) {
|
2018-10-10 01:06:03 +08:00
|
|
|
std::stringstream ss;
|
2021-05-17 06:44:37 +08:00
|
|
|
if (clear) {
|
|
|
|
ss << MOVE_LEFT << CLEAR_LINE;
|
|
|
|
for (int y = 1; y < dimy_; ++y) {
|
|
|
|
ss << MOVE_UP << CLEAR_LINE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ss << MOVE_LEFT;
|
|
|
|
for (int y = 1; y < dimy_; ++y) {
|
|
|
|
ss << MOVE_UP;
|
|
|
|
}
|
2018-10-10 01:06:03 +08:00
|
|
|
}
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
2020-05-25 07:34:13 +08:00
|
|
|
/// @brief Clear all the pixel from the screen.
|
2018-10-10 01:06:03 +08:00
|
|
|
void Screen::Clear() {
|
|
|
|
pixels_ = std::vector<std::vector<Pixel>>(dimy_,
|
|
|
|
std::vector<Pixel>(dimx_, Pixel()));
|
2019-06-30 00:52:58 +08:00
|
|
|
cursor_.x = dimx_ - 1;
|
|
|
|
cursor_.y = dimy_ - 1;
|
2018-10-10 01:06:03 +08:00
|
|
|
}
|
|
|
|
|
2019-06-30 00:52:58 +08:00
|
|
|
// clang-format off
|
2019-01-19 07:20:29 +08:00
|
|
|
void Screen::ApplyShader() {
|
|
|
|
// Merge box characters togethers.
|
2019-06-23 23:47:33 +08:00
|
|
|
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
|
2019-06-30 00:52:58 +08:00
|
|
|
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
|
2019-06-30 00:52:58 +08:00
|
|
|
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
|
|
|
|
2019-06-30 00:52:58 +08:00
|
|
|
// clang-format on
|
2019-01-19 07:20:29 +08:00
|
|
|
|
2020-02-12 04:44:55 +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.
|