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 <CmdReports.h>
29 #include <sstream>
30 #include <Context.h>
31 #include <Table.h>
32 #include <format.h>
33 #include <util.h>
34 
35 ////////////////////////////////////////////////////////////////////////////////
CmdReports()36 CmdReports::CmdReports ()
37 {
38   _keyword               = "reports";
39   _usage                 = "task          reports";
40   _description           = "Lists all supported reports";
41   _read_only             = true;
42   _displays_id           = false;
43   _needs_gc              = false;
44   _uses_context          = false;
45   _accepts_filter        = false;
46   _accepts_modifications = false;
47   _accepts_miscellaneous = false;
48   _category              = Command::Category::config;
49 }
50 
51 ////////////////////////////////////////////////////////////////////////////////
execute(std::string & output)52 int CmdReports::execute (std::string& output)
53 {
54   std::vector <std::string> reports;
55 
56   // Add custom reports.
57   for (auto& i : Context::getContext ().config)
58   {
59     if (i.first.substr (0, 7) == "report.")
60     {
61       std::string report = i.first.substr (7);
62       auto columns = report.find (".columns");
63       if (columns != std::string::npos)
64         reports.push_back (report.substr (0, columns));
65     }
66   }
67 
68   // Add known reports.
69   reports.push_back ("burndown.daily");
70   reports.push_back ("burndown.monthly");
71   reports.push_back ("burndown.weekly");
72   reports.push_back ("ghistory.annual");
73   reports.push_back ("ghistory.monthly");
74   reports.push_back ("history.annual");
75   reports.push_back ("history.monthly");
76   reports.push_back ("information");
77   reports.push_back ("projects");
78   reports.push_back ("summary");
79   reports.push_back ("tags");
80 
81   std::sort (reports.begin (), reports.end ());
82 
83   // Compose the output.
84   std::stringstream out;
85   Table view;
86   view.width (Context::getContext ().getWidth ());
87   view.add ("Report");
88   view.add ("Description");
89   setHeaderUnderline (view);
90 
91   for (auto& report : reports)
92   {
93     int row = view.addRow ();
94     view.set (row, 0, report);
95     view.set (row, 1, Context::getContext ().commands[report]->description ());
96   }
97 
98   out << optionalBlankLine ()
99       << view.render ()
100       << optionalBlankLine ()
101       << format ("{1} reports\n", reports.size ());
102 
103   output = out.str ();
104   return 0;
105 }
106 
107 ////////////////////////////////////////////////////////////////////////////////
108