-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathScreen.h
61 lines (44 loc) · 1.12 KB
/
Screen.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
#ifndef SCREEN_H
#define SCREEN_H
#include<string>
#include<iostream>
class Screen
{
public:
using pos = std::string::size_type;
Screen() = default;
Screen(pos l, pos r) : height(l), width(r), contents(l*r, ' ') { }
Screen(pos l, pos r, char c) : height(l), width(r), contents(l*r, c) { }
char get() const { return contents[cursor]; }
inline char get(pos l, pos r) const;
inline Screen move(pos l, pos r);
inline Screen set(char c);
inline Screen set(pos l, pos r, char c);
const Screen display(std::ostream &os) const { do_display(os); return *this; }
Screen display(std::ostream &os) { do_display(os); return *this; }
private:
pos cursor = 0;
pos height = 0, width = 0;
std::string contents;
void do_display(std::ostream &os) const { os << contents; }
};
inline char Screen::get(pos l, pos r) const
{
return contents[l * width + r];
}
inline Screen Screen::move(pos l, pos r)
{
cursor = l * width + r;
return *this;
}
inline Screen Screen::set(char c)
{
contents[cursor] = c;
return *this;
}
inline Screen Screen::set(pos l, pos r, char c)
{
contents[l * width + r] = c;
return *this;
}
#endif