From d4b438b9d3211b6382f130309bdd54589908e7bd Mon Sep 17 00:00:00 2001 From: eth-p <32112321+eth-p@users.noreply.github.com> Date: Sat, 12 May 2018 13:44:10 -0700 Subject: [PATCH] Fix padding, add --wrap argument, disable wrap for non-tty. (Fixed) I'm not quite sure what was up with git on that last commit, but it's all properly committed now. --- src/app.rs | 19 ++++++++++++++++++- src/printer.rs | 27 ++++++++++++++++++++++++--- src/style.rs | 1 + 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/app.rs b/src/app.rs index 85dc542b..803cc0cd 100644 --- a/src/app.rs +++ b/src/app.rs @@ -80,6 +80,14 @@ impl App { .default_value("auto") .help("When to use the pager"), ) + .arg( + Arg::with_name("wrap") + .long("wrap") + .takes_value(true) + .possible_values(&["character", "never"]) + .default_value("character") + .help("When to wrap text"), + ) .arg( Arg::with_name("list-languages") .long("list-languages") @@ -135,7 +143,16 @@ impl App { true_color: is_truecolor_terminal(), output_components: self.output_components()?, language: self.matches.value_of("language"), - output_wrap: OutputWrap::Character, + output_wrap: if ! self.interactive_output { + // We don't have the tty width when piping to another program. + // There's no point in wrapping when this is the case. + OutputWrap::None + } else { + match self.matches.value_of("wrap") { + Some("character") => OutputWrap::Character, + Some("never") | _ => OutputWrap::None, + } + }, colored_output: match self.matches.value_of("color") { Some("always") => true, Some("never") => false, diff --git a/src/printer.rs b/src/printer.rs index 87975100..6867da96 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -94,7 +94,7 @@ impl<'a> Printer<'a> { regions: &[(highlighting::Style, &str)], ) -> Result<()> { let mut cursor: usize = 0; - let mut cursor_max: usize = self.config.term_width - 2; + let mut cursor_max: usize = self.config.term_width; // Line decoration. let decorations = self.gen_decorations(line_number); @@ -116,6 +116,27 @@ impl<'a> Printer<'a> { // Grid border. let border = if gutter_width > 0 && self.config.output_components.grid() { self.gen_border() + } else { + PrintSegment { + size: 0, + text: "".to_owned(), + } + }; + + cursor_max -= border.size; + write!(self.handle, "{}", border.text)?; + + // Line contents. + if self.config.output_wrap == OutputWrap::None { + let true_color = self.config.true_color; + let colored_output = self.config.colored_output; + + write!(self.handle, "{}", + regions.iter() + .map(|&(style, text)| as_terminal_escaped(style, text, true_color, colored_output)) + .collect::>() + .join(" ") + )?; } else { for &(style, text) in regions.iter() { let mut chars = text.chars().filter(|c| *c != '\n'); @@ -136,7 +157,7 @@ impl<'a> Printer<'a> { style, &*text, self.config.true_color, - self.config.colored_output + self.config.colored_output, ) )?; break; @@ -155,7 +176,7 @@ impl<'a> Printer<'a> { style, &*text, self.config.true_color, - self.config.colored_output + self.config.colored_output, ), " ".repeat(gutter_width), border.text.to_owned() diff --git a/src/style.rs b/src/style.rs index 491d0fec..c788f661 100644 --- a/src/style.rs +++ b/src/style.rs @@ -16,6 +16,7 @@ pub enum OutputComponent { #[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)] pub enum OutputWrap { Character, + None } impl OutputComponent {