Skip to content

Commit

Permalink
Introduce LineType
Browse files Browse the repository at this point in the history
  • Loading branch information
spoutn1k authored and djc committed Nov 15, 2024
1 parent b69f608 commit ec38ca8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 21 deletions.
41 changes: 33 additions & 8 deletions src/draw_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ pub(crate) enum LineAdjust {

pub(crate) struct DrawStateWrapper<'a> {
state: &'a mut DrawState,
orphan_lines: Option<&'a mut Vec<String>>,
orphan_lines: Option<&'a mut Vec<LineType>>,
}

impl<'a> DrawStateWrapper<'a> {
Expand All @@ -373,7 +373,7 @@ impl<'a> DrawStateWrapper<'a> {
}
}

pub(crate) fn for_multi(state: &'a mut DrawState, orphan_lines: &'a mut Vec<String>) -> Self {
pub(crate) fn for_multi(state: &'a mut DrawState, orphan_lines: &'a mut Vec<LineType>) -> Self {
Self {
state,
orphan_lines: Some(orphan_lines),
Expand Down Expand Up @@ -460,7 +460,7 @@ const MAX_BURST: u8 = 20;
#[derive(Clone, Debug, Default)]
pub(crate) struct DrawState {
/// The lines to print (can contain ANSI codes)
pub(crate) lines: Vec<String>,
pub(crate) lines: Vec<LineType>,
/// The number [`Self::lines`] entries that shouldn't be reaped by the next tick.
///
/// Note that this number may be different than the number of visual lines required to draw [`Self::lines`].
Expand Down Expand Up @@ -525,12 +525,13 @@ impl DrawState {
};

// Accumulate the displayed height in here. This differs from `full_height` in that it will
// not reflect the displayed content if the terminal height is exceeded.
// accurately reflect the number of lines that have been displayed on the terminal, if the
// full height exceeds the terminal height.
let mut real_height = VisualLines::default();

for (idx, line) in self.lines.iter().enumerate() {
let line_width = console::measure_text_width(line);
let diff = if line.is_empty() {
let line_width = console::measure_text_width(line.as_ref());
let diff = if line.as_ref().is_empty() {
// Empty line are new line
1
} else {
Expand Down Expand Up @@ -561,7 +562,7 @@ impl DrawState {
term.write_line("")?;
}

term.write_str(line)?;
term.write_str(line.as_ref())?;

if idx + 1 == self.lines.len() {
// Keep the cursor on the right terminal side
Expand All @@ -584,7 +585,7 @@ impl DrawState {

pub(crate) fn visual_line_count(
&self,
range: impl SliceIndex<[String], Output = [String]>,
range: impl SliceIndex<[LineType], Output = [LineType]>,
width: usize,
) -> VisualLines {
visual_line_count(&self.lines[range], width)
Expand Down Expand Up @@ -651,6 +652,30 @@ pub(crate) fn visual_line_count(lines: &[impl AsRef<str>], width: usize) -> Visu
real_lines.into()
}

#[derive(Clone, Debug)]
pub(crate) enum LineType {
Text(String),
Bar(String),
Empty,
}

impl LineType {}

impl AsRef<str> for LineType {
fn as_ref(&self) -> &str {
match self {
LineType::Text(s) | LineType::Bar(s) => s,
LineType::Empty => "",
}
}
}

impl PartialEq<str> for LineType {
fn eq(&self, other: &str) -> bool {
self.as_ref() == other
}
}

#[cfg(test)]
mod tests {
use crate::{MultiProgress, ProgressBar, ProgressDrawTarget};
Expand Down
13 changes: 7 additions & 6 deletions src/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use std::thread::panicking;
use std::time::Instant;

use crate::draw_target::{
visual_line_count, DrawState, DrawStateWrapper, LineAdjust, ProgressDrawTarget, VisualLines,
visual_line_count, DrawState, DrawStateWrapper, LineAdjust, LineType, ProgressDrawTarget,
VisualLines,
};
use crate::progress_bar::ProgressBar;
#[cfg(target_arch = "wasm32")]
Expand Down Expand Up @@ -217,7 +218,7 @@ pub(crate) struct MultiState {
alignment: MultiProgressAlignment,
/// Lines to be drawn above everything else in the MultiProgress. These specifically come from
/// calling `ProgressBar::println` on a pb that is connected to a `MultiProgress`.
orphan_lines: Vec<String>,
orphan_lines: Vec<LineType>,
/// The count of currently visible zombie lines.
zombie_lines_count: VisualLines,
}
Expand Down Expand Up @@ -267,7 +268,7 @@ impl MultiState {
pub(crate) fn draw(
&mut self,
mut force_draw: bool,
extra_lines: Option<Vec<String>>,
extra_lines: Option<Vec<LineType>>,
now: Instant,
) -> io::Result<()> {
if panicking() {
Expand Down Expand Up @@ -365,9 +366,9 @@ impl MultiState {
let msg = msg.as_ref();

// If msg is "", make sure a line is still printed
let lines: Vec<String> = match msg.is_empty() {
false => msg.lines().map(Into::into).collect(),
true => vec![String::new()],
let lines: Vec<LineType> = match msg.is_empty() {
false => msg.lines().map(|l| LineType::Text(Into::into(l))).collect(),
true => vec![LineType::Empty],
};

self.draw(true, Some(lines), now)
Expand Down
6 changes: 3 additions & 3 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use portable_atomic::{AtomicU64, AtomicU8, Ordering};
#[cfg(target_arch = "wasm32")]
use web_time::Instant;

use crate::draw_target::ProgressDrawTarget;
use crate::draw_target::{LineType, ProgressDrawTarget};
use crate::style::ProgressStyle;

pub(crate) struct BarState {
Expand Down Expand Up @@ -149,10 +149,10 @@ impl BarState {
};

let mut draw_state = drawable.state();
let lines: Vec<String> = msg.lines().map(Into::into).collect();
let lines: Vec<LineType> = msg.lines().map(|l| LineType::Text(Into::into(l))).collect();
// Empty msg should trigger newline as we are in println
if lines.is_empty() {
draw_state.lines.push(String::new());
draw_state.lines.push(LineType::Empty);
} else {
draw_state.lines.extend(lines);
}
Expand Down
10 changes: 6 additions & 4 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use unicode_segmentation::UnicodeSegmentation;
#[cfg(target_arch = "wasm32")]
use web_time::Instant;

use crate::draw_target::LineType;
use crate::format::{
BinaryBytes, DecimalBytes, FormattedDuration, HumanBytes, HumanCount, HumanDuration,
HumanFloatCount,
Expand Down Expand Up @@ -226,7 +227,7 @@ impl ProgressStyle {
pub(crate) fn format_state(
&self,
state: &ProgressState,
lines: &mut Vec<String>,
lines: &mut Vec<LineType>,
target_width: u16,
) {
let mut cur = String::new();
Expand Down Expand Up @@ -374,9 +375,10 @@ impl ProgressStyle {
}
}

/// This is used exclusively to add the bars built above to the lines to print
fn push_line(
&self,
lines: &mut Vec<String>,
lines: &mut Vec<LineType>,
cur: &mut String,
state: &ProgressState,
buf: &mut String,
Expand All @@ -394,11 +396,11 @@ impl ProgressStyle {
for (i, line) in expanded.split('\n').enumerate() {
// No newlines found in this case
if i == 0 && line.len() == expanded.len() {
lines.push(expanded);
lines.push(LineType::Bar(expanded));
break;
}

lines.push(line.to_string());
lines.push(LineType::Bar(line.to_string()));
}
}
}
Expand Down

0 comments on commit ec38ca8

Please sign in to comment.