From 9d32837669f0aef54a67fceee3bf7211b31ed98f Mon Sep 17 00:00:00 2001 From: sigoden Date: Wed, 8 Mar 2023 21:34:20 +0800 Subject: [PATCH] chore: optimize newline again (#43) * chore: optimize newline again command mode: should no extra newline chat mode: should have one newline * fix windows newline * fix macos newline --- src/main.rs | 4 ++-- src/render/mod.rs | 4 ++-- src/render/repl.rs | 3 +++ src/repl/handler.rs | 14 +++++++++++++- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8c24945..a25ab74 100644 --- a/src/main.rs +++ b/src/main.rs @@ -76,9 +76,9 @@ fn start_directive( let output = client.send_message(input)?; if highlight { let mut markdown_render = MarkdownRender::new(); - println!("{}\n", markdown_render.render(&output).trim_end()); + println!("{}", markdown_render.render(&output).trim()); } else { - println!("{}\n", output.trim_end()); + println!("{}", output.trim()); } output } else { diff --git a/src/render/mod.rs b/src/render/mod.rs index 001ae6c..a7465c9 100644 --- a/src/render/mod.rs +++ b/src/render/mod.rs @@ -37,10 +37,10 @@ pub fn render_stream( } drop(wg); }); - ReplyStreamHandler::new(Some(tx), abort_clone) + ReplyStreamHandler::new(Some(tx), repl, abort_clone) } else { drop(wg); - ReplyStreamHandler::new(None, abort) + ReplyStreamHandler::new(None, repl, abort) }; client.send_message_streaming(input, &mut stream_handler)?; let buffer = stream_handler.get_buffer(); diff --git a/src/render/repl.rs b/src/render/repl.rs index c267b97..c20f76f 100644 --- a/src/render/repl.rs +++ b/src/render/repl.rs @@ -70,6 +70,9 @@ fn repl_render_stream_inner( ReplyStreamEvent::Done => { let output = markdown_render.render_line_stateless(&buffer); queue!(writer, style::Print(output.trim_end()), style::Print("\n"))?; + if cfg!(windows) { + queue!(writer, style::Print("\n"))?; + } writer.flush()?; break; } diff --git a/src/repl/handler.rs b/src/repl/handler.rs index 5855208..22d8fd0 100644 --- a/src/repl/handler.rs +++ b/src/repl/handler.rs @@ -97,14 +97,20 @@ pub struct ReplyStreamHandler { sender: Option>, buffer: String, abort: SharedAbortSignal, + repl: bool, } impl ReplyStreamHandler { - pub fn new(sender: Option>, abort: SharedAbortSignal) -> Self { + pub fn new( + sender: Option>, + repl: bool, + abort: SharedAbortSignal, + ) -> Self { Self { sender, abort, buffer: String::new(), + repl, } } @@ -132,6 +138,12 @@ impl ReplyStreamHandler { if !self.buffer.ends_with('\n') { print_now!("\n") } + if self.repl { + print_now!("\n"); + if cfg!(macos) { + print_now!("\n") + } + } } } Ok(())