term: Change signature of `Constraint::from_env`

Return `None` if there's no terminal.
This commit is contained in:
cloudhead 2023-09-27 14:44:49 +02:00
parent 6a5753f4bc
commit bb40347121
No known key found for this signature in database
3 changed files with 7 additions and 8 deletions

View File

@ -146,7 +146,7 @@ pub fn run(options: Options, _ctx: impl term::Context) -> anyhow::Result<()> {
let mut hi = Highlighter::default(); let mut hi = Highlighter::default();
let pretty = diff.pretty(&mut hi, &(), &repo); let pretty = diff.pretty(&mut hi, &(), &repo);
pretty.write(Constraint::from_env().maximize()); pretty.write(Constraint::from_env().unwrap_or_default());
Ok(()) Ok(())
} }

View File

@ -372,6 +372,6 @@ mod test {
let mut hi = Highlighter::default(); let mut hi = Highlighter::default();
let pretty = diff.pretty(&mut hi, &(), &repo); let pretty = diff.pretty(&mut hi, &(), &repo);
pretty.write(Constraint::from_env().maximize()); pretty.write(Constraint::from_env().unwrap_or_default());
} }
} }

View File

@ -60,13 +60,12 @@ impl Constraint {
} }
/// Create a constraint from the terminal environment. /// Create a constraint from the terminal environment.
/// /// Returns [`None`] if the output device is not a terminal.
/// If standard out isn't a terminal, returns an unbounded constraint. pub fn from_env() -> Option<Self> {
pub fn from_env() -> Self {
if io::stdout().is_terminal() { if io::stdout().is_terminal() {
Self::max(viewport().unwrap_or(Size::MAX)) Some(Self::max(viewport().unwrap_or(Size::MAX)))
} else { } else {
Self::UNBOUNDED None
} }
} }
} }
@ -92,7 +91,7 @@ pub trait Element: fmt::Debug {
/// Print this element to stdout. /// Print this element to stdout.
fn print(&self) { 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()); println!("{}", line.to_string().trim_end());
} }
} }