on master help sign in
Skip '--quit-if-one-screen' for --paging=always

closes #97

commit96cc391f2d5865ba256d61b823c50225fc12e7e2 authorsharkdp <davidpeter@web.de> dateSat 19 May 2018, 10:31 (8 years ago) parenta1e1170
2 files changed+42−20
src/app.rs+21−6 src/main.rs+21−14
src/app.rs+21−6
@@ -181,16 +181,24 @@ impl App {
181181 Some("never") => false,
182182 Some("auto") | _ => self.interactive_output,
183183 },
184− paging: match self.matches.value_of("paging") {
185− Some("always") => true,
186− Some("never") => false,
184+ paging_mode: match self.matches.value_of("paging") {
185+ Some("always") => PagingMode::Always,
186+ Some("never") => PagingMode::Never,
187187 Some("auto") | _ => if files.contains(&None) {
188188 // If we are reading from stdin, only enable paging if we write to an
189189 // interactive terminal and if we do not *read* from an interactive
190190 // terminal.
191− self.interactive_output && !atty::is(Stream::Stdin)
191+ if self.interactive_output && !atty::is(Stream::Stdin) {
192+ PagingMode::QuitIfOneScreen
193+ } else {
194+ PagingMode::Never
195+ }
192196 } else {
193− self.interactive_output
197+ if self.interactive_output {
198+ PagingMode::QuitIfOneScreen
199+ } else {
200+ PagingMode::Never
201+ }
194202 },
195203 },
196204 term_width: Term::stdout().size().1 as usize,
@@ -230,13 +238,20 @@ impl App {
230238 }
231239}
232240
241+#[derive(Debug, Clone, Copy)]
242+pub enum PagingMode {
243+ Always,
244+ QuitIfOneScreen,
245+ Never,
246+}
247+
233248pub struct Config<'a> {
234249 pub true_color: bool,
235250 pub output_wrap: OutputWrap,
236251 pub output_components: OutputComponents,
237252 pub language: Option<&'a str>,
238253 pub colored_output: bool,
239− pub paging: bool,
254+ pub paging_mode: PagingMode,
240255 pub term_width: usize,
241256 pub files: Vec<Option<&'a str>>,
242257 pub theme: Option<&'a str>,
src/main.rs+21−14
@@ -37,7 +37,7 @@ use syntect::easy::HighlightLines;
3737use syntect::highlighting::Theme;
3838use syntect::parsing::SyntaxDefinition;
3939
40−use app::App;
40+use app::{App, PagingMode};
4141use assets::{config_dir, syntax_set_path, theme_set_path, HighlightingAssets};
4242use diff::get_git_diff;
4343use printer::Printer;
@@ -59,12 +59,27 @@ enum OutputType {
5959}
6060
6161impl OutputType {
62− fn pager() -> Result<Self> {
63− Ok(OutputType::Pager(Command::new("less")
64− .args(&["--quit-if-one-screen", "--RAW-CONTROL-CHARS", "--no-init"])
62+ fn from_mode(mode: PagingMode) -> Self {
63+ use PagingMode::*;
64+ match mode {
65+ Always => OutputType::try_pager(false),
66+ QuitIfOneScreen => OutputType::try_pager(true),
67+ _ => OutputType::stdout(),
68+ }
69+ }
70+
71+ /// Try to launch the pager. Fall back to stdout in case of errors.
72+ fn try_pager(quit_if_one_screen: bool) -> Self {
73+ let mut args = vec!["--RAW-CONTROL-CHARS", "--no-init"];
74+ if quit_if_one_screen {
75+ args.push("--quit-if-one-screen");
76+ }
77+ Command::new("less")
78+ .args(&args)
6579 .stdin(Stdio::piped())
6680 .spawn()
67− .chain_err(|| "Could not spawn pager")?))
81+ .map(OutputType::Pager)
82+ .unwrap_or_else(|_| OutputType::stdout())
6883 }
6984
7085 fn stdout() -> Self {
@@ -153,14 +168,6 @@ fn print_file(
153168 Ok(())
154169}
155170
156−fn get_output_type(paging: bool) -> OutputType {
157− if paging {
158− OutputType::pager().unwrap_or_else(|_| OutputType::stdout())
159− } else {
160− OutputType::stdout()
161− }
162−}
163−
164171fn run() -> Result<()> {
165172 let app = App::new();
166173
@@ -243,7 +250,7 @@ fn run() -> Result<()> {
243250 return Ok(());
244251 }
245252
246− let mut output_type = get_output_type(config.paging);
253+ let mut output_type = OutputType::from_mode(config.paging_mode);
247254 let handle = output_type.handle()?;
248255 let mut printer = Printer::new(handle, &config);
249256