Print the EPUB file name if more than 1 input file.

Change --no-filename to mean: Don't print the EPUB file name.
This commit is contained in:
tastytea 2021-05-27 14:46:23 +02:00
parent 0c45e7ac98
commit c376ce8466
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
5 changed files with 31 additions and 15 deletions

View File

@ -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

View File

@ -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())
{

View File

@ -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.")

View File

@ -80,6 +80,7 @@ std::vector<match> 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);

View File

@ -34,11 +34,12 @@ using match_context = std::pair<std::string, std::string>;
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