about summary refs log tree commit diff stats
path: root/yt/src/comments
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--crates/yt/src/comments/display.rs (renamed from yt/src/comments/display.rs)1
-rw-r--r--crates/yt/src/comments/mod.rs (renamed from yt/src/comments/mod.rs)41
-rw-r--r--crates/yt/src/comments/output.rs (renamed from yt/src/comments/output.rs)25
-rw-r--r--yt/src/comments/comment.rs64
4 files changed, 38 insertions, 93 deletions
diff --git a/yt/src/comments/display.rs b/crates/yt/src/comments/display.rs
index 4d678bc..6166b2b 100644
--- a/yt/src/comments/display.rs
+++ b/crates/yt/src/comments/display.rs
@@ -1,6 +1,7 @@
 // yt - A fully featured command line YouTube client
 //
 // Copyright (C) 2024 Benedikt Peetz <benedikt.peetz@b-peetz.de>
+// Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
 // SPDX-License-Identifier: GPL-3.0-or-later
 //
 // This file is part of Yt.
diff --git a/yt/src/comments/mod.rs b/crates/yt/src/comments/mod.rs
index afc90de..54031a4 100644
--- a/yt/src/comments/mod.rs
+++ b/crates/yt/src/comments/mod.rs
@@ -1,6 +1,7 @@
 // yt - A fully featured command line YouTube client
 //
 // Copyright (C) 2024 Benedikt Peetz <benedikt.peetz@b-peetz.de>
+// Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
 // SPDX-License-Identifier: GPL-3.0-or-later
 //
 // This file is part of Yt.
@@ -10,18 +11,15 @@
 
 use std::mem;
 
-use anyhow::{bail, Context, Result};
-use comment::{CommentExt, Comments};
+use anyhow::{Result, bail};
+use comment::{Comment, CommentExt, Comments, Parent};
 use output::display_fmt_and_less;
 use regex::Regex;
-use yt_dlp::wrapper::info_json::{Comment, InfoJson, Parent};
+use yt_dlp::json_cast;
 
 use crate::{
     app::App,
-    storage::video_database::{
-        getters::{get_currently_playing_video, get_video_info_json},
-        Video,
-    },
+    storage::video_database::{Video, get},
     unreachable::Unreachable,
 };
 
@@ -29,34 +27,37 @@ mod comment;
 mod display;
 pub mod output;
 
+pub mod description;
+pub use description::*;
+
 #[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? {
+        if let Some(video) = get::currently_focused_video(app).await? {
             video
         } else {
             bail!("Could not find a currently playing video!");
         };
 
-    let mut info_json: InfoJson = get_video_info_json(&currently_playing_video)
-        .await?
-        .unreachable(
-            "A currently *playing* must be cached. And thus the info.json should be available",
-        );
+    let info_json = get::video_info_json(&currently_playing_video)?.unreachable(
+        "A currently *playing* video must be cached. And thus the info.json should be available",
+    );
 
-    let base_comments = mem::take(&mut info_json.comments).with_context(|| {
-        format!(
+    let base_comments = if let Some(comments) = info_json.get("comments") {
+        json_cast!(comments, as_array)
+    } else {
+        bail!(
             "The video ('{}') does not have comments!",
             info_json
-                .title
-                .as_ref()
-                .unwrap_or(&("<No Title>".to_owned()))
+                .get("title")
+                .map(|val| json_cast!(val, as_str))
+                .unwrap_or("<No Title>")
         )
-    })?;
-    drop(info_json);
+    };
 
     let mut comments = Comments::new();
     for c in base_comments {
+        let c: Comment = serde_json::from_value(c.to_owned())?;
         if let Parent::Id(id) = &c.parent {
             comments.insert(&(id.clone()), CommentExt::from(c));
         } else {
diff --git a/yt/src/comments/output.rs b/crates/yt/src/comments/output.rs
index 626db2a..cb3a9c4 100644
--- a/yt/src/comments/output.rs
+++ b/crates/yt/src/comments/output.rs
@@ -1,6 +1,7 @@
 // yt - A fully featured command line YouTube client
 //
 // Copyright (C) 2024 Benedikt Peetz <benedikt.peetz@b-peetz.de>
+// Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
 // SPDX-License-Identifier: GPL-3.0-or-later
 //
 // This file is part of Yt.
@@ -14,6 +15,7 @@ use std::{
 };
 
 use anyhow::{Context, Result};
+use uu_fmt::{FmtOptions, process_text};
 
 use crate::unreachable::Unreachable;
 
@@ -25,15 +27,8 @@ pub async fn display_fmt_and_less(input: String) -> Result<()> {
         .spawn()
         .context("Failed to run less")?;
 
-    let mut child = Command::new("fmt")
-        .args(["--uniform-spacing", "--split-only", "--width=90"])
-        .stdin(Stdio::piped())
-        .stderr(Stdio::inherit())
-        .stdout(less.stdin.take().unreachable("Should be open"))
-        .spawn()
-        .context("Failed to run fmt")?;
-
-    let mut stdin = child.stdin.take().context("Failed to open stdin")?;
+    let input = format_text(&input);
+    let mut stdin = less.stdin.take().context("Failed to open stdin")?;
     std::thread::spawn(move || {
         stdin
             .write_all(input.as_bytes())
@@ -44,3 +39,15 @@ pub async fn display_fmt_and_less(input: String) -> Result<()> {
 
     Ok(())
 }
+
+#[must_use]
+pub fn format_text(input: &str) -> String {
+    let width = termsize::get().map_or(90, |size| size.cols);
+    let fmt_opts = FmtOptions {
+        uniform: true,
+        split_only: true,
+        ..FmtOptions::new(Some(width as usize), None, Some(4))
+    };
+
+    process_text(input, &fmt_opts)
+}
diff --git a/yt/src/comments/comment.rs b/yt/src/comments/comment.rs
deleted file mode 100644
index c998cdb..0000000
--- a/yt/src/comments/comment.rs
+++ /dev/null
@@ -1,64 +0,0 @@
-// 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 yt_dlp::wrapper::info_json::Comment;
-
-#[derive(Debug, Clone)]
-#[allow(clippy::module_name_repetitions)]
-pub struct CommentExt {
-    pub value: Comment,
-    pub replies: Vec<CommentExt>,
-}
-
-#[derive(Debug, Default)]
-pub struct Comments {
-    pub(super) vec: Vec<CommentExt>,
-}
-
-impl Comments {
-    pub fn new() -> Self {
-        Self::default()
-    }
-    pub fn push(&mut self, value: CommentExt) {
-        self.vec.push(value);
-    }
-    pub fn get_mut(&mut self, key: &str) -> Option<&mut CommentExt> {
-        self.vec.iter_mut().filter(|c| c.value.id.id == key).last()
-    }
-    pub fn insert(&mut self, key: &str, value: CommentExt) {
-        let parent = self
-            .vec
-            .iter_mut()
-            .filter(|c| c.value.id.id == key)
-            .last()
-            .expect("One of these should exist");
-        parent.push_reply(value);
-    }
-}
-impl CommentExt {
-    pub fn push_reply(&mut self, value: CommentExt) {
-        self.replies.push(value);
-    }
-    pub fn get_mut_reply(&mut self, key: &str) -> Option<&mut CommentExt> {
-        self.replies
-            .iter_mut()
-            .filter(|c| c.value.id.id == key)
-            .last()
-    }
-}
-
-impl From<Comment> for CommentExt {
-    fn from(value: Comment) -> Self {
-        Self {
-            replies: vec![],
-            value,
-        }
-    }
-}