-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathOutputter.cpp
More file actions
173 lines (152 loc) · 6.39 KB
/
Outputter.cpp
File metadata and controls
173 lines (152 loc) · 6.39 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "Outputter.hpp"
#include "td/utils/Slice-decl.h"
#include "td/utils/overloaded.h"
#include "td/utils/port/Clocks.h"
#include "td/utils/format.h"
#include "td/utils/format.h"
#include "ChatWindow.hpp"
#include "windows/Markup.hpp"
#include "windows/unicode.h"
#include <memory>
namespace td {
namespace format {
template <typename T>
struct ZeroPaddedInt {
T value;
td::uint32 pad;
};
StringBuilder &operator<<(StringBuilder &sb, ZeroPaddedInt<td::uint32> v) {
char buf[64];
auto l = sprintf(buf, "%0*u", v.pad, v.value);
return sb << td::CSlice(buf, buf + l);
}
} // namespace format
} // namespace td
namespace tdcurses {
Outputter &Outputter::operator<<(Date date) {
time_t t = date.date;
struct tm d;
localtime_r(&t, &d);
*this << "[";
auto now = td::Clocks::system();
if (now - date.date > 60 * 86400) {
*this << (d.tm_year + 1900) << "/";
}
if (now - date.date > 36000) {
*this << td::format::ZeroPaddedInt<td::uint32>{static_cast<td::uint32>(d.tm_mon + 1), 2} << "/";
*this << td::format::ZeroPaddedInt<td::uint32>{static_cast<td::uint32>(d.tm_mday), 2} << " ";
}
*this << td::format::ZeroPaddedInt<td::uint32>{static_cast<td::uint32>(d.tm_hour), 2} << ":"
<< td::format::ZeroPaddedInt<td::uint32>{static_cast<td::uint32>(d.tm_min), 2} << ":"
<< td::format::ZeroPaddedInt<td::uint32>{static_cast<td::uint32>(d.tm_sec), 2};
return *this << "]";
}
Outputter &Outputter::operator<<(FgColor color) {
set_color(color.color, true);
return *this;
}
Outputter &Outputter::operator<<(FgColorRgb color) {
set_color(color.color, true);
return *this;
}
Outputter &Outputter::operator<<(BgColor color) {
set_color(color.color, false);
return *this;
}
Outputter &Outputter::operator<<(BgColorRgb color) {
set_color(color.color, false);
return *this;
}
void Outputter::set_color(td::Variant<Color, ColorRGB> color, bool is_fg) {
auto &l = (is_fg ? fg_colors_stack_ : bg_colors_stack_);
if (color.get_offset() == color.offset<Color>() && color.get<Color>() == Color::Revert) {
l.pop_arg(*this, sb_.as_cslice().size(), markup_idx_++);
} else {
l.push_arg(*this, sb_.as_cslice().size(), markup_idx_++,
[color = std::move(color), is_fg](size_t from_pos, size_t from_idx, size_t to_pos,
size_t to_idx) -> windows::MarkupElement {
windows::MarkupElementPos from(from_pos, from_idx);
windows::MarkupElementPos to(to_pos, to_idx);
windows::MarkupElement el;
if (is_fg) {
color.visit(td::overloaded(
[&](Color c) { el = std::make_shared<windows::MarkupElementFgColor>(from, to, c); },
[&](ColorRGB c) { el = std::make_shared<windows::MarkupElementFgColorRGB>(from, to, c); }));
} else {
color.visit(td::overloaded(
[&](Color c) { el = std::make_shared<windows::MarkupElementBgColor>(from, to, c); },
[&](ColorRGB c) { el = std::make_shared<windows::MarkupElementBgColorRGB>(from, to, c); }));
}
return el;
});
}
}
std::vector<windows::MarkupElement> Outputter::markup() {
auto res = markup_;
fg_colors_stack_.flush_to(res, sb_.as_cslice().size(), markup_idx_++);
bg_colors_stack_.flush_to(res, sb_.as_cslice().size(), markup_idx_++);
pad_left_stack_.flush_to(res, sb_.as_cslice().size(), markup_idx_++);
for (auto &e : bool_stack_) {
e->flush_to(res, sb_.as_cslice().size(), markup_idx_++);
}
return res;
}
const td::td_api::message *Outputter::get_message(td::int64 chat_id, td::int64 message_id) {
if (!cur_chat_) {
return nullptr;
}
return cur_chat_->get_message_as_message(chat_id, message_id);
}
Outputter &Outputter::operator<<(const LeftPad &x) {
if (x.pad.size() > 0) {
pad_left_stack_.push_arg(
*this, sb_.as_cslice().size(), markup_idx_++,
[pad = std::move(x.pad), color = std::move(x.color)](size_t from_pos, size_t from_idx, size_t to_pos,
size_t to_idx) -> windows::MarkupElement {
windows::MarkupElementPos from(from_pos, from_idx);
windows::MarkupElementPos to(to_pos, to_idx);
windows::MarkupElement el;
color.visit(td::overloaded(
[&](Color c) { el = std::make_shared<windows::MarkupElementLeftPad>(from, to, pad, c); },
[&](ColorRGB c) { el = std::make_shared<windows::MarkupElementLeftPad>(from, to, pad, c); }));
return el;
});
} else {
pad_left_stack_.pop_arg(*this, sb_.as_cslice().size(), markup_idx_++);
}
return *this;
}
Outputter &Outputter::operator<<(const RightPad &x) {
td::int32 code = RIGHT_ALIGN_BLOCK_START + (td::int32)x.data.size();
char buf[6];
utf8_code_to_str(code, buf);
return *this << td::CSlice((char *)buf) << x.data;
}
Outputter &Outputter::operator<<(const Photo &obj) {
windows::MarkupElementPos from(sb_.as_cslice().size(), markup_idx_++);
windows::MarkupElementPos to(sb_.as_cslice().size(), markup_idx_++);
markup_.push_back(std::make_shared<windows::MarkupElementImage>(from, to, obj.path.str(), obj.height, obj.width, 20,
1000, obj.allow_pixel));
return *this;
}
Outputter &Outputter::operator<<(const UserpicPhoto &obj) {
windows::MarkupElementPos from(sb_.as_cslice().size(), markup_idx_++);
windows::MarkupElementPos to(sb_.as_cslice().size(), markup_idx_++);
markup_.push_back(std::make_shared<windows::MarkupElementImage>(from, to, obj.path.str(), obj.height, obj.width, 2, 4,
obj.allow_pixel));
return *this;
}
Outputter &Outputter::operator<<(const UserpicPhotoData &obj) {
windows::MarkupElementPos from(sb_.as_cslice().size(), markup_idx_++);
windows::MarkupElementPos to(sb_.as_cslice().size(), markup_idx_++);
markup_.push_back(std::make_shared<windows::MarkupElementImageData>(from, to, obj.data.str(), obj.height, obj.width,
2, 4, obj.allow_pixel));
return *this;
}
Outputter &Outputter::operator<<(const SoftTab &c) {
char buf[6];
td::uint32 cp = SOFT_TAB_START_CP + (c.size & 0x1f);
td::uint32 r = utf8_code_to_str(cp, buf);
return *this << td::Slice(buf, buf + r);
}
} // namespace tdcurses