2019-01-12 22:00:08 +08:00
|
|
|
#include "ftxui/screen/screen.hpp"
|
2019-01-27 04:52:55 +08:00
|
|
|
#include "ftxui/dom/node.hpp"
|
2019-01-12 22:00:08 +08:00
|
|
|
#include "ftxui/screen/string.hpp"
|
|
|
|
#include "ftxui/screen/terminal.hpp"
|
2018-10-10 01:06:03 +08:00
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
|
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";
|
|
|
|
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;
|
|
|
|
|
2019-06-23 23:47:33 +08:00
|
|
|
} // namespace
|
2019-01-20 05:06:05 +08:00
|
|
|
|
2019-01-27 04:52:55 +08:00
|
|
|
Dimension Dimension::Fixed(int v) {
|
|
|
|
return Dimension{v, v};
|
|
|
|
}
|
|
|
|
|
|
|
|
Dimension Dimension::Fit(std::unique_ptr<Node>& e) {
|
|
|
|
e->ComputeRequirement();
|
|
|
|
Terminal::Dimensions size = Terminal::Size();
|
|
|
|
return Dimension{std::min(e->requirement().min.x, size.dimx),
|
|
|
|
std::min(e->requirement().min.y, size.dimy)};
|
|
|
|
}
|
|
|
|
|
|
|
|
Dimension Dimension::Full() {
|
|
|
|
Terminal::Dimensions size = Terminal::Size();
|
|
|
|
return Dimension{size.dimx, size.dimy};
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
Screen Screen::Create(Dimension width, Dimension height) {
|
|
|
|
return Screen(width.dimx, height.dimy);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
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)) {}
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2019-01-03 05:33:59 +08:00
|
|
|
void UpdatePixelStyle(std::wstringstream& ss, Pixel& previous, Pixel& next) {
|
|
|
|
if (next.bold != previous.bold)
|
2019-01-07 00:10:35 +08:00
|
|
|
ss << (next.bold ? BOLD_SET : BOLD_RESET);
|
|
|
|
|
2019-01-03 05:33:59 +08:00
|
|
|
if (next.dim != previous.dim)
|
2019-01-07 00:10:35 +08:00
|
|
|
ss << (next.dim ? DIM_SET : DIM_RESET);
|
|
|
|
|
2019-01-05 09:03:49 +08:00
|
|
|
if (next.underlined != previous.underlined)
|
2019-01-07 00:10:35 +08:00
|
|
|
ss << (next.underlined ? UNDERLINED_SET : UNDERLINED_RESET);
|
|
|
|
|
2019-01-03 05:33:59 +08:00
|
|
|
if (next.blink != previous.blink)
|
2019-01-07 00:10:35 +08:00
|
|
|
ss << (next.blink ? BLINK_SET : BLINK_RESET);
|
|
|
|
|
2019-01-05 09:03:49 +08:00
|
|
|
if (next.inverted != previous.inverted)
|
2019-01-07 00:10:35 +08:00
|
|
|
ss << (next.inverted ? INVERTED_SET : INVERTED_RESET);
|
|
|
|
|
2019-01-05 09:03:49 +08:00
|
|
|
if (next.foreground_color != previous.foreground_color ||
|
|
|
|
next.background_color != previous.background_color) {
|
2020-02-12 04:44:55 +08:00
|
|
|
ss << L"\x1B[" +
|
|
|
|
to_wstring(std::to_string((uint8_t)next.foreground_color)) + L"m";
|
|
|
|
ss << L"\x1B[" +
|
2019-06-23 23:47:33 +08:00
|
|
|
to_wstring(std::to_string(10 + (uint8_t)next.background_color)) +
|
|
|
|
L"m";
|
2019-01-03 05:33:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
previous = next;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2019-01-27 04:52:55 +08:00
|
|
|
for (int y = 0; y < dimy_; ++y) {
|
2019-01-07 06:24:58 +08:00
|
|
|
if (y != 0)
|
|
|
|
ss << '\n';
|
2019-06-25 05:39:37 +08:00
|
|
|
for (int x = 0; x < dimx_;) {
|
|
|
|
auto& pixel = pixels_[y][x];
|
|
|
|
wchar_t c = pixel.character;
|
|
|
|
UpdatePixelStyle(ss, previous_pixel, pixel);
|
|
|
|
ss << c;
|
|
|
|
x += wchar_width(c);
|
2018-10-10 01:06:03 +08:00
|
|
|
}
|
|
|
|
}
|
2019-01-03 05:33:59 +08:00
|
|
|
|
2019-01-07 06:24:58 +08:00
|
|
|
Pixel final_pixel;
|
|
|
|
UpdatePixelStyle(ss, previous_pixel, final_pixel);
|
|
|
|
|
2018-10-10 01:06:03 +08:00
|
|
|
return to_string(ss.str());
|
|
|
|
}
|
|
|
|
|
2019-01-27 04:52:55 +08:00
|
|
|
wchar_t& Screen::at(int x, int y) {
|
2019-06-23 23:47:33 +08:00
|
|
|
return PixelAt(x, y).character;
|
2018-10-10 01:06:03 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
std::string Screen::ResetPosition() {
|
|
|
|
std::stringstream ss;
|
2019-01-19 07:20:29 +08:00
|
|
|
ss << MOVE_LEFT << CLEAR_LINE;
|
2019-01-27 04:52:55 +08:00
|
|
|
for (int y = 1; y < dimy_; ++y) {
|
2019-01-19 07:20:29 +08:00
|
|
|
ss << MOVE_UP << CLEAR_LINE;
|
2018-10-10 01:06:03 +08:00
|
|
|
}
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|