FTXUI  5.0.0
C++ functional terminal UI.
screen.hpp
Go to the documentation of this file.
1// Copyright 2020 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4#ifndef FTXUI_SCREEN_SCREEN_HPP
5#define FTXUI_SCREEN_SCREEN_HPP
6
7#include <cstdint> // for uint8_t
8#include <memory>
9#include <string> // for string, basic_string, allocator
10#include <vector> // for vector
11
12#include "ftxui/screen/box.hpp" // for Box
13#include "ftxui/screen/color.hpp" // for Color, Color::Default
14#include "ftxui/screen/terminal.hpp" // for Dimensions
15
16namespace ftxui {
17
18/// @brief A unicode character and its associated style.
19/// @ingroup screen
20struct Pixel {
22 : blink(false),
23 bold(false),
24 dim(false),
25 inverted(false),
26 underlined(false),
27 underlined_double(false),
28 strikethrough(false),
29 automerge(false) {}
30
31 // A bit field representing the style:
32 bool blink : 1;
33 bool bold : 1;
34 bool dim : 1;
35 bool inverted : 1;
36 bool underlined : 1;
38 bool strikethrough : 1;
39 bool automerge : 1;
40
41 // The hyperlink associated with the pixel.
42 // 0 is the default value, meaning no hyperlink.
43 uint8_t hyperlink = 0;
44
45 // The graphemes stored into the pixel. To support combining characters,
46 // like: a⃦, this can potentially contain multiple codepoints.
47 std::string character = " ";
48
49 // Colors:
52};
53
54/// @brief Define how the Screen's dimensions should look like.
55/// @ingroup screen
56namespace Dimension {
57Dimensions Fixed(int);
59} // namespace Dimension
60
61/// @brief A rectangular grid of Pixel.
62/// @ingroup screen
63class Screen {
64 public:
65 // Constructors:
66 Screen(int dimx, int dimy);
67 static Screen Create(Dimensions dimension);
68 static Screen Create(Dimensions width, Dimensions height);
69
70 // Access a character in the grid at a given position.
71 std::string& at(int x, int y);
72 const std::string& at(int x, int y) const;
73
74 // Access a cell (Pixel) in the grid at a given position.
75 Pixel& PixelAt(int x, int y);
76 const Pixel& PixelAt(int x, int y) const;
77
78 std::string ToString() const;
79
80 // Print the Screen on to the terminal.
81 void Print() const;
82
83 // Get screen dimensions.
84 int dimx() const { return dimx_; }
85 int dimy() const { return dimy_; }
86
87 // Move the terminal cursor n-lines up with n = dimy().
88 std::string ResetPosition(bool clear = false) const;
89
90 // Fill the screen with space.
91 void Clear();
92
93 void ApplyShader();
94
95 struct Cursor {
96 int x = 0;
97 int y = 0;
98
99 enum Shape {
102 Block = 2,
106 Bar = 6,
107 };
109 };
110 Cursor cursor() const { return cursor_; }
112
113 // Store an hyperlink in the screen. Return the id of the hyperlink. The id is
114 // used to identify the hyperlink when the user click on it.
115 uint8_t RegisterHyperlink(const std::string& link);
116 const std::string& Hyperlink(uint8_t id) const;
117
119
120 protected:
121 int dimx_;
122 int dimy_;
123 std::vector<std::vector<Pixel>> pixels_;
125 std::vector<std::string> hyperlinks_ = {""};
126};
127
128} // namespace ftxui
129
130#endif // FTXUI_SCREEN_SCREEN_HPP
A class representing terminal colors.
Definition: color.hpp:21
A rectangular grid of Pixel.
Definition: screen.hpp:63
void ApplyShader()
Definition: screen.cpp:534
const std::string & Hyperlink(uint8_t id) const
Definition: screen.cpp:574
int dimy() const
Definition: screen.hpp:85
std::string ToString() const
Definition: screen.cpp:416
void SetCursor(Cursor cursor)
Definition: screen.hpp:111
static Screen Create(Dimensions dimension)
Create a screen with the given dimension.
Definition: screen.cpp:391
Pixel & PixelAt(int x, int y)
Access a cell (Pixel) at a given position.
Definition: screen.cpp:470
std::string & at(int x, int y)
Access a character in a cell at a given position.
Definition: screen.cpp:456
Screen(int dimx, int dimy)
Definition: screen.cpp:395
uint8_t RegisterHyperlink(const std::string &link)
Definition: screen.cpp:561
Cursor cursor() const
Definition: screen.hpp:110
std::string ResetPosition(bool clear=false) const
Return a string to be printed in order to reset the cursor position to the beginning of the screen.
Definition: screen.cpp:500
Cursor cursor_
Definition: screen.hpp:124
void Clear()
Clear all the pixel from the screen.
Definition: screen.cpp:519
std::vector< std::string > hyperlinks_
Definition: screen.hpp:125
void Print() const
Definition: screen.cpp:449
int dimx() const
Definition: screen.hpp:84
std::vector< std::vector< Pixel > > pixels_
Definition: screen.hpp:123
Dimensions Fixed(int)
Definition: screen.cpp:372
Dimensions Full()
Definition: screen.cpp:379
A unicode character and its associated style.
Definition: screen.hpp:20
uint8_t hyperlink
Definition: screen.hpp:43
bool inverted
Definition: screen.hpp:35
bool strikethrough
Definition: screen.hpp:38
Color foreground_color
Definition: screen.hpp:51
bool blink
Definition: screen.hpp:32
Color background_color
Definition: screen.hpp:50
std::string character
Definition: screen.hpp:47
bool dim
Definition: screen.hpp:34
bool bold
Definition: screen.hpp:33
bool underlined
Definition: screen.hpp:36
bool automerge
Definition: screen.hpp:39
bool underlined_double
Definition: screen.hpp:37