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); // c++20
58 result.addChunk(std::string_view{&*it, size_t(et - it)}, splitColor);
59 result.chunks.insert(result.chunks.end(), it2, chunks.end());
60 for (auto& c : view::drop(result.chunks, 1)) {
61 c.pos -= pos;
62 }
63 }
64
65 line.erase(it, et);
66 if (it2[-1].pos == pos) --it2;
67 chunks.erase(it2, chunks.end());
68
69 return result;
70}
71
73{
74 return utf8::unchecked::size(line);
75}
76
78{
79 assert(i < chunks.size());
80 return chunks[i].color;
81}
82
83std::string_view ConsoleLine::chunkText(size_t i) const
84{
85 assert(i < chunks.size());
86 auto pos = chunks[i].pos;
87 auto len = ((i + 1) == chunks.size())
88 ? std::string_view::npos
89 : chunks[i + 1].pos - pos;
90 return std::string_view(line).substr(pos, len);
91}
92
93} // 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:9
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