/* 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 #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.filepath_epub != last_epub) { if (!opts.nocolor) { cout << termcolor::yellow; } cout << format(translate(" In {0:s}: \n").str(), fs::relative(match.filepath_epub)); last_epub = match.filepath_epub; if (!opts.nocolor) { cout << termcolor::reset; } } } std::vector metadata; if (!opts.no_fn_epub) { metadata.emplace_back(match.filepath_inside); } if (!match.headline.empty()) { // 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); } } if (!match.page.empty()) { metadata.emplace_back("page " + match.page); } if (!opts.nocolor) { cout << termcolor::italic; } for (const auto &part : metadata) { cout << part; if (part != *(metadata.rbegin())) { cout << ", "; } } cout << ": "; if (!opts.nocolor) { cout << termcolor::reset; } 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'; } } void json(const std::vector &matches) { nlohmann::json json; for (const auto &match : matches) { json[match.filepath_epub].push_back( {{"filepath", match.filepath_inside}, {"match", match.text}, {"context", match.context}, {"headline", match.headline}, {"page", match.page}}); } std::cout << json.dump(2) << '\n'; } } // namespace epubgrep::output