2023-08-19 19:56:36 +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.
|
2022-03-31 08:17:43 +08:00
|
|
|
#ifndef FTXUI_SCREEN_SCREEN_HPP
|
|
|
|
#define FTXUI_SCREEN_SCREEN_HPP
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2023-06-05 03:06:19 +08:00
|
|
|
#include <cstdint> // for uint8_t
|
2020-03-24 04:26:00 +08:00
|
|
|
#include <memory>
|
2023-06-05 03:06:19 +08:00
|
|
|
#include <string> // for string, basic_string, allocator
|
2021-08-07 02:32:33 +08:00
|
|
|
#include <vector> // for vector
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2021-08-07 02:32:33 +08:00
|
|
|
#include "ftxui/screen/box.hpp" // for Box
|
|
|
|
#include "ftxui/screen/color.hpp" // for Color, Color::Default
|
|
|
|
#include "ftxui/screen/terminal.hpp" // for Dimensions
|
2018-10-12 15:23:37 +08:00
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
namespace ftxui {
|
2019-01-07 00:10:35 +08:00
|
|
|
|
2020-05-25 07:34:13 +08:00
|
|
|
/// @brief A unicode character and its associated style.
|
|
|
|
/// @ingroup screen
|
2018-10-10 01:06:03 +08:00
|
|
|
struct Pixel {
|
2023-08-08 06:46:51 +08:00
|
|
|
Pixel()
|
|
|
|
: blink(false),
|
|
|
|
bold(false),
|
|
|
|
dim(false),
|
|
|
|
inverted(false),
|
|
|
|
underlined(false),
|
|
|
|
underlined_double(false),
|
|
|
|
strikethrough(false),
|
|
|
|
automerge(false) {}
|
2021-06-26 07:32:27 +08:00
|
|
|
|
|
|
|
// A bit field representing the style:
|
2021-06-13 02:33:02 +08:00
|
|
|
bool blink : 1;
|
|
|
|
bool bold : 1;
|
|
|
|
bool dim : 1;
|
|
|
|
bool inverted : 1;
|
|
|
|
bool underlined : 1;
|
2023-01-22 18:02:27 +08:00
|
|
|
bool underlined_double : 1;
|
|
|
|
bool strikethrough : 1;
|
2022-01-22 22:38:01 +08:00
|
|
|
bool automerge : 1;
|
2021-06-13 02:33:02 +08:00
|
|
|
|
2023-08-08 06:46:51 +08:00
|
|
|
// The hyperlink associated with the pixel.
|
|
|
|
// 0 is the default value, meaning no hyperlink.
|
|
|
|
uint8_t hyperlink = 0;
|
|
|
|
|
|
|
|
// The graphemes stored into the pixel. To support combining characters,
|
|
|
|
// like: a⃦, this can potentially contain multiple codepoints.
|
|
|
|
std::string character = " ";
|
|
|
|
|
|
|
|
// Colors:
|
|
|
|
Color background_color = Color::Default;
|
|
|
|
Color foreground_color = Color::Default;
|
2018-10-10 01:06:03 +08:00
|
|
|
};
|
|
|
|
|
2020-05-25 07:34:13 +08:00
|
|
|
/// @brief Define how the Screen's dimensions should look like.
|
|
|
|
/// @ingroup screen
|
2021-08-03 05:19:29 +08:00
|
|
|
namespace Dimension {
|
|
|
|
Dimensions Fixed(int);
|
|
|
|
Dimensions Full();
|
|
|
|
} // namespace Dimension
|
2019-01-27 04:52:55 +08:00
|
|
|
|
2020-05-25 07:34:13 +08:00
|
|
|
/// @brief A rectangular grid of Pixel.
|
|
|
|
/// @ingroup screen
|
2018-10-10 01:06:03 +08:00
|
|
|
class Screen {
|
|
|
|
public:
|
2020-05-25 07:34:13 +08:00
|
|
|
// Constructors:
|
2019-01-27 04:52:55 +08:00
|
|
|
Screen(int dimx, int dimy);
|
2021-08-03 05:19:29 +08:00
|
|
|
static Screen Create(Dimensions dimension);
|
|
|
|
static Screen Create(Dimensions width, Dimensions height);
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2023-07-26 06:41:16 +08:00
|
|
|
// Access a character in the grid at a given position.
|
2021-08-09 05:25:20 +08:00
|
|
|
std::string& at(int x, int y);
|
2023-07-26 06:41:16 +08:00
|
|
|
const std::string& at(int x, int y) const;
|
|
|
|
|
|
|
|
// Access a cell (Pixel) in the grid at a given position.
|
2019-01-27 04:52:55 +08:00
|
|
|
Pixel& PixelAt(int x, int y);
|
2023-07-26 06:41:16 +08:00
|
|
|
const Pixel& PixelAt(int x, int y) const;
|
|
|
|
|
|
|
|
std::string ToString() const;
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2023-07-26 06:41:16 +08:00
|
|
|
// Print the Screen on to the terminal.
|
|
|
|
void Print() const;
|
2018-10-10 01:06:03 +08:00
|
|
|
|
|
|
|
// Get screen dimensions.
|
2022-01-19 02:48:58 +08:00
|
|
|
int dimx() const { return dimx_; }
|
|
|
|
int dimy() const { return dimy_; }
|
2018-10-10 01:06:03 +08:00
|
|
|
|
|
|
|
// Move the terminal cursor n-lines up with n = dimy().
|
2022-03-31 08:17:43 +08:00
|
|
|
std::string ResetPosition(bool clear = false) const;
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2023-07-26 06:41:16 +08:00
|
|
|
// Fill the screen with space.
|
2018-10-10 01:06:03 +08:00
|
|
|
void Clear();
|
|
|
|
|
2019-01-19 07:20:29 +08:00
|
|
|
void ApplyShader();
|
|
|
|
|
2019-06-30 00:52:58 +08:00
|
|
|
struct Cursor {
|
2019-07-01 06:43:00 +08:00
|
|
|
int x = 0;
|
|
|
|
int y = 0;
|
2022-11-11 21:09:53 +08:00
|
|
|
|
|
|
|
enum Shape {
|
|
|
|
Hidden = 0,
|
|
|
|
BlockBlinking = 1,
|
|
|
|
Block = 2,
|
|
|
|
UnderlineBlinking = 3,
|
|
|
|
Underline = 4,
|
2022-12-28 20:17:56 +08:00
|
|
|
BarBlinking = 5,
|
|
|
|
Bar = 6,
|
2022-11-11 21:09:53 +08:00
|
|
|
};
|
|
|
|
Shape shape;
|
2019-06-30 00:52:58 +08:00
|
|
|
};
|
|
|
|
Cursor cursor() const { return cursor_; }
|
|
|
|
void SetCursor(Cursor cursor) { cursor_ = cursor; }
|
|
|
|
|
2023-06-05 03:06:19 +08:00
|
|
|
// Store an hyperlink in the screen. Return the id of the hyperlink. The id is
|
|
|
|
// used to identify the hyperlink when the user click on it.
|
2023-06-25 23:22:05 +08:00
|
|
|
uint8_t RegisterHyperlink(const std::string& link);
|
2023-06-05 03:06:19 +08:00
|
|
|
const std::string& Hyperlink(uint8_t id) const;
|
|
|
|
|
2022-03-31 08:17:43 +08:00
|
|
|
Box stencil;
|
|
|
|
|
2019-01-05 09:03:49 +08:00
|
|
|
protected:
|
2019-01-27 04:52:55 +08:00
|
|
|
int dimx_;
|
|
|
|
int dimy_;
|
2018-10-10 01:06:03 +08:00
|
|
|
std::vector<std::vector<Pixel>> pixels_;
|
2019-06-30 00:52:58 +08:00
|
|
|
Cursor cursor_;
|
2023-06-05 03:06:19 +08:00
|
|
|
std::vector<std::string> hyperlinks_ = {""};
|
2018-10-10 01:06:03 +08:00
|
|
|
};
|
|
|
|
|
2020-02-12 04:44:55 +08:00
|
|
|
} // namespace ftxui
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2022-03-31 08:17:43 +08:00
|
|
|
#endif // FTXUI_SCREEN_SCREEN_HPP
|