aboutsummaryrefslogtreecommitdiffstats
path: root/adenosine-cli/src/pretty.rs
blob: 306c056f2ff35d65ee80c2843c089e33ed47ab1f (plain)
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
use adenosine::app_bsky::PostView;
use anyhow::Result;
use std::io::Write;
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};

pub fn pp_post_view(pv: &PostView) -> Result<()> {
    let mut stdout = StandardStream::stdout(ColorChoice::Always);
    stdout.set_color(ColorSpec::new().set_fg(Some(Color::Yellow)).set_bold(true))?;

    write!(&mut stdout, "@{:<54.54}", pv.author.handle)?;
    stdout.reset()?;
    stdout.set_color(ColorSpec::new().set_dimmed(true))?;
    writeln!(&mut stdout, "{}", pv.indexedAt)?;
    stdout.reset()?;

    write!(&mut stdout, " ")?;
    if let Some(entities) = &pv.record.entities {
        let mut cur: usize = 0;
        for ent in entities {
            write!(
                &mut stdout,
                "{}",
                &pv.record.text[cur..ent.index.start as usize]
            )?;
            match ent.r#type.as_str() {
                "mention" => stdout
                    .set_color(ColorSpec::new().set_fg(Some(Color::Magenta)).set_bold(true))?,
                "hashtag" => {
                    stdout.set_color(ColorSpec::new().set_fg(Some(Color::Cyan)).set_bold(true))?
                }
                "link" => stdout.set_color(
                    ColorSpec::new()
                        .set_fg(Some(Color::Blue))
                        .set_underline(true),
                )?,
                _ => {}
            }
            write!(
                &mut stdout,
                "{}",
                &pv.record.text[ent.index.start as usize..ent.index.end as usize]
            )?;
            stdout.reset()?;
            cur = ent.index.end as usize;
        }
        writeln!(&mut stdout, "{}", &pv.record.text[cur..])?;
    } else if !pv.record.text.is_empty() {
        writeln!(&mut stdout, "{}", &pv.record.text)?;
    }

    if let Some(embed) = &pv.embed {
        if let Some(ext) = &embed.external {
            let desc = format!("{}: {}", ext.title, ext.description);
            stdout.set_color(
                ColorSpec::new()
                    .set_fg(Some(Color::Green))
                    .set_dimmed(true)
                    .set_underline(false),
            )?;
            writeln!(&mut stdout, " {:<70.70}", desc)?;
            write!(&mut stdout, " ")?;
            stdout.set_color(
                ColorSpec::new()
                    .set_fg(Some(Color::Green))
                    .set_dimmed(true)
                    .set_underline(true),
            )?;
            writeln!(&mut stdout, "{}", &ext.uri)?;
            stdout.reset()?;
        }
        if let Some(images) = &embed.images {
            for img in images.iter() {
                if !img.alt.is_empty() {
                    stdout.set_color(
                        ColorSpec::new()
                            .set_fg(Some(Color::Green))
                            .set_dimmed(true)
                            .set_underline(false),
                    )?;
                    writeln!(&mut stdout, " {:<70.70}", img.alt)?;
                }
                write!(&mut stdout, " ")?;
                stdout.set_color(
                    ColorSpec::new()
                        .set_fg(Some(Color::Green))
                        .set_dimmed(true)
                        .set_underline(true),
                )?;
                writeln!(&mut stdout, "{}", &img.fullsize)?;
                stdout.reset()?;
            }
        }
    }

    writeln!(&mut stdout, "\n")?;
    stdout.reset()?;
    Ok(())
}