VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
ChatColors.cpp
Go to the documentation of this file.
3#include <unordered_map>
4
6{
7
8// Map of color names to their corresponding control codes. Keys are lowercase for case-insensitive lookup.
9static const std::unordered_map<std::string, std::string_view> kNameTable = {
10 {"default", Default}, {"white", White}, {"darkred", DarkRed}, {"lightpurple", LightPurple},
11 {"green", Green}, {"olive", Olive}, {"lime", Lime}, {"red", Red},
12 {"gray", Gray}, {"grey", Grey}, {"yellow", Yellow}, {"lightyellow", LightYellow},
13 {"silver", Silver}, {"bluegrey", BlueGrey}, {"lightblue", LightBlue}, {"blue", Blue},
14 {"darkblue", DarkBlue}, {"purple", Purple}, {"magenta", Magenta}, {"lightred", LightRed},
15 {"gold", Gold}, {"orange", Orange},
16};
17
18std::string_view ParseNamed(std::string_view name)
19{
20 if (name.empty())
21 {
22 return Default;
23 }
24
25 auto it = kNameTable.find(Strings::ToLower(std::string(name)));
26 return it != kNameTable.end() ? it->second : Default;
27}
28
29std::string Strip(std::string_view text)
30{
31 std::string out;
32 out.reserve(text.size());
33 for (char c : text)
34 {
35 if (static_cast<unsigned char>(c) > 0x10) // 0x01-0x10 are chat color escape bytes
36 {
37 out.push_back(c);
38 }
39 }
40 return out;
41}
42
43std::vector<Labeled<std::string>> PaletteChoices(std::function<std::string(std::string_view canonicalName)> labelFor)
44{
45 std::vector<Labeled<std::string>> choices;
46 choices.reserve(Palette.size());
47
48 for (const auto& entry : Palette)
49 {
50 std::string label = labelFor ? labelFor(entry.Name) : std::string{};
51 if (label.empty())
52 {
53 label = std::string(entry.Name);
54 }
55 choices.push_back({.Label = std::move(label), .Value = std::string(entry.Name)});
56 }
57 return choices;
58}
59
60} // namespace VoltMod::ChatColors
static std::string ToLower(std::string_view str)
Definition Strings.cpp:100
constexpr std::string_view Orange
constexpr std::string_view Blue
constexpr std::string_view DarkRed
std::string_view ParseNamed(std::string_view name)
constexpr std::string_view Magenta
constexpr std::string_view Olive
constexpr std::string_view Gray
std::vector< Labeled< std::string > > PaletteChoices(std::function< std::string(std::string_view canonicalName)> labelFor)
constexpr std::string_view Silver
constexpr std::string_view White
constexpr std::string_view Default
constexpr std::string_view Yellow
constexpr std::string_view LightYellow
constexpr std::string_view Green
constexpr std::string_view LightRed
constexpr std::string_view DarkBlue
constexpr std::string_view Red
constexpr std::string_view Gold
constexpr std::array< NamedColor, 15 > Palette
constexpr std::string_view LightBlue
constexpr std::string_view Grey
std::string Strip(std::string_view text)
static const std::unordered_map< std::string, std::string_view > kNameTable
Definition ChatColors.cpp:9
constexpr std::string_view Lime
constexpr std::string_view LightPurple
constexpr std::string_view BlueGrey
constexpr std::string_view Purple
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56