summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsharkdp <davidpeter@web.de>2018-05-06 14:53:52 +0200
committerDavid Peter <sharkdp@users.noreply.github.com>2018-05-06 15:10:46 +0200
commit3fa70deaa7777af32b9480961b91bf70c2d04f20 (patch)
treee8d265ba7923e6dbfb0eabf22590650d2f97a190
parentd4553c6b386c2c71a9a177c3cdcb530eaf7ec59b (diff)
Run 'cargo fmt' and enforce via Travis
-rw-r--r--.travis.yml8
-rw-r--r--src/main.rs51
-rw-r--r--src/terminal.rs8
3 files changed, 41 insertions, 26 deletions
diff --git a/.travis.yml b/.travis.yml
index e639a0b8..b3fb6cc4 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -28,6 +28,14 @@ matrix:
rust: 1.24.0
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:
apt:
packages:
diff --git a/src/main.rs b/src/main.rs
index 2b0a5e22..e5603f51 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -24,7 +24,7 @@ use std::env;
use std::fs::{self, File};
use std::io::{self, BufRead, BufReader, Stdout, StdoutLock, Write};
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::Style;
@@ -80,8 +80,7 @@ impl<'a> OutputType<'a> {
.args(&["--quit-if-one-screen", "--RAW-CONTROL-CHARS", "--no-init"])
.stdin(Stdio::piped())
.spawn()
- .chain_err(|| "Could not spawn pager")?
- ))
+ .chain_err(|| "Could not spawn pager")?))
}
fn new_stdout(stdout: &'a Stdout) -> Self {
@@ -90,7 +89,10 @@ impl<'a> OutputType<'a> {
fn stdout(&mut self) -> Result<&mut Write> {
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,
})
}
@@ -179,7 +181,7 @@ fn print_file<P: AsRef<Path>>(
let mut highlighter = HighlightLines::new(syntax, theme);
let stdout = io::stdout();
- let mut output_type= if options.interactive_terminal {
+ let mut output_type = if options.interactive_terminal {
match OutputType::new_pager() {
Ok(pager) => pager,
Err(_) => OutputType::new_stdout(&stdout),
@@ -217,7 +219,6 @@ fn print_file<P: AsRef<Path>>(
OptionsStyle::Plain => {}
};
-
for (idx, maybe_line) in reader.lines().enumerate() {
let line_nr = idx + 1;
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
OptionsStyle::Plain => writeln!(
handle,
- "{}", as_terminal_escaped(&regions, options.true_color, options.colored_output))?,
- _ =>
- writeln!(
- handle,
- "{} {} {} {}",
- colors.line_number.paint(format!("{:4}", line_nr)),
- // Show git modification markers only for full style
- match options.style {
- OptionsStyle::Full => line_change,
- _ => Style::default().paint(" "),
- },
- colors.grid.paint("│"),
- as_terminal_escaped(&regions, options.true_color, options.colored_output)
- )?
+ "{}",
+ as_terminal_escaped(&regions, options.true_color, options.colored_output)
+ )?,
+ _ => writeln!(
+ handle,
+ "{} {} {} {}",
+ colors.line_number.paint(format!("{:4}", line_nr)),
+ // Show git modification markers only for full style
+ match options.style {
+ OptionsStyle::Full => line_change,
+ _ => Style::default().paint(" "),
+ },
+ colors.grid.paint("│"),
+ as_terminal_escaped(&regions, options.true_color, options.colored_output)
+ )?,
}
}
// Show bars for all but plain style
match options.style {
- OptionsStyle::LineNumbers | OptionsStyle::Full =>
- print_horizontal_line(handle, &colors.grid, '┴', term_width)?,
+ OptionsStyle::LineNumbers | OptionsStyle::Full => {
+ print_horizontal_line(handle, &colors.grid, '┴', term_width)?
+ }
OptionsStyle::Plain => {}
};
@@ -494,7 +497,7 @@ fn run() -> Result<()> {
.takes_value(true)
.possible_values(&["auto", "never", "always"])
.default_value("auto")
- .help("When to use colors")
+ .help("When to use colors"),
)
.subcommand(
SubCommand::with_name("init-cache")
@@ -554,7 +557,7 @@ fn main() {
if let Err(error) = result {
match 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);
diff --git a/src/terminal.rs b/src/terminal.rs
index 0e26376b..01c2459b 100644
--- a/src/terminal.rs
+++ b/src/terminal.rs
@@ -1,7 +1,7 @@
use std::fmt::Write;
-use ansi_term::Style;
use ansi_term::Colour::{Fixed, RGB};
+use ansi_term::Style;
use syntect::highlighting;
/// 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();
for &(ref style, text) in v.iter() {
let style = if !colored {