/
DisplayPanel.h
146 lines (118 loc) · 5.41 KB
/
DisplayPanel.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#ifndef DISPLAYPANEL_H
#define DISPLAYPANEL_H
#include <string>
#include <sstream>
#include <vector>
#include <memory>
class DisplayPanel
{
struct Info
{
Info(const std::string & lineIn, size_t realSizeIn);
std::string line;
size_t realSize;
};
public:
/// Constants
static const char NoColor = '.';
/// Types
enum LineStyle {Style_None, Style_Default, Style_Collapse};
typedef std::vector<Info> LineInfo;
struct Options
{
Options(char lineColorIn = NoColor, char textColorIn = NoColor);
Options(LineStyle lineStyleIn, char lineColorIn = NoColor, char textColorIn = NoColor);
LineStyle lineStyle;
char lineColor;
char textColor;
};
class Component
{
public:
virtual ~Component() {}
virtual LineInfo Render(const Options & options) const = 0;
virtual std::auto_ptr<Component> Clone() const = 0;
protected:
Component() {}
Component(const Component &) {}
void operator=(const Component &) {}
};
class Box : public Component
{
public:
Box() {}
explicit Box(const std::string & line);
Box(const std::string & line0, const std::string & line1);
virtual LineInfo Render(const Options & options) const;
virtual std::auto_ptr<Component> Clone() const;
void AddLine(const std::string & line);
unsigned int AddText(const std::string & text);
unsigned int LineCount() const;
private:
LineInfo m_lines;
};
class Split : public Component
{
public:
virtual ~Split();
void Add(const Component & component);
void Add(const Component & component0, const Component & component1);
void Add(const Component & component0, const Component & component1, const Component & component2);
protected:
Split();
Split(const Options & options);
Split(const Split & other);
const std::vector<Component *> & Components() const;
const Options & SplitOptions(const Options & globalOptions) const;
private:
void operator=(const Split &);
std::vector<Component *> m_components;
Options * m_localOptions;
};
class VerticalSplit : public Split
{
public:
VerticalSplit() {}
VerticalSplit(const Component & component);
VerticalSplit(const Component & component0, const Component & component1);
VerticalSplit(const Component & component0, const Component & component1, const Component & component2);
VerticalSplit(const Options & options);
virtual LineInfo Render(const Options & options) const;
virtual std::auto_ptr<Component> Clone() const;
};
class HorizontalSplit : public Split
{
public:
HorizontalSplit() {}
HorizontalSplit(const Component & component);
HorizontalSplit(const Component & component0, const Component & component1);
HorizontalSplit(const Component & component0, const Component & component1, const Component & component2);
HorizontalSplit(const Options & options);
virtual LineInfo Render(const Options & options) const;
virtual std::auto_ptr<Component> Clone() const;
};
/// Methods
static std::string Render(const Component & component, const Options & options = Options(), unsigned int minWidth = 0);
private:
static std::string RenderHorizontalLine(const Options & options, size_t maxLineLength);
struct Colorizer
{
explicit Colorizer(std::string & text, char color);
~Colorizer();
private:
Colorizer(const Colorizer &);
void operator=(const Colorizer &);
std::string * m_text;
char m_color;
};
};
inline DisplayPanel::Box::Box(const std::string & line) {AddLine(line);}
inline DisplayPanel::Box::Box(const std::string & line0, const std::string & line1) {AddLine(line0); AddLine(line1);}
inline unsigned int DisplayPanel::Box::LineCount() const {return m_lines.size();}
inline DisplayPanel::VerticalSplit::VerticalSplit(const Component & component) {Add(component);}
inline DisplayPanel::VerticalSplit::VerticalSplit(const Component & component0, const Component & component1) {Add(component0, component1);}
inline DisplayPanel::VerticalSplit::VerticalSplit(const Component & component0, const Component & component1, const Component & component2) {Add(component0, component1, component2);}
inline DisplayPanel::HorizontalSplit::HorizontalSplit(const Component & component) {Add(component);}
inline DisplayPanel::HorizontalSplit::HorizontalSplit(const Component & component0, const Component & component1) {Add(component0, component1);}
inline DisplayPanel::HorizontalSplit::HorizontalSplit(const Component & component0, const Component & component1, const Component & component2) {Add(component0, component1, component2);}
#endif