diff options
| author | sigoden <sigoden@gmail.com> | 2023-11-03 12:30:40 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-03 12:30:40 +0800 |
| commit | c20cd20d535dbf6194cd10d44c91eb87fc45b343 (patch) | |
| tree | 6b062176d68ab6fa6962ae036b61c5396bfee08a /src/render | |
| parent | b34e40e25f0b10fccc9de64b9f06b9290be88b22 (diff) | |
| download | aichat-c20cd20d535dbf6194cd10d44c91eb87fc45b343.tar.gz | |
fix: wrap and tokenize algorithm (#205)
* fix: wrap and tokenize algorithm
* update tests
* remove unnecessary tokenize from tiktoken
Diffstat (limited to 'src/render')
| -rw-r--r-- | src/render/markdown.rs | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/src/render/markdown.rs b/src/render/markdown.rs index 834c47b..f491fc1 100644 --- a/src/render/markdown.rs +++ b/src/render/markdown.rs @@ -82,13 +82,13 @@ impl MarkdownRender { .join("\n") } - pub fn render_with_indent(&mut self, text: &str, padding: usize) -> String { - let text = format!("{}{}", " ".repeat(padding), text); + pub fn render_with_indent(&mut self, text: &str, indent: usize) -> String { + let text = format!("{}{}", " ".repeat(indent), text); let output = self.render(&text); if output.starts_with('\n') { output } else { - output.chars().skip(padding).collect() + output.chars().skip(indent).collect() } } @@ -186,7 +186,7 @@ impl MarkdownRender { if is_code && !self.options.wrap_code { return line; } - textwrap::wrap(&line, width as usize).join("\n") + wrap(&line, width as usize) } else { line } @@ -203,6 +203,12 @@ 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]); + textwrap::wrap(&text[indent..], wrap_options).join("\n") +} + #[derive(Debug, Clone, Default)] pub struct RenderOptions { pub theme: Option<Theme>, @@ -381,5 +387,11 @@ std::error::Error>> { let expect = "To unzip a file in Rust, you can use the\n`zip` crate. Here's an example code"; assert_eq!(output, expect); + + let input = "Unzip a file"; + let output = render.render_with_indent(input, 76); + let expect = "\nUnzip a file"; + + assert_eq!(output, expect); } } |
