Extract creation of tags table into TagsTableBuilder

Signed-off-by: Thomas Lauf <thomas.lauf@tngtech.com>
This commit is contained in:
Thomas Lauf 2023-10-16 15:30:55 +02:00 committed by Thomas Lauf
parent 8b7298373d
commit 65985bcd37
6 changed files with 216 additions and 21 deletions

View file

@ -27,8 +27,10 @@ set (timew_SRCS AtomicFile.cpp AtomicFile.h
Journal.cpp Journal.h Journal.cpp Journal.h
Range.cpp Range.h Range.cpp Range.h
Rules.cpp Rules.h Rules.cpp Rules.h
TagDescription.cpp TagDescription.h
TagInfo.cpp TagInfo.h TagInfo.cpp TagInfo.h
TagInfoDatabase.cpp TagInfoDatabase.h TagInfoDatabase.cpp TagInfoDatabase.h
TagsTable.cpp TagsTable.h
Transaction.cpp Transaction.h Transaction.cpp Transaction.h
TransactionsFactory.cpp TransactionsFactory.h TransactionsFactory.cpp TransactionsFactory.h
UndoAction.cpp UndoAction.h UndoAction.cpp UndoAction.h

35
src/TagDescription.cpp Normal file
View file

@ -0,0 +1,35 @@
//////////////////////////////////////////////////////////////////////////////
//
// Copyright 2023, Gothenburg Bit Factory.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// https://www.opensource.org/licenses/mit-license.php
//
//////////////////////////////////////////////////////////////////////////////
#include <Color.h>
#include <TagDescription.h>
#include <utility>
TagDescription::TagDescription (std::string name, Color color, std::string description) :
name (std::move (name)),
color (color),
description (std::move (description))
{}

42
src/TagDescription.h Normal file
View file

@ -0,0 +1,42 @@
//////////////////////////////////////////////////////////////////////////////
//
// Copyright 2023, Gothenburg Bit Factory.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// https://www.opensource.org/licenses/mit-license.php
//
//////////////////////////////////////////////////////////////////////////////
#ifndef INCLUDED_TAGDESCRIPTION
#define INCLUDED_TAGDESCRIPTION
#include <string>
class TagDescription
{
public:
TagDescription (std::string , Color, std::string );
std::string name;
Color color;
std::string description;
};
#endif //INCLUDED_TAGDESCRIPTION

61
src/TagsTable.cpp Normal file
View file

@ -0,0 +1,61 @@
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2023, Gothenburg Bit Factory.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// https://www.opensource.org/licenses/mit-license.php
//
////////////////////////////////////////////////////////////////////////////////
#include <TagsTable.h>
////////////////////////////////////////////////////////////////////////////////
TagsTable::Builder TagsTable::builder ()
{
return {};
}
////////////////////////////////////////////////////////////////////////////////
TagsTable::Builder& TagsTable::Builder::withTagDescriptions (std::vector <TagDescription>& tagDescriptions)
{
_tagDescriptions = tagDescriptions;
return *this;
}
////////////////////////////////////////////////////////////////////////////////
Table TagsTable::Builder::build ()
{
Table table;
table.width (1024);
table.colorHeader (Color ("underline"));
table.add ("Tag");
table.add ("Description");
for (const auto& tagDescription : _tagDescriptions)
{
auto row = table.addRow ();
table.set (row, 0, tagDescription.name, tagDescription.color);
table.set (row, 1, tagDescription.description);
}
return table;
}
////////////////////////////////////////////////////////////////////////////////

51
src/TagsTable.h Normal file
View file

@ -0,0 +1,51 @@
//////////////////////////////////////////////////////////////////////////////
//
// Copyright 2023, Gothenburg Bit Factory.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// https://www.opensource.org/licenses/mit-license.php
//
//////////////////////////////////////////////////////////////////////////////
#ifndef INCLUDED_TAGSTABLEBUILDER
#define INCLUDED_TAGSTABLEBUILDER
#include <Table.h>
#include <TagDescription.h>
#include <vector>
class TagsTable
{
class Builder
{
public:
Builder& withTagDescriptions (std::vector <TagDescription>&);
Table build ();
private:
std::vector <TagDescription> _tagDescriptions {};
};
public:
static Builder builder ();
};
#endif //INCLUDED_TAGSTABLEBUILDER

View file

@ -1,6 +1,6 @@
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// //
// Copyright 2016 - 2023, Thomas Lauf, Paul Beckingham, Federico Hernandez. // Copyright 2016 - 2023, Gothenburg Bit Factory.
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal // of this software and associated documentation files (the "Software"), to deal
@ -29,6 +29,8 @@
#include <IntervalFilterAllWithTags.h> #include <IntervalFilterAllWithTags.h>
#include <IntervalFilterAndGroup.h> #include <IntervalFilterAndGroup.h>
#include <Table.h> #include <Table.h>
#include <TagDescription.h>
#include <TagsTable.h>
#include <commands.h> #include <commands.h>
#include <iostream> #include <iostream>
#include <set> #include <set>
@ -51,37 +53,39 @@ int CmdTags (
std::set <std::string> tags; std::set <std::string> tags;
for (const auto& interval : getTracked (database, rules, filtering)) for (const auto& interval : getTracked (database, rules, filtering))
for (auto& tag : interval.tags ()) {
for (const auto& tag : interval.tags ())
{
tags.insert (tag); tags.insert (tag);
}
}
// Shows all tags. // Shows all tags.
if (! tags.empty ()) if (tags.empty ())
{ {
Table table; if (verbose)
table.width (1024);
table.colorHeader (Color ("underline"));
table.add ("Tag");
table.add ("Description");
// TODO Show all tag metadata.
for (auto& tag : tags)
{ {
auto row = table.addRow (); std::cout << "No data found.\n";
table.set (row, 0, tag, tagColor (rules, tag));
auto name = std::string ("tags.") + tag + ".description";
table.set (row, 1, rules.has (name) ? rules.get (name) : "-");
} }
}
else
{
std::vector <TagDescription> tagDescriptions;
for (const auto& tag: tags)
{
auto name = std::string ("tags.") + tag + ".description";
tagDescriptions.emplace_back (tag, tagColor (rules, tag), rules.has (name) ? rules.get (name) : "-");
}
auto table = TagsTable::builder()
.withTagDescriptions (tagDescriptions)
.build ();
std::cout << '\n' std::cout << '\n'
<< table.render () << table.render ()
<< '\n'; << '\n';
} }
else
{
if (verbose)
std::cout << "No data found.\n";
}
return 0; return 0;
} }