<commit_before>:- use_module(library(ansi_term), [ansi_format/3]).
:- use_module(library(readutil), [read_line_to_codes/3]).
:- ['src/dcg.pl'].

% predicate has IO and logic intertwined, not as clean as I'd like
reformat_grep_output(Pattern) :-
    prompt(_,''),
    reformat_grep_output_(Pattern).

reformat_grep_output_(Pattern) :-
    read_line_to_codes(current_input,Line),
    Line \= end_of_file,
    !,
    phrase(grep_line(Path,N,Content),Line),
    ansi_format([bold,fg(green)],"~s",[Path]),
    nl,
    ansi_format([bold,fg(yellow)],"~d", [N]),
    format(":~s~n",[Content]),
    reformat_grep_output_(Pattern).
reformat_grep_output_(_).


grep_line(Path,N,Content) -->
    string_without(0':, Path), %'
    ":",
    integer(N),
    ":",
    rest_of_line(Content).<commit_msg>Much prettier output formatting<commit_after>:- use_module(library(ansi_term), [ansi_format/3]).
:- use_module(library(readutil), [read_line_to_codes/3]).
:- ['src/dcg.pl'].

% predicate has IO and logic intertwined, not as clean as I'd like
reformat_grep_output(Pattern) :-
    prompt(_,''),
    reformat_grep_output_(no(previous),Pattern).

reformat_grep_output_(PrevPath,Pattern) :-
    read_line_to_codes(current_input,Line),
    Line \= end_of_file,
    !,
    phrase(grep_line(Path,N,Content),Line),
    print_line(PrevPath,Path,N,Content),
    reformat_grep_output_(Path,Pattern).
reformat_grep_output_(_,_).


grep_line(Path,N,Content) -->
    string_without(0':, Path), %'
    ":",
    integer(N),
    ":",
    rest_of_line(Content).


print_line(PrevPath,Path,N,Content) :-
    ( PrevPath = Path ->
        true  % no header line needed
    ; otherwise ->
        ( PrevPath = no(previous) -> true; nl ),
        ansi_format([bold,fg(green)],"~s",[Path]),
        nl
    ),
    ansi_format([bold,fg(yellow)],"~d", [N]),
    format(":~s~n",[Content]).
{'fraction_non_alphanumeric': 0.18039003250270855, 'fraction_numerical': 0.0032502708559046588, 'mean_word_length': 4.307471264367816, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2823280', 'n_tokens_mistral': 706, 'n_tokens_neox': 686, 'n_words': 92}