Refactor and bug fix: Wrong vbox height.

This commit is contained in:
Arthur Sonzogni 2019-01-06 23:24:58 +01:00
parent e6614d6363
commit cf63aefa02
4 changed files with 14 additions and 21 deletions

View File

@ -25,8 +25,7 @@ class ScreenInteractive : public ftxui::screen::Screen {
class Delegate; class Delegate;
std::unique_ptr<Delegate> delegate_; std::unique_ptr<Delegate> delegate_;
void Clear(); void PrepareDraw();
void Draw();
bool quit_ = false; bool quit_ = false;
enum class Dimension { enum class Dimension {

View File

@ -127,21 +127,20 @@ void ScreenInteractive::Loop() {
terminal_configuration_new.c_lflag &= ~ECHO; terminal_configuration_new.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &terminal_configuration_new); tcsetattr(STDIN_FILENO, TCSANOW, &terminal_configuration_new);
Draw(); std::string reset_position;
while (!quit_) { while (!quit_) {
delegate_->OnEvent(GetEvent()); PrepareDraw();
std::cout << reset_position << ToString() << std::flush;
reset_position = ResetPosition();
Clear(); Clear();
Draw(); delegate_->OnEvent(GetEvent());
} }
while (!quit_)
;
// std::cout << std::endl;
// Restore the old terminal configuration. // Restore the old terminal configuration.
tcsetattr(STDIN_FILENO, TCSANOW, &terminal_configuration_old); tcsetattr(STDIN_FILENO, TCSANOW, &terminal_configuration_old);
} }
void ScreenInteractive::Draw() { void ScreenInteractive::PrepareDraw() {
auto document = delegate_->component()->Render(); auto document = delegate_->component()->Render();
size_t dimx; size_t dimx;
size_t dimy; size_t dimy;
@ -168,12 +167,6 @@ void ScreenInteractive::Draw() {
} }
Render(*this, document.get()); Render(*this, document.get());
std::cout << ToString() << std::flush;
}
void ScreenInteractive::Clear() {
std::cout << ResetPosition();
Screen::Clear();
} }
component::Delegate* ScreenInteractive::delegate() { component::Delegate* ScreenInteractive::delegate() {

View File

@ -51,7 +51,7 @@ class VBox : public Node {
remaining_flex -= child->requirement().flex.y; remaining_flex -= child->requirement().flex.y;
child_box.bottom += added_space; child_box.bottom += added_space;
} }
child_box.bottom = std::min(child_box.bottom, box.bottom-1); child_box.bottom = std::min(child_box.bottom, box.bottom);
child->SetBox(child_box); child->SetBox(child_box);
y = child_box.bottom + 1; y = child_box.bottom + 1;

View File

@ -56,17 +56,18 @@ std::string Screen::ToString() {
Pixel previous_pixel; Pixel previous_pixel;
for (size_t y = 0; y < dimy_; ++y) { for (size_t y = 0; y < dimy_; ++y) {
if (y != 0)
ss << '\n';
for (size_t x = 0; x < dimx_; ++x) { for (size_t x = 0; x < dimx_; ++x) {
UpdatePixelStyle(ss, previous_pixel, pixels_[y][x]); UpdatePixelStyle(ss, previous_pixel, pixels_[y][x]);
ss << pixels_[y][x].character; ss << pixels_[y][x].character;
} }
if (y + 1 < dimy_)
ss << '\n';
Pixel final_pixel;
UpdatePixelStyle(ss, previous_pixel, final_pixel);
} }
Pixel final_pixel;
UpdatePixelStyle(ss, previous_pixel, final_pixel);
return to_string(ss.str()); return to_string(ss.str());
} }
@ -93,10 +94,10 @@ Screen Screen::TerminalOutput(std::unique_ptr<dom::Node>& element) {
std::string Screen::ResetPosition() { std::string Screen::ResetPosition() {
std::stringstream ss; std::stringstream ss;
//ss << '\r';
for (size_t y = 1; y < dimy_; ++y) { for (size_t y = 1; y < dimy_; ++y) {
ss << "\e[2K\r\e[1A"; ss << "\e[2K\r\e[1A";
} }
ss << '\r';
return ss.str(); return ss.str();
} }