about summary refs log tree commit diff stats
path: root/src/videos/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/videos/mod.rs57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/videos/mod.rs b/src/videos/mod.rs
new file mode 100644
index 0000000..5bf34e3
--- /dev/null
+++ b/src/videos/mod.rs
@@ -0,0 +1,57 @@
+// 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 anyhow::Result;
+use futures::{stream::FuturesUnordered, TryStreamExt};
+use nucleo_matcher::{
+    pattern::{CaseMatching, Normalization, Pattern},
+    Matcher,
+};
+
+use crate::{
+    app::App,
+    storage::video_database::{getters::get_videos, VideoStatus},
+};
+
+pub async fn query(app: &App, search_query: Option<String>) -> Result<()> {
+    let all_videos = get_videos(app, &VideoStatus::ALL, None).await?;
+
+    // turn one video to a color display, to pre-warm the hash shrinking cache
+    if let Some(val) = all_videos.get(0) {
+        val.to_color_display(app).await?;
+    }
+
+    let all_video_strings: Vec<String> = all_videos
+        .into_iter()
+        .map(|vid| vid.to_color_display_owned(app))
+        .collect::<FuturesUnordered<_>>()
+        .try_collect()
+        .await?;
+
+    if let Some(query) = search_query {
+        let mut matcher = Matcher::new(nucleo_matcher::Config::DEFAULT.match_paths());
+
+        let matches = Pattern::parse(
+            &query.replace(' ', "\\ "),
+            CaseMatching::Ignore,
+            Normalization::Smart,
+        )
+        .match_list(all_video_strings, &mut matcher);
+
+        matches
+            .iter()
+            .rev()
+            .for_each(|(val, key)| println!("{} ({})", val, key));
+    } else {
+        println!("{}", all_video_strings.join("\n"))
+    }
+
+    Ok(())
+}