From c376ce846618f14a1f174952ce28868d3ce87fec Mon Sep 17 00:00:00 2001 From: tastytea Date: Thu, 27 May 2021 14:46:23 +0200 Subject: [PATCH] Print the EPUB file name if more than 1 input file. Change --no-filename to mean: Don't print the EPUB file name. --- man/epubgrep.1.adoc | 5 +++-- src/main.cpp | 27 ++++++++++++++++++++------- src/options.cpp | 2 +- src/search.cpp | 1 + src/search.hpp | 11 ++++++----- 5 files changed, 31 insertions(+), 15 deletions(-) diff --git a/man/epubgrep.1.adoc b/man/epubgrep.1.adoc index 0a55361..020bd13 100644 --- a/man/epubgrep.1.adoc +++ b/man/epubgrep.1.adoc @@ -59,8 +59,9 @@ Print _NUMBER_ words of context around matches. Do not color matches. *--no-filename*:: -Suppress the mentioning of file names on output. Chapters and page numbers will -still be output. +Suppress the mentioning of EPUB file names on output. File names inside the +EPUB, chapters and page numbers will still be output. + *-r*, *--recursive*: Read all files under each directory, recursively, following symbolic links only if they are on the command line. Silently skips directories that are not diff --git a/src/main.cpp b/src/main.cpp index d326dc5..eedc6f4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -228,19 +228,32 @@ int main(int argc, char *argv[]) for (const auto &matches_file : matches_all) { + fs::path last_epub; for (const auto &match : matches_file) { - if (vm.count("no-filename") == 0) + if (input_files.size() <= 1 || vm.count("no-filename") == 0) { - cout << match.filepath; + if (match.epub_filepath != last_epub) + { + if (vm.count("nocolor") == 0) + { + cout << termcolor::yellow; + } + + cout << format(translate(" In {0:s}: \n").str().data(), + fs::relative(match.epub_filepath)); + last_epub = match.epub_filepath; + + if (vm.count("nocolor") == 0) + { + cout << termcolor::reset; + } + } } + cout << match.filepath; if (!match.headline.empty()) { - if (vm.count("no-filename") == 0) - { - cout << ", "; - } - cout << match.headline; + cout << ", " << match.headline; } if (!match.page.empty()) { diff --git a/src/options.cpp b/src/options.cpp index a1d6929..293c0ad 100644 --- a/src/options.cpp +++ b/src/options.cpp @@ -74,7 +74,7 @@ po::variables_map parse_options(int argc, char *argv[]) .str().data()) ("nocolor", translate("Do not color matches.") .str().data()) ("no-filename", - translate("Suppress the mentioning of file names on output.") + translate("Suppress the mentioning of EPUB file names on output.") .str().data()) ("recursive,r", translate("Read all files under each directory, recursively.") diff --git a/src/search.cpp b/src/search.cpp index 84eba40..647e08e 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -80,6 +80,7 @@ std::vector search(const fs::path &filepath, std::string_view regex, boost::match_default)) { match match; // FIXME: Rename variable or struct. + match.epub_filepath = filepath; match.filepath = entry; match.text = match_result[0]; match.context = context(match_result, opts.context); diff --git a/src/search.hpp b/src/search.hpp index 579dd4a..48544a2 100644 --- a/src/search.hpp +++ b/src/search.hpp @@ -34,11 +34,12 @@ using match_context = std::pair; struct match { - std::string text; //!< Matched string. - match_context context; //!< The context around the match. - std::string filepath; //!< The file path of the matched line. - std::string headline; //!< The last headline, if available. - std::string page; //!< The page number, if available. + fs::path epub_filepath; //!< File path of the EPUB. + std::string text; //!< Matched string. + match_context context; //!< The context around the match. + std::string filepath; //!< The file path of the matched line. + std::string headline; //!< The last headline, if available. + std::string page; //!< The page number, if available. }; enum class regex_kind