aboutsummaryrefslogtreecommitdiffstats
path: root/adenosine-cli/src/pretty.rs
blob: 3d260a30fbaabeb1c227c262a18f6639dcbeebea (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
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
147
148
149
150
151
152
use adenosine::app_bsky::{FeedPostView, PostView, ThreadPostView};
use anyhow::Result;
use std::io::Write;
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};

pub fn pp_thread_post_view(tpv: &ThreadPostView) -> Result<()> {
    // TODO: this could do better
    if let Some(parent) = &tpv.parent {
        pp_thread_post_view(&parent)?;
    };
    if let Some(not_found) = &tpv.notFound {
        if *not_found == true {
            let mut stdout = StandardStream::stdout(ColorChoice::Always);
            stdout.set_color(ColorSpec::new().set_fg(Some(Color::Red)))?;
            writeln!(
                &mut stdout,
                "thread post not found: {}\n",
                tpv.uri.as_ref().expect("URI for post-not-found")
            )?;
            stdout.reset()?;
        }
    }
    if let Some(post) = &tpv.post {
        pp_post_view(&post)?;
    }
    if let Some(replies) = &tpv.replies {
        for child in replies {
            pp_thread_post_view(&child)?;
        }
    };
    Ok(())
}

pub fn pp_feed_post_view(fpv: &FeedPostView) -> Result<()> {
    let mut stdout = StandardStream::stdout(ColorChoice::Always);

    if let Some(repost) = &fpv.reason {
        stdout.set_color(ColorSpec::new().set_dimmed(true).set_italic(true))?;
        write!(&mut stdout, "re-posted by ")?;
        stdout.set_color(
            ColorSpec::new()
                .set_fg(Some(Color::Yellow))
                .set_dimmed(true)
                .set_italic(true),
        )?;
        writeln!(&mut stdout, "@{}", repost.by.handle)?;
        stdout.reset()?;
    }

    pp_post_view(&fpv.post)
}

pub fn pp_post_view(pv: &PostView) -> Result<()> {
    let mut stdout = StandardStream::stdout(ColorChoice::Always);

    if let Some(reply_to) = &pv.record.reply {
        stdout.set_color(ColorSpec::new().set_dimmed(true).set_italic(true))?;
        writeln!(&mut stdout, "reply to {}", reply_to.parent.uri)?;
        stdout.reset()?;
    }

    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()?;

    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()?;
            }
        }
    }

    stdout.set_color(ColorSpec::new().set_dimmed(true))?;
    writeln!(&mut stdout, "{}\n", pv.uri)?;

    stdout.reset()?;
    Ok(())
}