From 34fe0899cae39333d61d03b5a7b5872edf68a71c Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Thu, 1 Dec 2022 23:50:51 +0900 Subject: [PATCH] cli: colorize pager spawn error I've moved error handling to the call site to avoid passing &mut Ui around. --- src/ui.rs | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/src/ui.rs b/src/ui.rs index d0c84ff43..53cf4998b 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -150,12 +150,18 @@ impl Ui { } match self.output { - UiOutput::Paged { .. } => {} - UiOutput::Terminal { .. } => { - if io::stdout().is_tty() { - self.output = UiOutput::new_paged_else_terminal(&self.settings); + UiOutput::Terminal { .. } if io::stdout().is_tty() => { + match UiOutput::new_paged(&self.settings) { + Ok(new_output) => { + self.output = new_output; + } + Err(e) => { + self.write_warn(&format!("Failed to spawn pager: {}\n", e)) + .ok(); + } } } + UiOutput::Terminal { .. } | UiOutput::Paged { .. } => {} } } @@ -342,21 +348,11 @@ impl UiOutput { } } - fn new_paged_else_terminal(settings: &UserSettings) -> UiOutput { + fn new_paged(settings: &UserSettings) -> io::Result { let pager_cmd = pager_setting(settings); - let child_result = Command::new(pager_cmd).stdin(Stdio::piped()).spawn(); - match child_result { - Ok(mut child) => { - let child_stdin = child.stdin.take().unwrap(); - UiOutput::Paged { child, child_stdin } - } - Err(e) => { - io::stderr() - .write_fmt(format_args!("Failed to spawn pager: {}\n", e)) - .ok(); - UiOutput::new_terminal() - } - } + let mut child = Command::new(pager_cmd).stdin(Stdio::piped()).spawn()?; + let child_stdin = child.stdin.take().unwrap(); + Ok(UiOutput::Paged { child, child_stdin }) } }