Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow fallback in case of out of bound #135

Merged
merged 9 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ regex = { version = "1.10", default-features = false, features = ["std", "unicod
serde_json = "1.0.113"

[features]
default = ["regex"]
default = ["regex", "fast-lane"]
fast-lane = []

[dev-dependencies]
assert_cmd = "2.0.13"
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ FLAGS:

OPTIONS:
-f, --fields <bounds> Fields to keep, 1-indexed, comma separated.
Use colon to include everything in a range.
Use colon (:) to match a range (inclusive).
Use equal (=) to apply out of bound fallback.
Fields can be negative (-1 is the last field).
[default 1:]

Expand All @@ -64,6 +65,7 @@ OPTIONS:
-f 3,2 => cb
-f 3,1:2 => ca-b
-f -3:-2 => b-c
-f 1,8=fallback => afallback

To re-apply the delimiter add -j, to replace
it add -r (followed by the new delimiter).
Expand All @@ -84,6 +86,10 @@ OPTIONS:
-r, --replace-delimiter <new> Replace the delimiter with the provided text
-t, --trim <type> Trim the delimiter (greedy). Valid values are
(l|L)eft, (r|R)ight, (b|B)oth
--fallback-oob <fallback> Generic fallback output for any field that
cannot be found (oob stands for out of bound).
It's overridden by any fallback assigned to a
specific field (see -f for help)

Options precedence:
--trim and --compress-delimiter are applied before --fields or similar
Expand Down
46 changes: 41 additions & 5 deletions doc/tuc.1
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ Print fields as a JSON array of strings
.PD 0
.P
.PD
\ \ \ \ \ \ \ Use colon to include everything in a range.
\ \ \ \ \ \ \ Use colon (:) to match a range (inclusive).
.PD 0
.P
.PD
\ \ \ \ \ \ \ Use equal (=) to apply out of bound fallback.
.PD 0
.P
.PD
Expand Down Expand Up @@ -100,6 +104,10 @@ Print fields as a JSON array of strings
.P
.PD
\ \ \ \ \ \ \ \ \ \f[V]-f -3:-2 => b-c\f[R]
.PD 0
.P
.PD
\ \ \ \ \ \ \ \ \ \f[V]-f 1,8=fallback => afallback\f[R]
.PP
\ \ \ \ \ \ \ To re-apply the delimiter add -j, to replace
.PD 0
Expand Down Expand Up @@ -173,6 +181,24 @@ To merge lines, use --no-join
.P
.PD
\ \ \ \ \ \ \ Valid values are (l|L)eft, (r|R)ight, (b|B)oth
.PP
\ \ \ \ \f[B]--fallback-oob\f[R] [fallback]
.PD 0
.P
.PD
\ \ \ \ \ \ \ Generic fallback output for any field that
.PD 0
.P
.PD
\ \ \ \ \ \ \ cannot be found (oob stands for out of bound).
.PD 0
.P
.PD
\ \ \ \ \ \ \ It\[cq]s overridden by any fallback assigned to a
.PD 0
.P
.PD
\ \ \ \ \ \ \ specific field (see -f for help)
.SH OPTIONS PRECEDENCE
.PP
--trim and --compress-delimiter are applied before --fields or similar
Expand All @@ -181,15 +207,25 @@ To merge lines, use --no-join
--characters and --fields read and allocate memory one line at a time
.PP
--lines allocate memory one line at a time as long as the requested
fields are ordered and non-negative (e.g.\ -l 1,3:4,4,7), otherwise it
allocates the whole input in memory (it also happens when -p or -m are
being used)
fields are
.PD 0
.P
.PD
ordered and non-negative (e.g.\ -l 1,3:4,4,7), otherwise it allocates
.PD 0
.P
.PD
the whole input in memory (it also happens when -p or -m are being used)
.PP
--bytes allocate the whole input in memory
.SH COLORS
.PP
Help is displayed using colors.
Colors will be suppressed in the following circumstances:
Colors will be suppressed in the
.PD 0
.P
.PD
following circumstances:
.IP \[bu] 2
when the TERM environment variable is not set or set to \[lq]dumb\[rq]
.IP \[bu] 2
Expand Down
20 changes: 14 additions & 6 deletions doc/tuc.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ OPTIONS

| **-f**, **\--fields** [bounds]
| Fields to keep, 1-indexed, comma separated.
| Use colon to include everything in a range.
| Use colon (:) to match a range (inclusive).
| Use equal (=) to apply out of bound fallback.
| Fields can be negative (-1 is the last field).

| [default 1:]
Expand All @@ -66,6 +67,7 @@ OPTIONS
| `-f 3,2 => cb`
| `-f 3,1:2 => ca-b`
| `-f -3:-2 => b-c`
| `-f 1,8=fallback => afallback`

| To re-apply the delimiter add -j, to replace
| it add -r (followed by the new delimiter)
Expand Down Expand Up @@ -100,6 +102,12 @@ OPTIONS
| Trim the delimiter (greedy).
| Valid values are (l|L)eft, (r|R)ight, (b|B)oth

| **\--fallback-oob** [fallback]
| Generic fallback output for any field that
| cannot be found (oob stands for out of bound).
| It's overridden by any fallback assigned to a
| specific field (see -f for help)

OPTIONS PRECEDENCE
==================

Expand All @@ -110,17 +118,17 @@ MEMORY CONSUMPTION

\--characters and \--fields read and allocate memory one line at a time

\--lines allocate memory one line at a time as long as the requested fields are
ordered and non-negative (e.g. -l 1,3:4,4,7), otherwise it allocates
the whole input in memory (it also happens when -p or -m are being used)
| \--lines allocate memory one line at a time as long as the requested fields are
| ordered and non-negative (e.g. -l 1,3:4,4,7), otherwise it allocates
| the whole input in memory (it also happens when -p or -m are being used)

\--bytes allocate the whole input in memory

COLORS
======

Help is displayed using colors. Colors will be suppressed in the
following circumstances:
| Help is displayed using colors. Colors will be suppressed in the
| following circumstances:

- when the TERM environment variable is not set or set to "dumb"
- when the NO_COLOR environment variable is set (regardless of value)
Expand Down
39 changes: 38 additions & 1 deletion src/bin/tuc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,41 @@ use tuc::bounds::{BoundOrFiller, BoundsType, UserBoundsList};
use tuc::cut_bytes::read_and_cut_bytes;
use tuc::cut_lines::read_and_cut_lines;
use tuc::cut_str::read_and_cut_str;
use tuc::fast_lane::{read_and_cut_text_as_bytes, FastOpt};
use tuc::help::{get_help, get_short_help};
use tuc::options::{Opt, EOL};

#[cfg(feature = "fast-lane")]
use tuc::fast_lane::{read_and_cut_text_as_bytes, FastOpt};

#[cfg(feature = "regex")]
use tuc::options::RegexBag;

#[cfg(feature = "regex")]
use regex::bytes::Regex;

#[cfg(not(feature = "fast-lane"))]
struct FastOpt {}

#[cfg(not(feature = "fast-lane"))]
impl<'a> TryFrom<&'a Opt> for FastOpt {
type Error = &'static str;

fn try_from(_value: &'a Opt) -> Result<Self, Self::Error> {
Err("This binary was not compiled with the feature fast-lane")
}
}

#[cfg(not(feature = "fast-lane"))]
fn read_and_cut_text_as_bytes<R: std::io::BufRead, W: Write>(
_stdin: &mut R,
_stdout: &mut W,
_fast_opt: &FastOpt,
) -> Result<()> {
Err(anyhow::Error::msg(
"This binary was not compiled with the feature fast-lane",
))
}

fn parse_args() -> Result<Opt, pico_args::Error> {
let mut pargs = pico_args::Arguments::from_env();

Expand Down Expand Up @@ -185,6 +210,18 @@ fn parse_args() -> Result<Opt, pico_args::Error> {
bounds,
replace_delimiter,
trim: pargs.opt_value_from_str(["-t", "--trim"])?,
fallback_oob: pargs
.opt_value_from_str("--fallback-oob")
.or_else(|e| match e {
pico_args::Error::OptionWithoutAValue(_) => {
// We must consume the arg ourselves (it's not done on error)
pargs.contains("--fallback-oob=");

Ok(Some("".into()))
}
_ => Err(e),
})?
.map(|x: String| x.into()),
regex_bag,
};

Expand Down
Loading
Loading