on master help sign in
Run 'cargo fmt' and enforce via Travis
3fa70desharkdp
.travis.yml+8
@@ -28,6 +28,14 @@ matrix:
28 rust: 1.24.0
28 rust: 1.24.0
29 env: TARGET=x86_64-apple-darwin
29 env: TARGET=x86_64-apple-darwin
30
30
31+ # Code formatting check
32+ - os: linux
33+ rust: nightly
34+ # skip the global install step
35+ install:
36+ - cargo install --debug --force rustfmt-nightly
37+ script: cargo fmt -- --write-mode=diff
38+
31addons:
39addons:
32 apt:
40 apt:
33 packages:
41 packages:
src/main.rs+27−24
@@ -24,7 +24,7 @@ use std::env;
24use std::fs::{self, File};
24use std::fs::{self, File};
25use std::io::{self, BufRead, BufReader, Stdout, StdoutLock, Write};
25use std::io::{self, BufRead, BufReader, Stdout, StdoutLock, Write};
26use std::path::Path;
26use std::path::Path;
27−use std::process::{self, Command, Child, Stdio};
27+use std::process::{self, Child, Command, Stdio};
28
28
29use ansi_term::Colour::{Fixed, Green, Red, White, Yellow};
29use ansi_term::Colour::{Fixed, Green, Red, White, Yellow};
30use ansi_term::Style;
30use ansi_term::Style;
@@ -80,8 +80,7 @@ impl<'a> OutputType<'a> {
80 .args(&["--quit-if-one-screen", "--RAW-CONTROL-CHARS", "--no-init"])
80 .args(&["--quit-if-one-screen", "--RAW-CONTROL-CHARS", "--no-init"])
81 .stdin(Stdio::piped())
81 .stdin(Stdio::piped())
82 .spawn()
82 .spawn()
83− .chain_err(|| "Could not spawn pager")?
83+ .chain_err(|| "Could not spawn pager")?))
84− ))
85 }
84 }
86
85
87 fn new_stdout(stdout: &'a Stdout) -> Self {
86 fn new_stdout(stdout: &'a Stdout) -> Self {
@@ -90,7 +89,10 @@ impl<'a> OutputType<'a> {
90
89
91 fn stdout(&mut self) -> Result<&mut Write> {
90 fn stdout(&mut self) -> Result<&mut Write> {
92 Ok(match *self {
91 Ok(match *self {
93− OutputType::Pager(ref mut command) => command.stdin.as_mut().chain_err(|| "Could not open stdin for pager")?,
92+ OutputType::Pager(ref mut command) => command
93+ .stdin
94+ .as_mut()
95+ .chain_err(|| "Could not open stdin for pager")?,
94 OutputType::Stdout(ref mut handle) => handle,
96 OutputType::Stdout(ref mut handle) => handle,
95 })
97 })
96 }
98 }
@@ -179,7 +181,7 @@ fn print_file<P: AsRef<Path>>(
179 let mut highlighter = HighlightLines::new(syntax, theme);
181 let mut highlighter = HighlightLines::new(syntax, theme);
180
182
181 let stdout = io::stdout();
183 let stdout = io::stdout();
182− let mut output_type= if options.interactive_terminal {
184+ let mut output_type = if options.interactive_terminal {
183 match OutputType::new_pager() {
185 match OutputType::new_pager() {
184 Ok(pager) => pager,
186 Ok(pager) => pager,
185 Err(_) => OutputType::new_stdout(&stdout),
187 Err(_) => OutputType::new_stdout(&stdout),
@@ -217,7 +219,6 @@ fn print_file<P: AsRef<Path>>(
217 OptionsStyle::Plain => {}
219 OptionsStyle::Plain => {}
218 };
220 };
219
221
220−
221 for (idx, maybe_line) in reader.lines().enumerate() {
222 for (idx, maybe_line) in reader.lines().enumerate() {
222 let line_nr = idx + 1;
223 let line_nr = idx + 1;
223 let line = maybe_line.unwrap_or_else(|_| "<INVALID UTF-8>".into());
224 let line = maybe_line.unwrap_or_else(|_| "<INVALID UTF-8>".into());
@@ -239,27 +240,29 @@ fn print_file<P: AsRef<Path>>(
239 // Show only content for plain style
240 // Show only content for plain style
240 OptionsStyle::Plain => writeln!(
241 OptionsStyle::Plain => writeln!(
241 handle,
242 handle,
243+ "{}",
242− "{}", as_terminal_escaped(&regions, options.true_color, options.colored_output))?,
244+ as_terminal_escaped(&regions, options.true_color, options.colored_output)
243− _ =>
245+ )?,
244− writeln!(
246+ _ => writeln!(
245− handle,
247+ handle,
246− "{} {} {} {}",
248+ "{} {} {} {}",
247− colors.line_number.paint(format!("{:4}", line_nr)),
249+ colors.line_number.paint(format!("{:4}", line_nr)),
248− // Show git modification markers only for full style
250+ // Show git modification markers only for full style
249− match options.style {
251+ match options.style {
250− OptionsStyle::Full => line_change,
252+ OptionsStyle::Full => line_change,
251− _ => Style::default().paint(" "),
253+ _ => Style::default().paint(" "),
252− },
254+ },
253− colors.grid.paint("│"),
255+ colors.grid.paint("│"),
254− as_terminal_escaped(&regions, options.true_color, options.colored_output)
256+ as_terminal_escaped(&regions, options.true_color, options.colored_output)
255− )?
257+ )?,
256 }
258 }
257 }
259 }
258
260
259 // Show bars for all but plain style
261 // Show bars for all but plain style
260 match options.style {
262 match options.style {
261− OptionsStyle::LineNumbers | OptionsStyle::Full =>
263+ OptionsStyle::LineNumbers | OptionsStyle::Full => {
262− print_horizontal_line(handle, &colors.grid, '┴', term_width)?,
264+ print_horizontal_line(handle, &colors.grid, '┴', term_width)?
265+ }
263 OptionsStyle::Plain => {}
266 OptionsStyle::Plain => {}
264 };
267 };
265
268
@@ -494,7 +497,7 @@ fn run() -> Result<()> {
494 .takes_value(true)
497 .takes_value(true)
495 .possible_values(&["auto", "never", "always"])
498 .possible_values(&["auto", "never", "always"])
496 .default_value("auto")
499 .default_value("auto")
497− .help("When to use colors")
500+ .help("When to use colors"),
498 )
501 )
499 .subcommand(
502 .subcommand(
500 SubCommand::with_name("init-cache")
503 SubCommand::with_name("init-cache")
@@ -554,7 +557,7 @@ fn main() {
554 if let Err(error) = result {
557 if let Err(error) = result {
555 match error {
558 match error {
556 Error(ErrorKind::Io(ref io_error), _)
559 Error(ErrorKind::Io(ref io_error), _)
557− if io_error.kind() == io::ErrorKind::BrokenPipe => {}
560+ if io_error.kind() == io::ErrorKind::BrokenPipe => {}
558 _ => {
561 _ => {
559 eprintln!("{}: {}", Red.paint("[bat error]"), error);
562 eprintln!("{}: {}", Red.paint("[bat error]"), error);
560
563
src/terminal.rs+6−2
@@ -1,7 +1,7 @@
1use std::fmt::Write;
1use std::fmt::Write;
2
2
3−use ansi_term::Style;
4use ansi_term::Colour::{Fixed, RGB};
3use ansi_term::Colour::{Fixed, RGB};
4+use ansi_term::Style;
5use syntect::highlighting;
5use syntect::highlighting;
6
6
7/// Approximate a 24 bit color value by a 8 bit ANSI code
7/// Approximate a 24 bit color value by a 8 bit ANSI code
@@ -26,7 +26,11 @@ fn rgb2ansi(r: u8, g: u8, b: u8) -> u8 {
26 }
26 }
27}
27}
28
28
29−pub fn as_terminal_escaped(v: &[(highlighting::Style, &str)], true_color: bool, colored: bool) -> String {
29+pub fn as_terminal_escaped(
30+ v: &[(highlighting::Style, &str)],
31+ true_color: bool,
32+ colored: bool,
33+) -> String {
30 let mut s: String = String::new();
34 let mut s: String = String::new();
31 for &(ref style, text) in v.iter() {
35 for &(ref style, text) in v.iter() {
32 let style = if !colored {
36 let style = if !colored {