From bb40347121ee3c231aab84be286495a810326457 Mon Sep 17 00:00:00 2001 From: cloudhead Date: Wed, 27 Sep 2023 14:44:49 +0200 Subject: [PATCH] term: Change signature of `Constraint::from_env` Return `None` if there's no terminal. --- radicle-cli/src/commands/diff.rs | 2 +- radicle-cli/src/git/pretty_diff.rs | 2 +- radicle-term/src/element.rs | 11 +++++------ 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/radicle-cli/src/commands/diff.rs b/radicle-cli/src/commands/diff.rs index aaaed6ea..f020ebc2 100644 --- a/radicle-cli/src/commands/diff.rs +++ b/radicle-cli/src/commands/diff.rs @@ -146,7 +146,7 @@ pub fn run(options: Options, _ctx: impl term::Context) -> anyhow::Result<()> { let mut hi = Highlighter::default(); let pretty = diff.pretty(&mut hi, &(), &repo); - pretty.write(Constraint::from_env().maximize()); + pretty.write(Constraint::from_env().unwrap_or_default()); Ok(()) } diff --git a/radicle-cli/src/git/pretty_diff.rs b/radicle-cli/src/git/pretty_diff.rs index e5d5d0a4..976d9bd9 100644 --- a/radicle-cli/src/git/pretty_diff.rs +++ b/radicle-cli/src/git/pretty_diff.rs @@ -372,6 +372,6 @@ mod test { let mut hi = Highlighter::default(); let pretty = diff.pretty(&mut hi, &(), &repo); - pretty.write(Constraint::from_env().maximize()); + pretty.write(Constraint::from_env().unwrap_or_default()); } } diff --git a/radicle-term/src/element.rs b/radicle-term/src/element.rs index 38a0beac..2fb5501b 100644 --- a/radicle-term/src/element.rs +++ b/radicle-term/src/element.rs @@ -60,13 +60,12 @@ impl Constraint { } /// Create a constraint from the terminal environment. - /// - /// If standard out isn't a terminal, returns an unbounded constraint. - pub fn from_env() -> Self { + /// Returns [`None`] if the output device is not a terminal. + pub fn from_env() -> Option { if io::stdout().is_terminal() { - Self::max(viewport().unwrap_or(Size::MAX)) + Some(Self::max(viewport().unwrap_or(Size::MAX))) } else { - Self::UNBOUNDED + None } } } @@ -92,7 +91,7 @@ pub trait Element: fmt::Debug { /// Print this element to stdout. fn print(&self) { - for line in self.render(Constraint::from_env()) { + for line in self.render(Constraint::from_env().unwrap_or_default()) { println!("{}", line.to_string().trim_end()); } }