on master help sign in
Merge pull request #302 from eth-p/feature-tabs

Tab expansion support. closes #166 closes #184

commit52d0d6ca30085192bc0dd523a0ee4c583fa3f457 authorDavid Peter <sharkdp@users.noreply.github.com> dateWed 12 Sep 2018, 20:50 (8 years ago) parent4e3ab4da05494e
32 files changed+850−236
src/app.rs+218−185 src/main.rs+1 src/preprocessor.rs+34 src/printer.rs+24−15 tests/snapshots/generate_snapshots.py+27−6 tests/snapshots/output/changes.snapshot.txt+15−1 tests/snapshots/output/changes,grid.snapshot.txt → tests/snapshots/output/changes_grid.snapshot.txt+15−1 tests/snapshots/output/changes,grid,header.snapshot.txt → tests/snapshots/output/changes_grid_header.snapshot.txt+15−1 tests/snapshots/output/changes_grid_header_numbers.snapshot.txt+40 tests/snapshots/output/changes,grid,numbers.snapshot.txt → tests/snapshots/output/changes_grid_numbers.snapshot.txt+15−1 tests/snapshots/output/changes,header.snapshot.txt → tests/snapshots/output/changes_header.snapshot.txt+15−1 tests/snapshots/output/changes,header,numbers.snapshot.txt → tests/snapshots/output/changes_header_numbers.snapshot.txt+15−1 tests/snapshots/output/changes,numbers.snapshot.txt → tests/snapshots/output/changes_numbers.snapshot.txt+15−1 tests/snapshots/output/full.snapshot.txt+15−1 tests/snapshots/output/grid.snapshot.txt+14 tests/snapshots/output/grid,header.snapshot.txt → tests/snapshots/output/grid_header.snapshot.txt+14 tests/snapshots/output/grid,header,numbers.snapshot.txt → tests/snapshots/output/grid_header_numbers.snapshot.txt+14 tests/snapshots/output/grid,numbers.snapshot.txt → tests/snapshots/output/grid_numbers.snapshot.txt+14 tests/snapshots/output/header.snapshot.txt+14 tests/snapshots/output/header,numbers.snapshot.txt → tests/snapshots/output/header_numbers.snapshot.txt+14 tests/snapshots/output/numbers.snapshot.txt+14 tests/snapshots/output/plain.snapshot.txt+14 tests/snapshots/output/tabs_4.snapshot.txt+40 tests/snapshots/output/tabs_4_wrapped.snapshot.txt+40 tests/snapshots/output/tabs_8.snapshot.txt+40 tests/snapshots/output/tabs_8_wrapped.snapshot.txt+40 tests/snapshots/output/changes,grid,header,numbers.snapshot.txt → tests/snapshots/output/tabs_passthrough.snapshot.txt+15−1 tests/snapshots/output/tabs_passthrough_wrapped.snapshot.txt+40 tests/snapshots/sample.modified.rs+14 tests/snapshots/sample.rs+14 tests/tester.rs+6−2 tests/tests.rs+25−19
src/app.rs+218−185
@@ -16,7 +16,7 @@ use errors::*;
1616use line_range::LineRange;
1717use style::{OutputComponent, OutputComponents, OutputWrap};
1818
19−#[derive(Debug, Clone, Copy)]
19+#[derive(Debug, Clone, Copy, PartialEq)]
2020pub enum PagingMode {
2121 Always,
2222 QuitIfOneScreen,
@@ -41,6 +41,10 @@ pub struct Config<'a> {
4141 /// The character width of the terminal
4242 pub term_width: usize,
4343
44+ /// The width of tab characters.
45+ /// Currently, a value of 0 will cause tabs to be passed through without expanding them.
46+ pub tab_width: usize,
47+
4448 /// Whether or not to simply loop through all input (`cat` mode)
4549 pub loop_through: bool,
4650
@@ -130,7 +134,7 @@ impl App {
130134 to read from standard input.",
131135 ).multiple(true)
132136 .empty_values(false),
133− )
137+ )
134138 .arg(
135139 Arg::with_name("language")
136140 .short("l")
@@ -144,195 +148,209 @@ impl App {
144148 language names and file extensions."
145149 ).takes_value(true),
146150 ).arg(
147− Arg::with_name("list-languages")
148− .long("list-languages")
149− .conflicts_with("list-themes")
150− .help("Display all supported languages.")
151− .long_help("Display a list of supported languages for syntax highlighting."),
152− ).arg(
153− Arg::with_name("theme")
154− .long("theme")
155− .overrides_with("theme")
156− .takes_value(true)
157− .help("Set the color theme for syntax highlighting.")
158− .long_help(
159− "Set the theme for syntax highlighting. Use '--list-themes' to \
151+ Arg::with_name("list-languages")
152+ .long("list-languages")
153+ .conflicts_with("list-themes")
154+ .help("Display all supported languages.")
155+ .long_help("Display a list of supported languages for syntax highlighting."),
156+ ).arg(
157+ Arg::with_name("theme")
158+ .long("theme")
159+ .overrides_with("theme")
160+ .takes_value(true)
161+ .help("Set the color theme for syntax highlighting.")
162+ .long_help(
163+ "Set the theme for syntax highlighting. Use '--list-themes' to \
160164 see all available themes. To set a default theme, export the \
161165 BAT_THEME environment variable (e.g.: export \
162166 BAT_THEME=\"TwoDark\").",
163− ),
164− ).arg(
165− Arg::with_name("list-themes")
166− .long("list-themes")
167− .help("Display all supported highlighting themes.")
168− .long_help("Display a list of supported themes for syntax highlighting."),
169− ).arg(
170− Arg::with_name("style")
171− .long("style")
172− .value_name("style-components")
173− .use_delimiter(true)
174− .takes_value(true)
175− .possible_values(&[
176− "auto", "full", "plain", "changes", "header", "grid", "numbers",
177− ]).default_value("auto")
178− .help("Comma-separated list of style elements to display.")
179− .long_help(
180− "Configure which elements (line numbers, file headers, grid \
167+ ),
168+ ).arg(
169+ Arg::with_name("list-themes")
170+ .long("list-themes")
171+ .help("Display all supported highlighting themes.")
172+ .long_help("Display a list of supported themes for syntax highlighting."),
173+ ).arg(
174+ Arg::with_name("style")
175+ .long("style")
176+ .value_name("style-components")
177+ .use_delimiter(true)
178+ .takes_value(true)
179+ .possible_values(&[
180+ "auto", "full", "plain", "changes", "header", "grid", "numbers",
181+ ]).default_value("auto")
182+ .help("Comma-separated list of style elements to display.")
183+ .long_help(
184+ "Configure which elements (line numbers, file headers, grid \
181185 borders, Git modifications, ..) to display in addition to the \
182186 file contents. The argument is a comma-separated list of \
183187 components to display (e.g. 'numbers,changes,grid') or a \
184188 pre-defined style ('full')",
185− ),
186− ).arg(
187− Arg::with_name("plain")
188− .overrides_with("plain")
189− .short("p")
190− .long("plain")
191− .conflicts_with("style")
192− .conflicts_with("number")
193− .help("Show plain style (alias for '--style=plain').")
194− .long_help(
195− "Only show plain style, no decorations. This is an alias for \
189+ ),
190+ ).arg(
191+ Arg::with_name("plain")
192+ .overrides_with("plain")
193+ .short("p")
194+ .long("plain")
195+ .conflicts_with("style")
196+ .conflicts_with("number")
197+ .help("Show plain style (alias for '--style=plain').")
198+ .long_help(
199+ "Only show plain style, no decorations. This is an alias for \
196200 '--style=plain'",
197− ),
198− ).arg(
199− Arg::with_name("number")
200− .long("number")
201− .overrides_with("number")
202− .short("n")
203− .conflicts_with("style")
204− .help("Show line numbers (alias for '--style=numbers').")
205− .long_help(
206− "Only show line numbers, no other decorations. This is an alias for \
201+ ),
202+ ).arg(
203+ Arg::with_name("number")
204+ .long("number")
205+ .overrides_with("number")
206+ .short("n")
207+ .conflicts_with("style")
208+ .help("Show line numbers (alias for '--style=numbers').")
209+ .long_help(
210+ "Only show line numbers, no other decorations. This is an alias for \
207211 '--style=numbers'",
208− ),
209− ).arg(
210− Arg::with_name("line-range")
211− .long("line-range")
212− .overrides_with("line-range")
213− .takes_value(true)
214− .value_name("N:M")
215− .help("Only print the lines from N to M.")
216− .long_help(
217− "Only print the specified range of lines for each file. \
212+ ),
213+ ).arg(
214+ Arg::with_name("line-range")
215+ .long("line-range")
216+ .overrides_with("line-range")
217+ .takes_value(true)
218+ .value_name("N:M")
219+ .help("Only print the lines from N to M.")
220+ .long_help(
221+ "Only print the specified range of lines for each file. \
218222 For example:\n \
219223 '--line-range 30:40' prints lines 30 to 40\n \
220224 '--line-range :40' prints lines 1 to 40\n \
221225 '--line-range 40:' prints lines 40 to the end of the file",
222− ),
223− ).arg(
224− Arg::with_name("color")
225− .long("color")
226− .overrides_with("color")
227− .takes_value(true)
228− .value_name("when")
229− .possible_values(&["auto", "never", "always"])
230− .default_value("auto")
231− .help("When to use colors.")
232− .long_help("Specify when to use colored output. The automatic mode \
226+ ),
227+ ).arg(
228+ Arg::with_name("color")
229+ .long("color")
230+ .overrides_with("color")
231+ .takes_value(true)
232+ .value_name("when")
233+ .possible_values(&["auto", "never", "always"])
234+ .default_value("auto")
235+ .help("When to use colors.")
236+ .long_help("Specify when to use colored output. The automatic mode \
233237 only enables colors if an interactive terminal is detected."),
234− ).arg(
235− Arg::with_name("decorations")
236− .long("decorations")
237− .overrides_with("decorations")
238− .takes_value(true)
239− .value_name("when")
240− .possible_values(&["auto", "never", "always"])
241− .default_value("auto")
242− .help("When to show the decorations specified by '--style'.")
243− .long_help("Specify when to use the decorations that have been specified \
238+ ).arg(
239+ Arg::with_name("decorations")
240+ .long("decorations")
241+ .overrides_with("decorations")
242+ .takes_value(true)
243+ .value_name("when")
244+ .possible_values(&["auto", "never", "always"])
245+ .default_value("auto")
246+ .help("When to show the decorations specified by '--style'.")
247+ .long_help("Specify when to use the decorations that have been specified \
244248 via '--style'. The automatic mode only enables decorations if \
245249 an interactive terminal is detected."),
246− ).arg(
247− Arg::with_name("paging")
248− .long("paging")
249− .overrides_with("paging")
250− .takes_value(true)
251− .value_name("when")
252− .possible_values(&["auto", "never", "always"])
253− .default_value("auto")
254− .help("Specify when to use the pager.")
255− .long_help("Specify when to use the pager. To control which pager \
250+ ).arg(
251+ Arg::with_name("paging")
252+ .long("paging")
253+ .overrides_with("paging")
254+ .takes_value(true)
255+ .value_name("when")
256+ .possible_values(&["auto", "never", "always"])
257+ .default_value("auto")
258+ .help("Specify when to use the pager.")
259+ .long_help("Specify when to use the pager. To control which pager \
256260 is used, set the PAGER or BAT_PAGER environment \
257261 variables (the latter takes precedence). The default \
258262 pager is 'less'. To disable the pager permanently, set \
259263 BAT_PAGER to an empty string."),
264+ ).arg(
265+ Arg::with_name("wrap")
266+ .long("wrap")
267+ .overrides_with("wrap")
268+ .takes_value(true)
269+ .value_name("mode")
270+ .possible_values(&["auto", "never", "character"])
271+ .default_value("auto")
272+ .help("Specify the text-wrapping mode.")
273+ .long_help("Specify the text-wrapping mode."),
274+ ).arg(
275+ Arg::with_name("unbuffered")
276+ .short("u")
277+ .hidden_short_help(true)
278+ .long_help(
279+ "This option exists for POSIX-compliance reasons ('u' is for \
280+ 'unbuffered'). The output is always unbuffered - this option \
281+ is simply ignored.",
282+ ),
283+ ).arg(
284+ Arg::with_name("tabs")
285+ .long("tabs")
286+ .takes_value(true)
287+ .value_name("tabs")
288+ .validator(
289+ |t| t.parse::<u32>()
290+ .map_err(|_t| "must be a number")
291+ .map(|_t| ()) // Convert to Result<(), &str>
292+ .map_err(|e| e.to_string()) // Convert to Result<(), String>
293+ )
294+ .help("Sets the tab width.")
295+ .long_help("Sets the tab width. Use a width of 0 to pass tabs through \
296+ directly"),
297+ ).arg(
298+ Arg::with_name("terminal-width")
299+ .long("terminal-width")
300+ .takes_value(true)
301+ .value_name("width")
302+ .hidden(true)
303+ .help("Set the width of the terminal"),
304+ ).subcommand(
305+ SubCommand::with_name("cache")
306+ .about("Modify the syntax-definition and theme cache")
307+ .arg(
308+ Arg::with_name("init")
309+ .long("init")
310+ .short("i")
311+ .help("Initialize the syntax/theme cache.")
312+ .long_help(
313+ "Initialize the syntax/theme cache by loading from the \
314+ source directory (default: the configuration directory).",
315+ ),
316+ ).arg(
317+ Arg::with_name("clear")
318+ .long("clear")
319+ .short("c")
320+ .help("Remove the cached syntax definitions and themes."),
321+ ).arg(
322+ Arg::with_name("config-dir")
323+ .long("config-dir")
324+ .short("d")
325+ .help("Show bat's configuration directory."),
326+ ).group(
327+ ArgGroup::with_name("cache-actions")
328+ .args(&["init", "clear", "config-dir"])
329+ .required(arg_group_required),
260330 ).arg(
261− Arg::with_name("wrap")
262− .long("wrap")
263− .overrides_with("wrap")
331+ Arg::with_name("source")
332+ .long("source")
333+ .requires("init")
264334 .takes_value(true)
265− .value_name("mode")
266− .possible_values(&["auto", "never", "character"])
267− .default_value("auto")
268− .help("Specify the text-wrapping mode.")
269− .long_help("Specify the text-wrapping mode."),
335+ .value_name("dir")
336+ .help("Use a different directory to load syntaxes and themes from."),
270337 ).arg(
271− Arg::with_name("unbuffered")
272− .short("u")
273− .hidden_short_help(true)
274− .long_help(
275− "This option exists for POSIX-compliance reasons ('u' is for \
276− 'unbuffered'). The output is always unbuffered - this option \
277− is simply ignored.",
338+ Arg::with_name("target")
339+ .long("target")
340+ .requires("init")
341+ .takes_value(true)
342+ .value_name("dir")
343+ .help(
344+ "Use a different directory to store the cached syntax and theme set.",
278345 ),
279346 ).arg(
280− Arg::with_name("terminal-width")
281− .long("terminal-width")
282− .takes_value(true)
283− .value_name("width")
284− .hidden(true)
285− .help("Set the width of the terminal"),
286− ).subcommand(
287− SubCommand::with_name("cache")
288− .about("Modify the syntax-definition and theme cache")
289− .arg(
290− Arg::with_name("init")
291− .long("init")
292− .short("i")
293− .help("Initialize the syntax/theme cache.")
294− .long_help(
295− "Initialize the syntax/theme cache by loading from the \
296− source directory (default: the configuration directory).",
297− ),
298− ).arg(
299− Arg::with_name("clear")
300− .long("clear")
301− .short("c")
302− .help("Remove the cached syntax definitions and themes."),
303− ).arg(
304− Arg::with_name("config-dir")
305− .long("config-dir")
306− .short("d")
307− .help("Show bat's configuration directory."),
308− ).group(
309− ArgGroup::with_name("cache-actions")
310− .args(&["init", "clear", "config-dir"])
311− .required(arg_group_required),
312− ).arg(
313− Arg::with_name("source")
314− .long("source")
315− .requires("init")
316− .takes_value(true)
317− .value_name("dir")
318− .help("Use a different directory to load syntaxes and themes from."),
319− ).arg(
320− Arg::with_name("target")
321− .long("target")
322− .requires("init")
323− .takes_value(true)
324− .value_name("dir")
325− .help(
326− "Use a different directory to store the cached syntax and theme set.",
327− ),
328− ).arg(
329− Arg::with_name("blank")
330− .long("blank")
331− .requires("init")
332− .help("Create completely new syntax and theme sets \
347+ Arg::with_name("blank")
348+ .long("blank")
349+ .requires("init")
350+ .help("Create completely new syntax and theme sets \
333351 (instead of appending to the default sets).")
334− ),
335− ).help_message("Print this help message.")
352+ ),
353+ ).help_message("Print this help message.")
336354 .version_message("Show version information.")
337355 .get_matches()
338356 }
@@ -341,6 +359,27 @@ impl App {
341359 let files = self.files();
342360 let output_components = self.output_components()?;
343361
362+ let paging_mode = match self.matches.value_of("paging") {
363+ Some("always") => PagingMode::Always,
364+ Some("never") => PagingMode::Never,
365+ Some("auto") | _ => if files.contains(&InputFile::StdIn) {
366+ // If we are reading from stdin, only enable paging if we write to an
367+ // interactive terminal and if we do not *read* from an interactive
368+ // terminal.
369+ if self.interactive_output && !atty::is(Stream::Stdin) {
370+ PagingMode::QuitIfOneScreen
371+ } else {
372+ PagingMode::Never
373+ }
374+ } else {
375+ if self.interactive_output {
376+ PagingMode::QuitIfOneScreen
377+ } else {
378+ PagingMode::Never
379+ }
380+ },
381+ };
382+
344383 Ok(Config {
345384 true_color: is_truecolor_terminal(),
346385 language: self.matches.value_of("language"),
@@ -364,26 +403,7 @@ impl App {
364403 Some("never") => false,
365404 Some("auto") | _ => self.interactive_output,
366405 },
367− paging_mode: match self.matches.value_of("paging") {
368− Some("always") => PagingMode::Always,
369− Some("never") => PagingMode::Never,
370− Some("auto") | _ => if files.contains(&InputFile::StdIn) {
371− // If we are reading from stdin, only enable paging if we write to an
372− // interactive terminal and if we do not *read* from an interactive
373− // terminal.
374− if self.interactive_output && !atty::is(Stream::Stdin) {
375− PagingMode::QuitIfOneScreen
376− } else {
377− PagingMode::Never
378− }
379− } else {
380− if self.interactive_output {
381− PagingMode::QuitIfOneScreen
382− } else {
383− PagingMode::Never
384− }
385− },
386− },
406+ paging_mode,
387407 term_width: self
388408 .matches
389409 .value_of("terminal-width")
@@ -393,6 +413,19 @@ impl App {
393413 || self.matches.value_of("color") == Some("always")
394414 || self.matches.value_of("decorations") == Some("always")),
395415 files,
416+ tab_width: self
417+ .matches
418+ .value_of("tabs")
419+ .map(String::from)
420+ .or_else(|| env::var("BAT_TABS").ok())
421+ .and_then(|t| t.parse().ok())
422+ .unwrap_or(
423+ if output_components.plain() && paging_mode == PagingMode::Never {
424+ 0
425+ } else {
426+ 8
427+ },
428+ ),
396429 theme: self
397430 .matches
398431 .value_of("theme")
src/main.rs+1
@@ -24,6 +24,7 @@ mod decorations;
2424mod diff;
2525mod line_range;
2626mod output;
27+mod preprocessor;
2728mod printer;
2829mod style;
2930mod terminal;
src/preprocessor.rsnew file+34
@@ -0,0 +1,34 @@
1+use console::AnsiCodeIterator;
2+
3+/// Expand tabs like an ANSI-enabled expand(1).
4+pub fn expand(line: &str, width: usize, cursor: &mut usize) -> String {
5+ let mut buffer = String::with_capacity(line.len() * 2);
6+
7+ for chunk in AnsiCodeIterator::new(line) {
8+ match chunk {
9+ (text, true) => buffer.push_str(text),
10+ (mut text, false) => {
11+ while let Some(index) = text.find('\t') {
12+ // Add previous text.
13+ if index > 0 {
14+ *cursor += index;
15+ buffer.push_str(&text[0..index]);
16+ }
17+
18+ // Add tab.
19+ let spaces = width - (*cursor % width);
20+ *cursor += spaces;
21+ buffer.push_str(&*" ".repeat(spaces));
22+
23+ // Next.
24+ text = &text[index + 1..text.len()];
25+ }
26+
27+ *cursor += text.len();
28+ buffer.push_str(text);
29+ }
30+ }
31+ }
32+
33+ buffer
34+}
src/printer.rs+24−15
@@ -16,6 +16,7 @@ use decorations::{Decoration, GridBorderDecoration, LineChangesDecoration, LineN
1616use diff::get_git_diff;
1717use diff::LineChanges;
1818use errors::*;
19+use preprocessor::expand;
1920use style::OutputWrap;
2021use terminal::{as_terminal_escaped, to_ansi_color};
2122
@@ -152,6 +153,14 @@ impl<'a> InteractivePrinter<'a> {
152153
153154 Ok(())
154155 }
156+
157+ fn preprocess(&self, text: &str, cursor: &mut usize) -> String {
158+ if self.config.tab_width > 0 {
159+ expand(text, self.config.tab_width, cursor)
160+ } else {
161+ text.to_string()
162+ }
163+ }
155164}
156165
157166impl<'a> Printer for InteractivePrinter<'a> {
@@ -204,7 +213,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
204213 line_number: usize,
205214 line_buffer: &[u8],
206215 ) -> Result<()> {
207− let line = String::from_utf8_lossy(&line_buffer);
216+ let line = String::from_utf8_lossy(&line_buffer).to_string();
208217 let regions = self.highlighter.highlight(line.as_ref());
209218
210219 if out_of_range {
@@ -213,6 +222,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
213222
214223 let mut cursor: usize = 0;
215224 let mut cursor_max: usize = self.config.term_width;
225+ let mut cursor_total: usize = 0;
216226 let mut panel_wrap: Option<String> = None;
217227
218228 // Line decorations.
@@ -234,19 +244,14 @@ impl<'a> Printer for InteractivePrinter<'a> {
234244 let true_color = self.config.true_color;
235245 let colored_output = self.config.colored_output;
236246
237− write!(
238− handle,
239− "{}",
240− regions
241− .iter()
242− .map(|&(style, text)| as_terminal_escaped(
243− style,
244− text,
245− true_color,
246− colored_output,
247− )).collect::<Vec<_>>()
248− .join("")
249− )?;
247+ for &(style, region) in regions.iter() {
248+ let text = &*self.preprocess(region, &mut cursor_total);
249+ write!(
250+ handle,
251+ "{}",
252+ as_terminal_escaped(style, &*text, true_color, colored_output,)
253+ )?;
254+ }
250255
251256 if line.bytes().next_back() != Some(b'\n') {
252257 write!(handle, "\n")?;
@@ -273,7 +278,11 @@ impl<'a> Printer for InteractivePrinter<'a> {
273278
274279 // Regular text.
275280 (text, false) => {
276− let text = text.trim_right_matches(|c| c == '\r' || c == '\n');
281+ let text = self.preprocess(
282+ text.trim_right_matches(|c| c == '\r' || c == '\n'),
283+ &mut cursor_total,
284+ );
285+
277286 let mut chars = text.chars();
278287 let mut remaining = text.chars().count();
279288
tests/snapshots/generate_snapshots.py+27−6
@@ -1,43 +1,64 @@
11#!/usr/bin/env python3
2+
23import itertools
34import subprocess
45import pathlib
56import shutil
67
8+
79def generate_snapshots():
810 single_styles = ["changes", "grid", "header", "numbers"]
911 collective_styles = ["full", "plain"]
1012
1113 for num in range(len(single_styles)):
1214 for grouped in itertools.combinations(single_styles, num + 1):
13− generate_snapshot(",".join(grouped))
15+ generate_style_snapshot(",".join(grouped))
1416
1517 for style in collective_styles:
16− generate_snapshot(style)
18+ generate_style_snapshot(style)
19+
20+ generate_snapshot("tabs_passthrough", "--tabs=0 --style=full --wrap=never")
21+ generate_snapshot("tabs_passthrough_wrapped", "--tabs=0 --style=full --wrap=character")
22+ generate_snapshot("tabs_4", "--tabs=4 --style=full --wrap=never")
23+ generate_snapshot("tabs_4_wrapped", "--tabs=4 --style=full --wrap=character")
24+ generate_snapshot("tabs_8", "--tabs=8 --style=full --wrap=never")
25+ generate_snapshot("tabs_8_wrapped", "--tabs=8 --style=full --wrap=character")
26+
27+
28+def generate_style_snapshot(style):
29+ generate_snapshot(style.replace(",", "_"), "--style={}".format(style))
30+
1731
18−def generate_snapshot(option):
19− command = "../../target/debug/bat --style={0} sample.rs > output/{0}.snapshot.txt".format(
20− option
32+def generate_snapshot(name, arguments):
33+ command = "cargo run -- --paging=never --color=never --decorations=always "
34+ command += "{args} sample.rs > output/{name}.snapshot.txt".format(
35+ name=name,
36+ args=arguments
2137 )
22− print("generating snapshot for {}".format(option))
38+ print("generating snapshot for {}".format(name))
2339 subprocess.call(command, shell=True)
2440
41+
2542def build_bat():
2643 print("building bat")
2744 subprocess.call("cargo build", cwd="../..", shell=True)
2845
46+
2947def prepare_output_dir():
3048 shutil.rmtree("output", ignore_errors=True)
3149 pathlib.Path("output").mkdir()
3250
51+
3352def modify_sample_file():
3453 print("modifying sample.rs to show changes")
3554 shutil.copyfile("sample.modified.rs", "sample.rs")
3655
56+
3757def undo_sample_file_modification():
3858 print("undoing sample.rs modifications")
3959 subprocess.call("git checkout -- sample.rs", shell=True)
4060
61+
4162build_bat()
4263prepare_output_dir()
4364modify_sample_file()
tests/snapshots/output/changes.snapshot.txt+15−1
@@ -16,7 +16,21 @@ _ fn main() {
1616 fn area(rectangle: &Rectangle) -> u32 {
1717 rectangle.width * rectangle.height
1818 }
19−+
19+
2020+ fn perimeter(rectangle: &Rectangle) -> u32 {
2121+ (rectangle.width + rectangle.height) * 2
2222+ }
23++
24+ // Tab alignment:
25+ /*
26+ Indent
27+ 1 2 3 4
28+ 1 ?
29+ 22 ?
30+ 333 ?
31+ 4444 ?
32+ 55555 ?
33+ 666666 ?
34+ 7777777 ?
35+ 88888888 ?
36+~ */
tests/snapshots/output/changes,grid.snapshot.txt → tests/snapshots/output/changes_grid.snapshot.txtrenamed+15−1
@@ -16,8 +16,22 @@ _ │ fn main() {
1616 │ fn area(rectangle: &Rectangle) -> u32 {
1717 │ rectangle.width * rectangle.height
1818 │ }
19−+ │
19+ │
2020+ │ fn perimeter(rectangle: &Rectangle) -> u32 {
2121+ │ (rectangle.width + rectangle.height) * 2
2222+ │ }
23++ │
24+ │ // Tab alignment:
25+ │ /*
26+ │ Indent
27+ │ 1 2 3 4
28+ │ 1 ?
29+ │ 22 ?
30+ │ 333 ?
31+ │ 4444 ?
32+ │ 55555 ?
33+ │ 666666 ?
34+ │ 7777777 ?
35+ │ 88888888 ?
36+~ │ */
2337──┴─────────────────────────────────────────────────────────────────────────────
tests/snapshots/output/changes,grid,header.snapshot.txt → tests/snapshots/output/changes_grid_header.snapshot.txtrenamed+15−1
@@ -19,8 +19,22 @@ _ │ fn main() {
1919 │ fn area(rectangle: &Rectangle) -> u32 {
2020 │ rectangle.width * rectangle.height
2121 │ }
22−+ │
22+ │
2323+ │ fn perimeter(rectangle: &Rectangle) -> u32 {
2424+ │ (rectangle.width + rectangle.height) * 2
2525+ │ }
26++ │
27+ │ // Tab alignment:
28+ │ /*
29+ │ Indent
30+ │ 1 2 3 4
31+ │ 1 ?
32+ │ 22 ?
33+ │ 333 ?
34+ │ 4444 ?
35+ │ 55555 ?
36+ │ 666666 ?
37+ │ 7777777 ?
38+ │ 88888888 ?
39+~ │ */
2640──┴─────────────────────────────────────────────────────────────────────────────
tests/snapshots/output/changes_grid_header_numbers.snapshot.txtnew file+40
@@ -0,0 +1,40 @@
1+───────┬────────────────────────────────────────────────────────────────────────
2+ │ File: sample.rs
3+───────┼────────────────────────────────────────────────────────────────────────
4+ 1 │ struct Rectangle {
5+ 2 │ width: u32,
6+ 3 │ height: u32,
7+ 4 │ }
8+ 5 │
9+ 6 _ │ fn main() {
10+ 7 │ let rect1 = Rectangle { width: 30, height: 50 };
11+ 8 │
12+ 9 │ println!(
13+ 10 ~ │ "The perimeter of the rectangle is {} pixels.",
14+ 11 ~ │ perimeter(&rect1)
15+ 12 │ );
16+ 13 + │ println!(r#"This line contains invalid utf8: "�����"#;
17+ 14 │ }
18+ 15 │
19+ 16 │ fn area(rectangle: &Rectangle) -> u32 {
20+ 17 │ rectangle.width * rectangle.height
21+ 18 │ }
22+ 19 │
23+ 20 + │ fn perimeter(rectangle: &Rectangle) -> u32 {
24+ 21 + │ (rectangle.width + rectangle.height) * 2
25+ 22 + │ }
26+ 23 + │
27+ 24 │ // Tab alignment:
28+ 25 │ /*
29+ 26 │ Indent
30+ 27 │ 1 2 3 4
31+ 28 │ 1 ?
32+ 29 │ 22 ?
33+ 30 │ 333 ?
34+ 31 │ 4444 ?
35+ 32 │ 55555 ?
36+ 33 │ 666666 ?
37+ 34 │ 7777777 ?
38+ 35 │ 88888888 ?
39+ 36 ~ │ */
40+───────┴────────────────────────────────────────────────────────────────────────
tests/snapshots/output/changes,grid,numbers.snapshot.txt → tests/snapshots/output/changes_grid_numbers.snapshot.txtrenamed+15−1
@@ -16,8 +16,22 @@
1616 16 │ fn area(rectangle: &Rectangle) -> u32 {
1717 17 │ rectangle.width * rectangle.height
1818 18 │ }
19− 19 + │
19+ 19 │
2020 20 + │ fn perimeter(rectangle: &Rectangle) -> u32 {
2121 21 + │ (rectangle.width + rectangle.height) * 2
2222 22 + │ }
23+ 23 + │
24+ 24 │ // Tab alignment:
25+ 25 │ /*
26+ 26 │ Indent
27+ 27 │ 1 2 3 4
28+ 28 │ 1 ?
29+ 29 │ 22 ?
30+ 30 │ 333 ?
31+ 31 │ 4444 ?
32+ 32 │ 55555 ?
33+ 33 │ 666666 ?
34+ 34 │ 7777777 ?
35+ 35 │ 88888888 ?
36+ 36 ~ │ */
2337───────┴────────────────────────────────────────────────────────────────────────
tests/snapshots/output/changes,header.snapshot.txt → tests/snapshots/output/changes_header.snapshot.txtrenamed+15−1
@@ -17,7 +17,21 @@ _ fn main() {
1717 fn area(rectangle: &Rectangle) -> u32 {
1818 rectangle.width * rectangle.height
1919 }
20−+
20+
2121+ fn perimeter(rectangle: &Rectangle) -> u32 {
2222+ (rectangle.width + rectangle.height) * 2
2323+ }
24++
25+ // Tab alignment:
26+ /*
27+ Indent
28+ 1 2 3 4
29+ 1 ?
30+ 22 ?
31+ 333 ?
32+ 4444 ?
33+ 55555 ?
34+ 666666 ?
35+ 7777777 ?
36+ 88888888 ?
37+~ */
tests/snapshots/output/changes,header,numbers.snapshot.txt → tests/snapshots/output/changes_header_numbers.snapshot.txtrenamed+15−1
@@ -17,7 +17,21 @@
1717 16 fn area(rectangle: &Rectangle) -> u32 {
1818 17 rectangle.width * rectangle.height
1919 18 }
20− 19 +
20+ 19
2121 20 + fn perimeter(rectangle: &Rectangle) -> u32 {
2222 21 + (rectangle.width + rectangle.height) * 2
2323 22 + }
24+ 23 +
25+ 24 // Tab alignment:
26+ 25 /*
27+ 26 Indent
28+ 27 1 2 3 4
29+ 28 1 ?
30+ 29 22 ?
31+ 30 333 ?
32+ 31 4444 ?
33+ 32 55555 ?
34+ 33 666666 ?
35+ 34 7777777 ?
36+ 35 88888888 ?
37+ 36 ~ */
tests/snapshots/output/changes,numbers.snapshot.txt → tests/snapshots/output/changes_numbers.snapshot.txtrenamed+15−1
@@ -16,7 +16,21 @@
1616 16 fn area(rectangle: &Rectangle) -> u32 {
1717 17 rectangle.width * rectangle.height
1818 18 }
19− 19 +
19+ 19
2020 20 + fn perimeter(rectangle: &Rectangle) -> u32 {
2121 21 + (rectangle.width + rectangle.height) * 2
2222 22 + }
23+ 23 +
24+ 24 // Tab alignment:
25+ 25 /*
26+ 26 Indent
27+ 27 1 2 3 4
28+ 28 1 ?
29+ 29 22 ?
30+ 30 333 ?
31+ 31 4444 ?
32+ 32 55555 ?
33+ 33 666666 ?
34+ 34 7777777 ?
35+ 35 88888888 ?
36+ 36 ~ */
tests/snapshots/output/full.snapshot.txt+15−1
@@ -19,8 +19,22 @@
1919 16 │ fn area(rectangle: &Rectangle) -> u32 {
2020 17 │ rectangle.width * rectangle.height
2121 18 │ }
22− 19 + │
22+ 19 │
2323 20 + │ fn perimeter(rectangle: &Rectangle) -> u32 {
2424 21 + │ (rectangle.width + rectangle.height) * 2
2525 22 + │ }
26+ 23 + │
27+ 24 │ // Tab alignment:
28+ 25 │ /*
29+ 26 │ Indent
30+ 27 │ 1 2 3 4
31+ 28 │ 1 ?
32+ 29 │ 22 ?
33+ 30 │ 333 ?
34+ 31 │ 4444 ?
35+ 32 │ 55555 ?
36+ 33 │ 666666 ?
37+ 34 │ 7777777 ?
38+ 35 │ 88888888 ?
39+ 36 ~ │ */
2640───────┴────────────────────────────────────────────────────────────────────────
tests/snapshots/output/grid.snapshot.txt+14
@@ -20,4 +20,18 @@ fn area(rectangle: &Rectangle) -> u32 {
2020fn perimeter(rectangle: &Rectangle) -> u32 {
2121 (rectangle.width + rectangle.height) * 2
2222}
23+
24+// Tab alignment:
25+/*
26+ Indent
27+ 1 2 3 4
28+1 ?
29+22 ?
30+333 ?
31+4444 ?
32+55555 ?
33+666666 ?
34+7777777 ?
35+88888888 ?
36+*/
2337────────────────────────────────────────────────────────────────────────────────
tests/snapshots/output/grid,header.snapshot.txt → tests/snapshots/output/grid_header.snapshot.txtrenamed+14
@@ -23,4 +23,18 @@ fn area(rectangle: &Rectangle) -> u32 {
2323fn perimeter(rectangle: &Rectangle) -> u32 {
2424 (rectangle.width + rectangle.height) * 2
2525}
26+
27+// Tab alignment:
28+/*
29+ Indent
30+ 1 2 3 4
31+1 ?
32+22 ?
33+333 ?
34+4444 ?
35+55555 ?
36+666666 ?
37+7777777 ?
38+88888888 ?
39+*/
2640────────────────────────────────────────────────────────────────────────────────
tests/snapshots/output/grid,header,numbers.snapshot.txt → tests/snapshots/output/grid_header_numbers.snapshot.txtrenamed+14
@@ -23,4 +23,18 @@
2323 20 │ fn perimeter(rectangle: &Rectangle) -> u32 {
2424 21 │ (rectangle.width + rectangle.height) * 2
2525 22 │ }
26+ 23 │
27+ 24 │ // Tab alignment:
28+ 25 │ /*
29+ 26 │ Indent
30+ 27 │ 1 2 3 4
31+ 28 │ 1 ?
32+ 29 │ 22 ?
33+ 30 │ 333 ?
34+ 31 │ 4444 ?
35+ 32 │ 55555 ?
36+ 33 │ 666666 ?
37+ 34 │ 7777777 ?
38+ 35 │ 88888888 ?
39+ 36 │ */
2640─────┴──────────────────────────────────────────────────────────────────────────
tests/snapshots/output/grid,numbers.snapshot.txt → tests/snapshots/output/grid_numbers.snapshot.txtrenamed+14
@@ -20,4 +20,18 @@
2020 20 │ fn perimeter(rectangle: &Rectangle) -> u32 {
2121 21 │ (rectangle.width + rectangle.height) * 2
2222 22 │ }
23+ 23 │
24+ 24 │ // Tab alignment:
25+ 25 │ /*
26+ 26 │ Indent
27+ 27 │ 1 2 3 4
28+ 28 │ 1 ?
29+ 29 │ 22 ?
30+ 30 │ 333 ?
31+ 31 │ 4444 ?
32+ 32 │ 55555 ?
33+ 33 │ 666666 ?
34+ 34 │ 7777777 ?
35+ 35 │ 88888888 ?
36+ 36 │ */
2337─────┴──────────────────────────────────────────────────────────────────────────
tests/snapshots/output/header.snapshot.txt+14
@@ -21,3 +21,17 @@ fn area(rectangle: &Rectangle) -> u32 {
2121fn perimeter(rectangle: &Rectangle) -> u32 {
2222 (rectangle.width + rectangle.height) * 2
2323}
24+
25+// Tab alignment:
26+/*
27+ Indent
28+ 1 2 3 4
29+1 ?
30+22 ?
31+333 ?
32+4444 ?
33+55555 ?
34+666666 ?
35+7777777 ?
36+88888888 ?
37+*/
tests/snapshots/output/header,numbers.snapshot.txt → tests/snapshots/output/header_numbers.snapshot.txtrenamed+14
@@ -21,3 +21,17 @@
2121 20 fn perimeter(rectangle: &Rectangle) -> u32 {
2222 21 (rectangle.width + rectangle.height) * 2
2323 22 }
24+ 23
25+ 24 // Tab alignment:
26+ 25 /*
27+ 26 Indent
28+ 27 1 2 3 4
29+ 28 1 ?
30+ 29 22 ?
31+ 30 333 ?
32+ 31 4444 ?
33+ 32 55555 ?
34+ 33 666666 ?
35+ 34 7777777 ?
36+ 35 88888888 ?
37+ 36 */
tests/snapshots/output/numbers.snapshot.txt+14
@@ -20,3 +20,17 @@
2020 20 fn perimeter(rectangle: &Rectangle) -> u32 {
2121 21 (rectangle.width + rectangle.height) * 2
2222 22 }
23+ 23
24+ 24 // Tab alignment:
25+ 25 /*
26+ 26 Indent
27+ 27 1 2 3 4
28+ 28 1 ?
29+ 29 22 ?
30+ 30 333 ?
31+ 31 4444 ?
32+ 32 55555 ?
33+ 33 666666 ?
34+ 34 7777777 ?
35+ 35 88888888 ?
36+ 36 */
tests/snapshots/output/plain.snapshot.txt+14
@@ -20,3 +20,17 @@ fn area(rectangle: &Rectangle) -> u32 {
2020fn perimeter(rectangle: &Rectangle) -> u32 {
2121 (rectangle.width + rectangle.height) * 2
2222}
23+
24+// Tab alignment:
25+/*
26+ Indent
27+ 1 2 3 4
28+1 ?
29+22 ?
30+333 ?
31+4444 ?
32+55555 ?
33+666666 ?
34+7777777 ?
35+88888888 ?
36+*/
tests/snapshots/output/tabs_4.snapshot.txtnew file+40
@@ -0,0 +1,40 @@
1+───────┬────────────────────────────────────────────────────────────────────────
2+ │ File: sample.rs
3+───────┼────────────────────────────────────────────────────────────────────────
4+ 1 │ struct Rectangle {
5+ 2 │ width: u32,
6+ 3 │ height: u32,
7+ 4 │ }
8+ 5 │
9+ 6 _ │ fn main() {
10+ 7 │ let rect1 = Rectangle { width: 30, height: 50 };
11+ 8 │
12+ 9 │ println!(
13+ 10 ~ │ "The perimeter of the rectangle is {} pixels.",
14+ 11 ~ │ perimeter(&rect1)
15+ 12 │ );
16+ 13 + │ println!(r#"This line contains invalid utf8: "�����"#;
17+ 14 │ }
18+ 15 │
19+ 16 │ fn area(rectangle: &Rectangle) -> u32 {
20+ 17 │ rectangle.width * rectangle.height
21+ 18 │ }
22+ 19 │
23+ 20 + │ fn perimeter(rectangle: &Rectangle) -> u32 {
24+ 21 + │ (rectangle.width + rectangle.height) * 2
25+ 22 + │ }
26+ 23 + │
27+ 24 │ // Tab alignment:
28+ 25 │ /*
29+ 26 │ Indent
30+ 27 │ 1 2 3 4
31+ 28 │ 1 ?
32+ 29 │ 22 ?
33+ 30 │ 333 ?
34+ 31 │ 4444 ?
35+ 32 │ 55555 ?
36+ 33 │ 666666 ?
37+ 34 │ 7777777 ?
38+ 35 │ 88888888 ?
39+ 36 ~ │ */
40+───────┴────────────────────────────────────────────────────────────────────────
tests/snapshots/output/tabs_4_wrapped.snapshot.txtnew file+40
@@ -0,0 +1,40 @@
1+───────┬────────────────────────────────────────────────────────────────────────
2+ │ File: sample.rs
3+───────┼────────────────────────────────────────────────────────────────────────
4+ 1 │ struct Rectangle {
5+ 2 │ width: u32,
6+ 3 │ height: u32,
7+ 4 │ }
8+ 5 │
9+ 6 _ │ fn main() {
10+ 7 │ let rect1 = Rectangle { width: 30, height: 50 };
11+ 8 │
12+ 9 │ println!(
13+ 10 ~ │ "The perimeter of the rectangle is {} pixels.",
14+ 11 ~ │ perimeter(&rect1)
15+ 12 │ );
16+ 13 + │ println!(r#"This line contains invalid utf8: "�����"#;
17+ 14 │ }
18+ 15 │
19+ 16 │ fn area(rectangle: &Rectangle) -> u32 {
20+ 17 │ rectangle.width * rectangle.height
21+ 18 │ }
22+ 19 │
23+ 20 + │ fn perimeter(rectangle: &Rectangle) -> u32 {
24+ 21 + │ (rectangle.width + rectangle.height) * 2
25+ 22 + │ }
26+ 23 + │
27+ 24 │ // Tab alignment:
28+ 25 │ /*
29+ 26 │ Indent
30+ 27 │ 1 2 3 4
31+ 28 │ 1 ?
32+ 29 │ 22 ?
33+ 30 │ 333 ?
34+ 31 │ 4444 ?
35+ 32 │ 55555 ?
36+ 33 │ 666666 ?
37+ 34 │ 7777777 ?
38+ 35 │ 88888888 ?
39+ 36 ~ │ */
40+───────┴────────────────────────────────────────────────────────────────────────
tests/snapshots/output/tabs_8.snapshot.txtnew file+40
@@ -0,0 +1,40 @@
1+───────┬────────────────────────────────────────────────────────────────────────
2+ │ File: sample.rs
3+───────┼────────────────────────────────────────────────────────────────────────
4+ 1 │ struct Rectangle {
5+ 2 │ width: u32,
6+ 3 │ height: u32,
7+ 4 │ }
8+ 5 │
9+ 6 _ │ fn main() {
10+ 7 │ let rect1 = Rectangle { width: 30, height: 50 };
11+ 8 │
12+ 9 │ println!(
13+ 10 ~ │ "The perimeter of the rectangle is {} pixels.",
14+ 11 ~ │ perimeter(&rect1)
15+ 12 │ );
16+ 13 + │ println!(r#"This line contains invalid utf8: "�����"#;
17+ 14 │ }
18+ 15 │
19+ 16 │ fn area(rectangle: &Rectangle) -> u32 {
20+ 17 │ rectangle.width * rectangle.height
21+ 18 │ }
22+ 19 │
23+ 20 + │ fn perimeter(rectangle: &Rectangle) -> u32 {
24+ 21 + │ (rectangle.width + rectangle.height) * 2
25+ 22 + │ }
26+ 23 + │
27+ 24 │ // Tab alignment:
28+ 25 │ /*
29+ 26 │ Indent
30+ 27 │ 1 2 3 4
31+ 28 │ 1 ?
32+ 29 │ 22 ?
33+ 30 │ 333 ?
34+ 31 │ 4444 ?
35+ 32 │ 55555 ?
36+ 33 │ 666666 ?
37+ 34 │ 7777777 ?
38+ 35 │ 88888888 ?
39+ 36 ~ │ */
40+───────┴────────────────────────────────────────────────────────────────────────
tests/snapshots/output/tabs_8_wrapped.snapshot.txtnew file+40
@@ -0,0 +1,40 @@
1+───────┬────────────────────────────────────────────────────────────────────────
2+ │ File: sample.rs
3+───────┼────────────────────────────────────────────────────────────────────────
4+ 1 │ struct Rectangle {
5+ 2 │ width: u32,
6+ 3 │ height: u32,
7+ 4 │ }
8+ 5 │
9+ 6 _ │ fn main() {
10+ 7 │ let rect1 = Rectangle { width: 30, height: 50 };
11+ 8 │
12+ 9 │ println!(
13+ 10 ~ │ "The perimeter of the rectangle is {} pixels.",
14+ 11 ~ │ perimeter(&rect1)
15+ 12 │ );
16+ 13 + │ println!(r#"This line contains invalid utf8: "�����"#;
17+ 14 │ }
18+ 15 │
19+ 16 │ fn area(rectangle: &Rectangle) -> u32 {
20+ 17 │ rectangle.width * rectangle.height
21+ 18 │ }
22+ 19 │
23+ 20 + │ fn perimeter(rectangle: &Rectangle) -> u32 {
24+ 21 + │ (rectangle.width + rectangle.height) * 2
25+ 22 + │ }
26+ 23 + │
27+ 24 │ // Tab alignment:
28+ 25 │ /*
29+ 26 │ Indent
30+ 27 │ 1 2 3 4
31+ 28 │ 1 ?
32+ 29 │ 22 ?
33+ 30 │ 333 ?
34+ 31 │ 4444 ?
35+ 32 │ 55555 ?
36+ 33 │ 666666 ?
37+ 34 │ 7777777 ?
38+ 35 │ 88888888 ?
39+ 36 ~ │ */
40+───────┴────────────────────────────────────────────────────────────────────────
tests/snapshots/output/changes,grid,header,numbers.snapshot.txt → tests/snapshots/output/tabs_passthrough.snapshot.txtrenamed+15−1
@@ -19,8 +19,22 @@
1919 16 │ fn area(rectangle: &Rectangle) -> u32 {
2020 17 │ rectangle.width * rectangle.height
2121 18 │ }
22− 19 + │
22+ 19 │
2323 20 + │ fn perimeter(rectangle: &Rectangle) -> u32 {
2424 21 + │ (rectangle.width + rectangle.height) * 2
2525 22 + │ }
26+ 23 + │
27+ 24 │ // Tab alignment:
28+ 25 │ /*
29+ 26 │ Indent
30+ 27 │ 1 2 3 4
31+ 28 │ 1 ?
32+ 29 │ 22 ?
33+ 30 │ 333 ?
34+ 31 │ 4444 ?
35+ 32 │ 55555 ?
36+ 33 │ 666666 ?
37+ 34 │ 7777777 ?
38+ 35 │ 88888888 ?
39+ 36 ~ │ */
2640───────┴────────────────────────────────────────────────────────────────────────
tests/snapshots/output/tabs_passthrough_wrapped.snapshot.txtnew file+40
@@ -0,0 +1,40 @@
1+───────┬────────────────────────────────────────────────────────────────────────
2+ │ File: sample.rs
3+───────┼────────────────────────────────────────────────────────────────────────
4+ 1 │ struct Rectangle {
5+ 2 │ width: u32,
6+ 3 │ height: u32,
7+ 4 │ }
8+ 5 │
9+ 6 _ │ fn main() {
10+ 7 │ let rect1 = Rectangle { width: 30, height: 50 };
11+ 8 │
12+ 9 │ println!(
13+ 10 ~ │ "The perimeter of the rectangle is {} pixels.",
14+ 11 ~ │ perimeter(&rect1)
15+ 12 │ );
16+ 13 + │ println!(r#"This line contains invalid utf8: "�����"#;
17+ 14 │ }
18+ 15 │
19+ 16 │ fn area(rectangle: &Rectangle) -> u32 {
20+ 17 │ rectangle.width * rectangle.height
21+ 18 │ }
22+ 19 │
23+ 20 + │ fn perimeter(rectangle: &Rectangle) -> u32 {
24+ 21 + │ (rectangle.width + rectangle.height) * 2
25+ 22 + │ }
26+ 23 + │
27+ 24 │ // Tab alignment:
28+ 25 │ /*
29+ 26 │ Indent
30+ 27 │ 1 2 3 4
31+ 28 │ 1 ?
32+ 29 │ 22 ?
33+ 30 │ 333 ?
34+ 31 │ 4444 ?
35+ 32 │ 55555 ?
36+ 33 │ 666666 ?
37+ 34 │ 7777777 ?
38+ 35 │ 88888888 ?
39+ 36 ~ │ */
40+───────┴────────────────────────────────────────────────────────────────────────
tests/snapshots/sample.modified.rs+14
@@ -20,3 +20,17 @@ fn area(rectangle: &Rectangle) -> u32 {
2020fn perimeter(rectangle: &Rectangle) -> u32 {
2121 (rectangle.width + rectangle.height) * 2
2222}
23+
24+// Tab alignment:
25+/*
26+ Indent
27+ 1 2 3 4
28+1 ?
29+22 ?
30+333 ?
31+4444 ?
32+55555 ?
33+666666 ?
34+7777777 ?
35+88888888 ?
36+*/
tests/snapshots/sample.rs+14
@@ -16,3 +16,17 @@ fn main() {
1616fn area(rectangle: &Rectangle) -> u32 {
1717 rectangle.width * rectangle.height
1818}
19+
20+// Tab alignment:
21+/*
22+ Indent
23+ 1 2 3 4
24+1 ?
25+22 ?
26+333 ?
27+4444 ?
28+55555 ?
29+666666 ?
30+7777777 ?
31+88888888 ?
32+*/
tests/tester.rs+6−2
@@ -38,13 +38,17 @@ impl BatTester {
3838 BatTester { temp_dir, exe }
3939 }
4040
41− pub fn test_snapshot(&self, style: &str) {
41+ pub fn test_snapshot(&self, name: &str, style: &str, tab_width: u32, wrap: bool) {
4242 let output = Command::new(&self.exe)
4343 .current_dir(self.temp_dir.path())
4444 .args(&[
4545 "sample.rs",
46+ "--paging=never",
47+ "--color=never",
4648 "--decorations=always",
4749 "--terminal-width=80",
50+ &format!("--wrap={}", if wrap { "character" } else { "never" }),
51+ &format!("--tabs={}", tab_width),
4852 &format!("--style={}", style),
4953 ]).output()
5054 .expect("bat failed");
@@ -55,7 +59,7 @@ impl BatTester {
5559 .replace("tests/snapshots/", "");
5660
5761 let mut expected = String::new();
58− let mut file = File::open(format!("tests/snapshots/output/{}.snapshot.txt", style))
62+ let mut file = File::open(format!("tests/snapshots/output/{}.snapshot.txt", name))
5963 .expect("snapshot file missing");
6064 file.read_to_string(&mut expected)
6165 .expect("could not read snapshot file");
tests/tests.rs+25−19
@@ -3,33 +3,39 @@ mod tester;
33use tester::BatTester;
44
55macro_rules! snapshot_tests {
6− ($($test_name: ident: $style: expr,)*) => {
6+ ($($test_name: ident: $style: expr => [wrap: $wrap:expr, tabs: $tabs:expr],)*) => {
77 $(
88 #[test]
99 fn $test_name() {
1010 let bat_tester = BatTester::new();
11− bat_tester.test_snapshot($style);
11+ bat_tester.test_snapshot(stringify!($test_name), $style, $tabs, $wrap);
1212 }
1313 )*
1414 };
1515}
1616
1717snapshot_tests! {
18− changes: "changes",
19− grid: "grid",
20− header: "header",
21− numbers: "numbers",
22− changes_grid: "changes,grid",
23− changes_header: "changes,header",
24− changes_numbers: "changes,numbers",
25− grid_header: "grid,header",
26− grid_numbers: "grid,numbers",
27− header_numbers: "header,numbers",
28− changes_grid_header: "changes,grid,header",
29− changes_grid_numbers: "changes,grid,numbers",
30− changes_header_numbers: "changes,header,numbers",
31− grid_header_numbers: "grid,header,numbers",
32− changes_grid_header_numbers: "changes,grid,header,numbers",
33− full: "full",
34− plain: "plain",
18+ changes: "changes" => [wrap: false, tabs: 8],
19+ grid: "grid" => [wrap: false, tabs: 8],
20+ header: "header" => [wrap: false, tabs: 8],
21+ numbers: "numbers" => [wrap: false, tabs: 8],
22+ changes_grid: "changes,grid" => [wrap: false, tabs: 8],
23+ changes_header: "changes,header" => [wrap: false, tabs: 8],
24+ changes_numbers: "changes,numbers" => [wrap: false, tabs: 8],
25+ grid_header: "grid,header" => [wrap: false, tabs: 8],
26+ grid_numbers: "grid,numbers" => [wrap: false, tabs: 8],
27+ header_numbers: "header,numbers" => [wrap: false, tabs: 8],
28+ changes_grid_header: "changes,grid,header" => [wrap: false, tabs: 8],
29+ changes_grid_numbers: "changes,grid,numbers" => [wrap: false, tabs: 8],
30+ changes_header_numbers: "changes,header,numbers" => [wrap: false, tabs: 8],
31+ grid_header_numbers: "grid,header,numbers" => [wrap: false, tabs: 8],
32+ changes_grid_header_numbers: "changes,grid,header,numbers" => [wrap: false, tabs: 8],
33+ full: "full" => [wrap: false, tabs: 8],
34+ plain: "plain" => [wrap: false, tabs: 0],
35+ tabs_passthrough_wrapped: "full" => [wrap: true, tabs: 0],
36+ tabs_4_wrapped: "full" => [wrap: true, tabs: 4],
37+ tabs_8_wrapped: "full" => [wrap: true, tabs: 8],
38+ tabs_passthrough: "full" => [wrap: false, tabs: 0],
39+ tabs_4: "full" => [wrap: false, tabs: 4],
40+ tabs_8: "full" => [wrap: false, tabs: 8],
3541}