Skip to content

Commit

Permalink
feature: allow requiring select captures for output to show (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
solidiquis authored Jan 13, 2025
1 parent e0f1c65 commit 1e0a3b3
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ All notable changes to this project will be documented in this file.

This project adheres to [Semantic Versioning](http://semver.org/).

## [0.2.0] - 2025-01-12

Adds ability to require specified captures to have at least one match in order for output to show for a given line:

```
-r, --require <REQUIRE> Name of capture that must have at least one match for the output to show. Can be specified multiple times
```

## [0.1.0] - 2025-01-11

First release
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description = """
A text line processor that applies regular expressions with named captures
to input lines and transforms them using a user-generated template.
"""
version = "0.1.0"
version = "0.2.0"
edition = "2021"
readme = "README.md"
license = "MIT"
Expand Down
7 changes: 6 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,12 @@ pub struct Cli {
/// Input files
pub files: Vec<String>,

/// Force output to be line-buffered. By default, output is line buffered when stdout is a
/// Name of capture that must have at least one match for the output to show. Can be specified
/// multiple times
#[arg(short, long)]
pub require: Vec<String>,

/// Force output to be line-buffered. By default, output is line buffered when stdout is a
/// terminal and block-buffered otherwise
#[arg(long)]
pub line_buffered: bool,
Expand Down
8 changes: 7 additions & 1 deletion src/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub fn process_lines(tty: &mut TtyContext, args: &Cli) -> Result<()> {
template,
files,
line_buffered,
require,
..
} = args;

Expand Down Expand Up @@ -55,7 +56,7 @@ pub fn process_lines(tty: &mut TtyContext, args: &Cli) -> Result<()> {

let mut writer = init_output_writer(tty, *line_buffered);

for line in scanner {
'outer: for line in scanner {
// Each iteration starts with a fresh captures map. Doing it this way the lifetime of the
// new captures map contain the lifetime of `line`, allowing us to work with a `Vec<&str>`
// as opposed to `Vec<String>`. There's no telling how many matches there could possibly be
Expand All @@ -76,6 +77,11 @@ pub fn process_lines(tty: &mut TtyContext, args: &Cli) -> Result<()> {
}
}
}
for capture_name in require {
if captures_map.get(capture_name.as_str()).is_none_or(|c| c.is_empty()) {
continue 'outer;
}
}
let output_line = template.transform(&captures_map);

if output_line.is_empty() {
Expand Down

0 comments on commit 1e0a3b3

Please sign in to comment.