Skip to content

Commit

Permalink
Rename filename-only to files-with-matches
Browse files Browse the repository at this point in the history
Also introduce --files-without-matches.  Up dependencies and stuff
  • Loading branch information
lizmat committed Aug 29, 2022
1 parent 0ad0697 commit 8a2efb3
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 41 deletions.
2 changes: 1 addition & 1 deletion META6.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"has-word:ver<0.0.3>:auth<zef:lizmat>",
"highlighter:ver<0.0.12>:auth<zef:lizmat>",
"JSON::Fast:ver<0.17>:auth<cpan:TIMOTIMO>",
"rak:ver<0.0.16>:auth<zef:lizmat>",
"rak:ver<0.0.17>:auth<zef:lizmat>",
"String::Utils:ver<0.0.8>:auth<zef:lizmat>",
"META::constants:ver<0.0.2>:auth<zef:lizmat>"
],
Expand Down
23 changes: 14 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ $ rak foo bar # look for "foo" in directory "bar" recursively

$ rak '/ << foo >> /' # look for "foo" as word in current directory

$ rak foo --files-only # look for "foo", only produce filenames
$ rak foo --files-with-matches # look for "foo", only produce filenames

$ rak foo --before=2 --after=2 # also produce 2 lines before and after

Expand Down Expand Up @@ -133,7 +133,7 @@ If <git blame> information can be obtained, then the associated `Git::Blame::Fil

```bash
# show files with more than 10 commits
$ rak '*.commits > 10' --blame-per-file --filename-only
$ rak '*.commits > 10' --blame-per-file --files-with-matches
```

Requires that the [`Git::Blame::File`](https://raku.land/zef:lizmat/Git::Blame::File) is installed.
Expand Down Expand Up @@ -175,7 +175,7 @@ Indicate the number of lines that should be shown **around** any line that match
--count-only
------------

Flag. Indicate whether just the number of lines with matches should be calculated. When specified with a `True` value, will show a "N matches in M files" by default, and if the `:filename-only` option is also specified with a `True` value, will also list the file names with their respective counts.
Flag. Indicate whether just the number of lines with matches should be calculated. When specified with a `True` value, will show a "N matches in M files" by default, and if the `:files-with-matches` (or `files-without matches`) option is also specified with a `True` value, will just show total counts.

--csv-per-line
--------------
Expand Down Expand Up @@ -239,18 +239,23 @@ Predefined groups are `#raku`, `#perl`, `#c`, `#c++`, `#yaml`, <#ruby> `#python`
--file-separator-null
---------------------

Flag. Indicate to separate filenames by null bytes rather than newlines if the `--filename-only` option is specified with a `True` value.

--filename-only
---------------

Flag. If specified with a true value, will only produce the filenames of the files in which the pattern was found. Defaults to `False`.
Flag. Indicate to separate filenames by null bytes rather than newlines if the `--files-with-matches` option is specified with a `True` value.

--files-from=filename
---------------------

Indicate the path of the file to read filenames from instead of the expansion of paths from any positional arguments. "-" can be specified to read filenames from STDIN.

--files-with-matches
--------------------

Flag. If specified with a true value, will only produce the filenames of the files in which the pattern was found. Defaults to `False`.

--files-without-matches
-----------------------

Flag. If specified with a true value, will only produce the filenames of the files in which the pattern was **not** found. Defaults to `False`.

--filesize=condition
--------------------

Expand Down
27 changes: 16 additions & 11 deletions doc/App-Rak.rakudoc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ $ rak foo bar # look for "foo" in directory "bar" recursively

$ rak '/ << foo >> /' # look for "foo" as word in current directory

$ rak foo --files-only # look for "foo", only produce filenames
$ rak foo --files-with-matches # look for "foo", only produce filenames

$ rak foo --before=2 --after=2 # also produce 2 lines before and after

Expand Down Expand Up @@ -162,7 +162,7 @@ returned value is a string, then that string will be shown.
=begin code :lang<bash>

# show files with more than 10 commits
$ rak '*.commits > 10' --blame-per-file --filename-only
$ rak '*.commits > 10' --blame-per-file --files-with-matches

=end code

Expand Down Expand Up @@ -221,9 +221,9 @@ C<--before-context> arguments.

Flag. Indicate whether just the number of lines with matches should be
calculated. When specified with a C<True> value, will show a "N matches
in M files" by default, and if the C<:filename-only> option is also
specified with a C<True> value, will also list the file names with their
respective counts.
in M files" by default, and if the C<:files-with-matches> (or
C<files-without matches>) option is also specified with a C<True> value,
will just show total counts.

=head2 --csv-per-line

Expand Down Expand Up @@ -303,19 +303,24 @@ C<#python>, C<#markdown> and C<#text>.
=head2 --file-separator-null

Flag. Indicate to separate filenames by null bytes rather than newlines
if the C<--filename-only> option is specified with a C<True> value.

=head2 --filename-only

Flag. If specified with a true value, will only produce the filenames of
the files in which the pattern was found. Defaults to C<False>.
if the C<--files-with-matches> option is specified with a C<True> value.

=head2 --files-from=filename

Indicate the path of the file to read filenames from instead of the
expansion of paths from any positional arguments. "-" can be specified
to read filenames from STDIN.

=head2 --files-with-matches

Flag. If specified with a true value, will only produce the filenames of
the files in which the pattern was found. Defaults to C<False>.

=head2 --files-without-matches

Flag. If specified with a true value, will only produce the filenames of
the files in which the pattern was B<not> found. Defaults to C<False>.

=head2 --filesize=condition

If specified, indicates the C<Callable> that should return True to include a
Expand Down
59 changes: 48 additions & 11 deletions lib/App/Rak.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use Edit::Files:ver<0.0.4>:auth<zef:lizmat>;
use has-word:ver<0.0.3>:auth<zef:lizmat>;
use highlighter:ver<0.0.12>:auth<zef:lizmat>;
use JSON::Fast:ver<0.17>:auth<cpan:TIMOTIMO>;
use rak:ver<0.0.16>:auth<zef:lizmat>;
use rak:ver<0.0.17>:auth<zef:lizmat>;
use String::Utils:ver<0.0.8>:auth<zef:lizmat>;

# Defaults for highlighting on terminals
Expand Down Expand Up @@ -956,20 +956,19 @@ my multi sub MAIN(*@specs, *%n) { # *%_ causes compilation issues
setup-producers(@specs, %n, %rak);

# Only interested in filenames
my $filename-only;
if %n<filename-only>:delete {
$filename-only := True;
my $files-with-matches;
if %n<files-with-matches>:delete {

# Only interested in number of files
if %n<count-only>:delete {
my int $seen;
%rak<max-matches-per-source> := 1;
%rak<mapper> := -> $, @ --> Empty {
LAST sayer $seen == 0
?? "No files"
?? "No files with matches"
!! $seen == 1
?? "One file"
!! "$seen files";
?? "One file with matches"
!! "$seen files with matches";
++$seen;
}
}
Expand All @@ -984,9 +983,47 @@ my multi sub MAIN(*@specs, *%n) { # *%_ causes compilation issues
}
}

# Want to know which files
# Want to know files with matches
else {
%rak<sources-only> := True;
$files-with-matches := True;
%rak<sources-only> := True;
&line-post-proc = *.relative;
}
}

# Want to know files without matches
if %n<files-without-matches>:delete {

# Only interested in number of files
if %n<count-only>:delete {
my int $seen;
%rak<max-matches-per-source> := 1;
%rak<map-all> := True;
%rak<mapper> := -> $, @a --> Empty {
LAST sayer $seen == 0
?? "No files without matches"
!! $seen == 1
?? "One file without matches"
!! "$seen files without matches";
++$seen if @a == 0;
}
}

# Need to separate files with a null-byte
elsif %n<file-separator-null>:delete {
my @files;
%rak<max-matches-per-source> := 1;
%rak<map-all> := True;
%rak<mapper> := -> $source, @a --> Empty {
LAST sayer @files.join("\0");
@files.push: $source.relative if @a == 0;
}
}

# Want to know files without matches
else {
$files-with-matches := True; # same final handling
%rak<sources-without-only> := True;
&line-post-proc = *.relative;
}
}
Expand Down Expand Up @@ -1027,7 +1064,7 @@ my multi sub MAIN(*@specs, *%n) { # *%_ causes compilation issues
my $show-filename := %n<show-filename>:delete // True;
my $show-line-number := %n<show-line-number>:delete // True;
my int $only-first = .Int with %n<only-first>:delete;
%rak<omit-item-number> = True if $filename-only || !$show-line-number;
%rak<omit-item-number> = True unless $show-line-number;

# Set up statistics settings
my $count-only := %n<count-only>:delete;
Expand Down Expand Up @@ -1162,7 +1199,7 @@ my multi sub MAIN(*@specs, *%n) { # *%_ causes compilation issues
}

# Only want filename, so show its relative path
elsif $filename-only {
elsif $files-with-matches {
sayer $outer.relative;
last RESULT if ++$seen == $only-first;
}
Expand Down
48 changes: 39 additions & 9 deletions xt/02-simple.rakutest
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ my constant BON = "\e[1m"; # BOLD ON
my constant BOFF = "\e[22m"; # BOLD OFF

my $dir = $*TMPDIR.add("App-Rak");
my $abs := $dir.absolute;
my $rel := $dir.relative ~ $*SPEC.dir-sep;
my $dot = $?FILE.IO.parent.parent;
my $rak := $dot.add("bin").add("rak").relative;

Expand Down Expand Up @@ -38,7 +38,11 @@ END {
$dir.IO.rmdir;
}

my sub query-ok(*@query, :$ok is copy, :$head = 1) is test-assertion {
my sub query-ok(
*@query, # the actual parameters
:$ok is copy, # the expected result
:$head = 1, # whether to do the --only-first test with this number
) is test-assertion {
my @args = $*EXECUTABLE.absolute, "-I$dot", $rak, "--paths-from=$paths",
@query.Slip, '--degree=1';
$ok .= chomp;
Expand All @@ -49,9 +53,7 @@ my sub query-ok(*@query, :$ok is copy, :$head = 1) is test-assertion {
my $key = "huh?";

is $proc.err.slurp(:close), "", "is '@query[]' STDERR clean?";
$proc.out.lines.map({
.match($abs) ?? .IO.basename !! $_
}).join("\n")
$proc.out.lines.map(*.subst($rel, :g)).join("\n")
}

# Base query
Expand Down Expand Up @@ -421,8 +423,8 @@ nine
1:zero
OK

my $user = ~$*USER;
my $uid = +$*USER;
my $user := ~$*USER;
my $uid := +$*USER;
for "--user=$user", qq/--user=*eq"$user"/, "--user=$user,$user",
"--uid=$uid", "--uid=*==$uid" {
query-ok '--find', $_, ok => qq:to/OK/;
Expand All @@ -444,8 +446,8 @@ for "--user=!$user", qq/--user=*ne"$user"/, "--user=!$user,$user",
query-ok '--find', $_, ok => "";
}

my $group = ~$*GROUP;
my $gid = +$*GROUP;
my $group := ~$*GROUP;
my $gid := +$*GROUP;
for "--group=$group", qq/--group=*eq"$group"/, "--group=$group,$group",
"--gid=$gid", "--gid=*==$gid" {
query-ok '--find', $_, ok => qq:to/OK/;
Expand All @@ -467,6 +469,34 @@ for "--group=!$group", qq/--group=*ne"$group"/, "--group=!$group,$group",
query-ok '--find', $_, ok => "";
}

for <zero --files-with-matches>, <null --files-without-matches> {
query-ok $_, ok => qq:to/OK/;
zero
one
two
three
four
five
six
seven
eight
nine
OK
}

query-ok 'zero', '--files-with-matches', '--count-only', :!head,
ok => '10 files with matches';
query-ok 'null', '--files-without-matches', '--count-only', :!head,
ok => '10 files without matches';

for (
<zero --files-with-matches --file-separator-null>,
<null --files-without-matches --file-separator-null>,
) {
query-ok $_, :!head,
ok => "zero\0one\0two\0three\0four\0five\0six\0seven\0eight\0nine";
}

done-testing;

# vim: expandtab shiftwidth=4

0 comments on commit 8a2efb3

Please sign in to comment.