diff --git a/src/main.cpp b/src/main.cpp index b9fc35d..4a49953 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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 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; diff --git a/src/output.cpp b/src/output.cpp new file mode 100644 index 0000000..e1a5eee --- /dev/null +++ b/src/output.cpp @@ -0,0 +1,92 @@ +/* This file is part of epubgrep. + * Copyright © 2021 tastytea + * + * 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 . + */ + +#include "output.hpp" + +#include +#include +#include // For compatibility with fmt 4. +#include + +#include + +namespace epubgrep::output +{ + +using boost::locale::translate; +using fmt::format; +using std::cout; + +void print_matches(const std::vector &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 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 diff --git a/src/output.hpp b/src/output.hpp new file mode 100644 index 0000000..85a488b --- /dev/null +++ b/src/output.hpp @@ -0,0 +1,33 @@ +/* This file is part of epubgrep. + * Copyright © 2021 tastytea + * + * 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 . + */ + +#ifndef EPUBGREP_OUTPUT_HPP +#define EPUBGREP_OUTPUT_HPP + +#include "options.hpp" +#include "search.hpp" + +#include + +namespace epubgrep::output +{ + +void print_matches(const std::vector &matches, + const options::options &opts, bool single_file); + +} // namespace epubgrep::output + +#endif // EPUBGREP_OUTPUT_HPP