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

GothenburgBitFactory / taskwarrior / 9829041530

07 Jul 2024 05:19PM CUT coverage: 84.053% (-0.03%) from 84.083%
9829041530

push

github

web-flow
Remove duplicate check from task diag (#3545)

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

7 existing lines in 3 files now uncovered.

19202 of 22845 relevant lines covered (84.05%)

20027.35 hits per line

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

96.03
/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
#include <CmdCustom.h>
29
#include <random>
30
#include <sstream>
31
#include <map>
32
#include <vector>
33
#include <iostream>
34
#include <algorithm>
35
#include <stdlib.h>
36
#include <Context.h>
37
#include <Filter.h>
38
#include <Lexer.h>
39
#include <Version.h>
40
#include <ViewTask.h>
41
#include <format.h>
42
#include <shared.h>
43
#include <util.h>
44
#include <main.h>
45

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

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

74
  if (config.has (key))
2,292✔
75
    return config.getBoolean (key);
2,072✔
76
  else
77
    return _uses_context;
220✔
78
}
2,292✔
79

80
////////////////////////////////////////////////////////////////////////////////
81
int CmdCustom::execute (std::string& output)
636✔
82
{
83
  auto rc = 0;
636✔
84

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

91
  auto columns = split (reportColumns, ',');
636✔
92
  validateReportColumns (columns);
636✔
93

94
  auto labels = split (reportLabels, ',');
636✔
95

96
  if (columns.size () != labels.size () && labels.size () != 0)
636✔
97
    throw format ("There are different numbers of columns and labels for report '{1}'.", _keyword);
1✔
98

99
  auto sortOrder = split (reportSort, ',');
635✔
100
  if (sortOrder.size () != 0 &&
1,216✔
101
      sortOrder[0] != "none")
581✔
102
    validateSortColumns (sortOrder);
580✔
103

104
  // Add the report filter to any existing filter.
105
  if (reportFilter != "")
635✔
106
    Context::getContext ().cli2.addFilter (reportFilter);
486✔
107

108
  // Make sure reccurent tasks are generated.
109
  handleUntil ();
635✔
110
  handleRecurrence ();
635✔
111

112
  // Apply filter.
113
  Filter filter;
635✔
114
  std::vector <Task> filtered;
635✔
115
  filter.subset (filtered);
635✔
116

117
  std::vector <int> sequence;
634✔
118
  if (sortOrder.size () &&
1,214✔
119
      sortOrder[0] == "none")
580✔
120
  {
121
    // Assemble a sequence vector that represents the tasks listed in
122
    // Context::getContext ().cli2._uuid_ranges, in the order in which they appear. This
123
    // equates to no sorting, just a specified order.
124
    sortOrder.clear ();
1✔
125
    for (auto& i : Context::getContext ().cli2._uuid_list)
4✔
126
      for (unsigned int t = 0; t < filtered.size (); ++t)
12✔
127
        if (filtered[t].get ("uuid") == i)
9✔
128
          sequence.push_back (t);
3✔
129
  }
130
  else
131
  {
132
    // There is a sortOrder, so sorting will take place, which means the initial
133
    // order of sequence is ascending.
134
    for (unsigned int i = 0; i < filtered.size (); ++i)
1,952✔
135
      sequence.push_back (i);
1,319✔
136

137
    // Sort the tasks.
138
    if (sortOrder.size ())
633✔
139
      sort_tasks (filtered, sequence, reportSort);
579✔
140
  }
141

142
  // Configure the view.
143
  ViewTask view;
634✔
144
  view.width (Context::getContext ().getWidth ());
634✔
145
  view.leftMargin (Context::getContext ().config.getInteger ("indent.report"));
634✔
146
  view.extraPadding (Context::getContext ().config.getInteger ("row.padding"));
634✔
147
  view.intraPadding (Context::getContext ().config.getInteger ("column.padding"));
634✔
148

149
  if (Context::getContext ().color ())
634✔
150
  {
151
    Color label (Context::getContext ().config.get ("color.label"));
20✔
152
    view.colorHeader (label);
20✔
153

154
    Color label_sort (Context::getContext ().config.get ("color.label.sort"));
20✔
155
    view.colorSortHeader (label_sort);
20✔
156

157
    // If an alternating row color is specified, notify the table.
158
    Color alternate (Context::getContext ().config.get ("color.alternate"));
20✔
159
    if (alternate.nontrivial ())
20✔
160
    {
161
      view.colorOdd (alternate);
3✔
162
      view.intraColorOdd (alternate);
3✔
163
    }
164
  }
165

166
  // Capture columns that are sorted.
167
  std::vector <std::string> sortColumns;
634✔
168

169
  // Add the break columns, if any.
170
  for (const auto& so : sortOrder)
2,151✔
171
  {
172
    std::string name;
1,517✔
173
    bool ascending;
174
    bool breakIndicator;
175
    Context::getContext ().decomposeSortField (so, name, ascending, breakIndicator);
1,517✔
176

177
    if (breakIndicator)
1,517✔
178
      view.addBreak (name);
5✔
179

180
    sortColumns.push_back (name);
1,517✔
181
  }
1,517✔
182

183
  // Add the columns and labels.
184
  for (unsigned int i = 0; i < columns.size (); ++i)
7,769✔
185
  {
186
    Column* c = Column::factory (columns[i], _keyword);
7,148✔
187
    if (i < labels.size ())
7,135✔
188
      c->setLabel (labels[i]);
7,003✔
189

190
    bool sort = std::find (sortColumns.begin (), sortColumns.end (), c->name ()) != sortColumns.end ()
7,135✔
191
                  ? true
7,135✔
192
                  : false;
193

194
    view.add (c, sort);
7,135✔
195
  }
196

197
  // How many lines taken up by table header?
198
  int table_header = 0;
621✔
199
  if (Context::getContext ().verbose ("label"))
621✔
200
  {
201
    if (Context::getContext ().color () && Context::getContext ().config.getBoolean ("fontunderline"))
484✔
202
      table_header = 1;  // Underlining doesn't use extra line.
17✔
203
    else
204
      table_header = 2;  // Dashes use an extra line.
467✔
205
  }
206

207
  // Report output can be limited by rows or lines.
208
  auto maxrows = 0;
621✔
209
  auto maxlines = 0;
621✔
210
  Context::getContext ().getLimits (maxrows, maxlines);
621✔
211

212
  // Adjust for fluff in the output.
213
  if (maxlines)
621✔
214
    maxlines -= table_header
45✔
215
              + (Context::getContext ().verbose ("blank") ? 1 : 0)
90✔
216
              + (Context::getContext ().verbose ("footnote") ? Context::getContext ().footnotes.size () : 0)
90✔
217
              + (Context::getContext ().verbose ("affected") ? 1 : 0)
90✔
218
              + Context::getContext ().config.getInteger ("reserved.lines");  // For prompt, etc.
45✔
219

220
  // Render.
221
  std::stringstream out;
621✔
222
  if (filtered.size ())
621✔
223
  {
224
    view.truncateRows (maxrows);
550✔
225
    view.truncateLines (maxlines);
550✔
226

227
    out << optionalBlankLine ()
228
        << view.render (filtered, sequence)
550✔
229
        << optionalBlankLine ();
550✔
230

231
    // Print the number of rendered tasks
232
    if (Context::getContext ().verbose ("affected"))
550✔
233
    {
234
      out << (filtered.size () == 1
425✔
235
                ?  "1 task"
850✔
236
                : format ("{1} tasks", filtered.size ()));
425✔
237

238
      if (maxrows && maxrows < (int)filtered.size ())
425✔
239
        out << ", " << format ("{1} shown", maxrows);
5✔
240

241
      if (maxlines && maxlines < (int)filtered.size ())
425✔
242
        out << ", "
243
            << format ("truncated to {1} lines", maxlines - table_header);
1✔
244

245
      out << '\n';
425✔
246
    }
247
  }
248
  else
249
  {
250
    Context::getContext ().footnote ("No matches.");
71✔
251
    rc = 1;
71✔
252
  }
253

254
  // Inform user about the new release highlights if not presented yet
255
  Version news_version(Context::getContext ().config.get ("news.version"));
621✔
256
  Version current_version = Version::Current();
621✔
257
  if (news_version != current_version)
621✔
258
  {
259
    std::random_device device;
621✔
260
    std::mt19937 random_generator(device());
621✔
261
    std::uniform_int_distribution<std::mt19937::result_type> twentyfive_percent(1, 4);
621✔
262

263
    // 1 in 10 chance to display the message.
264
    if (twentyfive_percent(random_generator) == 4)
621✔
265
    {
266
      std::ostringstream notice;
134✔
267
      notice << "Recently upgraded to " << current_version << ". "
134✔
268
        "Please run 'task news' to read highlights about the new release.";
134✔
269
      if (Context::getContext ().verbose ("footnote"))
134✔
270
        Context::getContext ().footnote (notice.str());
101✔
271
      else if (Context::getContext ().verbose ("header"))
33✔
UNCOV
272
        Context::getContext ().header (notice.str());
×
273
    }
134✔
274
  }
621✔
275

276
  std::string location  = (Context::getContext ().data_dir);
621✔
277
  File pending_data = File (location + "/pending.data");
621✔
278
  if (pending_data.exists()) {
621✔
279
    Color warning = Color (Context::getContext ().config.get ("color.warning"));
×
280
    std::cerr << warning.colorize (
×
281
      format ("Found existing '*.data' files in {1}", location)) << "\n";
×
282
    std::cerr << "  Taskwarrior's storage format changed in 3.0, requiring a manual migration.\n";
×
283
    std::cerr << "  See https://github.com/GothenburgBitFactory/taskwarrior/releases.\n";
×
284
  }
285

286
  feedback_backlog ();
621✔
287
  output = out.str ();
621✔
288
  return rc;
621✔
289
}
778✔
290

291
////////////////////////////////////////////////////////////////////////////////
292
void CmdCustom::validateReportColumns (std::vector <std::string>& columns)
636✔
293
{
294
  for (auto& col : columns)
7,809✔
295
    legacyColumnMap (col);
7,173✔
296
}
636✔
297

298
////////////////////////////////////////////////////////////////////////////////
299
void CmdCustom::validateSortColumns (std::vector <std::string>& columns)
580✔
300
{
301
  for (auto& col : columns)
2,098✔
302
    legacySortColumnMap (col);
1,518✔
303
}
580✔
304

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