From 4e729db237dbfb93d32bfa43b60a09be07d49630 Mon Sep 17 00:00:00 2001 From: sigoden Date: Wed, 8 Mar 2023 13:44:37 +0800 Subject: chore: optimize output newline --- src/config.rs | 16 ++++------------ src/main.rs | 4 ++-- src/render/cmd.rs | 2 +- src/render/repl.rs | 2 +- src/repl/handler.rs | 6 ++---- 5 files changed, 10 insertions(+), 20 deletions(-) diff --git a/src/config.rs b/src/config.rs index 21784c5..3b24a08 100644 --- a/src/config.rs +++ b/src/config.rs @@ -114,26 +114,18 @@ impl Config { let timestamp = now(); let output = match self.role.as_ref() { None => { - format!( - "# CHAT:[{timestamp}]\n{}\n\n--------\n{}\n--------\n\n", - input.trim(), - output.trim(), - ) + format!("# CHAT:[{timestamp}]\n{input}\n--------\n{output}\n--------\n\n",) } Some(v) => { if v.name == TEMP_ROLE_NAME { format!( - "# CHAT:[{timestamp}]\n{}\n{}\n\n--------\n{}\n--------\n\n", - v.prompt, - input.trim(), - output.trim(), + "# CHAT:[{timestamp}]\n{}\n{input}\n--------\n{output}\n--------\n\n", + v.prompt ) } else { format!( - "# CHAT:[{timestamp}] ({})\n{}\n\n--------\n{}\n--------\n\n", + "# CHAT:[{timestamp}] ({})\n{input}\n--------\n{output}\n--------\n\n", v.name, - input.trim(), - output.trim(), ) } } diff --git a/src/main.rs b/src/main.rs index 3fc0f09..edb9907 100644 --- a/src/main.rs +++ b/src/main.rs @@ -77,9 +77,9 @@ fn start_directive( let output = client.send_message(input, prompt)?; if highlight { let mut markdown_render = MarkdownRender::new(); - println!("{}", markdown_render.render(&output)); + println!("{}\n", markdown_render.render(&output).trim_end()); } else { - println!("{output}"); + println!("{}\n", output.trim_end()); } output } else { diff --git a/src/render/cmd.rs b/src/render/cmd.rs index 5cf6957..bdf6226 100644 --- a/src/render/cmd.rs +++ b/src/render/cmd.rs @@ -38,7 +38,7 @@ pub fn cmd_render_stream(rx: Receiver, abort: SharedAbortSigna } ReplyStreamEvent::Done => { let output = markdown_render.render(&buffer); - print_now!("{}\n\n", output); + print_now!("{}\n", output.trim_end()); break; } } diff --git a/src/render/repl.rs b/src/render/repl.rs index 96c6364..151e37f 100644 --- a/src/render/repl.rs +++ b/src/render/repl.rs @@ -71,7 +71,7 @@ fn repl_render_stream_inner( } ReplyStreamEvent::Done => { let output = markdown_render.render_line_stateless(&buffer); - queue!(writer, style::Print(output), style::Print("\n"))?; + queue!(writer, style::Print(output.trim_end()), style::Print("\n"))?; writer.flush()?; break; } diff --git a/src/repl/handler.rs b/src/repl/handler.rs index dddd531..5959cd6 100644 --- a/src/repl/handler.rs +++ b/src/repl/handler.rs @@ -128,10 +128,8 @@ impl ReplyStreamHandler { let _ = tx.send(ReplyStreamEvent::Done); } None => { - if self.buffer.ends_with('\n') { - print_now!("\n"); - } else { - print_now!("\n\n"); + if !self.buffer.ends_with('\n') { + print_now!("\n") } } } -- cgit v1.2.3