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

GothenburgBitFactory / taskwarrior / 10119318781

27 Jul 2024 12:30AM UTC coverage: 84.363% (+0.08%) from 84.286%
10119318781

push

github

web-flow
Make `task news` nag configurable and deterministic (#3567)

This patch fixes #3497.

8 of 10 new or added lines in 2 files covered. (80.0%)

9 existing lines in 4 files now uncovered.

19347 of 22933 relevant lines covered (84.36%)

20359.83 hits per line

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

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

45
////////////////////////////////////////////////////////////////////////////////
46
CmdCustom::CmdCustom (
75,038✔
47
  const std::string& keyword,
48
  const std::string& usage,
49
  const std::string& description)
75,038✔
50
{
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
{
70
  auto config = Context::getContext ().config;
2,316✔
71
  auto key = "report." + _keyword + ".context";
2,316✔
72

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

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

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

90
  auto columns = split (reportColumns, ',');
642✔
91
  validateReportColumns (columns);
642✔
92

93
  auto labels = split (reportLabels, ',');
642✔
94

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

98
  auto sortOrder = split (reportSort, ',');
641✔
99
  if (sortOrder.size () != 0 &&
1,228✔
100
      sortOrder[0] != "none")
587✔
101
    validateSortColumns (sortOrder);
586✔
102

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

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

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

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

136
    // Sort the tasks.
137
    if (sortOrder.size ())
639✔
138
      sort_tasks (filtered, sequence, reportSort);
585✔
139
  }
140

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

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

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

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

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

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

176
    if (breakIndicator)
1,532✔
177
      view.addBreak (name);
5✔
178

179
    sortColumns.push_back (name);
1,532✔
180
  }
1,532✔
181

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

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

193
    view.add (c, sort);
7,213✔
194
  }
195

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

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

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

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

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

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

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

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

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

253
  // Inform user about the new release highlights if not presented yet
254
  Version news_version(Context::getContext ().config.get ("news.version"));
627✔
255
  Version current_version = Version::Current();
627✔
256
  auto should_nag = news_version != current_version && Context::getContext ().verbose ("news");
627✔
257
  if (should_nag)
627✔
258
  {
259
    std::ostringstream notice;
486✔
260
    notice << "Recently upgraded to " << current_version << ". "
486✔
261
      "Please run 'task news' to read highlights about the new release.";
486✔
262
    if (Context::getContext ().verbose ("footnote"))
486✔
263
      Context::getContext ().footnote (notice.str());
486✔
NEW
264
    else if (Context::getContext ().verbose ("header"))
×
NEW
265
      Context::getContext ().header (notice.str());
×
266
  }
486✔
267

268
  std::string location  = (Context::getContext ().data_dir);
627✔
269
  File pending_data = File (location + "/pending.data");
627✔
270
  if (pending_data.exists()) {
627✔
271
    Color warning = Color (Context::getContext ().config.get ("color.warning"));
×
272
    std::cerr << warning.colorize (
×
273
      format ("Found existing '*.data' files in {1}", location)) << "\n";
×
274
    std::cerr << "  Taskwarrior's storage format changed in 3.0, requiring a manual migration.\n";
×
275
    std::cerr << "  See https://github.com/GothenburgBitFactory/taskwarrior/releases.\n";
×
276
  }
277

278
  feedback_backlog ();
627✔
279
  output = out.str ();
627✔
280
  return rc;
627✔
281
}
784✔
282

283
////////////////////////////////////////////////////////////////////////////////
284
void CmdCustom::validateReportColumns (std::vector <std::string>& columns)
642✔
285
{
286
  for (auto& col : columns)
7,893✔
287
    legacyColumnMap (col);
7,251✔
288
}
642✔
289

290
////////////////////////////////////////////////////////////////////////////////
291
void CmdCustom::validateSortColumns (std::vector <std::string>& columns)
586✔
292
{
293
  for (auto& col : columns)
2,119✔
294
    legacySortColumnMap (col);
1,533✔
295
}
586✔
296

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