2
0
mirror of https://github.com/sharkdp/bat synced 2024-11-16 21:25:56 +00:00

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.
This commit is contained in:
Connor Kuehl 2018-05-08 12:20:45 -07:00 committed by David Peter
parent e5e47716b0
commit 7dc7133b3c
2 changed files with 5 additions and 6 deletions

View File

@ -582,14 +582,15 @@ fn run() -> Result<()> {
print!("{:width$}{}", lang.name, separator, width = longest); print!("{:width$}{}", lang.name, separator, width = longest);
// Line-wrapping for the possible file extension overflow. // 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` // Number of characters on this line so far, wrap before `desired_width`
let mut num_chars = 0; let mut num_chars = 0;
let comma_separator = ", ";
let mut extension = lang.file_extensions.iter().peekable(); let mut extension = lang.file_extensions.iter().peekable();
while let Some(word) = extension.next() { while let Some(word) = extension.next() {
// If we can't fit this word in, then create a line break and align it in. // 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; num_chars = 0;
print!("\n{:width$}{}", "", separator, width = longest); print!("\n{:width$}{}", "", separator, width = longest);
} }
@ -597,7 +598,7 @@ fn run() -> Result<()> {
num_chars += word.len(); num_chars += word.len();
print!("{}", word); print!("{}", word);
if extension.peek().is_some() { if extension.peek().is_some() {
print!(", "); print!("{}", comma_separator);
} }
} }
println!(); println!();

View File

@ -17,8 +17,6 @@ pub struct Printer<'a> {
impl<'a> Printer<'a> { impl<'a> Printer<'a> {
pub fn new(handle: &'a mut Write, options: &'a Options) -> Self { pub fn new(handle: &'a mut Write, options: &'a Options) -> Self {
let term_width = options.term_width;
let colors = if options.colored_output { let colors = if options.colored_output {
Colors::colored() Colors::colored()
} else { } else {
@ -28,7 +26,7 @@ impl<'a> Printer<'a> {
Printer { Printer {
handle, handle,
colors, colors,
term_width, term_width: options.term_width,
options, options,
line_changes: None, line_changes: None,
} }