Skip to content

Commit

Permalink
refactor: optimize render (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden authored Nov 2, 2023
1 parent 16eba9b commit dce6877
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 30 deletions.
33 changes: 33 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ async-trait = "0.1.74"
textwrap = "0.16.0"
ansi_colours = "1.2.2"
reqwest-eventsource = "0.5.0"
simplelog = "0.12.1"
log = "0.4.20"

[dependencies.reqwest]
version = "0.11.14"
Expand Down
18 changes: 18 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ impl Config {
config.setup_highlight();
config.setup_light_theme()?;

setup_logger()?;

Ok(config)
}

Expand Down Expand Up @@ -822,3 +824,19 @@ fn set_bool(target: &mut bool, value: &str) {
_ => {}
}
}

#[cfg(debug_assertions)]
fn setup_logger() -> Result<()> {
use simplelog::WriteLogger;
let file = std::fs::File::create(Config::local_path("debug.log")?)?;
let config = simplelog::ConfigBuilder::new()
.add_filter_allow_str("aichat")
.build();
WriteLogger::init(log::LevelFilter::Debug, config, file)?;
Ok(())
}

#[cfg(not(debug_assertions))]
fn setup_logger() -> Result<()> {
Ok(())
}
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ mod client;
mod config;
mod render;
mod repl;

#[macro_use]
extern crate log;
#[macro_use]
mod utils;

Expand Down
28 changes: 12 additions & 16 deletions src/render/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{MarkdownRender, ReplyEvent};

use crate::utils::{spaces, split_line_sematic, split_line_tail, AbortSignal};
use crate::utils::{split_line_sematic, split_line_tail, AbortSignal};

use anyhow::Result;
use crossbeam::channel::Receiver;
Expand All @@ -12,7 +12,7 @@ pub fn cmd_render_stream(
abort: &AbortSignal,
) -> Result<()> {
let mut buffer = String::new();
let mut col = 0;
let mut indent = 0;
loop {
if abort.aborted() {
return Ok(());
Expand All @@ -24,10 +24,9 @@ pub fn cmd_render_stream(
let text = format!("{buffer}{text}");
let (head, tail) = split_line_tail(&text);
buffer = tail.to_string();
let input = format!("{}{head}", spaces(col));
let output = render.render(&input);
println!("{}", &output[col..]);
col = 0;
let output = render.render_with_indent(head, indent);
println!("{}", output);
indent = 0;
} else {
buffer = format!("{buffer}{text}");
if !(render.is_code()
Expand All @@ -38,26 +37,23 @@ pub fn cmd_render_stream(
{
if let Some((head, remain)) = split_line_sematic(&buffer) {
buffer = remain;
let input = format!("{}{head}", spaces(col));
let output = render.render(&input);
let output = &output[col..];
let (_, tail) = split_line_tail(output);
if render.wrap_width().is_some() {
let output = render.render_with_indent(&head, indent);
let (_, tail) = split_line_tail(&output);
if let Some(width) = render.wrap_width() {
if output.contains('\n') {
col = display_width(tail);
indent = display_width(tail);
} else {
col += display_width(output);
indent += display_width(&output);
}
indent %= width as usize;
}
print!("{}", output);
}
}
}
}
ReplyEvent::Done => {
let input = format!("{}{buffer}", spaces(col));
let output = render.render(&input);
let output = &output[col..];
let output = render.render_with_indent(&buffer, indent);
println!("{}", output);
break;
}
Expand Down
23 changes: 23 additions & 0 deletions src/render/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ impl MarkdownRender {
.join("\n")
}

pub fn render_with_indent(&mut self, text: &str, padding: usize) -> String {
let text = format!("{}{}", " ".repeat(padding), text);
let output = self.render(&text);
if output.starts_with('\n') {
output
} else {
output.chars().skip(padding).collect()
}
}

pub fn render_line(&self, line: &str) -> String {
let (_, code_syntax, is_code) = self.check_line(line);
if is_code {
Expand Down Expand Up @@ -359,4 +369,17 @@ std::error::Error>> {
let output = render.render(TEXT);
assert_eq!(TEXT_WRAP_ALL, output);
}

#[test]
fn wrap_with_indent() {
let options = RenderOptions::default();
let mut render = MarkdownRender::init(options).unwrap();
render.wrap_width = Some(80);

let input = "To unzip a file in Rust, you can use the `zip` crate. Here's an example code";
let output = render.render_with_indent(input, 40);
let expect =
"To unzip a file in Rust, you can use the\n`zip` crate. Here's an example code";
assert_eq!(output, expect);
}
}
2 changes: 2 additions & 0 deletions src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ impl ReplyHandler {
}

pub fn text(&mut self, text: &str) -> Result<()> {
debug!("ReplyText: {}", text);
if self.buffer.is_empty() && text == "\n\n" {
return Ok(());
}
Expand All @@ -90,6 +91,7 @@ impl ReplyHandler {
}

pub fn done(&mut self) -> Result<()> {
debug!("ReplyDone");
let ret = self
.sender
.send(ReplyEvent::Done)
Expand Down
27 changes: 17 additions & 10 deletions src/render/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ fn repl_render_stream_inner(
) -> Result<()> {
let mut last_tick = Instant::now();
let tick_rate = Duration::from_millis(50);

let mut buffer = String::new();
let mut buffer_rows = 1;

let columns = terminal::size()?.0;

let mut clear_rows = 0;
loop {
if abort.aborted() {
return Ok(());
Expand All @@ -53,21 +55,22 @@ fn repl_render_stream_inner(
ReplyEvent::Text(text) => {
let (col, mut row) = cursor::position()?;

// fix unexpected duplicate lines on kitty, see https://github.com/sigoden/aichat/issues/105
// 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 {
row -= 1;
}

if row + 1 >= clear_rows {
queue!(writer, cursor::MoveTo(0, row.saturating_sub(clear_rows)))?;
if row + 1 >= buffer_rows {
queue!(writer, cursor::MoveTo(0, row + 1 - buffer_rows),)?;
} else {
let scroll_rows = clear_rows - row - 1;
let scroll_rows = buffer_rows - row - 1;
queue!(
writer,
terminal::ScrollUp(scroll_rows),
cursor::MoveTo(0, 0),
)?;
}
queue!(writer, terminal::Clear(terminal::ClearType::UntilNewLine))?;

if text.contains('\n') {
let text = format!("{buffer}{text}");
Expand All @@ -76,19 +79,18 @@ fn repl_render_stream_inner(
let output = render.render(head);
print_block(writer, &output, columns)?;
queue!(writer, style::Print(&buffer),)?;
clear_rows = 0;
buffer_rows = need_rows(&buffer, columns);
} else {
buffer = format!("{buffer}{text}");
let output = render.render_line(&buffer);
if output.contains('\n') {
let (head, tail) = split_line_tail(&output);
clear_rows = print_block(writer, head, columns)?;
buffer_rows = print_block(writer, head, columns)?;
queue!(writer, style::Print(&tail),)?;
buffer_rows += need_rows(tail, columns);
} else {
queue!(writer, style::Print(&output))?;
let buffer_width = display_width(&output) as u16;
let need_rows = (buffer_width + columns - 1) / columns;
clear_rows = need_rows.saturating_sub(1);
buffer_rows = need_rows(&output, columns);
}
}

Expand Down Expand Up @@ -147,3 +149,8 @@ fn print_block(writer: &mut Stdout, text: &str, columns: u16) -> Result<u16> {
}
Ok(num)
}

fn need_rows(text: &str, columns: u16) -> u16 {
let buffer_width = display_width(text) as u16;
(buffer_width + columns - 1) / columns
}
4 changes: 0 additions & 4 deletions src/utils/split_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ pub fn split_line_tail(text: &str) -> (&str, &str) {
}
}

pub fn spaces(n: usize) -> String {
" ".repeat(n)
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
enum Kind {
ParentheseStart,
Expand Down

0 comments on commit dce6877

Please sign in to comment.