From 09815a19a59525837154b2cce379d9a354861270 Mon Sep 17 00:00:00 2001 From: Brendan Jackman Date: Sat, 23 Nov 2024 21:25:29 +0100 Subject: [PATCH] Don't call buggy crossterm code when no tty I think this might finally be the source of the GHA failures. --- src/terminal.rs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/terminal.rs b/src/terminal.rs index a8a66de..42c2421 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -1,10 +1,11 @@ -use std::sync::RwLock; +use std::{future::pending, io::stdout, sync::RwLock}; use anyhow::Context as _; use async_stream::try_stream; use crossterm::{ event::{Event, EventStream}, terminal, + tty::IsTty as _, }; use futures::{Stream, StreamExt as _}; @@ -16,12 +17,16 @@ pub struct TerminalSizeWatcher { impl<'a> TerminalSizeWatcher { pub fn new() -> anyhow::Result { - // TODO: We're importing crossterm just for this lol - let (cols, rows) = terminal::size().context("getting terminal size")?; Ok(Self { - size: RwLock::new(Rect { - cols: cols.into(), - rows: rows.into(), + size: RwLock::new(if !stdout().is_tty() { + Rect { cols: 0, rows: 0 } + } else { + // TODO: We're importing crossterm just for this lol + let (cols, rows) = terminal::size().context("getting terminal size")?; + Rect { + cols: cols.into(), + rows: rows.into(), + } }), }) } @@ -29,8 +34,13 @@ impl<'a> TerminalSizeWatcher { // Returns an item whenever the terminal gets resized. When that happens you // should call size again pub fn resizes(&'a self) -> impl Stream> + use<'a> { - let mut reader = EventStream::new(); try_stream! { + // crossterm async code seems to be buggy when not a tty. + if !stdout().is_tty() { + pending::<()>().await; // Block forever. + yield (); + }; + let mut reader = EventStream::new(); loop { let event: Event = reader.next().await .context("terminal event stream terminated")?