Skip to content

Commit e25d7eb

Browse files
committed
refactor
- fix subtract with overflow - extract line wrapping into function
1 parent 454e6b9 commit e25d7eb

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

src/plumbing/progress.rs

+26-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::fmt::{Display, Formatter};
2+
use std::io::StdoutLock;
23

34
#[derive(Clone)]
45
enum Usage {
@@ -519,26 +520,7 @@ pub fn show_progress() -> anyhow::Result<()> {
519520
write!(stdout, "{icon} {config: <50}: ", icon = usage.icon())?;
520521

521522
if let Some(width) = width {
522-
let icon_and_config_width = 55;
523-
let width_after_config = width - icon_and_config_width;
524-
let usage = usage.to_string();
525-
let mut idx = 0;
526-
for word in usage.split(' ') {
527-
// +1 for the space after each word
528-
let word_len = word.chars().count() + 1;
529-
530-
if idx + word_len > width_after_config {
531-
writeln!(stdout)?;
532-
for _ in 0..icon_and_config_width {
533-
write!(stdout, " ")?;
534-
}
535-
idx = 0;
536-
}
537-
538-
write!(stdout, "{word} ")?;
539-
idx += word_len;
540-
}
541-
writeln!(stdout)?;
523+
write_with_linewrap(&mut stdout, &usage.to_string(), width)?;
542524
} else {
543525
writeln!(stdout, "{usage}")?;
544526
}
@@ -554,3 +536,27 @@ pub fn show_progress() -> anyhow::Result<()> {
554536
);
555537
Ok(())
556538
}
539+
540+
fn write_with_linewrap(stdout: &mut StdoutLock<'_>, text: &str, width: usize) -> Result<(), std::io::Error> {
541+
use std::io::Write;
542+
let icon_and_config_width = 55;
543+
let width_after_config = width.saturating_sub(icon_and_config_width);
544+
let mut idx = 0;
545+
for word in text.split(' ') {
546+
// +1 for the space after each word
547+
let word_len = word.chars().count() + 1;
548+
549+
if idx + word_len > width_after_config {
550+
writeln!(stdout)?;
551+
for _ in 0..icon_and_config_width {
552+
write!(stdout, " ")?;
553+
}
554+
idx = 0;
555+
}
556+
557+
write!(stdout, "{word} ")?;
558+
idx += word_len;
559+
}
560+
writeln!(stdout)?;
561+
Ok(())
562+
}

0 commit comments

Comments
 (0)