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

GothenburgBitFactory / taskwarrior / 11965242199

22 Nov 2024 02:19AM UTC coverage: 85.482% (-0.01%) from 85.496%
11965242199

push

github

web-flow
Updating the URL for Migration Documentation (#3694)

0 of 1 new or added line in 1 file covered. (0.0%)

3 existing lines in 2 files now uncovered.

19154 of 22407 relevant lines covered (85.48%)

22974.05 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,
76,840✔
50
                     const std::string& description) {
76,840✔
51
  _keyword = keyword;
76,840✔
52
  _usage = usage;
76,840✔
53
  _description = description;
76,840✔
54
  _read_only = true;
76,840✔
55
  _displays_id = true;
76,840✔
56
  _needs_gc = true;
76,840✔
57
  _uses_context = true;
76,840✔
58
  _accepts_filter = true;
76,840✔
59
  _accepts_modifications = false;
76,840✔
60
  _accepts_miscellaneous = false;
76,840✔
61
  _category = Category::report;
76,840✔
62
}
76,840✔
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,394✔
69
  auto config = Context::getContext().config;
2,394✔
70
  auto key = "report." + _keyword + ".context";
2,394✔
71

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

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

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

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

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

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

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

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

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

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

111
  std::vector<int> sequence;
661✔
112
  if (sortOrder.size() && sortOrder[0] == "none") {
661✔
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);
2,021✔
124

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

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

136
  if (Context::getContext().color()) {
661✔
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;
661✔
153

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

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

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

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

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

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

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

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

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

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

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

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

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

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

217
      out << '\n';
450✔
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"));
648✔
226
  Version current_version = Version::Current();
648✔
227
  auto should_nag = news_version != current_version && Context::getContext().verbose("news");
648✔
228
  if (should_nag) {
648✔
229
    std::ostringstream notice;
507✔
230
    notice << "Recently upgraded to " << current_version
507✔
231
           << ". "
232
              "Please run 'task news' to read highlights about the new release.";
507✔
233
    if (Context::getContext().verbose("footnote"))
507✔
234
      Context::getContext().footnote(notice.str());
507✔
235
    else if (Context::getContext().verbose("header"))
×
236
      Context::getContext().header(notice.str());
×
237
  }
507✔
238

239
  std::string location = (Context::getContext().data_dir);
648✔
240
  File pending_data = File(location + "/pending.data");
648✔
241
  if (pending_data.exists()) {
648✔
242
    Color warning = Color(Context::getContext().config.get("color.warning"));
×
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";
×
NEW
245
    std::cerr << "  See https://taskwarrior.org/docs/upgrade-3/\n";
×
246
  }
247

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

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

258
////////////////////////////////////////////////////////////////////////////////
259
void CmdCustom::validateSortColumns(std::vector<std::string>& columns) {
607✔
260
  for (auto& col : columns) legacySortColumnMap(col);
2,179✔
261
}
607✔
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