Added option to search for tags.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
83dedc1edb
commit
ef145b2b00
60
src/main.cpp
60
src/main.cpp
|
@ -23,6 +23,7 @@
|
|||
#include "parse_options.hpp"
|
||||
#include "uri.hpp"
|
||||
#include "export.hpp"
|
||||
#include "search.hpp"
|
||||
|
||||
using std::cout;
|
||||
using std::cerr;
|
||||
|
@ -69,39 +70,48 @@ int main(const int argc, const char *argv[])
|
|||
return 3;
|
||||
}
|
||||
}
|
||||
switch (opts.format)
|
||||
if (opts.format != export_format::undefined)
|
||||
{
|
||||
case export_format::csv:
|
||||
{
|
||||
if (file.is_open())
|
||||
vector<Database::entry> entries =
|
||||
db.retrieve(opts.span[0], opts.span[1]);
|
||||
if (!opts.search_tags.empty())
|
||||
{
|
||||
export_csv(db.retrieve(opts.span[0], opts.span[1]), file);
|
||||
file.close();
|
||||
entries = search_tags(entries, opts.search_tags);
|
||||
}
|
||||
else
|
||||
|
||||
switch (opts.format)
|
||||
{
|
||||
export_csv(db.retrieve(opts.span[0], opts.span[1]));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case export_format::asciidoc:
|
||||
{
|
||||
if (file.is_open())
|
||||
case export_format::csv:
|
||||
{
|
||||
export_adoc(db.retrieve(opts.span[0], opts.span[1]), file);
|
||||
file.close();
|
||||
if (file.is_open())
|
||||
{
|
||||
export_csv(entries, file);
|
||||
file.close();
|
||||
}
|
||||
else
|
||||
{
|
||||
export_csv(entries);
|
||||
}
|
||||
break;
|
||||
}
|
||||
else
|
||||
case export_format::asciidoc:
|
||||
{
|
||||
export_adoc(db.retrieve(opts.span[0], opts.span[1]));
|
||||
if (file.is_open())
|
||||
{
|
||||
export_adoc(entries, file);
|
||||
file.close();
|
||||
}
|
||||
else
|
||||
{
|
||||
export_adoc(entries);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
// Do nothing.
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
|
@ -37,6 +37,7 @@ const options parse_options(const int argc, const char *argv[])
|
|||
string format;
|
||||
string file;
|
||||
string span;
|
||||
string search_tags;
|
||||
options opts;
|
||||
|
||||
try
|
||||
|
@ -51,6 +52,10 @@ const options parse_options(const int argc, const char *argv[])
|
|||
op.add<popl::Value<string>>
|
||||
("S", "span", "Only export entries between YYYY-MM-DD,YYYY-MM-DD.",
|
||||
"", &span);
|
||||
op.add<popl::Value<string>>
|
||||
("s", "search-tags",
|
||||
"Search for tags. Format: tag1 AND tag2 OR tag3.",
|
||||
"", &search_tags);
|
||||
auto option_help = op.add<popl::Switch>
|
||||
("h", "help", "Show this help message.");
|
||||
auto option_version = op.add<popl::Switch>
|
||||
|
@ -129,6 +134,8 @@ const options parse_options(const int argc, const char *argv[])
|
|||
}
|
||||
}
|
||||
|
||||
opts.search_tags = search_tags;
|
||||
|
||||
if (op.non_option_args().size() > 0)
|
||||
{
|
||||
opts.uri = op.non_option_args().front();
|
||||
|
|
|
@ -39,6 +39,7 @@ typedef struct options
|
|||
string file;
|
||||
array<time_point, 2> span = {{ time_point(), system_clock::now() }};
|
||||
string uri;
|
||||
string search_tags;
|
||||
uint8_t status_code = 0;
|
||||
|
||||
options();
|
||||
|
|
|
@ -0,0 +1,83 @@
|
|||
/* This file is part of remwharead.
|
||||
* Copyright © 2019 tastytea <tastytea@tastytea.de>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <regex>
|
||||
#include <algorithm>
|
||||
#include "search.hpp"
|
||||
|
||||
using std::cerr;
|
||||
using std::endl;
|
||||
using std::regex;
|
||||
using std::regex_search;
|
||||
using std::smatch;
|
||||
using std::find;
|
||||
|
||||
const vector<Database::entry>
|
||||
search_tags(const vector<Database::entry> &entries, string expression)
|
||||
{
|
||||
vector<vector<string>> searchlist;
|
||||
const regex re_or("(.+?) (OR|\\|\\|) ");
|
||||
const regex re_and("(.+?) (AND|&&) ");
|
||||
smatch match;
|
||||
vector<Database::entry> result;
|
||||
|
||||
vector<string> subexpressions;
|
||||
{ // Split expression at OR.
|
||||
while (regex_search(expression, match, re_or))
|
||||
{
|
||||
subexpressions.push_back(match[1].str());
|
||||
expression = match.suffix().str();
|
||||
}
|
||||
subexpressions.push_back(expression);
|
||||
}
|
||||
|
||||
{
|
||||
for (string sub : subexpressions)
|
||||
{ // Split each OR-slice at AND.
|
||||
vector<string> tags;
|
||||
while (regex_search(sub, match, re_and))
|
||||
{
|
||||
tags.push_back(match[1].str());
|
||||
sub = match.suffix().str();
|
||||
}
|
||||
tags.push_back(sub);
|
||||
searchlist.push_back(tags);
|
||||
}
|
||||
}
|
||||
|
||||
for (const vector<string> &tags_or : searchlist)
|
||||
{
|
||||
for (const Database::entry &entry : entries)
|
||||
{ // Add entry to result if all tags in an OR-slice match.
|
||||
bool matched = true;
|
||||
for (const string &tag : tags_or)
|
||||
{
|
||||
const auto it = find(entry.tags.begin(), entry.tags.end(), tag);
|
||||
if (it == entry.tags.end())
|
||||
{
|
||||
matched = false;
|
||||
}
|
||||
}
|
||||
if (matched == true)
|
||||
{
|
||||
result.push_back(entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
/* This file is part of remwharead.
|
||||
* Copyright © 2019 tastytea <tastytea@tastytea.de>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef REMWHAREAD_SEARCH_HPP
|
||||
#define REMWHAREAD_SEARCH_HPP
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include "sqlite.hpp"
|
||||
|
||||
using std::vector;
|
||||
using std::string;
|
||||
|
||||
//! Seach database entries for tags.
|
||||
const vector<Database::entry>
|
||||
search_tags(const vector<Database::entry> &entries, string expression);
|
||||
|
||||
#endif // REMWHAREAD_SEARCH_HPP
|
Loading…
Reference in New Issue