Skip to content

Commit

Permalink
simplified + fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
pamburus committed Jun 11, 2024
1 parent c4c131d commit 55790aa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,7 @@ mod tests {
});
app.run(vec![input], &mut output).unwrap();
let actual = std::str::from_utf8(&output).unwrap();
let expected = "\u{1b}[0;2;3m2023-12-07 20:07:05.949 \u{1b}[0;36m|INF|\u{1b}[0;2;3m \u{1b}[0;32mmsg\u{1b}[0;2m=\u{1b}[0;93m[\u{1b}[0;39mx\u{1b}[0;2;3m \u{1b}[0;39my\u{1b}[0;93m]\u{1b}[0;2;3m \u{1b}[0;32mduration\u{1b}[0;2m=\u{1b}[0;39m15d\u{1b}[0;2;3m @ main.go:539\u{1b}[0m\n";
let expected = "\u{1b}[0;2;3m2023-12-07 20:07:05.949 \u{1b}[0;36m|INF| \u{1b}[0;32mmsg\u{1b}[0;2m=\u{1b}[0;93m[\u{1b}[0;39mx\u{1b}[0;2;3m \u{1b}[0;39my\u{1b}[0;93m] \u{1b}[0;32mduration\u{1b}[0;2m=\u{1b}[0;39m15d\u{1b}[0;2;3m @ main.go:539\u{1b}[0m\n";
assert_eq!(actual, expected);
}

Expand Down
68 changes: 29 additions & 39 deletions src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,19 +213,21 @@ impl RecordFormatter {
for (k, v) in x_fields.iter().chain(rec.fields()) {
for _ in 0..2 {
if !self.cfg.hide_empty_fields || !v.is_empty() {
let cp = fs.checkpoint(s);
match self.format_field(s, k, *v, &mut fs, Some(&self.cfg.fields)) {
FieldFormatResult::Ok => {}
FieldFormatResult::Hidden => {
some_fields_hidden = true;
}
FieldFormatResult::ExpansionNeeded => {
fs.restore(s, cp);
fs.expand = Some(true);
continue;
if fs.transact(s, |fs, s| {
match self.format_field(s, k, *v, fs, Some(&self.cfg.fields)) {
FieldFormatResult::Ok => true,
FieldFormatResult::Hidden => {
some_fields_hidden = true;
true
}
FieldFormatResult::ExpansionNeeded => {
fs.expand = Some(true);
false
}
}
}) {
break;
}
break;
}
}
}
Expand Down Expand Up @@ -533,24 +535,6 @@ impl<'a> FormattingStateWithRec<'a> {
}
result
}

fn checkpoint<S: StylingPush<Buf>>(&self, s: &mut S) -> Checkpoint {
Checkpoint {
dirty: self.dirty,
depth: self.depth,
first_line_used: self.first_line_used,
buf_len: s.batch(|buf| buf.len()),
}
}

fn restore<S: StylingPush<Buf>>(&mut self, s: &mut S, cp: Checkpoint) {
s.batch(|buf| {
buf.truncate(cp.buf_len);
});
self.dirty = cp.dirty;
self.depth = cp.depth;
self.first_line_used = cp.first_line_used;
}
}

impl<'a> Deref for FormattingStateWithRec<'a> {
Expand Down Expand Up @@ -590,15 +574,6 @@ struct FormattingState<'a> {

// ---

struct Checkpoint {
dirty: bool,
depth: usize,
first_line_used: bool,
buf_len: usize,
}

// ---

#[derive(Default)]
struct KeyPrefix {
value: OptimizedBuf<u8, 256>,
Expand Down Expand Up @@ -1096,7 +1071,8 @@ pub mod string {

let mask = buf[begin..].analyze();

const NON_PLAIN: Mask = mask!(Flag::DoubleQuote | Flag::Control | Flag::Backslash | Flag::Space);
const NON_PLAIN: Mask =
mask!(Flag::DoubleQuote | Flag::Control | Flag::Backslash | Flag::Space | Flag::EqualSign);
const POSITIVE_INTEGER: Mask = mask!(Flag::Digit);
const NUMBER: Mask = mask!(Flag::Digit | Flag::Dot | Flag::Minus);

Expand Down Expand Up @@ -2215,4 +2191,18 @@ mod tests {
"|INF| some-msg ts=some-unparsable-time"
);
}

#[test]
fn test_format_value_with_eq() {
let rec = |value| Record {
fields: RecordFields {
head: heapless::Vec::from_slice(&[("a", EncodedString::raw(value).into())]).unwrap(),
..Default::default()
},
..Default::default()
};

assert_eq!(format_no_color(&rec("x=y")), r#"a="x=y""#);
assert_eq!(format_no_color(&rec("|=>")), r#"a="|=>""#);
}
}

0 comments on commit 55790aa

Please sign in to comment.