Fix JSON output.

Bug: #3
This commit is contained in:
tastytea 2021-06-01 20:14:36 +02:00
parent f1cb16f6d0
commit 21989aabfe
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
4 changed files with 31 additions and 18 deletions

View File

@ -81,8 +81,12 @@ suppresses errors related to them.
*--debug*:: *--debug*::
Write debug output to the terminal and log file. Write debug output to the terminal and log file.
*--json*:: *--json*::
Output JSON instead of plain text. Output JSON instead of plain text. JSON will only output at the end of the
program. There will be an object named `generator` with the property
`epubgrep`, the value is the version of the program, as string. The rest is an
array named `matches`.
== USAGE == USAGE

View File

@ -220,7 +220,7 @@ int main(int argc, char *argv[])
std::async(std::launch::async, search_file, filepath)); std::async(std::launch::async, search_file, filepath));
DEBUGLOG << "Launched new thread"; DEBUGLOG << "Launched new thread";
if (!matches_all.empty()) if (!matches_all.empty() && !opts.json)
{ {
output::print_matches(matches_all[0], opts, output::print_matches(matches_all[0], opts,
input_files.size() == 1); input_files.size() == 1);
@ -231,13 +231,13 @@ int main(int argc, char *argv[])
DEBUGLOG << "Waiting for remaining threads to finish"; DEBUGLOG << "Waiting for remaining threads to finish";
futures_cleanup(true); futures_cleanup(true);
for (const auto &matches : matches_all) if (opts.json)
{ {
if (opts.json) output::json_all(matches_all);
{ }
output::json(matches); else
} {
else for (const auto &matches : matches_all)
{ {
output::print_matches(matches, opts, input_files.size() == 1); output::print_matches(matches, opts, input_files.size() == 1);
} }

View File

@ -16,6 +16,8 @@
#include "output.hpp" #include "output.hpp"
#include "version.hpp"
#include <boost/locale/message.hpp> #include <boost/locale/message.hpp>
#include <fmt/format.h> #include <fmt/format.h>
#include <fmt/ostream.h> // For compatibility with fmt 4. #include <fmt/ostream.h> // For compatibility with fmt 4.
@ -111,20 +113,27 @@ void print_matches(const std::vector<search::match> &matches,
} }
} }
void json(const std::vector<search::match> &matches) void json_all(const std::vector<std::vector<search::match>> &matches_all)
{ {
nlohmann::json json; nlohmann::json json;
for (const auto &match : matches) json["generator"] = {{"epubgrep", version}};
for (const auto &matches : matches_all)
{ {
json[match.filepath_epub].push_back( for (const auto &match : matches)
{{"filepath", match.filepath_inside}, {
{"match", match.text}, json["matches"].push_back(
{"context", match.context}, {{"filepath_epub", match.filepath_epub},
{"headline", match.headline}, {"filepath_inside", match.filepath_inside},
{"page", match.page}}); {"match", match.text},
{"context", match.context},
{"headline", match.headline},
{"page", match.page}});
}
} }
std::cout << json.dump(2) << '\n';
std::cout << json.dump() << '\n';
} }
} // namespace epubgrep::output } // namespace epubgrep::output

View File

@ -28,7 +28,7 @@ namespace epubgrep::output
void print_matches(const std::vector<search::match> &matches, void print_matches(const std::vector<search::match> &matches,
const options::options &opts, bool single_file); const options::options &opts, bool single_file);
void json(const std::vector<search::match> &matches); void json_all(const std::vector<std::vector<search::match>> &matches_all);
} // namespace epubgrep::output } // namespace epubgrep::output