<commit_before>package App::Ack::Files;

use App::Ack ();
use App::Ack::File ();

use File::Next 1.16 ();
use Errno qw(EACCES);

use warnings;
use strict;

=head1 NAME

App::Ack::Files

=head1 SYNOPSIS

A factory object for creating a stream of L<App::Ack::File> objects.

=head1 METHODS

=head2 from_argv( \%opt, \@starting_points )

Return an iterator that does the file finding for us.

=cut

sub from_argv {
    my $class = shift;
    my $opt   = shift;
    my $start = shift;

    my $self = bless {}, $class;

    my $file_filter    = undef;
    my $descend_filter = $opt->{descend_filter};

    if ( $opt->{n} ) {
        $descend_filter = sub {
            return 0;
        };
    }

    $self->{iter} =
        File::Next::files( {
            file_filter     => $opt->{file_filter},
            descend_filter  => $descend_filter,
            error_handler   => _generate_error_handler(),
            warning_handler => sub {},
            sort_files      => $opt->{sort_files},
            follow_symlinks => $opt->{follow},
        }, @{$start} );

    return $self;
}

=head2 from_file( \%opt, $filename )

Return an iterator that reads the list of files to search from a
given file.  If I<$filename> is '-', then it reads from STDIN.

=cut

sub from_file {
    my $class = shift;
    my $opt   = shift;
    my $file  = shift;

    my $error_handler = _generate_error_handler();
    my $iter =
        File::Next::from_file( {
            error_handler   => $error_handler,
            warning_handler => $error_handler,
            sort_files      => $opt->{sort_files},
        }, $file ) or return undef;

    return bless {
        iter => $iter,
    }, $class;
}


=head2 from_stdin()

This is for reading input lines from STDIN, not the list of files
from STDIN.

=cut


sub from_stdin {
    my $class = shift;

    my $self  = bless {}, $class;

    my $has_been_called = 0;

    $self->{iter} = sub {
        if ( !$has_been_called ) {
            $has_been_called = 1;
            return '-';
        }
        return;
    };

    return $self;
}


sub next {
    my $self = shift;

    my $file = $self->{iter}->() or return;

    return App::Ack::File->new( $file );
}


sub _generate_error_handler {<commit_msg>Use state variable.<commit_after>package App::Ack::Files;

use App::Ack ();
use App::Ack::File ();

use File::Next 1.16 ();
use Errno qw(EACCES);

use warnings;
use strict;
use 5.010;

=head1 NAME

App::Ack::Files

=head1 SYNOPSIS

A factory object for creating a stream of L<App::Ack::File> objects.

=head1 METHODS

=head2 from_argv( \%opt, \@starting_points )

Return an iterator that does the file finding for us.

=cut

sub from_argv {
    my $class = shift;
    my $opt   = shift;
    my $start = shift;

    my $self = bless {}, $class;

    my $file_filter    = undef;
    my $descend_filter = $opt->{descend_filter};

    if ( $opt->{n} ) {
        $descend_filter = sub {
            return 0;
        };
    }

    $self->{iter} =
        File::Next::files( {
            file_filter     => $opt->{file_filter},
            descend_filter  => $descend_filter,
            error_handler   => _generate_error_handler(),
            warning_handler => sub {},
            sort_files      => $opt->{sort_files},
            follow_symlinks => $opt->{follow},
        }, @{$start} );

    return $self;
}

=head2 from_file( \%opt, $filename )

Return an iterator that reads the list of files to search from a
given file.  If I<$filename> is '-', then it reads from STDIN.

=cut

sub from_file {
    my $class = shift;
    my $opt   = shift;
    my $file  = shift;

    my $error_handler = _generate_error_handler();
    my $iter =
        File::Next::from_file( {
            error_handler   => $error_handler,
            warning_handler => $error_handler,
            sort_files      => $opt->{sort_files},
        }, $file ) or return undef;

    return bless {
        iter => $iter,
    }, $class;
}


=head2 from_stdin()

This is for reading input lines from STDIN, not the list of files
from STDIN.

=cut


sub from_stdin {
    my $class = shift;

    my $self  = bless {}, $class;

    $self->{iter} = sub {
        state $has_been_called = 0;

        if ( !$has_been_called ) {
            $has_been_called = 1;
            return '-';
        }
        return;
    };

    return $self;
}


sub next {
    my $self = shift;

    my $file = $self->{iter}->() or return;

    return App::Ack::File->new( $file );
}


sub _generate_error_handler {
{'fraction_non_alphanumeric': 0.13583815028901733, 'fraction_numerical': 0.006224988883948421, 'mean_word_length': 2.2158684774839172, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 49, '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': '7446003', 'n_tokens_mistral': 1576, 'n_tokens_neox': 1504, 'n_words': 418}