From 7dc7133b3c82ab7034b16020a028f8b3bdab697e Mon Sep 17 00:00:00 2001 From: Connor Kuehl Date: Tue, 8 May 2018 12:20:45 -0700 Subject: [PATCH] Add revisions for line-wrapping and terminal width. * Adds separator.length() to calculation for desired width. * Replaces use of term_width with options.term_width. * Adds the comma and space separator to calculation for line-wrapping. --- src/main.rs | 7 ++++--- src/printer.rs | 4 +--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4f457068..24f9cabc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -582,14 +582,15 @@ fn run() -> Result<()> { print!("{:width$}{}", lang.name, separator, width = longest); // Line-wrapping for the possible file extension overflow. - let desired_width = options.term_width - longest; + let desired_width = options.term_width - longest - separator.len(); // Number of characters on this line so far, wrap before `desired_width` let mut num_chars = 0; + let comma_separator = ", "; let mut extension = lang.file_extensions.iter().peekable(); while let Some(word) = extension.next() { // If we can't fit this word in, then create a line break and align it in. - if word.len() + num_chars >= desired_width { + if word.len() + num_chars + comma_separator.len() >= desired_width { num_chars = 0; print!("\n{:width$}{}", "", separator, width = longest); } @@ -597,7 +598,7 @@ fn run() -> Result<()> { num_chars += word.len(); print!("{}", word); if extension.peek().is_some() { - print!(", "); + print!("{}", comma_separator); } } println!(); diff --git a/src/printer.rs b/src/printer.rs index 6f112ccc..b5cefa66 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -17,8 +17,6 @@ pub struct Printer<'a> { impl<'a> Printer<'a> { pub fn new(handle: &'a mut Write, options: &'a Options) -> Self { - let term_width = options.term_width; - let colors = if options.colored_output { Colors::colored() } else { @@ -28,7 +26,7 @@ impl<'a> Printer<'a> { Printer { handle, colors, - term_width, + term_width: options.term_width, options, line_changes: None, }