teditor  1.8.0@@fee5e94
Terminal based editor written in C++
colors.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stdint.h>
4 #include <unordered_map>
5 #include <vector>
6 #include <string>
7 
8 
9 namespace teditor {
10 
11 typedef uint16_t color_t;
12 
13 static const color_t Attr_None = 0x0000;
14 static const color_t Attr_Bold = 0x0100;
15 static const color_t Attr_Underline = 0x0200;
16 static const color_t Attr_Italic = 0x0400;
17 
18 struct AttrHelper {
19 public:
20  AttrHelper();
21  static color_t fromstr(const std::string& str);
22  static std::string tostr(color_t attr);
23 
24 private:
25  std::unordered_map<std::string,color_t> amap;
26  std::unordered_map<color_t,std::string> rmap;
27  static const AttrHelper Str2Attr;
28 };
29 
30 
31 struct ColorHelper {
32 public:
33  static color_t fromstr(const std::string& str);
34  static std::string tostr(color_t colo);
35 
36  struct Registrar {
37  Registrar(const std::string& str, color_t c) {
38  ColorHelper::get().amap[str] = c;
39  ColorHelper::get().rmap[c] = str;
40  }
41  }; // struct Registrar
42 
43 private:
44  ColorHelper() : amap(), rmap() {}
45 
46  std::unordered_map<std::string,color_t> amap;
47  std::unordered_map<color_t,std::string> rmap;
48 
49  static ColorHelper& get();
50  friend struct Registrar;
51 };
52 
53 #include "def_colors.h"
54 
55 
56 struct AttrColor {
58 
59  static const color_t Mask;
60 
61  AttrColor(): ac(0) {}
62  AttrColor(color_t in): ac(in) {}
63  AttrColor(color_t c, color_t a): ac(a | c) {}
64  void set(color_t c, color_t a) { ac = a | c; }
65  void clearAttr() { ac &= Mask; }
66  void setBold() { ac |= Attr_Bold; }
68  void setItalic() { ac |= Attr_Italic; }
69  bool isBold() const { return ac & Attr_Bold; }
70  bool isUnderline() const { return ac & Attr_Underline; }
71  bool isItalic() const { return ac & Attr_Italic; }
72  color_t color() const { return ac & Mask; }
73  void setColor(color_t col) { ac |= (Mask & col); }
74 };
75 
76 bool operator==(const AttrColor& a, const AttrColor& b);
77 bool operator!=(const AttrColor& a, const AttrColor& b);
78 
79 
81 struct NameColorPair {
82  std::string name, color;
83 };
84 
85 
87 class ColorMap {
88 public:
95  void add(const NameColorPair& ncp);
96 
97  const AttrColor& get(const std::string& name) const;
98 
99  void clear() { colors.clear(); }
100 
102  AttrColor readColor(const std::string& str);
103 
104 private:
105  std::unordered_map<std::string, AttrColor> colors;
106 };
107 
108 
109 template <typename Colors>
110 void populateColorMap(ColorMap& cm, bool clear=false) {
111  if(clear) cm.clear();
112  for(const auto& ncp : Colors::All) cm.add(ncp);
113 }
114 
115 }; // end namespace teditor
teditor::NameColorPair::color
std::string color
Definition: colors.h:82
teditor::split
Strings split(const std::string &str, char delim)
Definition: utils.cpp:48
teditor::AttrHelper::fromstr
static color_t fromstr(const std::string &str)
Definition: colors.cpp:21
ADD
#define ADD(type)
Definition: colors.cpp:9
teditor::ColorMap::clear
void clear()
Definition: colors.h:99
teditor::ColorMap::add
void add(const NameColorPair &ncp)
populate the map with the name-color pair At first, the color string is assumed to be another key to ...
Definition: colors.cpp:62
teditor::ColorMap
Definition: colors.h:87
teditor::AttrHelper::tostr
static std::string tostr(color_t attr)
Definition: colors.cpp:28
teditor::AttrColor
Definition: colors.h:56
teditor::AttrHelper
Definition: colors.h:18
teditor::AttrColor::isUnderline
bool isUnderline() const
Definition: colors.h:70
utils.h
teditor::ColorHelper
Definition: colors.h:31
teditor::NameColorPair::name
std::string name
Definition: colors.h:82
teditor::operator!=
bool operator!=(const AttrColor &a, const AttrColor &b)
Definition: colors.cpp:57
teditor::populateColorMap
void populateColorMap(ColorMap &cm, bool clear=false)
Definition: colors.h:110
teditor::AttrColor::setBold
void setBold()
Definition: colors.h:66
teditor::ColorMap::readColor
AttrColor readColor(const std::string &str)
Definition: colors.cpp:74
ASSERT
#define ASSERT(check, fmt,...)
Macro to assert with runtime_error exception if the check fails.
Definition: utils.h:35
teditor::AttrHelper::AttrHelper
AttrHelper()
Definition: colors.cpp:13
teditor::ColorHelper::Registrar
Definition: colors.h:36
teditor::operator==
bool operator==(const Cell &a, const Cell &b)
Definition: cell_buffer.cpp:33
def_colors.h
teditor::Attr_Underline
static const color_t Attr_Underline
Definition: colors.h:15
teditor::AttrColor::set
void set(color_t c, color_t a)
Definition: colors.h:64
teditor::Attr_Italic
static const color_t Attr_Italic
Definition: colors.h:16
teditor::AttrColor::isBold
bool isBold() const
Definition: colors.h:69
teditor::Attr_None
static const color_t Attr_None
Definition: colors.h:13
teditor::AttrColor::setUnderline
void setUnderline()
Definition: colors.h:67
teditor::AttrColor::setColor
void setColor(color_t col)
Definition: colors.h:73
teditor::ColorHelper::Registrar::Registrar
Registrar(const std::string &str, color_t c)
Definition: colors.h:37
colors.h
teditor::AttrColor::Mask
static const color_t Mask
Definition: colors.h:59
teditor::AttrColor::isItalic
bool isItalic() const
Definition: colors.h:71
teditor::AttrColor::color
color_t color() const
Definition: colors.h:72
teditor::AttrColor::AttrColor
AttrColor()
Definition: colors.h:61
teditor::ColorHelper::fromstr
static color_t fromstr(const std::string &str)
Definition: colors.cpp:41
teditor::NameColorPair
Definition: colors.h:81
teditor::ColorMap::get
const AttrColor & get(const std::string &name) const
Definition: colors.cpp:67
teditor::AttrColor::AttrColor
AttrColor(color_t c, color_t a)
Definition: colors.h:63
teditor::AttrColor::AttrColor
AttrColor(color_t in)
Definition: colors.h:62
teditor::Attr_Bold
static const color_t Attr_Bold
Definition: colors.h:14
teditor::AttrColor::clearAttr
void clearAttr()
Definition: colors.h:65
teditor::AttrColor::setItalic
void setItalic()
Definition: colors.h:68
teditor::ColorHelper::tostr
static std::string tostr(color_t colo)
Definition: colors.cpp:48
teditor::AttrColor::ac
color_t ac
Definition: colors.h:57
teditor
Definition: any.hpp:10
teditor::color_t
uint16_t color_t
Definition: colors.h:11