• 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

95.42
/src/commands/CmdCustom.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 <CmdCustom.h>
31
#include <Context.h>
32
#include <Filter.h>
33
#include <Lexer.h>
34
#include <Version.h>
35
#include <ViewTask.h>
36
#include <format.h>
37
#include <main.h>
38
#include <shared.h>
39
#include <stdlib.h>
40
#include <util.h>
41

42
#include <algorithm>
43
#include <iostream>
44
#include <map>
45
#include <sstream>
46
#include <vector>
47

48
////////////////////////////////////////////////////////////////////////////////
49
CmdCustom::CmdCustom(const std::string& keyword, const std::string& usage,
75,038✔
50
                     const std::string& description) {
75,038✔
51
  _keyword = keyword;
75,038✔
52
  _usage = usage;
75,038✔
53
  _description = description;
75,038✔
54
  _read_only = true;
75,038✔
55
  _displays_id = true;
75,038✔
56
  _needs_gc = true;
75,038✔
57
  _uses_context = true;
75,038✔
58
  _accepts_filter = true;
75,038✔
59
  _accepts_modifications = false;
75,038✔
60
  _accepts_miscellaneous = false;
75,038✔
61
  _category = Category::report;
75,038✔
62
}
75,038✔
63

64
////////////////////////////////////////////////////////////////////////////////
65
// Whether a report uses context is defined by the report.<name>.context
66
// configuration variable.
67
//
68
bool CmdCustom::uses_context() const {
2,316✔
69
  auto config = Context::getContext().config;
2,316✔
70
  auto key = "report." + _keyword + ".context";
2,316✔
71

72
  if (config.has(key))
2,316✔
73
    return config.getBoolean(key);
2,096✔
74
  else
75
    return _uses_context;
220✔
76
}
2,316✔
77

78
////////////////////////////////////////////////////////////////////////////////
79
int CmdCustom::execute(std::string& output) {
642✔
80
  auto rc = 0;
642✔
81

82
  // Load report configuration.
83
  auto reportColumns = Context::getContext().config.get("report." + _keyword + ".columns");
1,284✔
84
  auto reportLabels = Context::getContext().config.get("report." + _keyword + ".labels");
1,284✔
85
  auto reportSort = Context::getContext().config.get("report." + _keyword + ".sort");
1,284✔
86
  auto reportFilter = Context::getContext().config.get("report." + _keyword + ".filter");
1,284✔
87

88
  auto columns = split(reportColumns, ',');
642✔
89
  validateReportColumns(columns);
642✔
90

91
  auto labels = split(reportLabels, ',');
642✔
92

93
  if (columns.size() != labels.size() && labels.size() != 0)
642✔
94
    throw format("There are different numbers of columns and labels for report '{1}'.", _keyword);
1✔
95

96
  auto sortOrder = split(reportSort, ',');
641✔
97
  if (sortOrder.size() != 0 && sortOrder[0] != "none") validateSortColumns(sortOrder);
641✔
98

99
  // Add the report filter to any existing filter.
100
  if (reportFilter != "") Context::getContext().cli2.addFilter(reportFilter);
641✔
101

102
  // Make sure reccurent tasks are generated.
103
  handleUntil();
641✔
104
  handleRecurrence();
641✔
105

106
  // Apply filter.
107
  Filter filter;
641✔
108
  std::vector<Task> filtered;
641✔
109
  filter.subset(filtered);
641✔
110

111
  std::vector<int> sequence;
640✔
112
  if (sortOrder.size() && sortOrder[0] == "none") {
640✔
113
    // Assemble a sequence vector that represents the tasks listed in
114
    // Context::getContext ().cli2._uuid_ranges, in the order in which they appear. This
115
    // equates to no sorting, just a specified order.
116
    sortOrder.clear();
1✔
117
    for (auto& i : Context::getContext().cli2._uuid_list)
4✔
118
      for (unsigned int t = 0; t < filtered.size(); ++t)
12✔
119
        if (filtered[t].get("uuid") == i) sequence.push_back(t);
9✔
120
  } else {
121
    // There is a sortOrder, so sorting will take place, which means the initial
122
    // order of sequence is ascending.
123
    for (unsigned int i = 0; i < filtered.size(); ++i) sequence.push_back(i);
1,973✔
124

125
    // Sort the tasks.
126
    if (sortOrder.size()) sort_tasks(filtered, sequence, reportSort);
639✔
127
  }
128

129
  // Configure the view.
130
  ViewTask view;
640✔
131
  view.width(Context::getContext().getWidth());
640✔
132
  view.leftMargin(Context::getContext().config.getInteger("indent.report"));
640✔
133
  view.extraPadding(Context::getContext().config.getInteger("row.padding"));
640✔
134
  view.intraPadding(Context::getContext().config.getInteger("column.padding"));
640✔
135

136
  if (Context::getContext().color()) {
640✔
137
    Color label(Context::getContext().config.get("color.label"));
20✔
138
    view.colorHeader(label);
20✔
139

140
    Color label_sort(Context::getContext().config.get("color.label.sort"));
20✔
141
    view.colorSortHeader(label_sort);
20✔
142

143
    // If an alternating row color is specified, notify the table.
144
    Color alternate(Context::getContext().config.get("color.alternate"));
20✔
145
    if (alternate.nontrivial()) {
20✔
146
      view.colorOdd(alternate);
3✔
147
      view.intraColorOdd(alternate);
3✔
148
    }
149
  }
150

151
  // Capture columns that are sorted.
152
  std::vector<std::string> sortColumns;
640✔
153

154
  // Add the break columns, if any.
155
  for (const auto& so : sortOrder) {
2,172✔
156
    std::string name;
1,532✔
157
    bool ascending;
158
    bool breakIndicator;
159
    Context::getContext().decomposeSortField(so, name, ascending, breakIndicator);
1,532✔
160

161
    if (breakIndicator) view.addBreak(name);
1,532✔
162

163
    sortColumns.push_back(name);
1,532✔
164
  }
1,532✔
165

166
  // Add the columns and labels.
167
  for (unsigned int i = 0; i < columns.size(); ++i) {
7,853✔
168
    Column* c = Column::factory(columns[i], _keyword);
7,226✔
169
    if (i < labels.size()) c->setLabel(labels[i]);
7,213✔
170

171
    bool sort = std::find(sortColumns.begin(), sortColumns.end(), c->name()) != sortColumns.end()
7,213✔
172
                    ? true
7,213✔
173
                    : false;
174

175
    view.add(c, sort);
7,213✔
176
  }
177

178
  // How many lines taken up by table header?
179
  int table_header = 0;
627✔
180
  if (Context::getContext().verbose("label")) {
627✔
181
    if (Context::getContext().color() && Context::getContext().config.getBoolean("fontunderline"))
488✔
182
      table_header = 1;  // Underlining doesn't use extra line.
17✔
183
    else
184
      table_header = 2;  // Dashes use an extra line.
471✔
185
  }
186

187
  // Report output can be limited by rows or lines.
188
  auto maxrows = 0;
627✔
189
  auto maxlines = 0;
627✔
190
  Context::getContext().getLimits(maxrows, maxlines);
627✔
191

192
  // Adjust for fluff in the output.
193
  if (maxlines)
627✔
194
    maxlines -=
48✔
195
        table_header + (Context::getContext().verbose("blank") ? 1 : 0) +
96✔
196
        (Context::getContext().verbose("footnote") ? Context::getContext().footnotes.size() : 0) +
96✔
197
        (Context::getContext().verbose("affected") ? 1 : 0) +
96✔
198
        Context::getContext().config.getInteger("reserved.lines");  // For prompt, etc.
48✔
199

200
  // Render.
201
  std::stringstream out;
627✔
202
  if (filtered.size()) {
627✔
203
    view.truncateRows(maxrows);
556✔
204
    view.truncateLines(maxlines);
556✔
205

206
    out << optionalBlankLine() << view.render(filtered, sequence) << optionalBlankLine();
556✔
207

208
    // Print the number of rendered tasks
209
    if (Context::getContext().verbose("affected")) {
556✔
210
      out << (filtered.size() == 1 ? "1 task" : format("{1} tasks", filtered.size()));
429✔
211

212
      if (maxrows && maxrows < (int)filtered.size()) out << ", " << format("{1} shown", maxrows);
429✔
213

214
      if (maxlines && maxlines < (int)filtered.size())
429✔
215
        out << ", " << format("truncated to {1} lines", maxlines - table_header);
1✔
216

217
      out << '\n';
429✔
218
    }
219
  } else {
220
    Context::getContext().footnote("No matches.");
71✔
221
    rc = 1;
71✔
222
  }
223

224
  // Inform user about the new release highlights if not presented yet
225
  Version news_version(Context::getContext().config.get("news.version"));
627✔
226
  Version current_version = Version::Current();
627✔
227
  auto should_nag = news_version != current_version && Context::getContext().verbose("news");
627✔
228
  if (should_nag) {
627✔
229
    std::ostringstream notice;
486✔
230
    notice << "Recently upgraded to " << current_version
486✔
231
           << ". "
232
              "Please run 'task news' to read highlights about the new release.";
486✔
233
    if (Context::getContext().verbose("footnote"))
486✔
234
      Context::getContext().footnote(notice.str());
486✔
NEW
235
    else if (Context::getContext().verbose("header"))
×
NEW
236
      Context::getContext().header(notice.str());
×
237
  }
486✔
238

239
  std::string location = (Context::getContext().data_dir);
627✔
240
  File pending_data = File(location + "/pending.data");
627✔
241
  if (pending_data.exists()) {
627✔
NEW
242
    Color warning = Color(Context::getContext().config.get("color.warning"));
×
NEW
243
    std::cerr << warning.colorize(format("Found existing '*.data' files in {1}", location)) << "\n";
×
244
    std::cerr << "  Taskwarrior's storage format changed in 3.0, requiring a manual migration.\n";
×
245
    std::cerr << "  See https://github.com/GothenburgBitFactory/taskwarrior/releases.\n";
×
246
  }
247

248
  feedback_backlog();
627✔
249
  output = out.str();
627✔
250
  return rc;
627✔
251
}
784✔
252

253
////////////////////////////////////////////////////////////////////////////////
254
void CmdCustom::validateReportColumns(std::vector<std::string>& columns) {
642✔
255
  for (auto& col : columns) legacyColumnMap(col);
7,893✔
256
}
642✔
257

258
////////////////////////////////////////////////////////////////////////////////
259
void CmdCustom::validateSortColumns(std::vector<std::string>& columns) {
586✔
260
  for (auto& col : columns) legacySortColumnMap(col);
2,119✔
261
}
586✔
262

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