Run 'cargo fmt' and enforce via Travis

This commit is contained in:
sharkdp 2018-05-06 14:53:52 +02:00 committed by David Peter
parent d4553c6b38
commit 3fa70deaa7
3 changed files with 41 additions and 26 deletions

View File

@ -28,6 +28,14 @@ matrix:
rust: 1.24.0 rust: 1.24.0
env: TARGET=x86_64-apple-darwin env: TARGET=x86_64-apple-darwin
# Code formatting check
- os: linux
rust: nightly
# skip the global install step
install:
- cargo install --debug --force rustfmt-nightly
script: cargo fmt -- --write-mode=diff
addons: addons:
apt: apt:
packages: packages:

View File

@ -24,7 +24,7 @@ use std::env;
use std::fs::{self, File}; use std::fs::{self, File};
use std::io::{self, BufRead, BufReader, Stdout, StdoutLock, Write}; use std::io::{self, BufRead, BufReader, Stdout, StdoutLock, Write};
use std::path::Path; use std::path::Path;
use std::process::{self, Command, Child, Stdio}; use std::process::{self, Child, Command, Stdio};
use ansi_term::Colour::{Fixed, Green, Red, White, Yellow}; use ansi_term::Colour::{Fixed, Green, Red, White, Yellow};
use ansi_term::Style; use ansi_term::Style;
@ -80,8 +80,7 @@ impl<'a> OutputType<'a> {
.args(&["--quit-if-one-screen", "--RAW-CONTROL-CHARS", "--no-init"]) .args(&["--quit-if-one-screen", "--RAW-CONTROL-CHARS", "--no-init"])
.stdin(Stdio::piped()) .stdin(Stdio::piped())
.spawn() .spawn()
.chain_err(|| "Could not spawn pager")? .chain_err(|| "Could not spawn pager")?))
))
} }
fn new_stdout(stdout: &'a Stdout) -> Self { fn new_stdout(stdout: &'a Stdout) -> Self {
@ -90,7 +89,10 @@ impl<'a> OutputType<'a> {
fn stdout(&mut self) -> Result<&mut Write> { fn stdout(&mut self) -> Result<&mut Write> {
Ok(match *self { Ok(match *self {
OutputType::Pager(ref mut command) => command.stdin.as_mut().chain_err(|| "Could not open stdin for pager")?, OutputType::Pager(ref mut command) => command
.stdin
.as_mut()
.chain_err(|| "Could not open stdin for pager")?,
OutputType::Stdout(ref mut handle) => handle, OutputType::Stdout(ref mut handle) => handle,
}) })
} }
@ -179,7 +181,7 @@ fn print_file<P: AsRef<Path>>(
let mut highlighter = HighlightLines::new(syntax, theme); let mut highlighter = HighlightLines::new(syntax, theme);
let stdout = io::stdout(); let stdout = io::stdout();
let mut output_type= if options.interactive_terminal { let mut output_type = if options.interactive_terminal {
match OutputType::new_pager() { match OutputType::new_pager() {
Ok(pager) => pager, Ok(pager) => pager,
Err(_) => OutputType::new_stdout(&stdout), Err(_) => OutputType::new_stdout(&stdout),
@ -217,7 +219,6 @@ fn print_file<P: AsRef<Path>>(
OptionsStyle::Plain => {} OptionsStyle::Plain => {}
}; };
for (idx, maybe_line) in reader.lines().enumerate() { for (idx, maybe_line) in reader.lines().enumerate() {
let line_nr = idx + 1; let line_nr = idx + 1;
let line = maybe_line.unwrap_or_else(|_| "<INVALID UTF-8>".into()); let line = maybe_line.unwrap_or_else(|_| "<INVALID UTF-8>".into());
@ -239,27 +240,29 @@ fn print_file<P: AsRef<Path>>(
// Show only content for plain style // Show only content for plain style
OptionsStyle::Plain => writeln!( OptionsStyle::Plain => writeln!(
handle, handle,
"{}", as_terminal_escaped(&regions, options.true_color, options.colored_output))?, "{}",
_ => as_terminal_escaped(&regions, options.true_color, options.colored_output)
writeln!( )?,
handle, _ => writeln!(
"{} {} {} {}", handle,
colors.line_number.paint(format!("{:4}", line_nr)), "{} {} {} {}",
// Show git modification markers only for full style colors.line_number.paint(format!("{:4}", line_nr)),
match options.style { // Show git modification markers only for full style
OptionsStyle::Full => line_change, match options.style {
_ => Style::default().paint(" "), OptionsStyle::Full => line_change,
}, _ => Style::default().paint(" "),
colors.grid.paint(""), },
as_terminal_escaped(&regions, options.true_color, options.colored_output) colors.grid.paint(""),
)? as_terminal_escaped(&regions, options.true_color, options.colored_output)
)?,
} }
} }
// Show bars for all but plain style // Show bars for all but plain style
match options.style { match options.style {
OptionsStyle::LineNumbers | OptionsStyle::Full => OptionsStyle::LineNumbers | OptionsStyle::Full => {
print_horizontal_line(handle, &colors.grid, '┴', term_width)?, print_horizontal_line(handle, &colors.grid, '┴', term_width)?
}
OptionsStyle::Plain => {} OptionsStyle::Plain => {}
}; };
@ -494,7 +497,7 @@ fn run() -> Result<()> {
.takes_value(true) .takes_value(true)
.possible_values(&["auto", "never", "always"]) .possible_values(&["auto", "never", "always"])
.default_value("auto") .default_value("auto")
.help("When to use colors") .help("When to use colors"),
) )
.subcommand( .subcommand(
SubCommand::with_name("init-cache") SubCommand::with_name("init-cache")
@ -554,7 +557,7 @@ fn main() {
if let Err(error) = result { if let Err(error) = result {
match error { match error {
Error(ErrorKind::Io(ref io_error), _) Error(ErrorKind::Io(ref io_error), _)
if io_error.kind() == io::ErrorKind::BrokenPipe => {} if io_error.kind() == io::ErrorKind::BrokenPipe => {}
_ => { _ => {
eprintln!("{}: {}", Red.paint("[bat error]"), error); eprintln!("{}: {}", Red.paint("[bat error]"), error);

View File

@ -1,7 +1,7 @@
use std::fmt::Write; use std::fmt::Write;
use ansi_term::Style;
use ansi_term::Colour::{Fixed, RGB}; use ansi_term::Colour::{Fixed, RGB};
use ansi_term::Style;
use syntect::highlighting; use syntect::highlighting;
/// Approximate a 24 bit color value by a 8 bit ANSI code /// 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 {
} }
} }
pub fn as_terminal_escaped(v: &[(highlighting::Style, &str)], true_color: bool, colored: bool) -> String { pub fn as_terminal_escaped(
v: &[(highlighting::Style, &str)],
true_color: bool,
colored: bool,
) -> String {
let mut s: String = String::new(); let mut s: String = String::new();
for &(ref style, text) in v.iter() { for &(ref style, text) in v.iter() {
let style = if !colored { let style = if !colored {