From 65985bcd3731111ce3e6e5ae3e774205d7b7f58e Mon Sep 17 00:00:00 2001 From: Thomas Lauf Date: Mon, 16 Oct 2023 15:30:55 +0200 Subject: [PATCH] Extract creation of tags table into TagsTableBuilder Signed-off-by: Thomas Lauf --- src/CMakeLists.txt | 2 ++ src/TagDescription.cpp | 35 +++++++++++++++++++++++ src/TagDescription.h | 42 +++++++++++++++++++++++++++ src/TagsTable.cpp | 61 ++++++++++++++++++++++++++++++++++++++++ src/TagsTable.h | 51 +++++++++++++++++++++++++++++++++ src/commands/CmdTags.cpp | 46 ++++++++++++++++-------------- 6 files changed, 216 insertions(+), 21 deletions(-) create mode 100644 src/TagDescription.cpp create mode 100644 src/TagDescription.h create mode 100644 src/TagsTable.cpp create mode 100644 src/TagsTable.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 825cab4a..5e5e53e7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -27,8 +27,10 @@ set (timew_SRCS AtomicFile.cpp AtomicFile.h Journal.cpp Journal.h Range.cpp Range.h Rules.cpp Rules.h + TagDescription.cpp TagDescription.h TagInfo.cpp TagInfo.h TagInfoDatabase.cpp TagInfoDatabase.h + TagsTable.cpp TagsTable.h Transaction.cpp Transaction.h TransactionsFactory.cpp TransactionsFactory.h UndoAction.cpp UndoAction.h diff --git a/src/TagDescription.cpp b/src/TagDescription.cpp new file mode 100644 index 00000000..a2f00929 --- /dev/null +++ b/src/TagDescription.cpp @@ -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 +#include +#include + +TagDescription::TagDescription (std::string name, Color color, std::string description) : + name (std::move (name)), + color (color), + description (std::move (description)) +{} diff --git a/src/TagDescription.h b/src/TagDescription.h new file mode 100644 index 00000000..03c530f7 --- /dev/null +++ b/src/TagDescription.h @@ -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 + +class TagDescription +{ +public: + TagDescription (std::string , Color, std::string ); + + std::string name; + Color color; + std::string description; +}; + +#endif //INCLUDED_TAGDESCRIPTION diff --git a/src/TagsTable.cpp b/src/TagsTable.cpp new file mode 100644 index 00000000..4a6c0944 --- /dev/null +++ b/src/TagsTable.cpp @@ -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::Builder TagsTable::builder () +{ + return {}; +} + +//////////////////////////////////////////////////////////////////////////////// +TagsTable::Builder& TagsTable::Builder::withTagDescriptions (std::vector & 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; +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/TagsTable.h b/src/TagsTable.h new file mode 100644 index 00000000..73ba44df --- /dev/null +++ b/src/TagsTable.h @@ -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 +#include +#include + +class TagsTable +{ + class Builder + { + public: + Builder& withTagDescriptions (std::vector &); + + Table build (); + + private: + std::vector _tagDescriptions {}; + }; + +public: + static Builder builder (); +}; + +#endif //INCLUDED_TAGSTABLEBUILDER diff --git a/src/commands/CmdTags.cpp b/src/commands/CmdTags.cpp index 79250713..afb7f861 100644 --- a/src/commands/CmdTags.cpp +++ b/src/commands/CmdTags.cpp @@ -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 // of this software and associated documentation files (the "Software"), to deal @@ -29,6 +29,8 @@ #include #include #include +#include +#include #include #include #include @@ -51,37 +53,39 @@ int CmdTags ( std::set tags; for (const auto& interval : getTracked (database, rules, filtering)) - for (auto& tag : interval.tags ()) + { + for (const auto& tag : interval.tags ()) + { tags.insert (tag); + } + } // Shows all tags. - if (! tags.empty ()) + if (tags.empty ()) { - Table table; - table.width (1024); - table.colorHeader (Color ("underline")); - table.add ("Tag"); - table.add ("Description"); - // TODO Show all tag metadata. - - for (auto& tag : tags) + if (verbose) { - auto row = table.addRow (); - 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) : "-"); + std::cout << "No data found.\n"; } + } + else + { + std::vector 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' << table.render () << '\n'; } - else - { - if (verbose) - std::cout << "No data found.\n"; - } return 0; }