openMSX
ConsoleLine.cc
Go to the documentation of this file.
1#include "ConsoleLine.hh"
2
3#include "narrow.hh"
4#include "ranges.hh"
5#include "stl.hh"
6#include "strCat.hh"
7#include "utf8_unchecked.hh"
8#include "view.hh"
9
10namespace openmsx {
11
12ConsoleLine::ConsoleLine(std::string line_, imColor color)
13 : line(std::move(line_))
14 , chunks(1, {color, 0})
15{
16}
17
18void ConsoleLine::addChunk(std::string_view text, imColor color)
19{
20 chunks.emplace_back(Chunk{color, line.size()});
21 line.append(text.data(), text.size());
22}
23
25{
26 auto oldN = chunks.size();
27 auto oldPos = line.size();
28
29 strAppend(line, ln.line);
30
31 append(chunks, ln.chunks);
32 for (auto it = chunks.begin() + oldN, et = chunks.end(); it != et; ++it) {
33 it->pos += oldPos;
34 }
35}
36
41{
42 ConsoleLine result;
43 if (line.empty()) return result;
44 assert(!chunks.empty());
45
46 auto it = line.begin(), et = line.end();
47 while ((it != et) && column--) {
49 }
50 auto pos = narrow<unsigned>(std::distance(line.begin(), it));
51
52 auto it2 = ranges::upper_bound(chunks, pos, {}, &Chunk::pos);
53 assert(it2 != chunks.begin());
54 auto splitColor = it2[-1].color;
55
56 if (it != et) {
57 result.addChunk(std::string_view{it, et}, splitColor);
58 result.chunks.insert(result.chunks.end(), it2, chunks.end());
59 for (auto& c : view::drop(result.chunks, 1)) {
60 c.pos -= pos;
61 }
62 }
63
64 line.erase(it, et);
65 if (it2[-1].pos == pos) --it2;
66 chunks.erase(it2, chunks.end());
67
68 return result;
69}
70
72{
73 return utf8::unchecked::size(line);
74}
75
77{
78 assert(i < chunks.size());
79 return chunks[i].color;
80}
81
82std::string_view ConsoleLine::chunkText(size_t i) const
83{
84 assert(i < chunks.size());
85 auto pos = chunks[i].pos;
86 auto len = ((i + 1) == chunks.size())
87 ? std::string_view::npos
88 : chunks[i + 1].pos - pos;
89 return std::string_view(line).substr(pos, len);
90}
91
92} // namespace openmsx
This class represents a single text line in the console.
ConsoleLine splitAtColumn(unsigned column)
Split this line at a given column number.
void addLine(const ConsoleLine &ln)
Append another line (possibly containing multiple chunks).
std::string_view chunkText(size_t i) const
Get the text for the i-th chunk.
imColor chunkColor(size_t i) const
Get the color for the i-th chunk.
ConsoleLine()=default
Construct empty line.
void addChunk(std::string_view text, imColor color=imColor::TEXT)
Append a chunk with a (different) color.
size_t numChars() const
Get the number of UTF8 characters in this line.
This file implemented 3 utility functions:
Definition Autofire.cc:11
auto upper_bound(ForwardRange &&range, const T &value, Compare comp={}, Proj proj={})
Definition ranges.hh:124
STL namespace.
size_t size(std::string_view utf8)
uint32_t next(octet_iterator &it)
constexpr auto drop(Range &&range, size_t n)
Definition view.hh:502
void strAppend(std::string &result, Ts &&...ts)
Definition strCat.hh:752
std::string_view::size_type pos