summaryrefslogtreecommitdiffstats
path: root/src/render/cmd.rs
blob: 75768b94ecac2bb5b93f1e0292bdcd11395f6046 (plain) (blame)
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
use super::{MarkdownRender, ReplyEvent};

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

use anyhow::Result;
use crossbeam::channel::Receiver;
use textwrap::core::display_width;

pub fn cmd_render_stream(
    rx: &Receiver<ReplyEvent>,
    render: &mut MarkdownRender,
    abort: &AbortSignal,
) -> Result<()> {
    let mut buffer = String::new();
    let mut col = 0;
    loop {
        if abort.aborted() {
            return Ok(());
        }
        if let Ok(evt) = rx.try_recv() {
            match evt {
                ReplyEvent::Text(text) => {
                    if text.contains('\n') {
                        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;
                    } else {
                        buffer = format!("{buffer}{text}");
                        if !(render.is_code()
                            || buffer.len() < 40
                            || buffer.starts_with('#')
                            || buffer.starts_with('>')
                            || buffer.starts_with('|'))
                        {
                            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() {
                                    if output.contains('\n') {
                                        col = display_width(tail);
                                    } else {
                                        col += display_width(output);
                                    }
                                }
                                print!("{}", output);
                            }
                        }
                    }
                }
                ReplyEvent::Done => {
                    let input = format!("{}{buffer}", spaces(col));
                    let output = render.render(&input);
                    let output = &output[col..];
                    println!("{}", output);
                    break;
                }
            }
        }
    }
    Ok(())
}