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

new: Fields and message expansion feature #290

Draft
wants to merge 19 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
draft multiline expansion settings
  • Loading branch information
pamburus committed Jun 19, 2024
commit 14bf38986bc81761f52aa41a283fc1f7a186d75d
1 change: 1 addition & 0 deletions etc/defaults/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ formatting:
flatten: always
expansion:
mode: auto
multiline: standard
thresholds:
global: 1024
cumulative: 192
Expand Down
8 changes: 8 additions & 0 deletions schema/json/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,14 @@
"always"
]
},
"multiline": {
"type": "string",
"enum": [
"standard",
"disabled",
"inline"
]
},
"thresholds": {
"type": "object",
"additionalProperties": false,
Expand Down
18 changes: 9 additions & 9 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
model::{Filter, Parser, ParserSettings, RawRecord, Record, RecordFilter, RecordWithSourceConstructor},
query::Query,
scanning::{BufFactory, Delimit, Delimiter, Scanner, SearchExt, Segment, SegmentBuf, SegmentBufFactory},
settings::{ExpandOption, FieldShowOption, Fields, Formatting},
settings::{ExpansionMode, FieldShowOption, Fields, Formatting},
theme::{Element, StylingPush, Theme},
timezone::Tz,
IncludeExcludeKeyFilter, QueryNone,
Expand Down Expand Up @@ -75,7 +75,7 @@
pub delimiter: Delimiter,
pub unix_ts_unit: Option<UnixTimestampUnit>,
pub flatten: bool,
pub expand: ExpandOption,
pub expand: ExpansionMode,
}

impl Options {
Expand Down Expand Up @@ -445,17 +445,17 @@
}

fn follow(&self, inputs: Vec<InputReference>, output: &mut Output) -> Result<()> {
let si = &self.options.theme.indicators.sync;
let si_width = max(si.synced.width, si.failed.width);
let si_synced = si.synced.value.to_owned() + &repeat(' ').take(si_width - si.synced.width).collect::<String>();
let si_failed = si.failed.value.to_owned() + &repeat(' ').take(si_width - si.failed.width).collect::<String>();
let si_placeholder = repeat(' ').take(si_width).collect::<String>();
let mut input_badges = self.input_badges(inputs.iter());
if let Some(badges) = &mut input_badges {
for i in 0..badges.len() {
badges[i] = format!("{}{}", si_placeholder, badges[i]);
}
}

Check warning on line 458 in src/app.rs

View check run for this annotation

Codecov / codecov/patch

src/app.rs#L448-L458

Added lines #L448 - L458 were not covered by tests

let m = inputs.len();
let n = self.options.concurrency;
Expand Down Expand Up @@ -535,10 +535,10 @@
// spawn processing threads
let mut workers = Vec::with_capacity(n);
for _ in 0..n {
let worker = scope.spawn(closure!(ref bfo, ref parser, ref sfi, ref input_badges, ref si_placeholder, clone rxi, clone txo, |_| {

Check warning on line 538 in src/app.rs

View check run for this annotation

Codecov / codecov/patch

src/app.rs#L538

Added line #L538 was not covered by tests
let mut processor = self.new_segment_processor(&parser);
for (i, j, segment) in rxi.iter() {
let prefix = input_badges.as_ref().map(|b|b[i].as_str()).unwrap_or(si_placeholder);

Check warning on line 541 in src/app.rs

View check run for this annotation

Codecov / codecov/patch

src/app.rs#L541

Added line #L541 was not covered by tests
match segment {
Segment::Complete(segment) => {
let mut buf = bfo.new_buf();
Expand Down Expand Up @@ -576,14 +576,14 @@
}
if let Some(entry) = window.pop_first() {
let sync_indicator = if prev_ts.map(|ts| ts <= entry.0.0).unwrap_or(true) {
&si_synced

Check warning on line 579 in src/app.rs

View check run for this annotation

Codecov / codecov/patch

src/app.rs#L579

Added line #L579 was not covered by tests
} else {
&si_failed

Check warning on line 581 in src/app.rs

View check run for this annotation

Codecov / codecov/patch

src/app.rs#L581

Added line #L581 was not covered by tests
};
prev_ts = Some(entry.0.0);
mem_usage -= entry.1.1.end - entry.1.1.start;
output.write_all(sync_indicator.as_bytes())?;
output.write_all(&entry.1.0[entry.1.1.clone()][si_width..])?;

Check warning on line 586 in src/app.rs

View check run for this annotation

Codecov / codecov/patch

src/app.rs#L585-L586

Added lines #L585 - L586 were not covered by tests
output.write_all(&[b'\n'])?;
}
}
Expand Down Expand Up @@ -1068,7 +1068,7 @@
filtering::MatchOptions,
level::Level,
model::FieldFilterSet,
settings::{self, ExpandOption},
settings::{self, ExpansionMode},
syntax::*,
themecfg::{self, testing},
LinuxDateFormat,
Expand Down Expand Up @@ -1418,7 +1418,7 @@

let mut output = Vec::new();
let app = App::new(Options {
expand: ExpandOption::Always,
expand: ExpansionMode::Always,
..options()
});

Expand Down Expand Up @@ -1447,7 +1447,7 @@

let mut output = Vec::new();
let app = App::new(Options {
expand: ExpandOption::Never,
expand: ExpansionMode::Never,
..options()
});

Expand All @@ -1467,7 +1467,7 @@

let mut output = Vec::new();
let app = App::new(Options {
expand: ExpandOption::Always,
expand: ExpansionMode::Always,
theme: Theme::from(themecfg::Theme {
elements: themecfg::StylePack(hashmap! {
Element::ValueExpansion => themecfg::Style::default(),
Expand Down Expand Up @@ -1507,7 +1507,7 @@

let mut output = Vec::new();
let app = App::new(Options {
expand: ExpandOption::Always,
expand: ExpansionMode::Always,
theme: Theme::from(themecfg::Theme {
elements: themecfg::StylePack(hashmap! {
Element::ValueExpansion => themecfg::Style::default(),
Expand Down Expand Up @@ -1544,7 +1544,7 @@

let mut output = Vec::new();
let app = App::new(Options {
expand: ExpandOption::Auto,
expand: ExpansionMode::Auto,
..options()
});

Expand All @@ -1562,7 +1562,7 @@

let mut output = Vec::new();
let app = App::new(Options {
expand: ExpandOption::Auto,
expand: ExpansionMode::Auto,
hide_empty_fields: true,
..options()
});
Expand Down
51 changes: 27 additions & 24 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@
default_missing_value = "always",
help_heading = heading::OUTPUT,
)]
pub expand: ExpandOption,

Check warning on line 305 in src/cli.rs

View check run for this annotation

Codecov / codecov/patch

src/cli.rs#L305

Added line #L305 was not covered by tests

/// Show input number and/or input filename before each message.
#[arg(
Expand Down Expand Up @@ -507,28 +507,28 @@
Always,
}

impl From<settings::ExpandOption> for ExpandOption {
fn from(value: settings::ExpandOption) -> Self {
impl From<settings::ExpansionMode> for ExpandOption {
fn from(value: settings::ExpansionMode) -> Self {
match value {
settings::ExpandOption::Auto => Self::Auto,
settings::ExpandOption::Never => Self::Never,
settings::ExpandOption::Always => Self::Always,
settings::ExpansionMode::Auto => Self::Auto,
settings::ExpansionMode::Never => Self::Never,
settings::ExpansionMode::Always => Self::Always,
}
}
}

impl From<Option<settings::ExpandOption>> for ExpandOption {
fn from(value: Option<settings::ExpandOption>) -> Self {
impl From<Option<settings::ExpansionMode>> for ExpandOption {
fn from(value: Option<settings::ExpansionMode>) -> Self {
Self::from(value.unwrap_or_default())
}
}

impl Into<settings::ExpandOption> for ExpandOption {
fn into(self) -> settings::ExpandOption {
impl Into<settings::ExpansionMode> for ExpandOption {
fn into(self) -> settings::ExpansionMode {
match self {
Self::Auto => settings::ExpandOption::Auto,
Self::Never => settings::ExpandOption::Never,
Self::Always => settings::ExpandOption::Always,
Self::Auto => settings::ExpansionMode::Auto,
Self::Never => settings::ExpansionMode::Never,
Self::Always => settings::ExpansionMode::Always,
}
}
}
Expand Down Expand Up @@ -597,31 +597,34 @@
fn test_expand_option() {
assert_eq!(ExpandOption::from(None), ExpandOption::Auto);
assert_eq!(
ExpandOption::from(Some(settings::ExpandOption::Auto)),
ExpandOption::from(Some(settings::ExpansionMode::Auto)),
ExpandOption::Auto
);
assert_eq!(
ExpandOption::from(Some(settings::ExpandOption::Never)),
ExpandOption::from(Some(settings::ExpansionMode::Never)),
ExpandOption::Never
);
assert_eq!(
ExpandOption::from(Some(settings::ExpandOption::Always)),
ExpandOption::from(Some(settings::ExpansionMode::Always)),
ExpandOption::Always
);
assert_eq!(ExpandOption::from(settings::ExpandOption::Auto), ExpandOption::Auto);
assert_eq!(ExpandOption::from(settings::ExpandOption::Never), ExpandOption::Never);
assert_eq!(ExpandOption::from(settings::ExpandOption::Always), ExpandOption::Always);
assert_eq!(ExpandOption::from(settings::ExpansionMode::Auto), ExpandOption::Auto);
assert_eq!(ExpandOption::from(settings::ExpansionMode::Never), ExpandOption::Never);
assert_eq!(
Into::<settings::ExpandOption>::into(ExpandOption::Auto),
settings::ExpandOption::Auto
ExpandOption::from(settings::ExpansionMode::Always),
ExpandOption::Always
);
assert_eq!(
Into::<settings::ExpansionMode>::into(ExpandOption::Auto),
settings::ExpansionMode::Auto
);
assert_eq!(
Into::<settings::ExpandOption>::into(ExpandOption::Never),
settings::ExpandOption::Never
Into::<settings::ExpansionMode>::into(ExpandOption::Never),
settings::ExpansionMode::Never
);
assert_eq!(
Into::<settings::ExpandOption>::into(ExpandOption::Always),
settings::ExpandOption::Always
Into::<settings::ExpansionMode>::into(ExpandOption::Always),
settings::ExpansionMode::Always
);
}
}
64 changes: 39 additions & 25 deletions src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
filtering::IncludeExcludeSetting,
fmtx::{aligned_left, centered, OptimizedBuf, Push},
model::{self, Caller, Level, RawValue},
settings::{self, ExpandOption, FlattenOption, Formatting, Punctuation},
settings::{self, ExpansionMode, FlattenOption, Formatting, MultilineExpansion, Punctuation},
syntax::*,
theme::{Element, Styler, StylingPush, Theme},
IncludeExcludeKeyFilter,
Expand Down Expand Up @@ -65,31 +65,33 @@
// ---

pub struct Expansion {
pub mode: ExpandOption,
pub mode: ExpansionMode,
pub multiline: MultilineExpansion,
pub thresholds: ExpansionThresholds,
}

impl Expansion {
pub fn with_mode(mut self, mode: ExpandOption) -> Self {
pub fn with_mode(mut self, mode: ExpansionMode) -> Self {
self.mode = mode;
self
}
}

impl Default for Expansion {
fn default() -> Self {
Self {
mode: ExpandOption::Auto,
mode: ExpansionMode::Auto,
multiline: Default::default(),
thresholds: Default::default(),
}
}

Check warning on line 87 in src/formatting.rs

View check run for this annotation

Codecov / codecov/patch

src/formatting.rs#L81-L87

Added lines #L81 - L87 were not covered by tests
}

impl From<ExpandOption> for Expansion {
fn from(mode: ExpandOption) -> Self {
impl From<ExpansionMode> for Expansion {
fn from(mode: ExpansionMode) -> Self {
Self {
mode,
..Default::default()

Check warning on line 94 in src/formatting.rs

View check run for this annotation

Codecov / codecov/patch

src/formatting.rs#L91-L94

Added lines #L91 - L94 were not covered by tests
}
}
}
Expand All @@ -98,6 +100,7 @@
fn from(options: settings::ExpansionOptions) -> Self {
Self {
mode: options.mode.unwrap_or_default(),
multiline: options.multiline.unwrap_or_default(),
thresholds: options.thresholds.into(),
}
}
Expand All @@ -113,13 +116,13 @@
}

impl Default for ExpansionThresholds {
fn default() -> Self {
Self {
global: DEFAULT_EXPAND_ALL_THRESHOLD,
cumulative: DEFAULT_EXPAND_CUMULATIVE_THRESHOLD,
message: DEFAULT_EXPAND_MESSAGE_THRESHOLD,
field: DEFAULT_EXPAND_FIELD_THRESHOLD,
}

Check warning on line 125 in src/formatting.rs

View check run for this annotation

Codecov / codecov/patch

src/formatting.rs#L119-L125

Added lines #L119 - L125 were not covered by tests
}
}

Expand Down Expand Up @@ -274,7 +277,7 @@
fs.expand = fs.expand.or_else(|| {
let thresholds = &self.cfg.expansion.thresholds;
if fs.complexity >= thresholds.cumulative {
Some(true)

Check warning on line 280 in src/formatting.rs

View check run for this annotation

Codecov / codecov/patch

src/formatting.rs#L280

Added line #L280 was not covered by tests
} else if self.complexity(fs.complexity, rec, Some(&self.cfg.fields)) >= thresholds.global {
Some(true)
} else {
Expand Down Expand Up @@ -407,7 +410,7 @@
None => (None, setting, true),
}
}
None => (None, setting, true),

Check warning on line 413 in src/formatting.rs

View check run for this annotation

Codecov / codecov/patch

src/formatting.rs#L413

Added line #L413 was not covered by tests
};
if setting == IncludeExcludeSetting::Exclude && leaf {
continue;
Expand Down Expand Up @@ -482,11 +485,15 @@
s.element(Element::Message, |s| {
s.batch(|buf| {
let result = MessageFormatAuto::new(value)
.on_extended_space::<()>(if fs.expand.unwrap_or(true) {
ExtendedSpaceAction::Abort
} else {
ExtendedSpaceAction::FormatWithBacktick
})
.on_extended_space::<()>(
match (fs.expand.unwrap_or(true), self.cfg.expansion.multiline) {
(_, MultilineExpansion::Disabled) | (false, MultilineExpansion::Standard) => {
ExtendedSpaceAction::Escape
}
(true, MultilineExpansion::Standard) => ExtendedSpaceAction::Abort,
(_, MultilineExpansion::Inline) => ExtendedSpaceAction::Inline,

Check warning on line 494 in src/formatting.rs

View check run for this annotation

Codecov / codecov/patch

src/formatting.rs#L494

Added line #L494 was not covered by tests
},
)
.format(buf)
.unwrap();
match result {
Expand Down Expand Up @@ -603,15 +610,15 @@
filter: Option<&IncludeExcludeKeyFilter>,
) {
let result = if fs.expand == Some(true) {
Err((key, value))

Check warning on line 613 in src/formatting.rs

View check run for this annotation

Codecov / codecov/patch

src/formatting.rs#L613

Added line #L613 was not covered by tests
} else {
fs.fields_to_expand.push((key, value))
};

if let Err((key, value)) = result {
fs.expand = Some(true);
self.expand(s, fs);
_ = self.format_field(s, key, value, fs, filter);

Check warning on line 621 in src/formatting.rs

View check run for this annotation

Codecov / codecov/patch

src/formatting.rs#L619-L621

Added lines #L619 - L621 were not covered by tests
}
}
}
Expand Down Expand Up @@ -780,7 +787,7 @@
}

if fs.expand.is_none() && value.raw_str().len() > self.rf.cfg.expansion.thresholds.field {
return FieldFormatResult::ExpansionNeeded;

Check warning on line 790 in src/formatting.rs

View check run for this annotation

Codecov / codecov/patch

src/formatting.rs#L790

Added line #L790 was not covered by tests
}

let ffv = self.begin(s, key, value, fs);
Expand Down Expand Up @@ -834,8 +841,15 @@
ValueFormatAuto::new(value)
.on_extended_space(match fs.expand {
Some(true) => ExtendedSpaceAction::Expand(|buf: &mut Vec<u8>| self.add_prefix(buf, fs)),
Some(false) => ExtendedSpaceAction::FormatWithBacktick,
None => ExtendedSpaceAction::Abort,
Some(false) => match self.rf.cfg.expansion.multiline {
MultilineExpansion::Inline => ExtendedSpaceAction::Inline,

Check warning on line 845 in src/formatting.rs

View check run for this annotation

Codecov / codecov/patch

src/formatting.rs#L845

Added line #L845 was not covered by tests
_ => ExtendedSpaceAction::Escape,
},
None => match self.rf.cfg.expansion.multiline {
MultilineExpansion::Standard => ExtendedSpaceAction::Abort,
MultilineExpansion::Disabled => ExtendedSpaceAction::Escape,
MultilineExpansion::Inline => ExtendedSpaceAction::Inline,

Check warning on line 851 in src/formatting.rs

View check run for this annotation

Codecov / codecov/patch

src/formatting.rs#L850-L851

Added lines #L850 - L851 were not covered by tests
},
})
.with_complexity_limit(complexity_limit)
.format(buf)
Expand Down Expand Up @@ -874,7 +888,7 @@

if let Some(limit) = complexity_limit {
if FIXED_COMPLEXITY + value.get().len() > limit {
return ValueFormatResult::ExpansionNeeded;

Check warning on line 891 in src/formatting.rs

View check run for this annotation

Codecov / codecov/patch

src/formatting.rs#L891

Added line #L891 was not covered by tests
}
}

Expand Down Expand Up @@ -927,9 +941,9 @@

if let Some(limit) = complexity_limit {
if FIXED_COMPLEXITY + value.get().len() > limit {
return ValueFormatResult::ExpansionNeeded;

Check warning on line 944 in src/formatting.rs

View check run for this annotation

Codecov / codecov/patch

src/formatting.rs#L944

Added line #L944 was not covered by tests
}
}

Check warning on line 946 in src/formatting.rs

View check run for this annotation

Codecov / codecov/patch

src/formatting.rs#L946

Added line #L946 was not covered by tests

fs.complexity += FIXED_COMPLEXITY;
let xb = fs.expand.replace(false);
Expand Down Expand Up @@ -1115,7 +1129,7 @@
// ---

/// Error is an error which may occur in the application.
#[derive(Error, Debug, Clone, Copy, PartialEq, Eq)]

Check warning on line 1132 in src/formatting.rs

View check run for this annotation

Codecov / codecov/patch

src/formatting.rs#L1132

Added line #L1132 was not covered by tests
pub enum Error {
#[error(transparent)]
ParseError(#[from] encstr::Error),
Expand Down Expand Up @@ -1153,7 +1167,7 @@
// ---

pub enum ExtendedSpaceAction<P = ()> {
FormatWithBacktick,
Inline,
Expand(P),
Escape,
Abort,
Expand All @@ -1166,8 +1180,8 @@
F: FnOnce(&P) -> P2,
{
match self {
Self::Expand(prefix) => ExtendedSpaceAction::Expand(f(prefix)),

Check warning on line 1183 in src/formatting.rs

View check run for this annotation

Codecov / codecov/patch

src/formatting.rs#L1183

Added line #L1183 was not covered by tests
Self::FormatWithBacktick => ExtendedSpaceAction::FormatWithBacktick,
Self::Inline => ExtendedSpaceAction::Inline,
Self::Escape => ExtendedSpaceAction::Escape,
Self::Abort => ExtendedSpaceAction::Abort,
}
Expand Down Expand Up @@ -1218,7 +1232,7 @@
pub fn new(string: S) -> Self {
Self {
string,
xs_action: ExtendedSpaceAction::FormatWithBacktick,
xs_action: ExtendedSpaceAction::Inline,
complexity_limit: None,
}
}
Expand Down Expand Up @@ -1326,7 +1340,7 @@
const BTXS: Mask = mask!(Flag::Control | Flag::ExtendedSpace | Flag::Backtick);

match (mask & BTXS, &self.xs_action) {
(Z, _) | (XS, ExtendedSpaceAction::FormatWithBacktick) => {
(Z, _) | (XS, ExtendedSpaceAction::Inline) => {
buf.push(b'`');
buf.push(b'`');
buf[begin..].rotate_right(1);
Expand Down Expand Up @@ -1492,7 +1506,7 @@
const BTXS: Mask = mask!(Flag::Control | Flag::ExtendedSpace | Flag::Backtick);

match (mask & BTXS, &self.xs_action) {
(Z, _) | (XS, ExtendedSpaceAction::FormatWithBacktick) => {
(Z, _) | (XS, ExtendedSpaceAction::Inline) => {
buf.push(b'`');
buf.push(b'`');
buf[begin..].rotate_right(1);
Expand Down Expand Up @@ -2213,7 +2227,7 @@
fields.entry("c").entry("z").exclude();
s.theme = Default::default();
s.flatten = false;
s.expansion.mode = ExpandOption::Always;
s.expansion.mode = ExpansionMode::Always;
s.fields = fields.into();
}));

Expand Down Expand Up @@ -2248,7 +2262,7 @@
fields.entry("c").entry("z").exclude();
s.theme = Default::default();
s.flatten = true;
s.expansion.mode = ExpandOption::Always;
s.expansion.mode = ExpansionMode::Always;
s.fields = fields.into();
}));

Expand Down Expand Up @@ -2277,7 +2291,7 @@
let formatter = RecordFormatter::new(settings().with(|s| {
s.theme = Default::default();
s.flatten = false;
s.expansion.mode = ExpandOption::Auto;
s.expansion.mode = ExpansionMode::Auto;
}));

let obj = json_raw_value(r#"{"x":10,"y":"some\nmultiline\nvalue","z":30}"#);
Expand Down Expand Up @@ -2307,7 +2321,7 @@
fn test_expand_all_threshold() {
let formatter = RecordFormatter::new(settings().with(|s| {
s.theme = Default::default();
s.expansion.mode = ExpandOption::Auto;
s.expansion.mode = ExpansionMode::Auto;
s.expansion.thresholds.global = 2;
}));

Expand Down Expand Up @@ -2365,7 +2379,7 @@

let formatter = RecordFormatter::new(settings().with(|s| {
s.theme = Default::default();
s.expansion.mode = ExpandOption::Auto;
s.expansion.mode = ExpansionMode::Auto;
}));

assert_eq!(formatter.format_to_string(&rec), r#"m a=1 b=2 c=3"#);
Expand All @@ -2384,7 +2398,7 @@

let mut formatter = RecordFormatter::new(settings().with(|s| {
s.theme = Default::default();
s.expansion.mode = ExpandOption::Auto;
s.expansion.mode = ExpansionMode::Auto;
s.expansion.thresholds.message = 64;
}));

Expand Down Expand Up @@ -2457,7 +2471,7 @@

let formatter = RecordFormatter::new(settings().with(|s| {
s.theme = Default::default();
s.expansion.mode = ExpandOption::Always;
s.expansion.mode = ExpansionMode::Always;
}));

assert_eq!(
Expand Down
Loading
Loading