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
|
// yt - A fully featured command line YouTube client
//
// Copyright (C) 2024 Benedikt Peetz <benedikt.peetz@b-peetz.de>
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This file is part of Yt.
//
// You should have received a copy of the License along with this program.
// If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
use std::fmt::Write;
use chrono::{Local, TimeZone};
use chrono_humanize::{Accuracy, HumanTime, Tense};
use crate::comments::comment::CommentExt;
use super::comment::Comments;
impl Comments {
pub fn render(&self, color: bool) -> String {
self.render_help(color).expect("This should never fail.")
}
fn render_help(&self, color: bool) -> Result<String, std::fmt::Error> {
let mut f = String::new();
macro_rules! c {
($color_str:expr, $write:ident, $color:expr) => {
if $color {
$write.write_str(concat!("\x1b[", $color_str, "m"))?
}
};
}
fn format(
comment: &CommentExt,
f: &mut String,
ident_count: u32,
color: bool,
) -> std::fmt::Result {
let ident = &(0..ident_count).map(|_| " ").collect::<String>();
let value = &comment.value;
f.write_str(ident)?;
if value.author_is_uploader {
c!("91;1", f, color);
} else {
c!("35", f, color);
}
f.write_str(&value.author)?;
c!("0", f, color);
if value.edited || value.is_favorited {
f.write_str("[")?;
if value.edited {
f.write_str("")?;
}
if value.edited && value.is_favorited {
f.write_str(" ")?;
}
if value.is_favorited {
f.write_str("")?;
}
f.write_str("]")?;
}
c!("36;1", f, color);
write!(
f,
" {}",
HumanTime::from(
Local
.timestamp_opt(value.timestamp, 0)
.single()
.expect("This should be valid")
)
.to_text_en(Accuracy::Rough, Tense::Past)
)?;
c!("0", f, color);
// c!("31;1", f);
// f.write_fmt(format_args!(" [{}]", comment.value.like_count))?;
// c!("0", f);
f.write_str(":\n")?;
f.write_str(ident)?;
f.write_str(&value.text.replace('\n', &format!("\n{}", ident)))?;
f.write_str("\n")?;
if !comment.replies.is_empty() {
let mut children = comment.replies.clone();
children.sort_by(|a, b| a.value.timestamp.cmp(&b.value.timestamp));
for child in children {
format(&child, f, ident_count + 4, color)?;
}
} else {
f.write_str("\n")?;
}
Ok(())
}
if !&self.vec.is_empty() {
let mut children = self.vec.clone();
children.sort_by(|a, b| b.value.like_count.cmp(&a.value.like_count));
for child in children {
format(&child, &mut f, 0, color)?
}
}
Ok(f)
}
}
|