on master help sign in
Updates for review of PR 899
commit42e3825dafcfc0255471c2213d3c97ddb5a21fa6 authorDavid Tolnay <dtolnay@gmail.com> dateMon 30 Mar 2020, 13:18 (7 years ago) parent014d754
5 files changed+44−62
ci/script.bash+1 src/config.rs+3−2 src/controller.rs+27−14 src/errors.rs+7−40 src/output.rs+6−6
ci/script.bash+1
@@ -17,3 +17,4 @@ fi
1717cargo check --target "$TARGET" --verbose --lib --no-default-features
1818cargo check --target "$TARGET" --verbose --lib --no-default-features --features git
1919cargo check --target "$TARGET" --verbose --lib --no-default-features --features paging
20+cargo check --target "$TARGET" --verbose --lib --no-default-features --features git,paging
src/config.rs+3−2
@@ -5,14 +5,14 @@ pub use crate::syntax_mapping::{MappingTarget, SyntaxMapping};
55pub use crate::wrap::OutputWrap;
66
77#[derive(Debug, Clone, Copy, PartialEq)]
8+#[cfg(feature = "paging")]
89pub enum PagingMode {
9− #[cfg(feature = "paging")]
1010 Always,
11− #[cfg(feature = "paging")]
1211 QuitIfOneScreen,
1312 Never,
1413}
1514
15+#[cfg(feature = "paging")]
1616impl Default for PagingMode {
1717 fn default() -> Self {
1818 PagingMode::Never
@@ -53,6 +53,7 @@ pub struct Config<'a> {
5353 pub output_wrap: OutputWrap,
5454
5555 /// Pager or STDOUT
56+ #[cfg(feature = "paging")]
5657 pub paging_mode: PagingMode,
5758
5859 /// Specifies the lines that should be printed
src/controller.rs+27−14
@@ -1,8 +1,9 @@
11use std::io::{self, Write};
2−use std::path::Path;
32
43use crate::assets::HighlightingAssets;
5−use crate::config::{Config, PagingMode};
4+use crate::config::Config;
5+#[cfg(feature = "paging")]
6+use crate::config::PagingMode;
67use crate::errors::*;
78use crate::inputfile::{InputFile, InputFileReader};
89use crate::line_range::{LineRanges, RangeCheckResult};
@@ -24,22 +25,34 @@ impl<'b> Controller<'b> {
2425 }
2526
2627 pub fn run_with_error_handler(&self, handle_error: impl Fn(&Error)) -> Result<bool> {
27− // Do not launch the pager if NONE of the input files exist
28− let mut paging_mode = self.config.paging_mode;
29− if self.config.paging_mode != PagingMode::Never {
30− let call_pager = self.config.files.iter().any(|file| {
31− if let InputFile::Ordinary(path) = file {
32− return Path::new(path).exists();
33− } else {
34− return true;
28+ let mut output_type;
29+
30+ #[cfg(feature = "paging")]
31+ {
32+ use std::path::Path;
33+
34+ // Do not launch the pager if NONE of the input files exist
35+ let mut paging_mode = self.config.paging_mode;
36+ if self.config.paging_mode != PagingMode::Never {
37+ let call_pager = self.config.files.iter().any(|file| {
38+ if let InputFile::Ordinary(path) = file {
39+ return Path::new(path).exists();
40+ } else {
41+ return true;
42+ }
43+ });
44+ if !call_pager {
45+ paging_mode = PagingMode::Never;
3546 }
36− });
37− if !call_pager {
38− paging_mode = PagingMode::Never;
3947 }
48+ output_type = OutputType::from_mode(paging_mode, self.config.pager)?;
49+ }
50+
51+ #[cfg(not(feature = "paging"))]
52+ {
53+ output_type = OutputType::stdout();
4054 }
4155
42− let mut output_type = OutputType::from_mode(paging_mode, self.config.pager)?;
4356 let writer = output_type.handle()?;
4457 let mut no_errors: bool = true;
4558
src/errors.rs+7−40
@@ -2,20 +2,20 @@ use error_chain::error_chain;
22
33error_chain! {
44 foreign_links {
5− Clap(clap::Error);
6− Io(std::io::Error);
7− SyntectError(syntect::LoadingError);
8− ParseIntError(std::num::ParseIntError);
9− GlobParsingError(globset::Error);
5+ Clap(::clap::Error) #[cfg(feature = "application")];
6+ Io(::std::io::Error);
7+ SyntectError(::syntect::LoadingError);
8+ ParseIntError(::std::num::ParseIntError);
9+ GlobParsingError(::globset::Error);
1010 }
1111}
1212
1313pub fn default_error_handler(error: &Error) {
1414 match error {
1515 Error(ErrorKind::Io(ref io_error), _)
16− if io_error.kind() == std::io::ErrorKind::BrokenPipe =>
16+ if io_error.kind() == ::std::io::ErrorKind::BrokenPipe =>
1717 {
18− std::process::exit(0);
18+ ::std::process::exit(0);
1919 }
2020 _ => {
2121 use ansi_term::Colour::Red;
@@ -23,36 +23,3 @@ pub fn default_error_handler(error: &Error) {
2323 }
2424 };
2525}
26−
27−// Mock out a type for clap::Error if we aren't pulling in a dependency on clap.
28−//
29−// This can be removed after migrating away from error_chain to some modern
30−// derive-based error library such as thiserror, in favor of:
31−//
32−// #[derive(Error)]
33−// pub enum Error {
34−// #[cfg(feature = "application")]
35−// Clap(clap::Error),
36−// ...
37−// }
38−//
39−#[cfg(not(feature = "application"))]
40−mod clap {
41− use std::fmt::{self, Debug, Display};
42−
43− pub struct Error(());
44−
45− impl Display for Error {
46− fn fmt(&self, _formatter: &mut fmt::Formatter) -> fmt::Result {
47− unreachable!()
48− }
49− }
50−
51− impl Debug for Error {
52− fn fmt(&self, _formatter: &mut fmt::Formatter) -> fmt::Result {
53− unreachable!()
54− }
55− }
56−
57− impl std::error::Error for Error {}
58−}
src/output.rs+6−6
@@ -2,6 +2,7 @@ use std::io::{self, Write};
22#[cfg(feature = "paging")]
33use std::process::Child;
44
5+#[cfg(feature = "paging")]
56use crate::config::PagingMode;
67use crate::errors::*;
78#[cfg(feature = "paging")]
@@ -15,13 +16,12 @@ pub enum OutputType {
1516}
1617
1718impl OutputType {
19+ #[cfg(feature = "paging")]
1820 pub fn from_mode(mode: PagingMode, pager: Option<&str>) -> Result<Self> {
19− let _ = pager;
21+ use self::PagingMode::*;
2022 Ok(match mode {
21− #[cfg(feature = "paging")]
22− PagingMode::Always => OutputType::try_pager(false, pager)?,
23− #[cfg(feature = "paging")]
24− PagingMode::QuitIfOneScreen => OutputType::try_pager(true, pager)?,
23+ Always => OutputType::try_pager(false, pager)?,
24+ QuitIfOneScreen => OutputType::try_pager(true, pager)?,
2525 _ => OutputType::stdout(),
2626 })
2727 }
@@ -116,7 +116,7 @@ impl OutputType {
116116 }
117117 }
118118
119− fn stdout() -> Self {
119+ pub(crate) fn stdout() -> Self {
120120 OutputType::Stdout(io::stdout())
121121 }
122122