Put output stuff into own function in different file.

It got a little crowded in main(). 😊
This commit is contained in:
tastytea 2021-05-28 17:07:11 +02:00
parent 65de7f7efb
commit 59759b5934
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
3 changed files with 128 additions and 55 deletions

View File

@ -17,6 +17,7 @@
#include "files.hpp"
#include "fs-compat.hpp"
#include "options.hpp"
#include "output.hpp"
#include "search.hpp"
#include "zip.hpp"
@ -206,62 +207,9 @@ int main(int argc, char *argv[])
}
futures_cleanup(true);
for (const auto &matches_file : matches_all)
for (const auto &matches : matches_all)
{
fs::path last_epub;
for (const auto &match : matches_file)
{
if (input_files.size() > 1 && !opts.no_fn_fs)
{
if (match.epub_filepath != last_epub)
{
if (!opts.nocolor)
{
cout << termcolor::yellow;
}
cout << format(translate(" In {0:s}: \n").str(),
fs::relative(match.epub_filepath));
last_epub = match.epub_filepath;
if (!opts.nocolor)
{
cout << termcolor::reset;
}
}
}
vector<string> prefix;
if (!opts.no_fn_epub)
{
prefix.emplace_back(match.filepath);
}
if (!match.headline.empty())
{
prefix.emplace_back(match.headline);
}
if (!match.page.empty())
{
prefix.emplace_back("page " + match.page);
}
for (const auto &part : prefix)
{
cout << part;
if (part != *(prefix.rbegin()))
{
cout << ", ";
}
}
cout << ": " << match.context.first;
if (!opts.nocolor)
{
cout << termcolor::bright_magenta;
}
cout << match.text;
if (!opts.nocolor)
{
cout << termcolor::reset;
}
cout << match.context.second << '\n';
}
output::print_matches(matches, opts, input_files.size() == 1);
}
return return_code;

92
src/output.cpp Normal file
View File

@ -0,0 +1,92 @@
/* 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"
#include <boost/locale/message.hpp>
#include <fmt/format.h>
#include <fmt/ostream.h> // For compatibility with fmt 4.
#include <termcolor/termcolor.hpp>
#include <iostream>
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)
{
if (match.epub_filepath != last_epub)
{
if (!opts.nocolor)
{
cout << termcolor::yellow;
}
cout << format(translate(" In {0:s}: \n").str(),
fs::relative(match.epub_filepath));
last_epub = match.epub_filepath;
if (!opts.nocolor)
{
cout << termcolor::reset;
}
}
}
std::vector<std::string> prefix;
if (!opts.no_fn_epub)
{
prefix.emplace_back(match.filepath);
}
if (!match.headline.empty())
{
prefix.emplace_back(match.headline);
}
if (!match.page.empty())
{
prefix.emplace_back("page " + match.page);
}
for (const auto &part : prefix)
{
cout << part;
if (part != *(prefix.rbegin()))
{
cout << ", ";
}
}
cout << ": " << match.context.first;
if (!opts.nocolor)
{
cout << termcolor::bright_magenta;
}
cout << match.text;
if (!opts.nocolor)
{
cout << termcolor::reset;
}
cout << match.context.second << '\n';
}
}
} // namespace epubgrep::output

33
src/output.hpp Normal file
View File

@ -0,0 +1,33 @@
/* 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/>.
*/
#ifndef EPUBGREP_OUTPUT_HPP
#define EPUBGREP_OUTPUT_HPP
#include "options.hpp"
#include "search.hpp"
#include <vector>
namespace epubgrep::output
{
void print_matches(const std::vector<search::match> &matches,
const options::options &opts, bool single_file);
} // namespace epubgrep::output
#endif // EPUBGREP_OUTPUT_HPP