aboutsummaryrefslogtreecommitdiffstats
path: root/yt/src/comments
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--yt/src/comments/comment.rs3
-rw-r--r--yt/src/comments/display.rs14
-rw-r--r--yt/src/comments/mod.rs44
3 files changed, 37 insertions, 24 deletions
diff --git a/yt/src/comments/comment.rs b/yt/src/comments/comment.rs
index 752c510..c998cdb 100644
--- a/yt/src/comments/comment.rs
+++ b/yt/src/comments/comment.rs
@@ -11,6 +11,7 @@
use yt_dlp::wrapper::info_json::Comment;
#[derive(Debug, Clone)]
+#[allow(clippy::module_name_repetitions)]
pub struct CommentExt {
pub value: Comment,
pub replies: Vec<CommentExt>,
@@ -43,7 +44,7 @@ impl Comments {
}
impl CommentExt {
pub fn push_reply(&mut self, value: CommentExt) {
- self.replies.push(value)
+ self.replies.push(value);
}
pub fn get_mut_reply(&mut self, key: &str) -> Option<&mut CommentExt> {
self.replies
diff --git a/yt/src/comments/display.rs b/yt/src/comments/display.rs
index 7000063..4d678bc 100644
--- a/yt/src/comments/display.rs
+++ b/yt/src/comments/display.rs
@@ -23,8 +23,6 @@ impl Comments {
}
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 {
@@ -87,29 +85,31 @@ impl Comments {
f.write_str(":\n")?;
f.write_str(ident)?;
- f.write_str(&value.text.replace('\n', &format!("\n{}", ident)))?;
+ f.write_str(&value.text.replace('\n', &format!("\n{ident}")))?;
f.write_str("\n")?;
- if !comment.replies.is_empty() {
+ if comment.replies.is_empty() {
+ f.write_str("\n")?;
+ } else {
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(())
}
+ let mut f = String::new();
+
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)?
+ format(&child, &mut f, 0, color)?;
}
}
Ok(f)
diff --git a/yt/src/comments/mod.rs b/yt/src/comments/mod.rs
index 5fbc3fb..fd9f9da 100644
--- a/yt/src/comments/mod.rs
+++ b/yt/src/comments/mod.rs
@@ -25,12 +25,14 @@ use crate::{
getters::{get_currently_playing_video, get_video_info_json},
Video,
},
+ unreachable::Unreachable,
};
mod comment;
mod display;
-pub async fn get_comments(app: &App) -> Result<Comments> {
+#[allow(clippy::too_many_lines)]
+pub async fn get(app: &App) -> Result<Comments> {
let currently_playing_video: Video =
if let Some(video) = get_currently_playing_video(app).await? {
video
@@ -40,28 +42,38 @@ pub async fn get_comments(app: &App) -> Result<Comments> {
let mut info_json: InfoJson = get_video_info_json(&currently_playing_video)
.await?
- .expect("A currently *playing* must be cached. And thus the info.json should be available");
+ .unreachable(
+ "A currently *playing* must be cached. And thus the info.json should be available",
+ );
- let base_comments = mem::take(&mut info_json.comments).expect("A video should have comments");
+ let base_comments = mem::take(&mut info_json.comments).with_context(|| {
+ format!(
+ "The video ('{}') does not have comments!",
+ info_json
+ .title
+ .as_ref()
+ .unwrap_or(&("<No Title>".to_owned()))
+ )
+ })?;
drop(info_json);
let mut comments = Comments::new();
- base_comments.into_iter().for_each(|c| {
+ for c in base_comments {
if let Parent::Id(id) = &c.parent {
comments.insert(&(id.clone()), CommentExt::from(c));
} else {
comments.push(CommentExt::from(c));
}
- });
+ }
comments.vec.iter_mut().for_each(|comment| {
let replies = mem::take(&mut comment.replies);
let mut output_replies: Vec<CommentExt> = vec![];
- let re = Regex::new(r"\u{200b}?(@[^\t\s]+)\u{200b}?").unwrap();
+ let re = Regex::new(r"\u{200b}?(@[^\t\s]+)\u{200b}?").unreachable("This is hardcoded");
for reply in replies {
if let Some(replyee_match) = re.captures(&reply.value.text){
- let full_match = replyee_match.get(0).expect("This always exists");
+ let full_match = replyee_match.get(0).unreachable("This will always exist");
let text = reply.
value.
text[0..full_match.start()]
@@ -72,7 +84,7 @@ pub async fn get_comments(app: &App) -> Result<Comments> {
.text[full_match.end()..];
let text: &str = text.trim().trim_matches('\u{200b}');
- let replyee = replyee_match.get(1).expect("This should exist").as_str();
+ let replyee = replyee_match.get(1).unreachable("This should also exist").as_str();
if let Some(parent) = output_replies
@@ -87,7 +99,7 @@ pub async fn get_comments(app: &App) -> Result<Comments> {
parent.replies.push(CommentExt::from(Comment {
text: text.to_owned(),
..reply.value
- }))
+ }));
} else if let Some(parent) = output_replies
.iter_mut()
// .rev()
@@ -99,7 +111,7 @@ pub async fn get_comments(app: &App) -> Result<Comments> {
parent.replies.push(CommentExt::from(Comment {
text: text.to_owned(),
..reply.value
- }))
+ }));
} else if let Some(parent) = output_replies
.iter_mut()
// .rev()
@@ -110,7 +122,7 @@ pub async fn get_comments(app: &App) -> Result<Comments> {
parent.replies.push(CommentExt::from(Comment {
text: text.to_owned(),
..reply.value
- }))
+ }));
} else if let Some(parent) = output_replies.iter_mut()
// .rev()
.filter(|com| com.value.author == replyee)
@@ -119,7 +131,7 @@ pub async fn get_comments(app: &App) -> Result<Comments> {
parent.replies.push(CommentExt::from(Comment {
text: text.to_owned(),
..reply.value
- }))
+ }));
} else {
eprintln!(
"Failed to find a parent for ('{}') both directly and via replies! The reply text was:\n'{}'\n",
@@ -139,7 +151,7 @@ pub async fn get_comments(app: &App) -> Result<Comments> {
}
pub async fn comments(app: &App) -> Result<()> {
- let comments = get_comments(app).await?;
+ let comments = get(app).await?;
let mut less = Command::new("less")
.args(["--raw-control-chars"])
@@ -152,7 +164,7 @@ pub async fn comments(app: &App) -> Result<()> {
.args(["--uniform-spacing", "--split-only", "--width=90"])
.stdin(Stdio::piped())
.stderr(Stdio::inherit())
- .stdout(less.stdin.take().expect("Should be open"))
+ .stdout(less.stdin.take().unreachable("Should be open"))
.spawn()
.context("Failed to run fmt")?;
@@ -160,7 +172,7 @@ pub async fn comments(app: &App) -> Result<()> {
std::thread::spawn(move || {
stdin
.write_all(comments.render(true).as_bytes())
- .expect("Should be able to write to stdin of fmt");
+ .unreachable("Should be able to write to the stdin of less");
});
let _ = less.wait().context("Failed to await less")?;
@@ -173,6 +185,6 @@ mod test {
#[test]
fn test_string_replacement() {
let s = "A \n\nB\n\nC".to_owned();
- assert_eq!("A \n \n B\n \n C", s.replace('\n', "\n "))
+ assert_eq!("A \n \n B\n \n C", s.replace('\n', "\n "));
}
}