Skip to content

Commit

Permalink
Add --count-only and framework for --help
Browse files Browse the repository at this point in the history
  • Loading branch information
lizmat committed Jul 18, 2022
1 parent 1be567c commit 173aedf
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 36 deletions.
14 changes: 10 additions & 4 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
Revision history for App-Rak

{{$NEXT}}
- Add dependency on META::constants
- Add dependency on CLI::Help to get --help support
- Make sure that option replacements occur recursively
- Bump dependency on "Files::Containing" to get :count-only functionality
- Add support for --count-only functionality
- Cleaned up Changes

0.0.13 2022-07-17T10:17:34+02:00
- Fix issue when there was no config file available
- Remove mentions of now removed option names
- Allow editor to be named with --edit

0.0.12 2022-07-16T22:17:25+02:00
- Process named arg substitutions in the order they are specified
- Process option substitutions in the order they are specified
- Remove all aliases for each option: each option can only be accessed
by default using a single (long) name, for clarity. Users can add
their own shortcuts with --save, or add a set from someone adhering
Expand All @@ -19,8 +25,8 @@ Revision history for App-Rak
in line with names used

0.0.11 2022-07-16T13:29:57+02:00
- Remove "--with" named argument: you can now use any saved named
argument directly, without having to use --with
- Remove "--with" option: you can now use any saved option directly,
without having to use --with
- Bump dependency on "highlighter" to get colum fix for regexes
and fix for highlighting on regexes issue
- Bump dependency on "Edit::Files" to not call editor if nothing to edit
Expand Down Expand Up @@ -64,7 +70,7 @@ Revision history for App-Rak
- Bump dependency on Files::Containing to get new semantics if
a Callable is specified as the pattern
- Bump dependency on highlighter to be able to use the
:summary-if-larger-than feayture
:summary-if-larger-than feature
- Bump dependency on CLI::Version just for the sake of it
- Improved internal documentation

Expand Down
10 changes: 7 additions & 3 deletions META6.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,25 @@
"build-depends": [
],
"depends": [
"CLI::Help:ver<0.0.2>:auth<zef:lizmat>",
"CLI::Version:ver<0.0.3>:auth<zef:lizmat>",
"META::constants:ver<0.0.2>:auth<zef:lizmat>",
"highlighter:ver<0.0.11>:auth<zef:lizmat>",
"Files::Containing:ver<0.0.10>:auth<zef:lizmat>",
"Files::Containing:ver<0.0.11>:auth<zef:lizmat>",
"as-cli-arguments:ver<0.0.3>:auth<zef:lizmat>",
"Edit::Files:ver<0.0.4>:auth<zef:lizmat>",
"JSON::Fast:ver<0.17>:auth<cpan:TIMOTIMO>"
],
"description": "a CLI for searching strings in files",
"description": "a CLI for searching strings in files and more",
"license": "Artistic-2.0",
"name": "App::Rak",
"perl": "6.d",
"provides": {
"App::Rak": "lib/App/Rak.rakumod"
},
"resources": [
"help.txt",
"help/edit.txt"
],
"source-url": "https://github.com/lizmat/App-Rak.git",
"tags": [
Expand All @@ -31,5 +35,5 @@
],
"test-depends": [
],
"version": "0.0.13"
"version": "0.0.14"
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
NAME
====

App::Rak - a CLI for searching strings in files
App::Rak - a CLI for searching strings in files and more

SYNOPSIS
========
Expand Down
86 changes: 62 additions & 24 deletions lib/App/Rak.rakumod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# The modules that we need here, with their full identities
use highlighter:ver<0.0.11>:auth<zef:lizmat>;
use Files::Containing:ver<0.0.10>:auth<zef:lizmat>;
use Files::Containing:ver<0.0.11>:auth<zef:lizmat>;
use as-cli-arguments:ver<0.0.3>:auth<zef:lizmat>;
use Edit::Files:ver<0.0.4>:auth<zef:lizmat>;
use JSON::Fast:ver<0.17>:auth<cpan:TIMOTIMO>;
Expand Down Expand Up @@ -99,11 +99,33 @@ my sub add-before-after($io, @initially-selected, int $before, int $after) {
@selected
}

# Set up the --help handler
use META::constants:ver<0.0.2>:auth<zef:lizmat> $?DISTRIBUTION;
my sub HELP($text, @keys, :$verbose) {
my $SCRIPT := $*PROGRAM.basename;
my $header := "$SCRIPT - " ~ DESCRIPTION;
say $header;
say "-" x $header.chars;
if @keys {
say "Specific help about '@keys[]':";
say "";
}
say $text;

if $verbose {
say "";
say CREDITS;
say "";
say "Thank you for using $SCRIPT!";
}
}

# Entry point for CLI processing
my proto sub MAIN(|) is export {*}

# Make sure we can do -V --version
# Make sure we can do --help and --version
use CLI::Version:ver<0.0.3>:auth<zef:lizmat> $?DISTRIBUTION, &MAIN;
use CLI::Help:ver<0.0.2>:auth<zef:lizmat> %?RESOURCES, &MAIN, &HELP;

# Main handler
my multi sub MAIN(*@specs, *%n) { # *%_ causes compilation issues
Expand All @@ -129,15 +151,14 @@ my multi sub MAIN(*@specs, *%n) { # *%_ causes compilation issues
exit;
}

# Translate any custom parameters
# Recursively translate any custom parameters
my @strange;
for original-nameds() -> $option {
my $value := %n{$option};
my sub translate($option, $value) {
if %config{$option} -> %adding {
if Bool.ACCEPTS($value) {
%n{$option}:delete;
if $value {
%n{.key} = .value unless %n{.key}:exists for %adding;
translate(.key, .value) unless %n{.key}:exists for %adding;
}
else {
%n{.key}:delete for %adding;
Expand All @@ -148,6 +169,7 @@ my multi sub MAIN(*@specs, *%n) { # *%_ causes compilation issues
}
}
}
translate($_, %n{$_}) for original-nameds;
meh "These options must be flags, did you mean: @strange[] ?" if @strange;

my $needle = %n<pattern>:delete // @specs.shift;
Expand Down Expand Up @@ -186,21 +208,19 @@ my multi sub MAIN(*@specs, *%n) { # *%_ causes compilation issues
?? go-edit-files($editor, $needle, @paths, %n)
!! is-simple-Callable($needle) && (%n<replace-files>:delete)
?? replace-files($needle, @paths, %n)
!! (%n<files-with-matches>:delete)
?? files-only($needle, @paths, %n)
!! want-lines($needle, @paths, %n);
!! (%n<count-only>:delete)
?? count-only($needle, @paths, %n)
!! (%n<files-with-matches>:delete)
?? files-only($needle, @paths, %n)
!! want-lines($needle, @paths, %n);
}

my sub go-edit-files($editor, $needle, @paths, %_ --> Nil) {
CATCH { meh .message }

my $files-only := %_<files-with-matches>:delete;
my %ignore := named-args %_,
:ignorecase<i ignore-case>,
:ignoremark<m ignore-mark>,
;
my %additional = |(named-args %_, :batch, :degree, :max-count), |%ignore;

my %ignore := named-args %_, :ignorecase :ignoremark;
my %additional = |(named-args %_, :batch, :degree, :max-count), |%ignore;
meh-if-unexpected(%_);

edit-files ($files-only
Expand All @@ -218,17 +238,28 @@ my sub replace-files($needle, @paths, %_ --> Nil) {
NYI "replace-files: under construction";
}

my sub count-only($needle, @paths, %_ --> Nil) {
my $files-with-matches := %_<files-with-matches>:delete;
my %additional := named-args %_,
:ignorecase, :ignoremark, :invert-match, :batch, :degree;
meh-if-unexpected(%_);

my int $files;
my int $matches;
for files-containing $needle, @paths, :count-only, |%additional {
++$files;
$matches += .value;
say .key.relative ~ ': ' ~ .value if $files-with-matches;
}
say "$matches matches in $files files";
}

my sub files-only($needle, @paths, %_ --> Nil) {
my %additional := named-args %_,
:ignorecase<i ignore-case>,
:ignoremark<m ignore-mark>,
:invert-match<v>,
:batch,
:degree,
;
:ignorecase, :ignoremark, :invert-match, :batch, :degree;
meh-if-unexpected(%_);

.relative.say
say .relative
for files-containing $needle, @paths, :files-only, |%additional;
}

Expand Down Expand Up @@ -341,7 +372,7 @@ my sub want-lines($needle, @paths, %_ --> Nil) {
=head1 NAME
App::Rak - a CLI for searching strings in files
App::Rak - a CLI for searching strings in files and more
=head1 SYNOPSIS
Expand Down Expand Up @@ -412,6 +443,13 @@ Indicate the number of lines that should be shown B<around> any line that
matches. Defaults to B<0>. Overrides any a C<--after-context> or
C<--before-context> arguments.
=head2 --count-only
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<:files-with-matches> option is also specified with
a C<True> value, will also list the file names with their respective counts.
=head2 --edit
Indicate whether the patterns found should be fed into an editor for
Expand Down Expand Up @@ -554,7 +592,7 @@ context for lines was specified, else defaults to C<False>.
If the only argument, shows the name and version of the script, and the
system it is running on.
=head1 CREATING YOUR OPTIONS
=head1 CREATING YOUR OWN OPTIONS
You can use the C<--save> option to save a set of options and than later
access them with the given name:
Expand Down
1 change: 1 addition & 0 deletions resources/help.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
See https://raku.land/zef:lizmat/App::Rak for now.
1 change: 1 addition & 0 deletions resources/help/edit.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
See https://raku.land/zef:lizmat/App::Rak#--edit for now
4 changes: 2 additions & 2 deletions run-tests
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
unit sub MAIN(:$author);
unit sub MAIN(:a($author));

say run(<raku --version>, :out).out.slurp.chomp;
say "Running on $*DISTRO.gist().\n";
Expand Down Expand Up @@ -41,7 +41,7 @@ sub test-dir($dir) {
}

test-dir("t");
test-dir("xt") if $author;
test-dir("xt") if $author && "xt".IO.e;

if @failed {
say "FAILED: {+@failed} of $done:";
Expand Down
5 changes: 3 additions & 2 deletions t/01-basic.rakutest
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use Test;
use App::Rak;
}

pass "replace me";
plan 1;
pass "still alive";

done-testing;
# vim: expandtab shiftwidth=4

0 comments on commit 173aedf

Please sign in to comment.