From b16913fec38f604ab045d6f16ddba62481b09a6c Mon Sep 17 00:00:00 2001 From: sigoden Date: Fri, 6 Sep 2024 06:38:29 +0800 Subject: fix: render stream failed due to read cursor position timeout (#835) --- src/render/mod.rs | 5 +++-- src/render/stream.rs | 12 +++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) (limited to 'src/render') diff --git a/src/render/mod.rs b/src/render/mod.rs index ce7191f..2c87e78 100644 --- a/src/render/mod.rs +++ b/src/render/mod.rs @@ -15,13 +15,14 @@ pub async fn render_stream( config: &GlobalConfig, abort: AbortSignal, ) -> Result<()> { - if *IS_STDOUT_TERMINAL { + let ret = if *IS_STDOUT_TERMINAL { let render_options = config.read().render_options()?; let mut render = MarkdownRender::init(render_options)?; markdown_stream(rx, &mut render, &abort).await } else { raw_stream(rx, &abort).await - } + }; + ret.map_err(|err| err.context("Failed to reader stream")) } pub fn render_error(err: anyhow::Error, highlight: bool) { diff --git a/src/render/stream.rs b/src/render/stream.rs index 042bfe5..2264c16 100644 --- a/src/render/stream.rs +++ b/src/render/stream.rs @@ -28,6 +28,9 @@ pub async fn markdown_stream( disable_raw_mode()?; + if ret.is_err() { + println!(); + } ret } @@ -78,7 +81,14 @@ async fn markdown_stream_inner( // tab width hacking text = text.replace('\t', " "); - let (col, mut row) = cursor::position()?; + let mut attempts = 0; + let (col, mut row) = loop { + match cursor::position() { + Ok(pos) => break pos, + Err(_) if attempts < 3 => attempts += 1, + Err(e) => return Err(e.into()), + } + }; // Fix unexpected duplicate lines on kitty, see https://github.com/sigoden/aichat/issues/105 if col == 0 && row > 0 && display_width(&buffer) == columns as usize { -- cgit v1.2.3