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
|
// yt - A fully featured command line YouTube client
//
// Copyright (C) 2025 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::path::Path;
use crate::{
ansi_escape_codes::{cursor_up, erase_in_display_from_cursor},
app::App,
storage::video_database::{Video, VideoStatus, get, notify::wait_for_db_write},
};
use anyhow::Result;
use futures::{TryStreamExt, stream::FuturesOrdered};
/// Extract the values of the [`VideoStatus::Cached`] value from a Video.
fn cache_values(video: &Video) -> (&Path, bool) {
if let VideoStatus::Cached {
cache_path,
is_focused,
} = &video.status
{
(cache_path, *is_focused)
} else {
unreachable!("All of these videos should be cached");
}
}
/// # Panics
/// Only if internal assertions fail.
pub async fn playlist(app: &App, watch: bool) -> Result<()> {
let mut previous_output_length = 0;
loop {
let playlist = get::playlist(app).await?.to_videos();
let output = playlist
.into_iter()
.map(|video| async move {
let mut output = String::new();
let (_, is_focused) = cache_values(&video);
if is_focused {
output.push_str("🔻 ");
} else {
output.push_str(" ");
}
output.push_str(&video.title_fmt(app));
output.push_str(" (");
output.push_str(&video.parent_subscription_name_fmt(app));
output.push(')');
output.push_str(" [");
output.push_str(&video.duration_fmt(app));
if is_focused {
output.push_str(" (");
output.push_str(&if let Some(duration) = video.duration.as_secs() {
format!("{:0.0}%", (video.watch_progress.as_secs() / duration) * 100)
} else {
video.watch_progress_fmt(app)
});
output.push(')');
}
output.push(']');
output.push('\n');
Ok::<String, anyhow::Error>(output)
})
.collect::<FuturesOrdered<_>>()
.try_collect::<String>()
.await?;
// Delete the previous output
cursor_up(previous_output_length);
erase_in_display_from_cursor();
previous_output_length = output.chars().filter(|ch| *ch == '\n').count();
print!("{output}");
if !watch {
break;
}
wait_for_db_write(app).await?;
}
Ok(())
}
|