• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

GothenburgBitFactory / taskwarrior / 12343201393

15 Dec 2024 11:30PM UTC coverage: 84.419% (-1.1%) from 85.522%
12343201393

Pull #3724

github

web-flow
Merge 532931b9f into ddae5c4ba
Pull Request #3724: Support importing Taskwarrior v2.x data files

15 of 145 new or added lines in 4 files covered. (10.34%)

183 existing lines in 48 files now uncovered.

19289 of 22849 relevant lines covered (84.42%)

23168.82 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.36
/src/rules.cpp
1
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright 2006 - 2021, Tomas Babej, Paul Beckingham, Federico Hernandez.
4
//
5
// Permission is hereby granted, free of charge, to any person obtaining a copy
6
// of this software and associated documentation files (the "Software"), to deal
7
// in the Software without restriction, including without limitation the rights
8
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the Software is
10
// furnished to do so, subject to the following conditions:
11
//
12
// The above copyright notice and this permission notice shall be included
13
// in all copies or substantial portions of the Software.
14
//
15
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
// SOFTWARE.
22
//
23
// https://www.opensource.org/licenses/mit-license.php
24
//
25
////////////////////////////////////////////////////////////////////////////////
26

27
#include <cmake.h>
28
// cmake.h include header must come first
29

30
#include <Context.h>
31
#include <Datetime.h>
32
#include <main.h>
33
#include <shared.h>
34
#include <stdlib.h>
35

36
static std::map<std::string, Color> gsColor;
37
static std::vector<std::string> gsPrecedence;
38
static Datetime now;
39

40
////////////////////////////////////////////////////////////////////////////////
41
void initializeColorRules() {
4,495✔
42
  // If color is not enable/supported, short circuit.
43
  if (!Context::getContext().color()) return;
4,495✔
44

45
  try {
46
    gsColor.clear();
183✔
47
    gsPrecedence.clear();
183✔
48

49
    // Load all the configuration values, filter to only the ones that begin with
50
    // "color.", then store name/value in gsColor, and name in rules.
51
    std::vector<std::string> rules;
183✔
52
    for (const auto& v : Context::getContext().config) {
45,249✔
53
      if (!v.first.compare(0, 6, "color.", 6)) {
45,066✔
54
        Color c(v.second);
8,876✔
55
        gsColor[v.first] = c;
8,876✔
56

57
        rules.push_back(v.first);
8,876✔
58
      }
59
    }
60

61
    // Load the rule.precedence.color list, split it, then autocomplete against
62
    // the 'rules' vector loaded above.
63
    std::vector<std::string> results;
183✔
64
    auto precedence = split(Context::getContext().config.get("rule.precedence.color"), ',');
366✔
65

66
    for (const auto& p : precedence) {
2,928✔
67
      // Add the leading "color." string.
68
      std::string rule = "color." + p;
2,745✔
69
      autoComplete(rule, rules, results, 3);  // Hard-coded 3.
2,745✔
70

71
      for (auto& r : results) gsPrecedence.push_back(r);
6,126✔
72
    }
2,745✔
73
  }
183✔
74

75
  catch (const std::string& e) {
×
76
    Context::getContext().error(e);
×
UNCOV
77
  }
×
78
}
79

80
////////////////////////////////////////////////////////////////////////////////
81
static void applyColor(const Color& base, Color& c, bool merge) {
44✔
82
  if (merge)
44✔
83
    c.blend(base);
42✔
84
  else
85
    c = base;
2✔
86
}
44✔
87

88
////////////////////////////////////////////////////////////////////////////////
89
static void colorizeBlocked(Task& task, const Color& base, Color& c, bool merge) {
38✔
90
  if (task.is_blocked) applyColor(base, c, merge);
38✔
91
}
38✔
92

93
////////////////////////////////////////////////////////////////////////////////
94
static void colorizeBlocking(Task& task, const Color& base, Color& c, bool merge) {
38✔
95
  if (task.is_blocking) applyColor(base, c, merge);
38✔
96
}
38✔
97

98
////////////////////////////////////////////////////////////////////////////////
99
static void colorizeTagged(Task& task, const Color& base, Color& c, bool merge) {
14✔
100
  if (task.getTagCount()) applyColor(base, c, merge);
14✔
101
}
14✔
102

103
////////////////////////////////////////////////////////////////////////////////
104
static void colorizeActive(Task& task, const Color& base, Color& c, bool merge) {
38✔
105
  // TODO: Not consistent with the implementation of the +ACTIVE virtual tag
106
  if (task.has("start") && !task.has("end")) applyColor(base, c, merge);
116✔
107
}
38✔
108

109
////////////////////////////////////////////////////////////////////////////////
110
static void colorizeScheduled(Task& task, const Color& base, Color& c, bool merge) {
38✔
111
  // TODO: Not consistent with the implementation of the +SCHEDULED virtual tag
112
  if (task.has("scheduled") && Datetime(task.get_date("scheduled")) <= now)
114✔
113
    applyColor(base, c, merge);
×
114
}
38✔
115

116
////////////////////////////////////////////////////////////////////////////////
117
static void colorizeUntil(Task& task, const Color& base, Color& c, bool merge) {
×
118
  if (task.has("until")) applyColor(base, c, merge);
×
UNCOV
119
}
×
120

121
////////////////////////////////////////////////////////////////////////////////
122
static void colorizeTag(Task& task, const std::string& rule, const Color& base, Color& c,
72✔
123
                        bool merge) {
124
  if (task.hasTag(rule.substr(10))) applyColor(base, c, merge);
72✔
125
}
72✔
126

127
////////////////////////////////////////////////////////////////////////////////
128
static void colorizeProject(Task& task, const std::string& rule, const Color& base, Color& c,
24✔
129
                            bool merge) {
130
  // Observe the case sensitivity setting.
131
  bool sensitive = Context::getContext().config.getBoolean("search.case.sensitive");
72✔
132

133
  auto project = task.get("project");
24✔
134
  auto rule_trunc = rule.substr(14);
24✔
135

136
  // Match project names leftmost.
137
  if (rule_trunc.length() <= project.length())
24✔
138
    if (compare(rule_trunc, project.substr(0, rule_trunc.length()), sensitive))
1✔
139
      applyColor(base, c, merge);
1✔
140
}
24✔
141

142
////////////////////////////////////////////////////////////////////////////////
143
static void colorizeProjectNone(Task& task, const Color& base, Color& c, bool merge) {
1✔
144
  if (!task.has("project")) applyColor(base, c, merge);
2✔
145
}
1✔
146

147
////////////////////////////////////////////////////////////////////////////////
148
static void colorizeTagNone(Task& task, const Color& base, Color& c, bool merge) {
1✔
149
  if (task.getTagCount() == 0) applyColor(base, c, merge);
1✔
150
}
1✔
151

152
////////////////////////////////////////////////////////////////////////////////
153
static void colorizeKeyword(Task& task, const std::string& rule, const Color& base, Color& c,
25✔
154
                            bool merge) {
155
  // Observe the case sensitivity setting.
156
  auto sensitive = Context::getContext().config.getBoolean("search.case.sensitive");
50✔
157

158
  // The easiest thing to check is the description, because it is just one
159
  // attribute.
160
  if (find(task.get("description"), rule.substr(14), sensitive) != std::string::npos)
75✔
161
    applyColor(base, c, merge);
2✔
162

163
  // Failing the description check, look at all annotations, returning on the
164
  // first match.
165
  else {
166
    for (const auto& att : task.getAnnotations()) {
23✔
167
      if (find(att.second, rule.substr(14), sensitive) != std::string::npos) {
×
168
        applyColor(base, c, merge);
×
169
        return;
×
170
      }
171
    }
23✔
172
  }
173
}
174

175
////////////////////////////////////////////////////////////////////////////////
176
static void colorizeUDA(Task& task, const std::string& rule, const Color& base, Color& c,
161✔
177
                        bool merge) {
178
  // Is the rule color.uda.name.value or color.uda.name?
179
  auto pos = rule.find('.', 10);
161✔
180
  if (pos == std::string::npos) {
161✔
181
    if (task.has(rule.substr(10))) applyColor(base, c, merge);
23✔
182
  } else {
183
    auto uda = rule.substr(10, pos - 10);
138✔
184
    auto val = rule.substr(pos + 1);
138✔
185
    if ((val == "none" && !task.has(uda)) || task.get(uda) == val) applyColor(base, c, merge);
138✔
186
  }
138✔
187
}
161✔
188

189
////////////////////////////////////////////////////////////////////////////////
190
static void colorizeDue(Task& task, const Color& base, Color& c, bool merge) {
38✔
191
  if (task.is_due()) applyColor(base, c, merge);
38✔
192
}
38✔
193

194
////////////////////////////////////////////////////////////////////////////////
195
static void colorizeDueToday(Task& task, const Color& base, Color& c, bool merge) {
38✔
196
  if (task.is_duetoday()) applyColor(base, c, merge);
38✔
197
}
38✔
198

199
////////////////////////////////////////////////////////////////////////////////
200
static void colorizeOverdue(Task& task, const Color& base, Color& c, bool merge) {
38✔
201
  if (task.is_overdue()) applyColor(base, c, merge);
38✔
202
}
38✔
203

204
////////////////////////////////////////////////////////////////////////////////
205
static void colorizeRecurring(Task& task, const Color& base, Color& c, bool merge) {
38✔
206
  if (task.has("recur")) applyColor(base, c, merge);
76✔
207
}
38✔
208

209
////////////////////////////////////////////////////////////////////////////////
210
static void colorizeCompleted(Task& task, const Color& base, Color& c, bool merge) {
×
211
  if (task.getStatus() == Task::completed) applyColor(base, c, merge);
×
UNCOV
212
}
×
213

214
////////////////////////////////////////////////////////////////////////////////
215
static void colorizeDeleted(Task& task, const Color& base, Color& c, bool merge) {
×
216
  if (task.getStatus() == Task::deleted) applyColor(base, c, merge);
×
UNCOV
217
}
×
218

219
////////////////////////////////////////////////////////////////////////////////
220
void autoColorize(Task& task, Color& c) {
1,404✔
221
  // The special tag 'nocolor' overrides all auto and specific colorization.
222
  if (!Context::getContext().color() || task.hasTag("nocolor")) {
1,506✔
223
    c = Color();
1,355✔
224
    return;
1,355✔
225
  }
226

227
  auto merge = Context::getContext().config.getBoolean("rule.color.merge");
98✔
228

229
  // Note: c already contains colors specifically assigned via command.
230
  // Note: These rules form a hierarchy - the last rule is King, hence the
231
  //       reverse iterator.
232

233
  for (auto r = gsPrecedence.rbegin(); r != gsPrecedence.rend(); ++r) {
1,014✔
234
    Color base = gsColor[*r];
965✔
235
    if (base.nontrivial()) {
965✔
236
      if (*r == "color.blocked")
602✔
237
        colorizeBlocked(task, base, c, merge);
38✔
238
      else if (*r == "color.blocking")
564✔
239
        colorizeBlocking(task, base, c, merge);
38✔
240
      else if (*r == "color.tagged")
526✔
241
        colorizeTagged(task, base, c, merge);
14✔
242
      else if (*r == "color.active")
512✔
243
        colorizeActive(task, base, c, merge);
38✔
244
      else if (*r == "color.scheduled")
474✔
245
        colorizeScheduled(task, base, c, merge);
38✔
246
      else if (*r == "color.until")
436✔
247
        colorizeUntil(task, base, c, merge);
×
248
      else if (*r == "color.project.none")
436✔
249
        colorizeProjectNone(task, base, c, merge);
1✔
250
      else if (*r == "color.tag.none")
435✔
251
        colorizeTagNone(task, base, c, merge);
1✔
252
      else if (*r == "color.due")
434✔
253
        colorizeDue(task, base, c, merge);
38✔
254
      else if (*r == "color.due.today")
396✔
255
        colorizeDueToday(task, base, c, merge);
38✔
256
      else if (*r == "color.overdue")
358✔
257
        colorizeOverdue(task, base, c, merge);
38✔
258
      else if (*r == "color.recurring")
320✔
259
        colorizeRecurring(task, base, c, merge);
38✔
260
      else if (*r == "color.completed")
282✔
261
        colorizeCompleted(task, base, c, merge);
×
262
      else if (*r == "color.deleted")
282✔
263
        colorizeDeleted(task, base, c, merge);
×
264

265
      // Wildcards
266
      else if (!r->compare(0, 10, "color.tag.", 10))
282✔
267
        colorizeTag(task, *r, base, c, merge);
72✔
268
      else if (!r->compare(0, 14, "color.project.", 14))
210✔
269
        colorizeProject(task, *r, base, c, merge);
24✔
270
      else if (!r->compare(0, 14, "color.keyword.", 14))
186✔
271
        colorizeKeyword(task, *r, base, c, merge);
25✔
272
      else if (!r->compare(0, 10, "color.uda.", 10))
161✔
273
        colorizeUDA(task, *r, base, c, merge);
161✔
274
    }
275
  }
276
}
277

278
////////////////////////////////////////////////////////////////////////////////
279
std::string colorizeHeader(const std::string& input) {
364✔
280
  if (gsColor["color.header"].nontrivial()) return gsColor["color.header"].colorize(input);
1,208✔
281

282
  return input;
124✔
283
}
284

285
////////////////////////////////////////////////////////////////////////////////
286
std::string colorizeFootnote(const std::string& input) {
72✔
287
  if (gsColor["color.footnote"].nontrivial()) return gsColor["color.footnote"].colorize(input);
256✔
288

289
  return input;
16✔
290
}
291

292
////////////////////////////////////////////////////////////////////////////////
293
std::string colorizeError(const std::string& input) {
1✔
294
  if (gsColor["color.error"].nontrivial()) return gsColor["color.error"].colorize(input);
4✔
295

296
  return input;
×
297
}
298

299
////////////////////////////////////////////////////////////////////////////////
300
std::string colorizeDebug(const std::string& input) {
8✔
301
  if (gsColor["color.debug"].nontrivial()) return gsColor["color.debug"].colorize(input);
32✔
302

303
  return input;
×
304
}
305

306
////////////////////////////////////////////////////////////////////////////////
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc