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

GothenburgBitFactory / taskwarrior / 10152339701

29 Jul 2024 09:45PM UTC coverage: 84.437% (+0.07%) from 84.372%
10152339701

push

github

web-flow
Merge pull request #3566 from felixschurk/add-clang-format

Add clang-format to enforce style guide

12359 of 13760 new or added lines in 147 files covered. (89.82%)

123 existing lines in 42 files now uncovered.

19070 of 22585 relevant lines covered (84.44%)

19724.02 hits per line

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

91.94
/src/commands/CmdShow.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 <CmdShow.h>
31
#include <Context.h>
32
#include <FS.h>
33
#include <Table.h>
34
#include <format.h>
35
#include <main.h>
36
#include <util.h>
37

38
#include <algorithm>
39
#include <sstream>
40
#include <vector>
41

42
#define STRING_CMD_SHOW_DIFFER_COLOR "These are highlighted in {1} above."
43
#define STRING_CMD_SHOW_CONFIG_ERROR \
44
  "Configuration error: {1} contains an unrecognized value '{2}'."
45

46
extern std::string configurationDefaults;
47

48
////////////////////////////////////////////////////////////////////////////////
49
CmdShow::CmdShow() {
4,389✔
50
  _keyword = "show";
4,389✔
51
  _usage = "task          show [all | substring]";
4,389✔
52
  _description = "Shows all configuration variables or subset";
4,389✔
53
  _read_only = true;
4,389✔
54
  _displays_id = false;
4,389✔
55
  _needs_gc = false;
4,389✔
56
  _uses_context = false;
4,389✔
57
  _accepts_filter = false;
4,389✔
58
  _accepts_modifications = false;
4,389✔
59
  _accepts_miscellaneous = true;
4,389✔
60
  _category = Command::Category::config;
4,389✔
61
}
4,389✔
62

63
////////////////////////////////////////////////////////////////////////////////
64
int CmdShow::execute(std::string& output) {
27✔
65
  int rc = 0;
27✔
66
  std::stringstream out;
27✔
67

68
  // Obtain the arguments from the description.  That way, things like '--'
69
  // have already been handled.
70
  std::vector<std::string> words = Context::getContext().cli2.getWords();
27✔
71
  if (words.size() > 1) throw std::string("You can only specify 'all' or a search string.");
27✔
72

73
  int width = Context::getContext().getWidth();
26✔
74

75
  // Complain about configuration variables that are not recognized.
76
  // These are the regular configuration variables.
77
  // Note that there is a leading and trailing space, to make it easier to
78
  // search for whole words.
79
  std::string recognized =
80
      " abbreviation.minimum"
81
      " active.indicator"
82
      " allow.empty.filter"
83
      " avoidlastcolumn"
84
      " bulk"
85
      " calendar.details"
86
      " calendar.details.report"
87
      " calendar.holidays"
88
      " calendar.legend"
89
      " calendar.monthsperline"
90
      " calendar.offset"
91
      " calendar.offset.value"
92
      " color"
93
      " color.active"
94
      " color.alternate"
95
      " color.blocked"
96
      " color.blocking"
97
      " color.burndown.done"
98
      " color.burndown.pending"
99
      " color.burndown.started"
100
      " color.calendar.due"
101
      " color.calendar.due.today"
102
      " color.calendar.holiday"
103
      " color.calendar.scheduled"
104
      " color.calendar.overdue"
105
      " color.calendar.today"
106
      " color.calendar.weekend"
107
      " color.calendar.weeknumber"
108
      " color.completed"
109
      " color.debug"
110
      " color.deleted"
111
      " color.due"
112
      " color.due.today"
113
      " color.warning"
114
      " color.error"
115
      " color.footnote"
116
      " color.header"
117
      " color.history.add"
118
      " color.history.delete"
119
      " color.history.done"
120
      " color.label"
121
      " color.label.sort"
122
      " color.overdue"
123
      " color.recurring"
124
      " color.scheduled"
125
      " color.summary.background"
126
      " color.summary.bar"
127
      " color.sync.added"
128
      " color.sync.changed"
129
      " color.sync.rejected"
130
      " color.tagged"
131
      " color.undo.after"
132
      " color.undo.before"
133
      " color.until"
134
      " column.padding"
135
      " complete.all.tags"
136
      " confirmation"
137
      " context"
138
      " data.location"
139
      " dateformat"
140
      " dateformat.annotation"
141
      " dateformat.edit"
142
      " dateformat.holiday"
143
      " dateformat.info"
144
      " dateformat.report"
145
      " date.iso"
146
      " debug"
147
      " debug.hooks"
148
      " debug.parser"
149
      " default.command"
150
      " default.due"
151
      " default.project"
152
      " default.scheduled"
153
      " defaultheight"
154
      " defaultwidth"
155
      " default.timesheet.filter"
156
      " dependency.confirmation"
157
      " dependency.indicator"
158
      " dependency.reminder"
159
      " detection"
160
      " displayweeknumber"
161
      " due"
162
      " editor"
163
      " exit.on.missing.db"
164
      " purge.on-sync"
165
      " expressions"
166
      " fontunderline"
167
      " gc"
168
      " hooks"
169
      " hooks.location"
170
      " hyphenate"
171
      " indent.annotation"
172
      " indent.report"
173
      " journal.info"
174
      " journal.time"
175
      " journal.time.start.annotation"
176
      " journal.time.stop.annotation"
177
      " json.array"
178
      " limit"
179
      " list.all.projects"
180
      " list.all.tags"
181
      " nag"
182
      " news.version"
183
      " obfuscate"
184
      " print.empty.columns"
185
      " recurrence"
186
      " recurrence.confirmation"
187
      " recurrence.indicator"
188
      " recurrence.limit"
189
      " regex"
190
      " reserved.lines"
191
      " row.padding"
192
      " rule.color.merge"
193
      " rule.precedence.color"
194
      " search.case.sensitive"
195
      " sugar"
196
      " summary.all.projects"
197
      " sync.local.server_dir"
198
      " sync.gcp.credential_path"
199
      " sync.gcp.bucket"
200
      " sync.server.client_id"
201
      " sync.encryption_secret"
202
      " sync.server.url"
203
      " sync.server.origin"
204
      " tag.indicator"
205
      " undo.style"
206
      " urgency.active.coefficient"
207
      " urgency.scheduled.coefficient"
208
      " urgency.annotations.coefficient"
209
      " urgency.blocked.coefficient"
210
      " urgency.blocking.coefficient"
211
      " urgency.due.coefficient"
212
      " urgency.project.coefficient"
213
      " urgency.tags.coefficient"
214
      " urgency.waiting.coefficient"
215
      " urgency.age.coefficient"
216
      " urgency.age.max"
217
      " urgency.inherit"
218
      " verbose"
219
      " weekstart"
220
      " xterm.title"
221
      " ";
26✔
222

223
  // This configuration variable is supported, but not documented.  It exists
224
  // so that unit tests can force color to be on even when the output from task
225
  // is redirected to a file, or stdout is not a tty.
226
  recognized += "_forcecolor ";
26✔
227

228
  std::vector<std::string> unrecognized;
26✔
229
  for (auto& i : Context::getContext().config) {
6,421✔
230
    // Disallow partial matches by tacking a leading and trailing space on each
231
    // variable name.
232
    std::string pattern = ' ' + i.first + ' ';
6,395✔
233
    if (recognized.find(pattern) == std::string::npos) {
6,395✔
234
      // These are special configuration variables, because their name is
235
      // dynamic.
236
      if (i.first.substr(0, 14) != "color.keyword." && i.first.substr(0, 14) != "color.project." &&
6,288✔
237
          i.first.substr(0, 10) != "color.tag." && i.first.substr(0, 10) != "color.uda." &&
6,262✔
238
          i.first.substr(0, 8) != "context." && i.first.substr(0, 8) != "holiday." &&
6,132✔
239
          i.first.substr(0, 7) != "report." && i.first.substr(0, 6) != "alias." &&
6,132✔
240
          i.first.substr(0, 5) != "hook." && i.first.substr(0, 4) != "uda." &&
3,332✔
241
          i.first.substr(0, 8) != "default." && i.first.substr(0, 21) != "urgency.user.project." &&
3,254✔
242
          i.first.substr(0, 17) != "urgency.user.tag." &&
3,254✔
243
          i.first.substr(0, 21) != "urgency.user.keyword." &&
9,516✔
244
          i.first.substr(0, 12) != "urgency.uda.") {
3,228✔
245
        unrecognized.push_back(i.first);
6✔
246
      }
247
    }
248
  }
6,395✔
249

250
  // Find all the values that match the defaults, for highlighting.
251
  std::vector<std::string> default_values;
26✔
252
  Configuration default_config;
26✔
253
  default_config.parse(configurationDefaults);
26✔
254

255
  for (auto& i : Context::getContext().config)
6,421✔
256
    if (i.second != default_config.get(i.first)) default_values.push_back(i.first);
6,395✔
257

258
  // Create output view.
259
  Table view;
26✔
260
  view.width(width);
26✔
261
  view.add("Config Variable");
26✔
262
  view.add("Value");
26✔
263
  setHeaderUnderline(view);
26✔
264

265
  Color error;
26✔
266
  Color warning;
26✔
267
  if (Context::getContext().color()) {
26✔
NEW
268
    error = Color(Context::getContext().config.get("color.error"));
×
NEW
269
    warning = Color(Context::getContext().config.get("color.warning"));
×
270
  }
271

272
  bool issue_error = false;
26✔
273
  bool issue_warning = false;
26✔
274

275
  std::string section;
26✔
276

277
  // Look for the first plausible argument which could be a pattern
278
  if (words.size()) section = words[0];
26✔
279

280
  if (section == "all") section = "";
26✔
281

282
  std::string::size_type loc;
283
  for (auto& i : Context::getContext().config) {
6,421✔
284
    loc = i.first.find(section, 0);
6,395✔
285
    if (loc != std::string::npos) {
6,395✔
286
      // Look for unrecognized.
287
      Color color;
4,933✔
288
      if (std::find(unrecognized.begin(), unrecognized.end(), i.first) != unrecognized.end()) {
4,933✔
289
        issue_error = true;
4✔
290
        color = error;
4✔
291
      } else if (std::find(default_values.begin(), default_values.end(), i.first) !=
4,929✔
292
                 default_values.end()) {
9,858✔
293
        issue_warning = true;
160✔
294
        color = warning;
160✔
295
      }
296

297
      std::string value = i.second;
4,933✔
298
      int row = view.addRow();
4,933✔
299
      view.set(row, 0, i.first, color);
4,933✔
300
      view.set(row, 1, value, color);
4,933✔
301

302
      if (default_config[i.first] != value && default_config[i.first] != "") {
4,933✔
303
        row = view.addRow();
123✔
304
        view.set(row, 0, std::string("  ") + "Default value", color);
123✔
305
        view.set(row, 1, default_config[i.first], color);
123✔
306
      }
307
    }
4,933✔
308
  }
309

310
  out << '\n'
311
      << view.render() << (view.rows() == 0 ? "No matching configuration variables." : "")
26✔
312
      << (view.rows() == 0 ? "\n\n" : "\n");
26✔
313

314
  if (issue_warning) {
26✔
315
    out << "Some of your .taskrc variables differ from the default values.\n";
23✔
316

317
    if (Context::getContext().color() && warning.nontrivial())
23✔
NEW
318
      out << "  " << format(STRING_CMD_SHOW_DIFFER_COLOR, warning.colorize("color")) << "\n\n";
×
319
  }
320

321
  // Display the unrecognized variables.
322
  if (issue_error) {
26✔
323
    out << "Your .taskrc file contains these unrecognized variables:\n";
4✔
324

325
    for (auto& i : unrecognized) out << "  " << i << '\n';
8✔
326

327
    if (Context::getContext().color() && error.nontrivial())
4✔
NEW
328
      out << '\n' << format(STRING_CMD_SHOW_DIFFER_COLOR, error.colorize("color"));
×
329

330
    out << "\n\n";
4✔
331
  }
332

333
  out << legacyCheckForDeprecatedVariables();
26✔
334
  out << legacyCheckForDeprecatedColumns();
26✔
335

336
  // TODO Check for referenced but missing theme files.
337
  // TODO Check for referenced but missing string files.
338

339
  // Check for bad values in rc.calendar.details.
340
  std::string calendardetails = Context::getContext().config.get("calendar.details");
52✔
341
  if (calendardetails != "full" && calendardetails != "sparse" && calendardetails != "none")
26✔
NEW
342
    out << format(STRING_CMD_SHOW_CONFIG_ERROR, "calendar.details", calendardetails) << '\n';
×
343

344
  // Check for bad values in rc.calendar.holidays.
345
  std::string calendarholidays = Context::getContext().config.get("calendar.holidays");
52✔
346
  if (calendarholidays != "full" && calendarholidays != "sparse" && calendarholidays != "none")
26✔
NEW
347
    out << format(STRING_CMD_SHOW_CONFIG_ERROR, "calendar.holidays", calendarholidays) << '\n';
×
348

349
  // Verify installation.  This is mentioned in the documentation as the way
350
  // to ensure everything is properly installed.
351

352
  if (Context::getContext().config.size() == 0) {
26✔
UNCOV
353
    out << "Configuration error: .taskrc contains no entries.\n";
×
354
    rc = 1;
×
355
  } else {
356
    Directory location(Context::getContext().config.get("data.location"));
52✔
357

358
    if (location._data == "")
26✔
359
      out << "Configuration error: data.location not specified in .taskrc file.\n";
×
360

361
    if (!location.exists())
26✔
362
      out << "Configuration error: data.location contains a directory name that doesn't exist, or "
NEW
363
             "is unreadable.\n";
×
364
  }
26✔
365

366
  output = out.str();
26✔
367
  return rc;
26✔
368
}
28✔
369

370
////////////////////////////////////////////////////////////////////////////////
371
CmdShowRaw::CmdShowRaw() {
4,389✔
372
  _keyword = "_show";
4,389✔
373
  _usage = "task          _show";
4,389✔
374
  _description = "Shows all configuration settings in a machine-readable format";
4,389✔
375
  _read_only = true;
4,389✔
376
  _displays_id = false;
4,389✔
377
  _category = Command::Category::internal;
4,389✔
378
}
4,389✔
379

380
////////////////////////////////////////////////////////////////////////////////
381
int CmdShowRaw::execute(std::string& output) {
1✔
382
  // Get all the settings and sort alphabetically by name.
383
  auto all = Context::getContext().config.all();
1✔
384
  std::sort(all.begin(), all.end());
1✔
385

386
  // Display them all.
387
  std::stringstream out;
1✔
388
  for (auto& i : all) out << i << '=' << Context::getContext().config.get(i) << '\n';
246✔
389

390
  output = out.str();
1✔
391
  return 0;
1✔
392
}
1✔
393

394
////////////////////////////////////////////////////////////////////////////////
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

© 2026 Coveralls, Inc