1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
use super::{cmd::split_line_tail, MarkdownRender};
use crate::repl::{ReplyStreamEvent, SharedAbortSignal};
use anyhow::Result;
use crossbeam::channel::Receiver;
use crossterm::{
cursor,
event::{self, Event, KeyCode, KeyModifiers},
queue, style,
terminal::{self, disable_raw_mode, enable_raw_mode},
};
use std::{
io::{self, Stdout, Write},
time::{Duration, Instant},
};
use textwrap::core::display_width;
#[allow(clippy::module_name_repetitions)]
pub fn repl_render_stream(
rx: &Receiver<ReplyStreamEvent>,
render: &mut MarkdownRender,
abort: &SharedAbortSignal,
) -> Result<()> {
enable_raw_mode()?;
let mut stdout = io::stdout();
let ret = repl_render_stream_inner(rx, render, abort, &mut stdout);
disable_raw_mode()?;
ret
}
fn repl_render_stream_inner(
rx: &Receiver<ReplyStreamEvent>,
render: &mut MarkdownRender,
abort: &SharedAbortSignal,
writer: &mut Stdout,
) -> Result<()> {
let mut last_tick = Instant::now();
let tick_rate = Duration::from_millis(50);
let mut buffer = String::new();
let columns = terminal::size()?.0;
let mut clear_rows = 0;
loop {
if abort.aborted() {
return Ok(());
}
if let Ok(evt) = rx.try_recv() {
match evt {
ReplyStreamEvent::Text(text) => {
let (col, mut row) = cursor::position()?;
// fix unxpected 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 - clear_rows))?;
} else {
let scroll_rows = clear_rows - row - 1;
queue!(
writer,
terminal::ScrollUp(scroll_rows),
cursor::MoveTo(0, 0),
)?;
}
if text.contains('\n') {
let text = format!("{buffer}{text}");
let (head, tail) = split_line_tail(&text);
buffer = tail.to_string();
let output = render.render(head);
print_block(writer, &output, columns)?;
queue!(writer, style::Print(&buffer),)?;
clear_rows = 0;
} 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)?;
queue!(writer, style::Print(&tail),)?;
} 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);
}
}
writer.flush()?;
}
ReplyStreamEvent::Done => {
queue!(writer, style::Print("\n"))?;
writer.flush()?;
break;
}
}
continue;
}
let timeout = tick_rate
.checked_sub(last_tick.elapsed())
.unwrap_or_else(|| Duration::from_secs(0));
if crossterm::event::poll(timeout)? {
if let Event::Key(key) = event::read()? {
match key.code {
KeyCode::Char('c') if key.modifiers == KeyModifiers::CONTROL => {
abort.set_ctrlc();
return Ok(());
}
KeyCode::Char('d') if key.modifiers == KeyModifiers::CONTROL => {
abort.set_ctrld();
return Ok(());
}
_ => {}
}
}
}
if last_tick.elapsed() >= tick_rate {
last_tick = Instant::now();
}
}
Ok(())
}
fn print_block(writer: &mut Stdout, text: &str, columns: u16) -> Result<u16> {
let mut num = 0;
for line in text.split('\n') {
queue!(
writer,
style::Print(line),
style::Print("\n"),
cursor::MoveLeft(columns),
)?;
num += 1;
}
Ok(num)
}
|