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

Clear lines that are wrapped because of terminal width (#272) #273

Merged
merged 3 commits into from
Apr 6, 2023
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@ All user visible changes to `cucumber` crate will be documented in this file. Th
- [`tracing`] crate integration behind the `tracing` feature flag. ([#213], [#258], [#261])
- Support of `--report-time` CLI option for `writer::Libtest`. ([#264], [#265])

### Fixed

- Clearing lines that are wrapped because of terminal width. ([#272], [#273])

[#213]: /../../issues/213
[#258]: /../../pull/258
[#261]: /../../pull/261
[#264]: /../../issues/264
[#265]: /../../pull/265
[#272]: /../../discussions/272
[#273]: /../../pull/273



Expand Down
45 changes: 23 additions & 22 deletions src/writer/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,9 @@ impl<Out: io::Write> Basic<Out> {
&mut self,
feature: &gherkin::Feature,
) -> io::Result<()> {
self.lines_to_clear = 1;
self.output.write_line(
&self
.styles
.ok(format!("{}: {}", feature.keyword, feature.name)),
)
let out = format!("{}: {}", feature.keyword, feature.name);
self.lines_to_clear += self.styles.lines_count(&out);
self.output.write_line(&self.styles.ok(out))
}

/// Outputs the [`Rule`]'s [started]/[scenario]/[finished] event.
Expand Down Expand Up @@ -324,14 +321,15 @@ impl<Out: io::Write> Basic<Out> {
&mut self,
rule: &gherkin::Rule,
) -> io::Result<()> {
self.lines_to_clear = 1;
self.indent += 2;
self.output.write_line(&self.styles.ok(format!(
let out = format!(
"{indent}{}: {}",
rule.keyword,
rule.name,
indent = " ".repeat(self.indent)
)))
);
self.lines_to_clear += self.styles.lines_count(&out);
self.indent += 2;
self.output.write_line(&self.styles.ok(out))
}

/// Outputs the [`Scenario`]'s [started]/[background]/[step] event.
Expand Down Expand Up @@ -386,7 +384,7 @@ impl<Out: io::Write> Basic<Out> {

/// Outputs the [`event::Scenario::Log`].
pub(crate) fn emit_log(&mut self, msg: impl AsRef<str>) -> io::Result<()> {
self.lines_to_clear += msg.as_ref().lines().count();
self.lines_to_clear += self.styles.lines_count(msg.as_ref());
self.re_output_after_clear.push_str(msg.as_ref());
self.output.write_str(msg)
}
Expand Down Expand Up @@ -446,25 +444,28 @@ impl<Out: io::Write> Basic<Out> {
scenario: &gherkin::Scenario,
retries: Option<Retries>,
) -> io::Result<()> {
self.lines_to_clear = 1;
self.indent += 2;

if let Some(retries) = retries.filter(|r| r.current > 0) {
self.output.write_line(&self.styles.retry(format!(
let out = format!(
"{}{}: {} | Retry attempt: {}/{}",
" ".repeat(self.indent),
scenario.keyword,
scenario.name,
retries.current,
retries.left + retries.current,
)))
);
self.lines_to_clear += self.styles.lines_count(&out);
self.output.write_line(&self.styles.retry(out))
} else {
self.output.write_line(&self.styles.ok(format!(
let out = format!(
"{}{}: {}",
" ".repeat(self.indent),
scenario.keyword,
scenario.name,
)))
);
self.lines_to_clear += self.styles.lines_count(&out);
self.output.write_line(&self.styles.ok(out))
}
}

Expand Down Expand Up @@ -529,7 +530,7 @@ impl<Out: io::Write> Basic<Out> {
) -> io::Result<()> {
self.indent += 4;
if self.styles.is_present {
let output = format!(
let out = format!(
"{indent}{}{}{}{}",
step.keyword,
step.value,
Expand All @@ -550,8 +551,8 @@ impl<Out: io::Write> Basic<Out> {
.unwrap_or_default(),
indent = " ".repeat(self.indent),
);
self.lines_to_clear += output.lines().count();
self.write_line(&output)?;
self.lines_to_clear += self.styles.lines_count(&out);
self.write_line(&out)?;
}
Ok(())
}
Expand Down Expand Up @@ -804,7 +805,7 @@ impl<Out: io::Write> Basic<Out> {
) -> io::Result<()> {
self.indent += 4;
if self.styles.is_present {
let output = format!(
let out = format!(
"{indent}> {}{}{}{}",
step.keyword,
step.value,
Expand All @@ -825,8 +826,8 @@ impl<Out: io::Write> Basic<Out> {
.unwrap_or_default(),
indent = " ".repeat(self.indent.saturating_sub(2)),
);
self.lines_to_clear += output.lines().count();
self.write_line(&output)?;
self.lines_to_clear += self.styles.lines_count(&out);
self.write_line(&out)?;
}
Ok(())
}
Expand Down
33 changes: 33 additions & 0 deletions src/writer/out.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ pub struct Styles {
/// [`Style`] for rendering __bold__.
pub bold: Style,

/// [`Term`] width.
///
/// [`Term`]: console::Term
pub term_width: Option<u16>,

/// Indicates whether the terminal was detected.
pub is_present: bool,
}
Expand All @@ -54,6 +59,7 @@ impl Default for Styles {
retry: Style::new().magenta(),
header: Style::new().blue(),
bold: Style::new().bold(),
term_width: console::Term::stdout().size_checked().map(|(w, _h)| w),
is_present: io::stdout().is_terminal() && console::colors_enabled(),
}
}
Expand Down Expand Up @@ -94,6 +100,7 @@ impl Styles {
retry: self.retry.clone().bright(),
header: self.header.clone().bright(),
bold: self.bold.clone().bright(),
term_width: self.term_width,
is_present: self.is_present,
}
}
Expand Down Expand Up @@ -163,6 +170,32 @@ impl Styles {
input.into()
}
}

/// Returns number of lines for the provided `s`tring, considering wrapping
/// because of the [`Term`] width.
///
/// [`Term`]: console::Term
#[must_use]
pub fn lines_count(&self, s: impl AsRef<str>) -> usize {
// TODO: Remove, once `int_roundings` feature is stabilized:
// https://github.com/rust-lang/rust/issues/88581
let div_ceil = |l, r| {
let d = l / r;
let rem = l % r;
if rem > 0 && r > 0 {
d + 1
} else {
d
}
};
s.as_ref()
.lines()
.map(|l| {
self.term_width
.map_or(1, |w| div_ceil(l.len(), usize::from(w)))
})
.sum()
}
}

/// [`io::Write`] extension for easier manipulation with strings and special
Expand Down