Skip to content

Commit

Permalink
Always return a correct terminal width > 0 (#295)
Browse files Browse the repository at this point in the history
  • Loading branch information
krucod3 committed Jun 13, 2024
1 parent a4719ff commit 25462ff
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions ank/src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,27 @@ pub(crate) fn output_fn(args: fmt::Arguments<'_>) {
}
}

fn terminal_width() -> usize {
let terminal_width = terminal::size().unwrap_or((80, 0)).0 as usize;

// This is a workaround for terminals that return a wrong width of 0 instead of None
if 0 == terminal_width {
return 80;
}
terminal_width
}

pub(crate) fn output_update_fn(args: fmt::Arguments<'_>) {
if !is_quiet() {
let args = args.to_string();

let terminal_width = terminal::size().unwrap_or((80, 0)).0 as usize;
// limit line length to terminal_width by introducing newline characters
let args = args
.split('\n')
.flat_map(|line| {
line.chars()
.collect::<Vec<_>>()
.chunks(terminal_width)
.chunks(terminal_width())
.map(|x| x.iter().collect::<String>())
.collect::<Vec<_>>()
})
Expand Down

0 comments on commit 25462ff

Please sign in to comment.