FTXUI  3.0.0
C++ functional terminal UI.
screen.hpp
Go to the documentation of this file.
1 #ifndef FTXUI_SCREEN_SCREEN_HPP
2 #define FTXUI_SCREEN_SCREEN_HPP
3 
4 #include <memory>
5 #include <string> // for string, allocator, basic_string
6 #include <vector> // for vector
7 
8 #include "ftxui/screen/box.hpp" // for Box
9 #include "ftxui/screen/color.hpp" // for Color, Color::Default
10 #include "ftxui/screen/terminal.hpp" // for Dimensions
11 
12 namespace ftxui {
13 
14 /// @brief A unicode character and its associated style.
15 /// @ingroup screen
16 struct Pixel {
17  bool operator==(const Pixel& other) const;
18 
19  // The graphemes stored into the pixel. To support combining characters,
20  // like: a⃦, this can potentially contains multiple codepoitns.
21  std::string character = " ";
22 
23  // Colors:
26 
27  // A bit field representing the style:
28  bool blink : 1;
29  bool bold : 1;
30  bool dim : 1;
31  bool inverted : 1;
32  bool underlined : 1;
33  bool automerge : 1;
34 
36  : blink(false),
37  bold(false),
38  dim(false),
39  inverted(false),
40  underlined(false),
41  automerge(false) {}
42 };
43 
44 /// @brief Define how the Screen's dimensions should look like.
45 /// @ingroup screen
46 namespace Dimension {
47 Dimensions Fixed(int);
48 Dimensions Full();
49 } // namespace Dimension
50 
51 /// @brief A rectangular grid of Pixel.
52 /// @ingroup screen
53 class Screen {
54  public:
55  // Constructors:
56  Screen(int dimx, int dimy);
57  static Screen Create(Dimensions dimension);
58  static Screen Create(Dimensions width, Dimensions height);
59 
60  // Node write into the screen using Screen::at.
61  std::string& at(int x, int y);
62  Pixel& PixelAt(int x, int y);
63 
64  // Convert the screen into a printable string in the terminal.
65  std::string ToString();
66  void Print();
67 
68  // Get screen dimensions.
69  int dimx() const { return dimx_; }
70  int dimy() const { return dimy_; }
71 
72  // Move the terminal cursor n-lines up with n = dimy().
73  std::string ResetPosition(bool clear = false) const;
74 
75  // Fill with space.
76  void Clear();
77 
78  void ApplyShader();
79 
80  struct Cursor {
81  int x = 0;
82  int y = 0;
83  };
84  Cursor cursor() const { return cursor_; }
86 
88 
89  protected:
90  int dimx_;
91  int dimy_;
92  std::vector<std::vector<Pixel>> pixels_;
94 
95  private:
96 };
97 
98 } // namespace ftxui
99 
100 #endif // FTXUI_SCREEN_SCREEN_HPP
101 
102 // Copyright 2020 Arthur Sonzogni. All rights reserved.
103 // Use of this source code is governed by the MIT license that can be found in
104 // the LICENSE file.
A class representing terminal colors.
Definition: color.hpp:17
A rectangular grid of Pixel.
Definition: screen.hpp:53
void ApplyShader()
Definition: screen.cpp:497
int dimy() const
Definition: screen.hpp:70
void SetCursor(Cursor cursor)
Definition: screen.hpp:85
static Screen Create(Dimensions dimension)
Create a screen with the given dimension.
Definition: screen.cpp:384
Pixel & PixelAt(int x, int y)
Access a Pixel at a given position.
Definition: screen.cpp:447
std::string & at(int x, int y)
Access a character a given position.
Definition: screen.cpp:440
Screen(int dimx, int dimy)
Definition: screen.cpp:388
std::string ToString()
Definition: screen.cpp:407
Cursor cursor() const
Definition: screen.hpp:84
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:470
void Print()
Definition: screen.cpp:433
Cursor cursor_
Definition: screen.hpp:93
void Clear()
Clear all the pixel from the screen.
Definition: screen.cpp:489
int dimx() const
Definition: screen.hpp:69
std::vector< std::vector< Pixel > > pixels_
Definition: screen.hpp:92
Dimensions Fixed(int)
Definition: screen.cpp:365
Dimensions Full()
Definition: screen.cpp:372
A unicode character and its associated style.
Definition: screen.hpp:16
bool operator==(const Pixel &other) const
Definition: screen.cpp:350
bool inverted
Definition: screen.hpp:31
Color foreground_color
Definition: screen.hpp:25
bool blink
Definition: screen.hpp:28
Color background_color
Definition: screen.hpp:24
std::string character
Definition: screen.hpp:21
bool dim
Definition: screen.hpp:30
bool bold
Definition: screen.hpp:29
bool underlined
Definition: screen.hpp:32
bool automerge
Definition: screen.hpp:33