Skip to content

Commit

Permalink
new: disable input info in raw mode unless explicitly requested (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
pamburus authored May 2, 2024
1 parent 51048eb commit df3bf58
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
4 changes: 2 additions & 2 deletions 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 @@ -4,7 +4,7 @@ members = [".", "crate/encstr"]
[workspace.package]
repository = "https://github.com/pamburus/hl"
authors = ["Pavel Ivanov <[email protected]>"]
version = "0.29.0-alpha.2"
version = "0.29.0-alpha.3"
edition = "2021"
license = "MIT"

Expand Down
32 changes: 31 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ impl Options {
fn with_raw_fields(self, raw_fields: bool) -> Self {
Self { raw_fields, ..self }
}

#[cfg(test)]
fn with_raw(self, raw: bool) -> Self {
Self { raw, ..self }
}

#[cfg(test)]
fn with_input_info(self, input_info: Option<InputInfo>) -> Self {
Self { input_info, ..self }
}
}

#[derive(Default)]
Expand Down Expand Up @@ -160,7 +170,10 @@ pub struct App {
pub type Output = dyn Write + Send + Sync;

impl App {
pub fn new(options: Options) -> Self {
pub fn new(mut options: Options) -> Self {
if options.raw && options.input_info == Some(InputInfo::Auto) {
options.input_info = None
}
Self { options }
}

Expand Down Expand Up @@ -1133,6 +1146,23 @@ mod tests {
);
}

#[test]
fn test_cat_raw_multiple_inputs() {
let input1 =
r#"{"caller":"main.go:539","duration":"15d","level":"info","ts":"2023-12-07T20:07:05.949Z","msg":"xy"}"#;
let input2 =
r#"{"caller":"main.go:539","duration":"15d","level":"info","ts":"2023-12-07T20:07:06.944Z","msg":"xy"}"#;
let mut output = Vec::new();
let mut ff = IncludeExcludeKeyFilter::new(MatchOptions::default());
ff.entry("duration").exclude();
let app = App::new(options().with_input_info(Some(InputInfo::Auto)).with_raw(true));
app.run(vec![input(input1), input(input2)], &mut output).unwrap();
assert_eq!(
std::str::from_utf8(&output).unwrap(),
format!("{}\n{}\n", input1, input2),
);
}

fn input<S: Into<String>>(s: S) -> InputHolder {
InputHolder::new(InputReference::File("-".into()), Some(Box::new(Cursor::new(s.into()))))
}
Expand Down

0 comments on commit df3bf58

Please sign in to comment.