App-Saisons

 view release on metacpan or  search on metacpan

lib/App/Saisons/UI.pm  view on Meta::CPAN

}
sub _build_entries {
    my ($sessions, $running, $sel_ids, $group_by, $sort_col, $sort_rev) = @_;
    $sort_col //= 'date';
    my @valid = grep { ref $_ eq 'HASH' } @$sessions;
    if ($group_by) {
        my $group_key = $group_by eq 'agent'
            ? sub { _short_class(ref $_[0]{_adapter}) }
            : sub { $_[0]{cwd} };
        my %by_dir;
        push @{ $by_dir{$group_key->($_)} }, $_ for @valid;
        my %sorted_dir;
        for my $dir (keys %by_dir) {
            $sorted_dir{$dir} = [ _sorted($by_dir{$dir}, $sort_col, $sort_rev, $sel_ids, $running) ];
        }
        my $dir_cmp = $SORT_CMP{$sort_col} // $SORT_CMP{date};
        my @dirs = sort {
            my $ann_a = { %{$sorted_dir{$a}[0]}, _sel => $sel_ids->{$sorted_dir{$a}[0]{id}} ? 1 : 0,
                                                 _run => $running->{$sorted_dir{$a}[0]{id}} ? 1 : 0 };
            my $ann_b = { %{$sorted_dir{$b}[0]}, _sel => $sel_ids->{$sorted_dir{$b}[0]{id}} ? 1 : 0,
                                                 _run => $running->{$sorted_dir{$b}[0]{id}} ? 1 : 0 };
            $dir_cmp->($ann_a, $ann_b) || $a cmp $b
        } keys %sorted_dir;
        @dirs = reverse @dirs if $sort_rev;
        my @entries;
        for my $dir (@dirs) {
            push @entries, { type => 'header', label => $dir };
            push @entries, _entry_rows(@{ $sorted_dir{$dir} });
        }
        return @entries;
    }
    return _entry_rows(_sorted(\@valid, $sort_col, $sort_rev, $sel_ids, $running));
}
sub _entry_rows {
    return map {
        my $s = $_;
        ({ type => 'session', session => $s },
         map { { %$_, type => 'match' } } @{ $s->{_match} // [] });
    } @_;
}
sub _truncate_path {
    my ($path, $width) = @_;
    return sprintf "%-${width}s", length($path) > $width
        ? '...' . substr($path, -($width - 3))
        : $path;
}
use constant MAX_MATCH_LINES => 3;
sub _search_content {
    my ($session, $query) = @_;
    my ($title, $cwd, $q) = $CASE_SENSITIVE
        ? ($session->{title}, $session->{cwd}, $query)
        : (lc $session->{title}, lc $session->{cwd}, lc $query);
    return {} if index($title, $q) >= 0;
    return {} if index($cwd,   $q) >= 0;
    my $re = _qr($query) or return undef;
    my @files;
    push @files, $session->{_file} if $session->{_file} && -f $session->{_file};
    if (my $dir = $session->{_dir}) {
        push @files, _find_jsonl_files($dir);
    }
    @files = sort { ((stat($b))[9] // 0) <=> ((stat($a))[9] // 0) } @files;
    my @rows;
    my $found = 0;
    for my $file (@files) {
        last if $found >= MAX_MATCH_LINES;
        open my $fh, '<', $file or next;
        my @lines;
        while (my $l = <$fh>) {
            no warnings 'utf8';
            push @lines, decode('UTF-8', $l, Encode::FB_DEFAULT);
        }
        close $fh;
        my @hits;
        for (my $i = $#lines; $i >= 0; $i--) {
            next unless $lines[$i] =~ $re;
            push @hits, $i;
            last if $found + @hits >= MAX_MATCH_LINES;
        }
        next unless @hits;
        $found += @hits;
        my %is_hit = map { $_ => 1 } @hits;
        my @blocks;
        for my $i (@hits) {
            my ($lo, $hi) = ($i - 1, $i + 1);
            $lo = 0       if $lo < 0;
            $hi = $#lines if $hi > $#lines;
            if (@blocks && $hi >= $blocks[-1][0] - 1) {
                $blocks[-1][0] = $lo if $lo < $blocks[-1][0];
            } else {
                push @blocks, [$lo, $hi];
            }
        }
        for my $b (@blocks) {
            push @rows, { gap => 1 } if @rows;
            for my $i ($b->[0] .. $b->[1]) {
                if ($is_hit{$i} && $lines[$i] =~ $re) {
                    my ($ms, $me) = ($-[0], $+[0]);
                    push @rows, { matched => 1, time => _line_time($lines[$i]),
                                  text => _match_snippet($lines[$i], $ms, $me) };
                } else {
                    push @rows, { matched => 0, time => _line_time($lines[$i]),
                                  text => _ctx_snippet($lines[$i]) };
                }
            }
        }
    }
    return @rows ? { rows => \@rows } : undef;
}
sub _clean_snippet {
    my ($snip) = @_;
    $snip =~ s/\\u([0-9a-fA-F]{4})/chr(hex($1))/ge;
    $snip =~ s/\\[ntr]/ /g;
    $snip =~ s/\\(["\\\/])/$1/g;
    $snip =~ s/[\x00-\x1f]/ /g;
    $snip =~ s/\s+/ /g;
    $snip =~ s/^\s+|\s+$//g;
    return $snip;
}
sub _match_snippet {
    my ($line, $mstart, $mend) = @_;
    chomp $line;



( run in 1.602 second using v1.01-cache-2.11-cpan-500dd33cbaf )