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,
}) })
} }
@ -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,9 +240,10 @@ 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!( )?,
_ => writeln!(
handle, handle,
"{} {} {} {}", "{} {} {} {}",
colors.line_number.paint(format!("{:4}", line_nr)), colors.line_number.paint(format!("{:4}", line_nr)),
@ -252,14 +254,15 @@ fn print_file<P: AsRef<Path>>(
}, },
colors.grid.paint(""), colors.grid.paint(""),
as_terminal_escaped(&regions, options.true_color, options.colored_output) 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")

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 {