2021-05-28 17:07:11 +02:00
|
|
|
/* This file is part of epubgrep.
|
|
|
|
* Copyright © 2021 tastytea <tastytea@tastytea.de>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "output.hpp"
|
|
|
|
|
2021-06-01 20:14:36 +02:00
|
|
|
#include "version.hpp"
|
|
|
|
|
2021-05-28 17:07:11 +02:00
|
|
|
#include <boost/locale/message.hpp>
|
|
|
|
#include <fmt/format.h>
|
|
|
|
#include <fmt/ostream.h> // For compatibility with fmt 4.
|
2021-06-01 19:17:44 +02:00
|
|
|
#include <nlohmann/json.hpp>
|
2021-05-28 17:07:11 +02:00
|
|
|
#include <termcolor/termcolor.hpp>
|
|
|
|
|
|
|
|
#include <iostream>
|
2021-05-30 19:03:59 +02:00
|
|
|
#include <sstream>
|
2021-05-28 17:07:11 +02:00
|
|
|
|
|
|
|
namespace epubgrep::output
|
|
|
|
{
|
|
|
|
|
|
|
|
using boost::locale::translate;
|
|
|
|
using fmt::format;
|
|
|
|
using std::cout;
|
|
|
|
|
|
|
|
void print_matches(const std::vector<search::match> &matches,
|
|
|
|
const options::options &opts, bool single_file)
|
|
|
|
{
|
|
|
|
fs::path last_epub;
|
|
|
|
for (const auto &match : matches)
|
|
|
|
{
|
|
|
|
if (!single_file && !opts.no_fn_fs)
|
|
|
|
{
|
2021-06-01 19:17:44 +02:00
|
|
|
if (match.filepath_epub != last_epub)
|
2021-05-28 17:07:11 +02:00
|
|
|
{
|
|
|
|
if (!opts.nocolor)
|
|
|
|
{
|
|
|
|
cout << termcolor::yellow;
|
|
|
|
}
|
|
|
|
cout << format(translate(" In {0:s}: \n").str(),
|
2021-06-01 19:17:44 +02:00
|
|
|
fs::relative(match.filepath_epub));
|
|
|
|
last_epub = match.filepath_epub;
|
2021-05-28 17:07:11 +02:00
|
|
|
if (!opts.nocolor)
|
|
|
|
{
|
|
|
|
cout << termcolor::reset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-30 18:09:31 +02:00
|
|
|
std::vector<std::string> metadata;
|
2021-05-28 17:07:11 +02:00
|
|
|
if (!opts.no_fn_epub)
|
|
|
|
{
|
2021-06-01 19:15:00 +02:00
|
|
|
metadata.emplace_back(match.filepath_inside);
|
2021-05-28 17:07:11 +02:00
|
|
|
}
|
|
|
|
if (!match.headline.empty())
|
|
|
|
{
|
2021-05-30 19:03:59 +02:00
|
|
|
// <https://github.com/ikalnytskyi/termcolor/issues/45>
|
|
|
|
if (!opts.nocolor && termcolor::_internal::is_colorized(cout))
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << termcolor::colorize << termcolor::underline
|
|
|
|
<< match.headline << termcolor::reset << termcolor::italic;
|
|
|
|
metadata.emplace_back(ss.str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
metadata.emplace_back(match.headline);
|
|
|
|
}
|
2021-05-28 17:07:11 +02:00
|
|
|
}
|
|
|
|
if (!match.page.empty())
|
|
|
|
{
|
2021-05-30 18:09:31 +02:00
|
|
|
metadata.emplace_back("page " + match.page);
|
2021-05-28 17:07:11 +02:00
|
|
|
}
|
2021-05-30 19:03:59 +02:00
|
|
|
if (!opts.nocolor)
|
|
|
|
{
|
|
|
|
cout << termcolor::italic;
|
|
|
|
}
|
2021-05-30 18:09:31 +02:00
|
|
|
for (const auto &part : metadata)
|
2021-05-28 17:07:11 +02:00
|
|
|
{
|
|
|
|
cout << part;
|
2021-05-30 18:09:31 +02:00
|
|
|
if (part != *(metadata.rbegin()))
|
2021-05-28 17:07:11 +02:00
|
|
|
{
|
|
|
|
cout << ", ";
|
|
|
|
}
|
|
|
|
}
|
2021-05-30 19:03:59 +02:00
|
|
|
cout << ": ";
|
|
|
|
if (!opts.nocolor)
|
|
|
|
{
|
|
|
|
cout << termcolor::reset;
|
|
|
|
}
|
|
|
|
cout << match.context.first;
|
2021-05-28 17:07:11 +02:00
|
|
|
if (!opts.nocolor)
|
|
|
|
{
|
|
|
|
cout << termcolor::bright_magenta;
|
|
|
|
}
|
|
|
|
cout << match.text;
|
|
|
|
if (!opts.nocolor)
|
|
|
|
{
|
|
|
|
cout << termcolor::reset;
|
|
|
|
}
|
|
|
|
cout << match.context.second << '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-01 20:14:36 +02:00
|
|
|
void json_all(const std::vector<std::vector<search::match>> &matches_all)
|
2021-06-01 19:17:44 +02:00
|
|
|
{
|
|
|
|
nlohmann::json json;
|
|
|
|
|
2021-06-01 21:17:29 +02:00
|
|
|
json["generator"] = {{"epubgrep", std::string(version)}};
|
2021-06-01 20:14:36 +02:00
|
|
|
|
|
|
|
for (const auto &matches : matches_all)
|
2021-06-01 19:17:44 +02:00
|
|
|
{
|
2021-06-01 20:14:36 +02:00
|
|
|
for (const auto &match : matches)
|
|
|
|
{
|
|
|
|
json["matches"].push_back(
|
2021-06-01 21:17:29 +02:00
|
|
|
{{"filepath_epub", match.filepath_epub.string()},
|
2021-06-01 20:14:36 +02:00
|
|
|
{"filepath_inside", match.filepath_inside},
|
|
|
|
{"match", match.text},
|
2021-06-01 21:17:29 +02:00
|
|
|
{"context", {match.context.first, match.context.second}},
|
2021-06-01 20:14:36 +02:00
|
|
|
{"headline", match.headline},
|
|
|
|
{"page", match.page}});
|
|
|
|
}
|
2021-06-01 19:17:44 +02:00
|
|
|
}
|
2021-06-01 20:14:36 +02:00
|
|
|
|
|
|
|
std::cout << json.dump() << '\n';
|
2021-06-01 19:17:44 +02:00
|
|
|
}
|
|
|
|
|
2021-05-28 17:07:11 +02:00
|
|
|
} // namespace epubgrep::output
|