Skip to content
Draft
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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ readme = "README.md"
exclude = ["screenshots/*"]

[dependencies]
console = { version = "0.15", default-features = false, features = ["ansi-parsing"] }
# console = { version = "0.16", default-features = false, features = ["ansi-parsing"] }
console = { git = "https://github.com/remi-dupre/console.git", branch = "ansi-slice", default-features = false, features = ["ansi-parsing"] }
futures-core = { version = "0.3", default-features = false, optional = true }
number_prefix = "0.4"
portable-atomic = "1.0.0"
Expand Down
39 changes: 25 additions & 14 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::mem;
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;

use console::{measure_text_width, Style};
use console::{measure_text_width, slice_str, Style};
#[cfg(target_arch = "wasm32")]
use instant::Instant;
#[cfg(feature = "unicode-segmentation")]
Expand Down Expand Up @@ -694,21 +694,26 @@ struct PaddedStringDisplay<'a> {
impl<'a> fmt::Display for PaddedStringDisplay<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let cols = measure_text_width(self.str);
let excess = cols.saturating_sub(self.width);
if excess > 0 && !self.truncate {
return f.write_str(self.str);
} else if excess > 0 {
let (start, end) = match self.align {
Alignment::Left => (0, self.str.len() - excess),
Alignment::Right => (excess, self.str.len()),
Alignment::Center => (
excess / 2,
self.str.len() - excess.saturating_sub(excess / 2),
),
let text_width = measure_text_width(self.str);
let excess = text_width.saturating_sub(self.width);

if excess > 0 {
let truncated = {
if self.truncate {
match self.align {
Alignment::Left => slice_str(self.str, "", 0..self.width, ""),
Alignment::Right => slice_str(self.str, "", excess..text_width, ""),
Alignment::Center => {
slice_str(self.str, "", excess / 2..text_width - (excess + 1) / 2, "")
}
}
} else {
self.str.into()
}
};

return f.write_str(self.str.get(start..end).unwrap_or(self.str));
}
return f.write_str(&truncated);
};

let diff = self.width.saturating_sub(cols);
let (left_pad, right_pad) = match self.align {
Expand Down Expand Up @@ -918,6 +923,12 @@ mod tests {
state.message = TabExpandedString::NoTabs("abcdefghijklmnopqrst".into());
style.format_state(&state, &mut buf, WIDTH);
assert_eq!(&buf[0], "fghijklmno");

buf.clear();
let style = ProgressStyle::with_template("{wide_msg}").unwrap();
state.message = TabExpandedString::NoTabs("\x1b[31mabcdefghijklmnopqrst\x1b[0m".into());
style.format_state(&state, &mut buf, WIDTH);
assert_eq!(&buf[0], "\x1b[31mabcdefghij\u{1b}[0m");
}

#[test]
Expand Down