on master help sign in
Add check for terminal-width in output_wrap config value in not interactive case

Co-authored-by: allevo <tomallevi@gmail.com> Co-authored-by: gildo <gildo.fiorito@gmail.com>

commit6311ca22f9a6b413e5c3146c26966cdb321e493a authorfusillicode <fusillicode@gmail.com> dateSat 21 Dec 2019, 10:50 (7 years ago) parent41d5475
1 file changed+24−26
src/bin/bat/app.rs+24−26
src/bin/bat/app.rs+24−26
@@ -119,6 +119,24 @@ impl App {
119119 }
120120 }
121121
122+ let maybe_term_width = self.matches.value_of("terminal-width").and_then(|w| {
123+ if w.starts_with('+') || w.starts_with('-') {
124+ // Treat argument as a delta to the current terminal width
125+ w.parse().ok().map(|delta: i16| {
126+ let old_width: u16 = Term::stdout().size().1;
127+ let new_width: i32 = i32::from(old_width) + i32::from(delta);
128+
129+ if new_width <= 0 {
130+ old_width as usize
131+ } else {
132+ new_width as usize
133+ }
134+ })
135+ } else {
136+ w.parse().ok()
137+ }
138+ });
139+
122140 Ok(Config {
123141 true_color: is_truecolor_terminal(),
124142 language: self.matches.value_of("language").or_else(|| {
@@ -129,11 +147,7 @@ impl App {
129147 }
130148 }),
131149 show_nonprintable: self.matches.is_present("show-all"),
132− output_wrap: if !self.interactive_output {
133− // We don't have the tty width when piping to another program.
134− // There's no point in wrapping when this is the case.
135− OutputWrap::None
136− } else {
150+ output_wrap: if self.interactive_output || maybe_term_width.is_some() {
137151 match self.matches.value_of("wrap") {
138152 Some("character") => OutputWrap::Character,
139153 Some("never") => OutputWrap::None,
@@ -145,6 +159,10 @@ impl App {
145159 }
146160 }
147161 }
162+ } else {
163+ // We don't have the tty width when piping to another program.
164+ // There's no point in wrapping when this is the case.
165+ OutputWrap::None
148166 },
149167 colored_output: match self.matches.value_of("color") {
150168 Some("always") => true,
@@ -152,27 +170,7 @@ impl App {
152170 Some("auto") | _ => self.interactive_output,
153171 },
154172 paging_mode,
155− term_width: self
156− .matches
157− .value_of("terminal-width")
158− .and_then(|w| {
159− if w.starts_with('+') || w.starts_with('-') {
160− // Treat argument as a delta to the current terminal width
161− w.parse().ok().map(|delta: i16| {
162− let old_width: u16 = Term::stdout().size().1;
163− let new_width: i32 = i32::from(old_width) + i32::from(delta);
164−
165− if new_width <= 0 {
166− old_width as usize
167− } else {
168− new_width as usize
169− }
170− })
171− } else {
172− w.parse().ok()
173− }
174− })
175− .unwrap_or(Term::stdout().size().1 as usize),
173+ term_width: maybe_term_width.unwrap_or(Term::stdout().size().1 as usize),
176174 loop_through: !(self.interactive_output
177175 || self.matches.value_of("color") == Some("always")
178176 || self.matches.value_of("decorations") == Some("always")),