summaryrefslogtreecommitdiff
path: root/src/util/coloredstring.cpp
blob: 7db586550b30efb75d1f0bf06d70d86e41d26c68 (plain)
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
/*
Copyright (C) 2013 xyz, Ilya Zhuravlev <whatever@xyz.is>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "coloredstring.h"
#include "util/string.h"

ColoredString::ColoredString()
{}

ColoredString::ColoredString(const std::wstring &string, const std::vector<SColor> &colors):
	m_string(string),
	m_colors(colors)
{}

ColoredString::ColoredString(const std::wstring &s) {
	m_string = colorizeText(s, m_colors, SColor(255, 255, 255, 255));
}

void ColoredString::operator=(const wchar_t *str) {
	m_string = colorizeText(str, m_colors, SColor(255, 255, 255, 255));
}

size_t ColoredString::size() const {
	return m_string.size();
}

ColoredString ColoredString::substr(size_t pos, size_t len) const {
	if (pos == m_string.length())
		return ColoredString();
	if (len == std::string::npos || pos + len > m_string.length()) {
		return ColoredString(
		           m_string.substr(pos, std::string::npos),
		           std::vector<SColor>(m_colors.begin() + pos, m_colors.end())
		       );
	} else {
		return ColoredString(
		           m_string.substr(pos, len),
		           std::vector<SColor>(m_colors.begin() + pos, m_colors.begin() + pos + len)
		       );
	}
}

const wchar_t *ColoredString::c_str() const {
	return m_string.c_str();
}

const std::vector<SColor> &ColoredString::getColors() const {
	return m_colors;
}

const std::wstring &ColoredString::getString() const {
	return m_string;
}