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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
#include <logger.h>
#include <colormanager.h>
#include <utils.h>
#include <pb_view.h>
#include <feedlist_formaction.h>
#include <itemlist_formaction.h>
#include <itemview_formaction.h>
#include <help_formaction.h>
#include <filebrowser_formaction.h>
#include <urlview_formaction.h>
#include <select_formaction.h>
#include <exceptions.h>
#include <config.h>
using namespace podbeuter;
namespace newsbeuter {
colormanager::colormanager() : colors_loaded_(false) { }
colormanager::~colormanager() { }
void colormanager::register_commands(configparser& cfgparser) {
cfgparser.register_handler("color", this);
}
void colormanager::handle_action(const std::string& action, const std::vector<std::string>& params) {
LOG(LOG_DEBUG, "colormanager::handle_action(%s,...) was called",action.c_str());
if (action == "color") {
if (params.size() < 3) {
throw confighandlerexception(AHS_TOO_FEW_PARAMS);
}
/*
* the command syntax is:
* color <element> <fgcolor> <bgcolor> [<attribute> ...]
*/
std::string element = params[0];
std::string fgcolor = params[1];
std::string bgcolor = params[2];
if (!utils::is_valid_color(fgcolor))
throw confighandlerexception(utils::strprintf(_("`%s' is not a valid color"), fgcolor.c_str()));
if (!utils::is_valid_color(bgcolor))
throw confighandlerexception(utils::strprintf(_("`%s' is not a valid color"), bgcolor.c_str()));
std::vector<std::string> attribs;
for (unsigned int i=3; i<params.size(); ++i) {
if (!utils::is_valid_attribute(params[i]))
throw confighandlerexception(utils::strprintf(_("`%s' is not a valid attribute"), params[i].c_str()));
attribs.push_back(params[i]);
}
/* we only allow certain elements to be configured, also to indicate the user possible mis-spellings */
if (element == "listnormal" || element == "listfocus" || element == "listnormal_unread" || element =="listfocus_unread"
|| element == "info" || element == "background" || element == "article") {
fg_colors[element] = fgcolor;
bg_colors[element] = bgcolor;
attributes[element] = attribs;
colors_loaded_ = true;
} else
throw confighandlerexception(utils::strprintf(_("`%s' is not a valid configuration element"), element.c_str()));
} else
throw confighandlerexception(AHS_INVALID_COMMAND);
}
void colormanager::dump_config(std::vector<std::string>& config_output) {
for (auto color : fg_colors) {
std::string configline = utils::strprintf("color %s %s %s", color.first.c_str(), color.second.c_str(), bg_colors[color.first].c_str());
for (auto attrib : attributes[color.first]) {
configline.append(" ");
configline.append(attrib);
}
config_output.push_back(configline);
}
}
/*
* this is podbeuter-specific color management
* TODO: refactor this
*/
void colormanager::set_pb_colors(podbeuter::pb_view * v) {
auto fgcit = fg_colors.begin();
auto bgcit = bg_colors.begin();
auto attit = attributes.begin();
for (; fgcit != fg_colors.end(); ++fgcit, ++bgcit, ++attit) {
std::string colorattr;
if (fgcit->second != "default") {
colorattr.append("fg=");
colorattr.append(fgcit->second);
}
if (bgcit->second != "default") {
if (colorattr.length() > 0)
colorattr.append(",");
colorattr.append("bg=");
colorattr.append(bgcit->second);
}
for (auto attr : attit->second) {
if (colorattr.length() > 0)
colorattr.append(",");
colorattr.append("attr=");
colorattr.append(attr);
}
LOG(LOG_DEBUG,"colormanager::set_pb_colors: %s %s\n",fgcit->first.c_str(), colorattr.c_str());
v->dllist_form.set(fgcit->first, colorattr);
v->help_form.set(fgcit->first, colorattr);
if (fgcit->first == "article") {
std::string styleend_str;
if (bgcit->second != "default") {
styleend_str.append("bg=");
styleend_str.append(bgcit->second);
}
if (styleend_str.length() > 0)
styleend_str.append(",");
styleend_str.append("attr=bold");
v->help_form.set("styleend", styleend_str.c_str());
}
}
}
}
|