Skip to content

Commit

Permalink
Removed -c/-m/-l/-L options. Need to reconsider design
Browse files Browse the repository at this point in the history
  • Loading branch information
p-ranav committed Apr 24, 2022
1 parent 1a87475 commit c8eb737
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 133 deletions.
Binary file modified images/demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 0 additions & 59 deletions source/help.hpp

This file was deleted.

46 changes: 3 additions & 43 deletions source/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <argparse.hpp>
#include <help.hpp>
#include <searcher.hpp>
#include <unistd.h>
namespace fs = std::filesystem;
Expand Down Expand Up @@ -27,39 +26,13 @@ int main(int argc, char* argv[])
program.add_argument("-j")
.help("Number of threads")
.scan<'d', int>()
.default_value(3);

// Generic Output Control
program.add_argument("-c", "--count")
.help("Print a count of matching lines for each input file.")
.default_value(false)
.implicit_value(true);

program.add_argument("-L", "--files-without-match")
.help("Print only filenames of files that do not contain matches")
.default_value(false)
.implicit_value(true);

program.add_argument("-l", "--files-with-matches")
.help("Print only filenames of files that contain matches.")
.default_value(false)
.implicit_value(true);

program.add_argument("-m", "--max-count")
.help("Stop reading a file after NUM matching lines.")
.default_value(0)
.required()
.scan<'i', std::size_t>();
.default_value(5);

try {
program.parse_args(argc, argv);
} catch (const std::runtime_error& err) {
if (program.get<bool>("-h")) {
search::print_help();
} else {
std::cerr << err.what() << std::endl;
search::print_help();
}
std::cerr << err.what() << std::endl;
std::cerr << program;
std::exit(1);
}

Expand Down Expand Up @@ -99,24 +72,11 @@ int main(int argc, char* argv[])
auto query = program.get<std::string>("query");
auto filter = program.get<std::string>("-f");
auto num_threads = program.get<int>("-j");
auto print_count = program.get<bool>("-c");
auto enforce_max_count = program.is_used("-m");
std::size_t max_count = 0;
if (enforce_max_count) {
max_count = program.get<std::size_t>("-m");
}
auto print_only_file_matches = program.get<bool>("-l");
auto print_only_file_without_matches = program.get<bool>("-L");

// Configure a searcher
search::searcher searcher;
searcher.m_query = query;
searcher.m_filter = filter;
searcher.m_print_count = print_count;
searcher.m_enforce_max_count = enforce_max_count;
searcher.m_max_count = max_count;
searcher.m_print_only_file_matches = print_only_file_matches;
searcher.m_print_only_file_without_matches = print_only_file_without_matches;
searcher.m_is_stdout = is_stdout;
searcher.m_is_path_from_terminal = is_path_from_terminal;

Expand Down
27 changes: 1 addition & 26 deletions source/searcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ std::size_t searcher::file_search(std::string_view filename,
bool first_search = true;
bool printed_file_name = false;
std::size_t current_line_number = 1;
auto last_newline_pos = haystack_begin;
auto no_file_name = filename.empty();

while (it != haystack_end) {
Expand All @@ -122,7 +121,7 @@ std::size_t searcher::file_search(std::string_view filename,
it = needle_search(m_query, it, haystack_end);
#endif

if (it != haystack_end && !m_print_only_file_without_matches) {
if (it != haystack_end) {
// needle found in haystack

if (!no_file_name) {
Expand Down Expand Up @@ -166,31 +165,7 @@ std::size_t searcher::file_search(std::string_view filename,
#else
auto newline_after = std::find(it, haystack_end, '\n');
#endif

if (last_newline_pos == haystack_begin) {
last_newline_pos = haystack_begin + newline_before;
if (newline_before != std::string_view::npos) {
current_line_number +=
std::count_if(haystack_begin,
last_newline_pos,
[](char c) { return c == '\n'; });
}
}

current_line_number += std::count_if(last_newline_pos + 1,
newline_after + 1,
[](char c) { return c == '\n'; });
if (m_is_stdout) {
// Print line number in bold magenta
fmt::format_to(std::back_inserter(out),
"\033[1;35m{}\033[0m: ",
current_line_number);
} else {
fmt::format_to(std::back_inserter(out), "{}: ", current_line_number);
}

// Get line [newline_before, newline_after]

auto line_size =
std::size_t(newline_after - (haystack_begin + newline_before) - 1);
line = haystack.substr(newline_before + 1, line_size);
Expand Down
5 changes: 0 additions & 5 deletions source/searcher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ struct searcher
static inline std::unique_ptr<thread_pool> m_ts;
static inline std::string_view m_query;
static inline std::string_view m_filter;
static inline bool m_print_count;
static inline bool m_enforce_max_count;
static inline std::size_t m_max_count;
static inline bool m_print_only_file_matches;
static inline bool m_print_only_file_without_matches;
static inline bool m_is_stdout;
static inline bool m_is_path_from_terminal;

Expand Down

0 comments on commit c8eb737

Please sign in to comment.