summaryrefslogtreecommitdiffstats
path: root/src/render
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2023-11-03 18:36:17 +0800
committerGitHub <noreply@github.com>2023-11-03 18:36:17 +0800
commit887bf0a744646c6d9cfb361ba0c929d4a4342337 (patch)
tree3e3144e000f4f738fd495d8f5261d1f67f4dc3a0 /src/render
parent811e605fbfd5b4973cbff9439db14c76c76b140f (diff)
downloadaichat-887bf0a744646c6d9cfb361ba0c929d4a4342337.tar.gz
fix: fix rendering markdown in REPL (#207)
Diffstat (limited to 'src/render')
-rw-r--r--src/render/markdown.rs4
-rw-r--r--src/render/repl.rs9
2 files changed, 11 insertions, 2 deletions
diff --git a/src/render/markdown.rs b/src/render/markdown.rs
index f491fc1..f300854 100644
--- a/src/render/markdown.rs
+++ b/src/render/markdown.rs
@@ -205,7 +205,9 @@ impl MarkdownRender {
fn wrap(text: &str, width: usize) -> String {
let indent: usize = text.chars().take_while(|c| *c == ' ').count();
- let wrap_options = textwrap::Options::new(width).initial_indent(&text[0..indent]);
+ let wrap_options = textwrap::Options::new(width)
+ .wrap_algorithm(textwrap::WrapAlgorithm::FirstFit)
+ .initial_indent(&text[0..indent]);
textwrap::wrap(&text[indent..], wrap_options).join("\n")
}
diff --git a/src/render/repl.rs b/src/render/repl.rs
index 31c597e..34212c3 100644
--- a/src/render/repl.rs
+++ b/src/render/repl.rs
@@ -70,7 +70,9 @@ fn repl_render_stream_inner(
cursor::MoveTo(0, 0),
)?;
}
- queue!(writer, terminal::Clear(terminal::ClearType::UntilNewLine))?;
+
+ // No guarantee that text returned by render will not be re-layouted, so it is better to clear it.
+ queue!(writer, terminal::Clear(terminal::ClearType::FromCursorDown))?;
if text.contains('\n') {
let text = format!("{buffer}{text}");
@@ -79,6 +81,9 @@ fn repl_render_stream_inner(
let output = render.render(head);
print_block(writer, &output, columns)?;
queue!(writer, style::Print(&buffer),)?;
+
+ // No guarantee the buffer width of the buffer will not exceed the number of columns.
+ // So we calcuate the number of rows needed, rather than setting it directly to 1.
buffer_rows = need_rows(&buffer, columns);
} else {
buffer = format!("{buffer}{text}");
@@ -87,6 +92,8 @@ fn repl_render_stream_inner(
let (head, tail) = split_line_tail(&output);
buffer_rows = print_block(writer, head, columns)?;
queue!(writer, style::Print(&tail),)?;
+
+ // Same as above.
buffer_rows += need_rows(tail, columns);
} else {
queue!(writer, style::Print(&output))?;