mirror of
https://github.com/GothenburgBitFactory/timewarrior.git
synced 2025-07-07 20:06:39 +02:00
Extract join function
- use join function for std::vector<std::string> from libshared - add join function for std::set<std::string> - add joinQuotedIfNeeded for std::set<std::string> and std::vector<std::string> - add unit tests - These functions could be replaced with templates and moved to libshared.
This commit is contained in:
parent
95380bbe96
commit
35e5098b51
7 changed files with 125 additions and 29 deletions
|
@ -84,14 +84,7 @@ int CmdReport (
|
|||
|
||||
rules.set ("temp.report.start", filter.range.start.toEpoch () > 0 ? filter.range.start.toISO () : "");
|
||||
rules.set ("temp.report.end", filter.range.end.toEpoch () > 0 ? filter.range.end.toISO () : "");
|
||||
std::string combinedTags;
|
||||
for (auto& tag : filter.tags ())
|
||||
{
|
||||
if (combinedTags != "")
|
||||
combinedTags += ",";
|
||||
combinedTags += quoteIfNeeded (tag);
|
||||
}
|
||||
rules.set ("temp.report.tags", combinedTags);
|
||||
rules.set ("temp.report.tags", joinQuotedIfNeeded (",", filter.tags ()));
|
||||
rules.set ("temp.version", VERSION);
|
||||
|
||||
std::stringstream header;
|
||||
|
|
|
@ -105,13 +105,7 @@ int CmdSummary (
|
|||
if (track.range.is_open () && day <= Datetime ())
|
||||
today.end = Datetime ();
|
||||
|
||||
std::string tags = "";
|
||||
for (auto& tag : track.tags ())
|
||||
{
|
||||
if (tags != "")
|
||||
tags += ", ";
|
||||
tags += tag;
|
||||
}
|
||||
std::string tags = join(", ", track.tags());
|
||||
|
||||
if (ids)
|
||||
table.set (row, 3, format ("@{1}", track.id), colorID);
|
||||
|
@ -156,11 +150,7 @@ int CmdSummary (
|
|||
|
||||
if (filter.tags ().size ())
|
||||
{
|
||||
std::vector <std::string> tags;
|
||||
for (auto& tag : filter.tags ())
|
||||
tags.push_back (quoteIfNeeded (tag));
|
||||
|
||||
std::cout << " tagged with " << join (", ", tags);
|
||||
std::cout << " tagged with " << joinQuotedIfNeeded (", ", filter.tags ());
|
||||
}
|
||||
|
||||
std::cout << ".\n";
|
||||
|
|
|
@ -95,10 +95,7 @@ int CmdTag (
|
|||
// Feedback.
|
||||
if (rules.getBoolean ("verbose"))
|
||||
{
|
||||
std::cout << "Added";
|
||||
for (auto& tag : tags)
|
||||
std::cout << ' ' << quoteIfNeeded (tag);
|
||||
std::cout << " to @" << id << '\n';
|
||||
std::cout << "Added " << joinQuotedIfNeeded (" ", tags) << " to @" << id << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -94,10 +94,7 @@ int CmdUntag (
|
|||
|
||||
if (rules.getBoolean ("verbose"))
|
||||
{
|
||||
std::cout << "Removed";
|
||||
for (auto& tag : tags)
|
||||
std::cout << ' ' << quoteIfNeeded (tag);
|
||||
std::cout << " from @" << id << '\n';
|
||||
std::cout << "Removed " << joinQuotedIfNeeded (" ", tags) << " from @" << id << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -90,6 +90,9 @@ void debug (const std::string&);
|
|||
// utiŀ.cpp
|
||||
std::string escape (const std::string&, int);
|
||||
std::string quoteIfNeeded (const std::string&);
|
||||
std::string join(const std::string& glue, const std::set <std::string>& array);
|
||||
std::string joinQuotedIfNeeded(const std::string& glue, const std::set <std::string>& array);
|
||||
std::string joinQuotedIfNeeded(const std::string& glue, const std::vector <std::string>& array);
|
||||
|
||||
// dom.cpp
|
||||
bool domGet (Database&, const Rules&, const std::string&, std::string&);
|
||||
|
|
63
src/util.cpp
63
src/util.cpp
|
@ -76,3 +76,66 @@ std::string quoteIfNeeded (const std::string& input)
|
|||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
std::string join(const std::string& glue, const std::set <std::string>& array)
|
||||
{
|
||||
if (array.empty ())
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
auto iterator = array.begin ();
|
||||
|
||||
std::string value = *iterator++;
|
||||
|
||||
while (iterator != array.end ())
|
||||
{
|
||||
value += glue + *iterator++;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
std::string joinQuotedIfNeeded(const std::string& glue, const std::set <std::string>& array)
|
||||
{
|
||||
if (array.empty ())
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
auto iterator = array.begin ();
|
||||
|
||||
std::string value = *iterator++;
|
||||
|
||||
while (iterator != array.end ())
|
||||
{
|
||||
value += glue + quoteIfNeeded(*iterator++);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
std::string joinQuotedIfNeeded(const std::string& glue, const std::vector <std::string>& array)
|
||||
{
|
||||
if (array.empty ())
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
auto iterator = array.begin ();
|
||||
|
||||
std::string value = *iterator++;
|
||||
|
||||
while (iterator != array.end ())
|
||||
{
|
||||
value += glue + quoteIfNeeded(*iterator++);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue