mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2024-11-22 18:59:59 +08:00
Improve const correctness in the Screen class (#701)
- Add a const variant to the accessor functions - Make Print and ToString functions const This fixes: https://github.com/ArthurSonzogni/FTXUI/issues/700
This commit is contained in:
parent
43240a5fd4
commit
b50f1fffc8
@ -66,13 +66,18 @@ class Screen {
|
|||||||
static Screen Create(Dimensions dimension);
|
static Screen Create(Dimensions dimension);
|
||||||
static Screen Create(Dimensions width, Dimensions height);
|
static Screen Create(Dimensions width, Dimensions height);
|
||||||
|
|
||||||
// Node write into the screen using Screen::at.
|
// Access a character in the grid at a given position.
|
||||||
std::string& at(int x, int y);
|
std::string& at(int x, int y);
|
||||||
Pixel& PixelAt(int x, int y);
|
const std::string& at(int x, int y) const;
|
||||||
|
|
||||||
// Convert the screen into a printable string in the terminal.
|
// Access a cell (Pixel) in the grid at a given position.
|
||||||
std::string ToString();
|
Pixel& PixelAt(int x, int y);
|
||||||
void Print();
|
const Pixel& PixelAt(int x, int y) const;
|
||||||
|
|
||||||
|
std::string ToString() const;
|
||||||
|
|
||||||
|
// Print the Screen on to the terminal.
|
||||||
|
void Print() const;
|
||||||
|
|
||||||
// Get screen dimensions.
|
// Get screen dimensions.
|
||||||
int dimx() const { return dimx_; }
|
int dimx() const { return dimx_; }
|
||||||
@ -81,7 +86,7 @@ class Screen {
|
|||||||
// Move the terminal cursor n-lines up with n = dimy().
|
// Move the terminal cursor n-lines up with n = dimy().
|
||||||
std::string ResetPosition(bool clear = false) const;
|
std::string ResetPosition(bool clear = false) const;
|
||||||
|
|
||||||
// Fill with space.
|
// Fill the screen with space.
|
||||||
void Clear();
|
void Clear();
|
||||||
|
|
||||||
void ApplyShader();
|
void ApplyShader();
|
||||||
|
@ -428,9 +428,11 @@ Screen::Screen(int dimx, int dimy)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Produce a std::string that can be used to print the Screen on the terminal.
|
/// Produce a std::string that can be used to print the Screen on the
|
||||||
/// Don't forget to flush stdout. Alternatively, you can use Screen::Print();
|
/// terminal.
|
||||||
std::string Screen::ToString() {
|
/// @note Don't forget to flush stdout. Alternatively, you can use
|
||||||
|
/// Screen::Print();
|
||||||
|
std::string Screen::ToString() const {
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
|
|
||||||
Pixel previous_pixel;
|
Pixel previous_pixel;
|
||||||
@ -456,24 +458,39 @@ std::string Screen::ToString() {
|
|||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Screen::Print() {
|
// Print the Screen to the terminal.
|
||||||
|
void Screen::Print() const {
|
||||||
std::cout << ToString() << '\0' << std::flush;
|
std::cout << ToString() << '\0' << std::flush;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief Access a character a given position.
|
/// @brief Access a character in a cell at a given position.
|
||||||
/// @param x The character position along the x-axis.
|
/// @param x The cell position along the x-axis.
|
||||||
/// @param y The character position along the y-axis.
|
/// @param y The cell position along the y-axis.
|
||||||
std::string& Screen::at(int x, int y) {
|
std::string& Screen::at(int x, int y) {
|
||||||
return PixelAt(x, y).character;
|
return PixelAt(x, y).character;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief Access a Pixel at a given position.
|
/// @brief Access a character in a cell at a given position.
|
||||||
/// @param x The pixel position along the x-axis.
|
/// @param x The cell position along the x-axis.
|
||||||
/// @param y The pixel position along the y-axis.
|
/// @param y The cell position along the y-axis.
|
||||||
|
const std::string& Screen::at(int x, int y) const {
|
||||||
|
return PixelAt(x, y).character;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// @brief Access a cell (Pixel) at a given position.
|
||||||
|
/// @param x The cell position along the x-axis.
|
||||||
|
/// @param y The cell position along the y-axis.
|
||||||
Pixel& Screen::PixelAt(int x, int y) {
|
Pixel& Screen::PixelAt(int x, int y) {
|
||||||
return stencil.Contain(x, y) ? pixels_[y][x] : dev_null_pixel();
|
return stencil.Contain(x, y) ? pixels_[y][x] : dev_null_pixel();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @brief Access a cell (Pixel) at a given position.
|
||||||
|
/// @param x The cell position along the x-axis.
|
||||||
|
/// @param y The cell position along the y-axis.
|
||||||
|
const Pixel& Screen::PixelAt(int x, int y) const {
|
||||||
|
return stencil.Contain(x, y) ? pixels_[y][x] : dev_null_pixel();
|
||||||
|
}
|
||||||
|
|
||||||
/// @brief Return a string to be printed in order to reset the cursor position
|
/// @brief Return a string to be printed in order to reset the cursor position
|
||||||
/// to the beginning of the screen.
|
/// to the beginning of the screen.
|
||||||
///
|
///
|
||||||
|
Loading…
Reference in New Issue
Block a user