diff options
| author | sigoden <sigoden@gmail.com> | 2024-09-06 06:38:29 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-09-06 06:38:29 +0800 |
| commit | b16913fec38f604ab045d6f16ddba62481b09a6c (patch) | |
| tree | 4cd36b11b85c4613e9022e423a06b40f7589a49c /src/render | |
| parent | d92f102a85eb98ee0cb3eaa8a775b7d3528090ac (diff) | |
| download | aichat-b16913fec38f604ab045d6f16ddba62481b09a6c.tar.gz | |
fix: render stream failed due to read cursor position timeout (#835)
Diffstat (limited to 'src/render')
| -rw-r--r-- | src/render/mod.rs | 5 | ||||
| -rw-r--r-- | src/render/stream.rs | 12 |
2 files changed, 14 insertions, 3 deletions
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 { |
