-
-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathStatusLineBuilder.cpp
479 lines (392 loc) · 16.2 KB
/
StatusLineBuilder.cpp
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
// SPDX-License-Identifier: Apache-2.0
#include <vtbackend/Color.h>
#include <vtbackend/StatusLineBuilder.h>
#include <vtbackend/Terminal.h>
#include <crispy/interpolated_string.h>
#include <crispy/utils.h>
#include <libunicode/convert.h>
#include <range/v3/range/conversion.hpp>
#include <range/v3/view/join.hpp>
#include <range/v3/view/transform.hpp>
#include <chrono>
#include <cstdio>
#include <format>
using namespace std::string_view_literals;
#if defined(_WIN32)
#define popen _popen
#define pclose _pclose
#endif
namespace vtbackend
{
namespace // helper functions
{
std::string_view modeString(ViMode mode) noexcept
{
switch (mode)
{
case ViMode::Normal: return "NORMAL"sv;
case ViMode::Insert: return "INSERT"sv;
case ViMode::Visual: return "VISUAL"sv;
case ViMode::VisualLine: return "VISUAL LINE"sv;
case ViMode::VisualBlock: return "VISUAL BLOCK"sv;
}
crispy::unreachable();
}
} // namespace
std::optional<RGBColor> tryParseColorAttribute(crispy::string_interpolation const& interpolation,
std::string_view key)
{
if (auto const i = interpolation.attributes.find(key); i != interpolation.attributes.end())
return parseColor(i->second);
return std::nullopt;
}
std::optional<StatusLineDefinitions::Item> makeStatusLineItem(
crispy::interpolated_string_fragment const& fragment)
{
if (std::holds_alternative<std::string_view>(fragment))
return StatusLineDefinitions::Text { StatusLineDefinitions::Styles {},
std::string(std::get<std::string_view>(fragment)) };
auto const& interpolation = std::get<crispy::string_interpolation>(fragment);
auto styles = StatusLineDefinitions::Styles {};
auto constexpr FlagMappings = std::array {
std::pair { "Bold", CellFlag::Bold },
std::pair { "Faint", CellFlag::Faint },
std::pair { "Italic", CellFlag::Italic },
std::pair { "Underline", CellFlag::Underline },
std::pair { "Blinking", CellFlag::Blinking },
std::pair { "Inverse", CellFlag::Inverse },
std::pair { "CrossedOut", CellFlag::CrossedOut },
std::pair { "DoubleUnderline", CellFlag::DoublyUnderlined },
std::pair { "CurlyUnderline", CellFlag::CurlyUnderlined },
std::pair { "DottedUnderline", CellFlag::DottedUnderline },
std::pair { "DashedUnderline", CellFlag::DashedUnderline },
std::pair { "RapidBlinking", CellFlag::RapidBlinking },
std::pair { "Overline", CellFlag::Overline },
};
for (auto&& [text, flag]: FlagMappings)
if (interpolation.flags.count(text))
styles.flags.enable(flag);
styles.foregroundColor = tryParseColorAttribute(interpolation, "Color");
styles.backgroundColor = tryParseColorAttribute(interpolation, "BackgroundColor");
if (auto const i = interpolation.attributes.find("Left"); i != interpolation.attributes.end())
styles.textLeft = i->second;
if (auto const i = interpolation.attributes.find("Right"); i != interpolation.attributes.end())
styles.textRight = i->second;
if (interpolation.name == "CellSGR")
return StatusLineDefinitions::CellSGR { styles };
if (interpolation.name == "CellTextUTF8")
return StatusLineDefinitions::CellTextUtf8 { styles };
if (interpolation.name == "CellTextUTF32")
return StatusLineDefinitions::CellTextUtf32 { styles };
if (interpolation.name == "Clock")
return StatusLineDefinitions::Clock { styles };
if (interpolation.name == "Command")
{
if (interpolation.attributes.count("Program"))
{
return StatusLineDefinitions::Command {
styles,
std::string(interpolation.attributes.at("Program")),
};
}
else
return std::nullopt;
}
if (interpolation.name == "HistoryLineCount")
return StatusLineDefinitions::HistoryLineCount { styles };
if (interpolation.name == "Hyperlink")
return StatusLineDefinitions::Hyperlink { styles };
if (interpolation.name == "InputMode")
return StatusLineDefinitions::InputMode { styles };
if (interpolation.name == "ProtectedMode")
return StatusLineDefinitions::ProtectedMode { styles };
if (interpolation.name == "SearchMode")
return StatusLineDefinitions::SearchMode { styles };
if (interpolation.name == "SearchPrompt")
return StatusLineDefinitions::SearchPrompt { styles };
if (interpolation.name == "Title")
return StatusLineDefinitions::Title { styles };
if (interpolation.name == "Text")
return StatusLineDefinitions::Text {
styles,
std::string(interpolation.attributes.at("text")),
};
if (interpolation.name == "VTType")
return StatusLineDefinitions::VTType { styles };
if (interpolation.name == "Tabs")
{
std::optional<RGBColor> activeColor = tryParseColorAttribute(interpolation, "ActiveColor");
std::optional<RGBColor> activeBackground = tryParseColorAttribute(interpolation, "ActiveBackground");
return StatusLineDefinitions::Tabs {
styles,
activeColor,
activeBackground,
};
}
return std::nullopt;
}
StatusLineSegment parseStatusLineSegment(std::string_view text)
{
auto segment = StatusLineSegment {};
// Parses a string like:
// "{Clock:Bold,Italic,Color=#FFFF00} | {VTType} | {InputMode} {Search:Bold,Color=Yellow}"
auto const interpolations = crispy::parse_interpolated_string(text);
for (auto const& fragment: interpolations)
{
if (std::holds_alternative<std::string_view>(fragment))
{
segment.emplace_back(StatusLineDefinitions::Text {
StatusLineDefinitions::Styles {}, std::string(std::get<std::string_view>(fragment)) });
}
else if (auto const item = makeStatusLineItem(std::get<crispy::string_interpolation>(fragment)))
{
segment.emplace_back(*item);
}
}
return segment;
}
StatusLineDefinition parseStatusLineDefinition(std::string_view left,
std::string_view middle,
std::string_view right)
{
return StatusLineDefinition {
.left = parseStatusLineSegment(left),
.middle = parseStatusLineSegment(middle),
.right = parseStatusLineSegment(right),
};
}
struct VTSerializer
{
Terminal const& vt;
StatusLineStyling styling;
std::string result {};
std::string makeTextColor(std::optional<RGBColor> const& color, std::string_view defaultSequence = {})
{
if (!color)
return std::string(defaultSequence);
return std::format("\033[38:2:{}:{}:{}m", color->red, color->green, color->blue);
}
std::string makeBackgroundColor(std::optional<RGBColor> const& color,
std::string_view defaultSequence = {})
{
if (!color)
return std::string(defaultSequence);
return std::format("\033[48:2:{}:{}:{}m", color->red, color->green, color->blue);
}
void applyStyles(StatusLineDefinitions::Styles const& styles) // {{{
{
if (styling == StatusLineStyling::Disabled)
return;
result += makeTextColor(styles.foregroundColor);
result += makeBackgroundColor(styles.backgroundColor);
result += styles.flags.reduce(std::string {}, [](std::string&& result, CellFlag flag) -> std::string {
switch (flag)
{
case CellFlag::None: return result;
case CellFlag::Bold: return std::move(result) + "\033[1m";
case CellFlag::Italic: return std::move(result) + "\033[3m";
case CellFlag::Underline: return std::move(result) + "\033[4m";
case CellFlag::DottedUnderline: return std::move(result) + "\033[4:1m";
case CellFlag::CurlyUnderlined: return std::move(result) + "\033[4:3m";
case CellFlag::DoublyUnderlined: return std::move(result) + "\033[4:4m";
case CellFlag::DashedUnderline: return std::move(result) + "\033[4:5m";
case CellFlag::Blinking: return std::move(result) + "\033[5m";
case CellFlag::RapidBlinking: return std::move(result) + "\033[6m";
case CellFlag::Inverse: return std::move(result) + "\033[7m";
case CellFlag::Hidden: return std::move(result) + "\033[8m";
case CellFlag::CrossedOut: return std::move(result) + "\033[9m";
case CellFlag::Framed: return std::move(result) + "\033[51m";
case CellFlag::Encircled: return std::move(result) + "\033[52m";
case CellFlag::Overline: return std::move(result) + "\033[53m";
case CellFlag::Faint: return std::move(result) + "\033[2m";
case CellFlag::CharacterProtected:
default: return result;
}
});
} // }}}
std::string operator()(StatusLineDefinitions::Item const& item)
{
std::visit(
[this](auto const& item) {
if (auto const text = visit(item); !text.empty())
{
if constexpr (std::is_same_v<decltype(item), StatusLineDefinitions::Text>)
result += text;
else
{
if (styling == StatusLineStyling::Enabled)
{
result += SGRSAVE();
applyStyles(item);
}
result += item.textLeft;
result += text;
result += item.textRight;
if (styling == StatusLineStyling::Enabled)
result += SGRRESTORE();
}
}
},
item);
return result;
}
std::string operator()(StatusLineSegment const& segment)
{
std::string result;
for (auto const& item: segment)
result += std::visit(*this, item);
return result;
}
// {{{
std::string visit(StatusLineDefinitions::Title const&) { return vt.windowTitle(); }
std::string visit(StatusLineDefinitions::CellSGR const&)
{
auto const currentMousePosition = vt.currentMousePosition();
auto const cellFlags = vt.currentScreen().cellFlagsAt(currentMousePosition);
return std::format("{}", cellFlags);
}
std::string visit(StatusLineDefinitions::CellTextUtf32 const&)
{
auto const currentMousePosition = vt.currentMousePosition();
if (!vt.contains(currentMousePosition))
return {};
auto const cellText = vt.currentScreen().cellTextAt(currentMousePosition);
auto const cellText32 = unicode::convert_to<char32_t>(std::string_view(cellText));
return ranges::views::transform(
cellText32, [](char32_t ch) { return std::format("U+{:04X}", static_cast<uint32_t>(ch)); })
| ranges::views::join(" ") | ranges::to<std::string>;
}
std::string visit(StatusLineDefinitions::CellTextUtf8 const&)
{
auto const currentMousePosition = vt.currentMousePosition();
if (!vt.contains(currentMousePosition))
return {};
return crispy::escape(vt.currentScreen().cellTextAt(currentMousePosition));
}
std::string visit(StatusLineDefinitions::Clock const&)
{
crispy::ignore_unused(this);
// TODO: Find a more convinient way; The following is printing the time in UTC,
// but we need it in local time.
// return std::format("{:%H:%M}", std::chrono::system_clock::now());
auto now = std::chrono::system_clock::now();
std::time_t const nowTimeT = std::chrono::system_clock::to_time_t(now);
std::tm const* tm = std::localtime(&nowTimeT);
std::stringstream out;
out << std::put_time(tm, "%H:%M");
return out.str();
}
std::string visit(StatusLineDefinitions::HistoryLineCount const&)
{
if (!vt.isPrimaryScreen())
return {};
if (vt.viewport().scrollOffset().value)
{
auto const pct =
double(vt.viewport().scrollOffset()) / double(vt.primaryScreen().historyLineCount());
return std::format("{}/{} {:3}%",
vt.viewport().scrollOffset(),
vt.primaryScreen().historyLineCount(),
int(pct * 100));
}
else
return std::format("{}", vt.primaryScreen().historyLineCount());
}
std::string visit(StatusLineDefinitions::Hyperlink const&)
{
if (auto const hyperlink = vt.currentScreen().hyperlinkAt(vt.currentMousePosition()))
return std::format("{}", hyperlink->uri);
return {};
}
std::string visit(StatusLineDefinitions::InputMode const&)
{
return std::string(modeString(vt.inputHandler().mode()));
}
std::string visit(StatusLineDefinitions::ProtectedMode const&)
{
if (vt.allowInput())
return {};
return " (PROTECTED)";
}
std::string visit(StatusLineDefinitions::TraceMode const&)
{
std::string result;
result += "TRACING";
if (!vt.traceHandler().pendingSequences().empty())
result += std::format(" (#{}): {}",
vt.traceHandler().pendingSequences().size(),
vt.traceHandler().pendingSequences().front());
return result;
}
std::string visit(StatusLineDefinitions::SearchMode const&)
{
if (!vt.search().pattern.empty() || vt.inputHandler().isEditingSearch())
return " SEARCH";
return {};
}
std::string visit(StatusLineDefinitions::SearchPrompt const&)
{
if (vt.inputHandler().isEditingSearch())
return std::format("Search: {}█",
unicode::convert_to<char>(std::u32string_view(vt.search().pattern)));
return {};
}
std::string visit(StatusLineDefinitions::Command const& item)
{
crispy::ignore_unused(this);
std::string result;
if (FILE* fp = popen(item.command.c_str(), "r"); fp)
{
char buffer[256] {};
while (fgets(buffer, sizeof(buffer), fp) != nullptr)
{
result += buffer;
}
pclose(fp);
// Only keep first line
if (auto const pos = result.find('\n'); pos != std::string::npos)
result.erase(pos);
}
else
result = std::strerror(errno);
return result;
}
std::string visit(StatusLineDefinitions::Text const& item)
{
crispy::ignore_unused(this);
return item.text;
}
std::string visit(StatusLineDefinitions::VTType const&) { return std::format("{}", vt.terminalId()); }
std::string visit(StatusLineDefinitions::Tabs const& tabs)
{
auto const tabsInfo = vt.guiTabsInfoForStatusLine();
std::string fragment;
for (size_t position: std::views::iota(1u, tabsInfo.tabCount + 1))
{
if (!fragment.empty())
fragment += ' ';
auto const isActivePosition = position == tabsInfo.activeTabPosition;
auto const activePositionStylized =
isActivePosition && (tabs.activeColor || tabs.activeBackground);
if (activePositionStylized)
{
fragment += SGRSAVE();
fragment += makeTextColor(tabs.activeColor);
fragment += makeBackgroundColor(tabs.activeBackground);
}
fragment += std::to_string(position);
if (activePositionStylized)
fragment += SGRRESTORE();
}
return fragment;
}
// }}}
};
std::string serializeToVT(Terminal const& vt, StatusLineSegment const& segment, StatusLineStyling styling)
{
auto serializer = VTSerializer { vt, styling };
for (auto const& item: segment)
serializer(item);
return serializer.result;
}
} // namespace vtbackend