Extract common struct Dimensions from Terminal (#171)

- Convert Dimension to namespace to allow defining Fit method from dom.
- Use Dimensions extracted from Terminal as replacement struct.
- Convert Terminal to namespace as it only defines static members.
- Remove dom references from screen library (circular dependency).
This commit is contained in:
Tushar Maheshwari 2021-08-03 02:49:29 +05:30 committed by GitHub
parent 34d955e9ac
commit 49e8cc57d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 45 additions and 49 deletions

View File

@ -8,6 +8,7 @@
#include "ftxui/screen/box.hpp" #include "ftxui/screen/box.hpp"
#include "ftxui/screen/color.hpp" #include "ftxui/screen/color.hpp"
#include "ftxui/screen/screen.hpp" #include "ftxui/screen/screen.hpp"
#include "ftxui/screen/terminal.hpp"
namespace ftxui { namespace ftxui {
class Node; class Node;
@ -102,6 +103,10 @@ Element nothing(Element element);
// combinaison with dbox. // combinaison with dbox.
Element clear_under(Element element); Element clear_under(Element element);
namespace Dimension {
Dimensions Fit(Element&);
} // namespace Dimension
} // namespace ftxui } // namespace ftxui
// Make container able to take any number of children as input. // Make container able to take any number of children as input.

View File

@ -7,10 +7,9 @@
#include "ftxui/screen/box.hpp" #include "ftxui/screen/box.hpp"
#include "ftxui/screen/color.hpp" #include "ftxui/screen/color.hpp"
#include "ftxui/screen/terminal.hpp"
namespace ftxui { namespace ftxui {
class Node;
using Element = std::shared_ptr<Node>;
/// @brief A unicode character and its associated style. /// @brief A unicode character and its associated style.
/// @ingroup screen /// @ingroup screen
@ -41,14 +40,10 @@ struct Pixel {
/// @brief Define how the Screen's dimensions should look like. /// @brief Define how the Screen's dimensions should look like.
/// @ingroup screen /// @ingroup screen
struct Dimension { namespace Dimension {
static Dimension Fixed(int); Dimensions Fixed(int);
static Dimension Fit(Element&); Dimensions Full();
static Dimension Full(); } // namespace Dimension
int dimx;
int dimy;
};
/// @brief A rectangular grid of Pixel. /// @brief A rectangular grid of Pixel.
/// @ingroup screen /// @ingroup screen
@ -56,8 +51,8 @@ class Screen {
public: public:
// Constructors: // Constructors:
Screen(int dimx, int dimy); Screen(int dimx, int dimy);
static Screen Create(Dimension dimension); static Screen Create(Dimensions dimension);
static Screen Create(Dimension width, Dimension height); static Screen Create(Dimensions width, Dimensions height);
// Node write into the screen using Screen::at. // Node write into the screen using Screen::at.
wchar_t& at(int x, int y); wchar_t& at(int x, int y);

View File

@ -2,24 +2,23 @@
#define FTXUI_CORE_TERMINAL_HPP #define FTXUI_CORE_TERMINAL_HPP
namespace ftxui { namespace ftxui {
struct Dimensions {
class Terminal { int dimx;
public: int dimy;
struct Dimensions {
int dimx;
int dimy;
};
static Dimensions Size();
enum Color {
Palette1,
Palette16,
Palette256,
TrueColor,
};
static Color ColorSupport();
}; };
namespace Terminal {
Dimensions Size();
enum Color {
Palette1,
Palette16,
Palette256,
TrueColor,
};
Color ColorSupport();
} // namespace Terminal
} // namespace ftxui } // namespace ftxui
#endif /* end of include guard: FTXUI_CORE_TERMINAL_HPP */ #endif /* end of include guard: FTXUI_CORE_TERMINAL_HPP */

View File

@ -59,6 +59,16 @@ Element operator|(Element element, Decorator decorator) {
return decorator(std::move(element)); return decorator(std::move(element));
} }
/// The minimal dimension that will fit the given element.
/// @see Fixed
/// @see Full
Dimensions Dimension::Fit(Element& e) {
e->ComputeRequirement();
Dimensions size = Dimension::Full();
return {std::min(e->requirement().min_x, size.dimx),
std::min(e->requirement().min_y, size.dimy)};
}
} // namespace ftxui } // namespace ftxui
// Copyright 2020 Arthur Sonzogni. All rights reserved. // Copyright 2020 Arthur Sonzogni. All rights reserved.

View File

@ -2,8 +2,6 @@
#include <iostream> // for operator<<, basic_ostream, wstringstream, stringstream, flush, cout, ostream #include <iostream> // for operator<<, basic_ostream, wstringstream, stringstream, flush, cout, ostream
#include <sstream> // IWYU pragma: keep #include <sstream> // IWYU pragma: keep
#include "ftxui/dom/node.hpp" // for Element, Node
#include "ftxui/dom/requirement.hpp" // for Requirement
#include "ftxui/screen/screen.hpp" #include "ftxui/screen/screen.hpp"
#include "ftxui/screen/string.hpp" // for to_string, wchar_width #include "ftxui/screen/string.hpp" // for to_string, wchar_width
#include "ftxui/screen/terminal.hpp" // for Terminal::Dimensions, Terminal #include "ftxui/screen/terminal.hpp" // for Terminal::Dimensions, Terminal
@ -93,42 +91,31 @@ void UpdatePixelStyle(std::wstringstream& ss, Pixel& previous, Pixel& next) {
/// A fixed dimension. /// A fixed dimension.
/// @see Fit /// @see Fit
/// @see Full /// @see Full
Dimension Dimension::Fixed(int v) { Dimensions Dimension::Fixed(int v) {
return Dimension{v, v}; return {v, v};
}
/// The minimal dimension that will fit the given element.
/// @see Fixed
/// @see Full
Dimension Dimension::Fit(Element& 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)};
} }
/// Use the terminal dimensions. /// Use the terminal dimensions.
/// @see Fixed /// @see Fixed
/// @see Fit /// @see Fit
Dimension Dimension::Full() { Dimensions Dimension::Full() {
Terminal::Dimensions size = Terminal::Size(); return Terminal::Size();
return Dimension{size.dimx, size.dimy};
} }
// static // static
/// Create a screen with the given dimension along the x-axis and y-axis. /// Create a screen with the given dimension along the x-axis and y-axis.
Screen Screen::Create(Dimension width, Dimension height) { Screen Screen::Create(Dimensions width, Dimensions height) {
return Screen(width.dimx, height.dimy); return Screen(width.dimx, height.dimy);
} }
// static // static
/// Create a screen with the given dimension. /// Create a screen with the given dimension.
Screen Screen::Create(Dimension dimension) { Screen Screen::Create(Dimensions dimension) {
return Screen(dimension.dimx, dimension.dimy); return Screen(dimension.dimx, dimension.dimy);
} }
Screen::Screen(int dimx, int dimy) Screen::Screen(int dimx, int dimy)
: stencil({0, dimx - 1, 0, dimy - 1}), : stencil{0, dimx - 1, 0, dimy - 1},
dimx_(dimx), dimx_(dimx),
dimy_(dimy), dimy_(dimy),
pixels_(dimy, std::vector<Pixel>(dimx)) { pixels_(dimy, std::vector<Pixel>(dimx)) {

View File

@ -18,7 +18,7 @@
namespace ftxui { namespace ftxui {
Terminal::Dimensions Terminal::Size() { Dimensions Terminal::Size() {
#if defined(__EMSCRIPTEN__) #if defined(__EMSCRIPTEN__)
return Dimensions{140, 43}; return Dimensions{140, 43};
#elif defined(_WIN32) #elif defined(_WIN32)