OXYGENHUB
◆ main openmsx / src / console / ConsoleLine.hh
1.9 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
1
#ifndef CONSOLE_LINE_HH
2
#define CONSOLE_LINE_HH
3
4
#include "ImGuiUtils.hh"
5
6
#include <cstdint>
7
#include <string>
8
#include <string_view>
9
#include <vector>
10
11
namespace openmsx {
12
13
/** This class represents a single text line in the console.
14
  * The line can have several chunks with different colors. */
15
class ConsoleLine
16
{
17
public:
18
	/** Construct empty line. */
19
	ConsoleLine() = default;
20
21
	/** Reinitialize to an empty line. */
22
	void clear() {
23
		line.clear();
24
		chunks.clear();
25
	}
26
27
	/** Construct line with a single color (by default white). */
28
	explicit ConsoleLine(std::string line, imColor color = imColor::TEXT);
29
30
	/** Append a chunk with a (different) color. */
31
	void addChunk(std::string_view text, imColor color = imColor::TEXT);
32
33
	/** Append another line (possibly containing multiple chunks). */
34
	void addLine(const ConsoleLine& ln);
35
36
	/** Split this line at a given column number.
37
	  * This line will contain (up to) 'column' number of characters.
38
	  * The remainder of the line is returned, this could be an empty line. */
39
	ConsoleLine splitAtColumn(unsigned column);
40
41
	/** Get the number of UTF8 characters in this line. So multi-byte
42
	  * characters are counted as a single character. */
43
	[[nodiscard]] size_t numChars() const;
44
	/** Get the total string, ignoring color differences. */
45
	[[nodiscard]] const std::string& str() const { return line; }
46
47
	/** Get the number of different chunks. Each chunk is a a part of the
48
	  * line that has the same color. */
49
	[[nodiscard]] size_t numChunks() const { return chunks.size(); }
50
	/** Get the color for the i-th chunk. */
51
	[[nodiscard]] imColor chunkColor(size_t i) const;
52
	/** Get the text for the i-th chunk. */
53
	[[nodiscard]] std::string_view chunkText(size_t i) const;
54
55
	[[nodiscard]] const auto& getChunks() const { return chunks; }
56
57
public:
58
	struct Chunk {
59
		imColor color;
60
		std::string_view::size_type pos;
61
	};
62
63
private:
64
	std::string line;
65
	std::vector<Chunk> chunks;
66
};
67
68
} // namespace openmsx
69
70
#endif