Replace the Podcasts placeholder with subscriptions, seasons and episodes
The tab opens the FRID podcast store (podcasts.sqlite3, migrating a prototype database in place; newer or foreign files are reported and left unchanged) off the UI thread and shows subscriptions with unplayed counts and check status, podcast -> seasons -> episodes navigation, and episodes with saved position, played state, duration and date. Episode search, played filter and order run as paged store queries with request sequence numbers. Adding a feed, checking one or all feeds, marking played and unsubscribing (with a confirmation that progress is kept) go through Effect/AppEvent; a scheduler refreshes due feeds in the background. New actions are bound in the podcasts keymap context and listed in a Podcasts help section. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
07dcd0587c
commit
99c4d05dec
16 files changed
+2957
-63
No files matched your search
+94
-14
@@ -134,33 +134,113 @@ mod tests {
|
||||
assert_eq!((cell.fg, cell.bg), (highlight.fg, highlight.bg));
|
||||
}
|
||||
|
||||
use crate::app::podcast_fixtures;
|
||||
|
||||
#[test]
|
||||
fn podcasts_tab_shows_placeholder() {
|
||||
fn podcasts_tab_reports_opening_and_failed_store() {
|
||||
let mut state = AppState::default();
|
||||
state.active_tab = Tab::Podcasts;
|
||||
let screen = render(&state, 100, 12);
|
||||
assert!(screen.contains(podcasts::UNAVAILABLE), "{screen}");
|
||||
}
|
||||
assert!(screen.contains("opening the podcast database"), "{screen}");
|
||||
|
||||
#[test]
|
||||
fn podcasts_placeholder_wraps_without_losing_text_on_narrow_terminals() {
|
||||
let mut state = AppState::default();
|
||||
state.active_tab = Tab::Podcasts;
|
||||
for width in [60, 40] {
|
||||
let screen = render(&state, width, 14);
|
||||
state.podcasts.store = crate::app::state::PodcastStoreStatus::Failed(
|
||||
"podcast database schema v9 is newer than this Furumi supports".into(),
|
||||
);
|
||||
for width in [100, 50] {
|
||||
let screen = render(&state, width, 16);
|
||||
let words: Vec<&str> = screen
|
||||
.split(|c: char| c.is_whitespace() || c == '│')
|
||||
.filter(|word| !word.is_empty())
|
||||
.collect();
|
||||
for word in ["available", "progress", "appear", "here."] {
|
||||
assert!(
|
||||
words.contains(&word),
|
||||
"{word} missing at {width}:\n{screen}"
|
||||
);
|
||||
for word in ["unavailable", "v9", "newer", "unchanged."] {
|
||||
assert!(words.contains(&word), "{word} at {width}:\n{screen}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn podcasts_list_shows_actions_counts_and_errors() {
|
||||
let state = podcast_fixtures::ready_state(Vec::new());
|
||||
let screen = render(&state, 100, 14);
|
||||
assert!(screen.contains("+ Add podcast (RSS feed URL)"), "{screen}");
|
||||
assert!(screen.contains("Check all podcasts"), "{screen}");
|
||||
assert!(screen.contains("No subscriptions yet"), "{screen}");
|
||||
|
||||
let mut row = podcast_fixtures::row("Test Show", 2, 3, 0);
|
||||
row.fetch.last_error = Some("HTTP 503 Service Unavailable".into());
|
||||
row.fetch.consecutive_failures = 2;
|
||||
let mut state = podcast_fixtures::ready_state(vec![row]);
|
||||
state.podcasts.selected = crate::app::state::PODCAST_ACTION_ROWS;
|
||||
// Wide: side panel with details; narrow: status below the list.
|
||||
for width in [110, 60] {
|
||||
let screen = render(&state, width, 30);
|
||||
assert!(screen.contains("! Test Show"), "{screen}");
|
||||
assert!(screen.contains("2 unplayed · 3 ep"), "{screen}");
|
||||
assert!(screen.contains("last check failed"), "{screen}");
|
||||
assert!(screen.contains("503"), "{screen}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn podcast_seasons_and_episodes_render_progress() {
|
||||
use music_dht::podcast::ProgressStatus;
|
||||
|
||||
let mut state =
|
||||
podcast_fixtures::ready_state(vec![podcast_fixtures::row("Test Show", 1, 5, 1)]);
|
||||
state.podcasts.opened = Some(crate::app::state::OpenedPodcast {
|
||||
podcast_id: podcast_fixtures::podcast_id(),
|
||||
season_cursor: Some(0),
|
||||
season: None,
|
||||
episode_cursor: 0,
|
||||
});
|
||||
state.podcasts.seasons = Some((
|
||||
podcast_fixtures::podcast_id(),
|
||||
Loadable::Ready(podcast_fixtures::seasons()),
|
||||
));
|
||||
let screen = render(&state, 100, 14);
|
||||
assert!(screen.contains("All episodes"), "{screen}");
|
||||
assert!(screen.contains("1 unplayed · 5 ep"), "{screen}");
|
||||
assert!(screen.contains("Season 1"), "{screen}");
|
||||
assert!(screen.contains("2021–2022"), "{screen}");
|
||||
assert!(screen.contains("Without a season"), "{screen}");
|
||||
|
||||
let state = podcast_fixtures::with_episodes(
|
||||
podcast_fixtures::ready_state(vec![podcast_fixtures::row("Test Show", 1, 3, 0)]),
|
||||
vec![
|
||||
podcast_fixtures::episode(1, "Pilot", Some((ProgressStatus::Completed, 0))),
|
||||
podcast_fixtures::episode(2, "Second", Some((ProgressStatus::InProgress, 754_000))),
|
||||
podcast_fixtures::episode(3, "Third", None),
|
||||
],
|
||||
);
|
||||
let screen = render(&state, 110, 30);
|
||||
assert!(
|
||||
screen.contains("3 episodes · all episodes · newest first"),
|
||||
"{screen}"
|
||||
);
|
||||
assert!(screen.contains("✓ 2024-06-15 S1E1 Pilot"), "{screen}");
|
||||
assert!(screen.contains("played · 45:00"), "{screen}");
|
||||
assert!(screen.contains("◐ 2024-06-15 S1E2 Second"), "{screen}");
|
||||
assert!(screen.contains("12:34 / 45:00"), "{screen}");
|
||||
assert!(screen.contains("● 2024-06-15 S1E3 Third"), "{screen}");
|
||||
assert!(screen.contains("Notes for Pilot."), "{screen}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn podcast_unsubscribe_confirmation_says_progress_is_kept() {
|
||||
let mut state =
|
||||
podcast_fixtures::ready_state(vec![podcast_fixtures::row("Test Show", 0, 1, 0)]);
|
||||
state.popup = Some(crate::app::state::Popup::ConfirmPodcastUnsubscribe {
|
||||
podcast_id: podcast_fixtures::podcast_id(),
|
||||
title: "Test Show".into(),
|
||||
});
|
||||
let screen = render(&state, 100, 24);
|
||||
assert!(
|
||||
screen.contains("Unsubscribe from \"Test Show\"?"),
|
||||
"{screen}"
|
||||
);
|
||||
assert!(screen.contains("progress and history are kept"), "{screen}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn help_lists_tab_names_for_digit_shortcuts() {
|
||||
let mut state = AppState::default();
|
||||
|
||||
+728
-39
@@ -1,58 +1,747 @@
|
||||
//! Podcasts tab: subscriptions with their check status, a podcast's
|
||||
//! seasons, and its episodes with saved position, played state, duration
|
||||
//! and date. Rendering only reads `AppState`.
|
||||
|
||||
use furumi_library::podcasts::{EpisodeListItem, SeasonSummary};
|
||||
use music_dht::podcast::ProgressStatus;
|
||||
use ratatui::Frame;
|
||||
use ratatui::layout::{Alignment, Rect};
|
||||
use ratatui::text::Line;
|
||||
use ratatui::widgets::{Block, Paragraph, Wrap};
|
||||
use ratatui::layout::{Alignment, Constraint, Layout, Rect};
|
||||
use ratatui::style::{Color, Modifier, Style};
|
||||
use ratatui::text::{Line, Span};
|
||||
use ratatui::widgets::{Block, Borders, Paragraph, Wrap};
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
||||
use super::theme;
|
||||
use crate::app::state::AppState;
|
||||
use super::{loading_line, theme};
|
||||
use crate::app::state::{
|
||||
ART_HEADER_HEIGHT, ART_HEADER_WIDTH, AppState, ArtState, Loadable, PodcastListRow, PodcastRow,
|
||||
PodcastStoreStatus, SeasonRow,
|
||||
};
|
||||
use crate::app::update::podcasts::{played_label, sort_label};
|
||||
|
||||
/// Placeholder notice shown until podcast support lands: the tab already
|
||||
/// holds its final position so the numeric shortcuts stay stable.
|
||||
pub(crate) const UNAVAILABLE: &str = "Podcasts are not available in this build yet.";
|
||||
/// Width below which the side panel with the cover is dropped.
|
||||
const SIDE_PANEL_MIN_TOTAL_WIDTH: u16 = 80;
|
||||
const SIDE_PANEL_WIDTH: u16 = ART_HEADER_WIDTH + 2;
|
||||
|
||||
pub(crate) const EMPTY_HINT: &str =
|
||||
"No subscriptions yet. Choose \"Add podcast\" and paste an RSS feed URL.";
|
||||
pub(crate) const STORE_FAILED_NOTE: &str = "The podcast database was left unchanged. \
|
||||
Music, playlists and sync keep working.";
|
||||
|
||||
pub fn draw(frame: &mut Frame, area: Rect, state: &AppState) {
|
||||
let tab = &state.podcasts;
|
||||
match &tab.store {
|
||||
PodcastStoreStatus::Opening => {
|
||||
let inner = bordered(frame, area, state, " Podcasts ".into());
|
||||
return centered_line(
|
||||
frame,
|
||||
inner,
|
||||
loading_line(state, "opening the podcast database…"),
|
||||
);
|
||||
}
|
||||
PodcastStoreStatus::Failed(err) => {
|
||||
let inner = bordered(frame, area, state, " Podcasts ".into());
|
||||
let lines = vec![
|
||||
Line::styled("Podcasts are unavailable", theme::header_for(state)),
|
||||
Line::default(),
|
||||
Line::styled(err.clone(), error_style()),
|
||||
Line::default(),
|
||||
Line::styled(STORE_FAILED_NOTE, theme::dim()),
|
||||
];
|
||||
frame.render_widget(
|
||||
Paragraph::new(lines)
|
||||
.alignment(Alignment::Center)
|
||||
.wrap(Wrap { trim: true }),
|
||||
inner,
|
||||
);
|
||||
return;
|
||||
}
|
||||
PodcastStoreStatus::Ready => {}
|
||||
}
|
||||
match &tab.opened {
|
||||
None => draw_list(frame, area, state),
|
||||
Some(opened) if opened.season.is_none() => draw_seasons(frame, area, state),
|
||||
Some(_) => draw_episodes(frame, area, state),
|
||||
}
|
||||
}
|
||||
|
||||
fn bordered(frame: &mut Frame, area: Rect, state: &AppState, title: String) -> Rect {
|
||||
let block = Block::bordered()
|
||||
.title(" Podcasts ")
|
||||
.title(title)
|
||||
.title_style(theme::header_for(state))
|
||||
.border_style(theme::border_for(state));
|
||||
let inner = block.inner(area);
|
||||
frame.render_widget(block, area);
|
||||
if inner.height == 0 {
|
||||
inner
|
||||
}
|
||||
|
||||
fn centered_line(frame: &mut Frame, area: Rect, line: Line) {
|
||||
if area.height == 0 {
|
||||
return;
|
||||
}
|
||||
let middle = Rect {
|
||||
y: area.y + area.height / 2,
|
||||
height: 1,
|
||||
..area
|
||||
};
|
||||
frame.render_widget(Paragraph::new(line).alignment(Alignment::Center), middle);
|
||||
}
|
||||
|
||||
let lines = vec![
|
||||
Line::styled(UNAVAILABLE, theme::header_for(state)),
|
||||
Line::default(),
|
||||
Line::styled(
|
||||
"Subscriptions, episodes and listening progress will appear here.",
|
||||
theme::dim(),
|
||||
),
|
||||
];
|
||||
// Center on the estimated wrapped height, but let the paragraph use every
|
||||
// row down to the bottom so narrow terminals never cut off the text.
|
||||
let wrapped = estimated_rows(&lines, inner.width).min(inner.height);
|
||||
let top = inner.height.saturating_sub(wrapped) / 2;
|
||||
let body = Rect {
|
||||
y: inner.y + top,
|
||||
height: inner.height - top,
|
||||
fn error_style() -> Style {
|
||||
Style::new().fg(Color::Red)
|
||||
}
|
||||
|
||||
/// First row so that `cursor` stays visible, roughly centered.
|
||||
fn first_visible(cursor: usize, len: usize, visible: usize) -> usize {
|
||||
cursor
|
||||
.saturating_sub(visible / 2)
|
||||
.min(len.saturating_sub(visible))
|
||||
}
|
||||
|
||||
fn row_area(area: Rect, offset: usize) -> Rect {
|
||||
Rect {
|
||||
y: area.y + offset as u16,
|
||||
height: 1,
|
||||
..area
|
||||
}
|
||||
}
|
||||
|
||||
/// A row with a left part and a right-aligned dim part.
|
||||
fn draw_row(
|
||||
frame: &mut Frame,
|
||||
area: Rect,
|
||||
state: &AppState,
|
||||
left: Line<'static>,
|
||||
right: Option<String>,
|
||||
selected: bool,
|
||||
) {
|
||||
frame.render_widget(Paragraph::new(left), area);
|
||||
if let Some(right) = right {
|
||||
frame.render_widget(
|
||||
Paragraph::new(Line::styled(format!("{right} "), theme::dim()))
|
||||
.alignment(Alignment::Right),
|
||||
area,
|
||||
);
|
||||
}
|
||||
if selected {
|
||||
frame
|
||||
.buffer_mut()
|
||||
.set_style(area, theme::tab_active_for(state));
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Subscriptions
|
||||
|
||||
fn draw_list(frame: &mut Frame, area: Rect, state: &AppState) {
|
||||
let tab = &state.podcasts;
|
||||
let inner = bordered(frame, area, state, " Podcasts ".to_string());
|
||||
match &tab.list {
|
||||
Some(Loadable::Failed(error)) => {
|
||||
return centered_line(frame, inner, Line::styled(error.clone(), error_style()));
|
||||
}
|
||||
None | Some(Loadable::Loading) => {
|
||||
return centered_line(frame, inner, loading_line(state, "loading podcasts…"));
|
||||
}
|
||||
Some(Loadable::Ready(_)) => {}
|
||||
}
|
||||
let (list_area, side_area) = split_side_panel(inner);
|
||||
if let Some(side) = side_area {
|
||||
draw_side_panel(frame, side, state, tab.current_podcast());
|
||||
}
|
||||
let rows = tab.list_rows();
|
||||
let visible = usize::from(list_area.height.max(1));
|
||||
let first = first_visible(tab.selected, rows.len(), visible);
|
||||
for (index, row) in rows.iter().enumerate().skip(first).take(visible) {
|
||||
let (left, right) = list_row(state, *row, list_area.width);
|
||||
draw_row(
|
||||
frame,
|
||||
row_area(list_area, index - first),
|
||||
state,
|
||||
left,
|
||||
right,
|
||||
index == tab.selected,
|
||||
);
|
||||
}
|
||||
let used = rows.len().saturating_sub(first).min(visible) as u16;
|
||||
let below = Rect {
|
||||
y: list_area.y + used + 1,
|
||||
height: list_area.height.saturating_sub(used + 1),
|
||||
..list_area
|
||||
};
|
||||
if below.height == 0 {
|
||||
return;
|
||||
}
|
||||
let hint = if tab.podcasts().is_empty() {
|
||||
Line::styled(EMPTY_HINT, theme::dim())
|
||||
} else if side_area.is_none()
|
||||
&& let Some(row) = tab.current_podcast()
|
||||
{
|
||||
// Narrow terminal: the selected podcast's status below the list.
|
||||
fetch_status_line(state, row)
|
||||
} else {
|
||||
Line::default()
|
||||
};
|
||||
frame.render_widget(Paragraph::new(hint).wrap(Wrap { trim: true }), below);
|
||||
}
|
||||
|
||||
fn list_row(state: &AppState, row: PodcastListRow, width: u16) -> (Line<'static>, Option<String>) {
|
||||
let tab = &state.podcasts;
|
||||
match row {
|
||||
PodcastListRow::Add => {
|
||||
let label = if tab.subscribing {
|
||||
format!("{} Adding podcast…", state.spinner())
|
||||
} else {
|
||||
"+ Add podcast (RSS feed URL)…".to_string()
|
||||
};
|
||||
(Line::styled(label, theme::accent_for(state)), None)
|
||||
}
|
||||
PodcastListRow::RefreshAll => {
|
||||
let label = if tab.refreshing.is_empty() {
|
||||
"↻ Check all podcasts for new episodes".to_string()
|
||||
} else {
|
||||
format!(
|
||||
"{} Checking {} feed(s) for new episodes…",
|
||||
state.spinner(),
|
||||
tab.refreshing.len()
|
||||
)
|
||||
};
|
||||
(Line::styled(label, theme::accent_for(state)), None)
|
||||
}
|
||||
PodcastListRow::Podcast(index) => {
|
||||
let Some(podcast) = tab.podcasts().get(index) else {
|
||||
return (Line::raw(""), None);
|
||||
};
|
||||
let marker = if tab.refreshing.contains(podcast.id()) {
|
||||
Span::styled(format!("{} ", state.spinner()), theme::accent_for(state))
|
||||
} else if podcast.fetch.last_error.is_some() {
|
||||
Span::styled("! ", error_style())
|
||||
} else {
|
||||
Span::raw(" ")
|
||||
};
|
||||
let summary = &podcast.summary;
|
||||
let meta = if summary.unplayed_count > 0 {
|
||||
format!(
|
||||
"{} unplayed · {} ep",
|
||||
summary.unplayed_count, summary.episode_count
|
||||
)
|
||||
} else {
|
||||
format!("{} ep", summary.episode_count)
|
||||
};
|
||||
let title_width = usize::from(width).saturating_sub(2 + meta.width() + 2);
|
||||
let left = Line::from(vec![
|
||||
marker,
|
||||
Span::raw(fit_width(podcast.title(), title_width)),
|
||||
]);
|
||||
(left, Some(meta))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Main area plus an optional right-hand panel for the cover and details.
|
||||
fn split_side_panel(area: Rect) -> (Rect, Option<Rect>) {
|
||||
if area.width < SIDE_PANEL_MIN_TOTAL_WIDTH {
|
||||
return (area, None);
|
||||
}
|
||||
let [main, side] =
|
||||
Layout::horizontal([Constraint::Min(0), Constraint::Length(SIDE_PANEL_WIDTH)]).areas(area);
|
||||
(main, Some(side))
|
||||
}
|
||||
|
||||
fn draw_side_panel(frame: &mut Frame, area: Rect, state: &AppState, podcast: Option<&PodcastRow>) {
|
||||
let block = Block::default()
|
||||
.borders(Borders::LEFT)
|
||||
.border_style(theme::border_for(state));
|
||||
let inner = block.inner(area);
|
||||
frame.render_widget(block, area);
|
||||
let Some(podcast) = podcast else {
|
||||
return;
|
||||
};
|
||||
let inner = Rect {
|
||||
x: inner.x + 1,
|
||||
width: inner.width.saturating_sub(1),
|
||||
..inner
|
||||
};
|
||||
frame.render_widget(
|
||||
Paragraph::new(lines)
|
||||
.alignment(Alignment::Center)
|
||||
.wrap(Wrap { trim: true }),
|
||||
body,
|
||||
let art_height = ART_HEADER_HEIGHT.min(inner.height);
|
||||
draw_cover(
|
||||
frame,
|
||||
Rect {
|
||||
height: art_height,
|
||||
width: ART_HEADER_WIDTH.min(inner.width),
|
||||
..inner
|
||||
},
|
||||
state,
|
||||
podcast,
|
||||
);
|
||||
let text_area = Rect {
|
||||
y: inner.y + art_height + 1,
|
||||
height: inner.height.saturating_sub(art_height + 1),
|
||||
..inner
|
||||
};
|
||||
if text_area.height == 0 {
|
||||
return;
|
||||
}
|
||||
let summary = &podcast.summary;
|
||||
let mut lines = vec![Line::styled(
|
||||
podcast.title().to_string(),
|
||||
Style::new().add_modifier(Modifier::BOLD),
|
||||
)];
|
||||
if let Some(author) = &summary.meta.author {
|
||||
lines.push(Line::styled(author.clone(), theme::dim()));
|
||||
}
|
||||
let mut counts = format!("{} episodes", summary.episode_count);
|
||||
if summary.season_count > 0 {
|
||||
counts.push_str(&format!(" · {} seasons", summary.season_count));
|
||||
}
|
||||
lines.push(Line::styled(counts, theme::dim()));
|
||||
lines.push(Line::styled("private", theme::dim()));
|
||||
lines.push(fetch_status_line(state, podcast));
|
||||
if let Some(description) = &summary.meta.description {
|
||||
lines.push(Line::raw(""));
|
||||
lines.push(Line::raw(description.clone()));
|
||||
}
|
||||
frame.render_widget(Paragraph::new(lines).wrap(Wrap { trim: true }), text_area);
|
||||
}
|
||||
|
||||
/// Refresh status of one podcast: checking, the last error, or when it was
|
||||
/// last checked.
|
||||
fn fetch_status_line(state: &AppState, podcast: &PodcastRow) -> Line<'static> {
|
||||
if state.podcasts.refreshing.contains(podcast.id()) {
|
||||
return loading_line(state, "checking for new episodes…");
|
||||
}
|
||||
let fetch = &podcast.fetch;
|
||||
if let Some(error) = &fetch.last_error {
|
||||
let mut text = format!("last check failed: {error}");
|
||||
if fetch.consecutive_failures > 1 {
|
||||
text.push_str(&format!(" ({} times)", fetch.consecutive_failures));
|
||||
}
|
||||
if let Some(next) = fetch.next_check_at_ms {
|
||||
text.push_str(&format!("; next try {}", relative_time(next)));
|
||||
}
|
||||
return Line::styled(text, error_style());
|
||||
}
|
||||
match fetch.last_success_at_ms {
|
||||
Some(at) => Line::styled(format!("checked {}", relative_time(at)), theme::dim()),
|
||||
None => Line::styled("not checked yet", theme::dim()),
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_cover(frame: &mut Frame, area: Rect, state: &AppState, podcast: &PodcastRow) {
|
||||
let art = podcast.fetch.cover_path.as_ref().and_then(|path| {
|
||||
state.art.get(&crate::art::cache_key(
|
||||
path,
|
||||
ART_HEADER_WIDTH,
|
||||
ART_HEADER_HEIGHT,
|
||||
))
|
||||
});
|
||||
match art {
|
||||
Some(ArtState::Ready(image)) => {
|
||||
frame.render_widget(Paragraph::new(super::art::to_text(image)), area);
|
||||
}
|
||||
Some(ArtState::Loading) => centered_line(frame, area, Line::styled("…", theme::dim())),
|
||||
_ => centered_line(frame, area, Line::styled("🎙", theme::dim())),
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Seasons
|
||||
|
||||
fn draw_seasons(frame: &mut Frame, area: Rect, state: &AppState) {
|
||||
let tab = &state.podcasts;
|
||||
let podcast = tab.current_podcast();
|
||||
let title = podcast.map_or("…".to_string(), |row| row.title().to_string());
|
||||
let inner = bordered(frame, area, state, format!(" Podcasts ▸ {title} "));
|
||||
let seasons = match &tab.seasons {
|
||||
Some((_, Loadable::Failed(error))) => {
|
||||
return centered_line(frame, inner, Line::styled(error.clone(), error_style()));
|
||||
}
|
||||
_ => match tab.opened_seasons() {
|
||||
Some(seasons) => seasons,
|
||||
None => return centered_line(frame, inner, loading_line(state, "loading seasons…")),
|
||||
},
|
||||
};
|
||||
let (main, side) = split_side_panel(inner);
|
||||
if let Some(side) = side {
|
||||
draw_side_panel(frame, side, state, podcast);
|
||||
}
|
||||
let cursor = tab
|
||||
.opened
|
||||
.as_ref()
|
||||
.and_then(|opened| opened.season_cursor)
|
||||
.unwrap_or(0);
|
||||
let rows = tab.season_rows();
|
||||
let visible = usize::from(main.height.max(1));
|
||||
let first = first_visible(cursor, rows.len(), visible);
|
||||
for (index, row) in rows.iter().enumerate().skip(first).take(visible) {
|
||||
let (label, meta) = match row {
|
||||
SeasonRow::All => {
|
||||
let (episodes, unplayed) = seasons.iter().fold((0, 0), |(e, u), season| {
|
||||
(e + season.episode_count, u + season.unplayed_count)
|
||||
});
|
||||
("All episodes".to_string(), counts(episodes, unplayed))
|
||||
}
|
||||
SeasonRow::Group(index) => {
|
||||
let Some(season) = seasons.get(*index) else {
|
||||
continue;
|
||||
};
|
||||
(season_title(season), season_meta(season))
|
||||
}
|
||||
};
|
||||
let width = usize::from(main.width).saturating_sub(meta.width() + 4);
|
||||
draw_row(
|
||||
frame,
|
||||
row_area(main, index - first),
|
||||
state,
|
||||
Line::raw(format!(" {}", fit_width(&label, width))),
|
||||
Some(meta),
|
||||
index == cursor,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn season_title(season: &SeasonSummary) -> String {
|
||||
match season.season {
|
||||
Some(number) => format!("Season {number}"),
|
||||
None => "Without a season".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
fn counts(episodes: u64, unplayed: u64) -> String {
|
||||
if unplayed > 0 {
|
||||
format!("{unplayed} unplayed · {episodes} ep")
|
||||
} else {
|
||||
format!("{episodes} ep")
|
||||
}
|
||||
}
|
||||
|
||||
fn season_meta(season: &SeasonSummary) -> String {
|
||||
let mut meta = counts(season.episode_count, season.unplayed_count);
|
||||
let years = (
|
||||
season.first_published_at_ms.map(year_of),
|
||||
season.last_published_at_ms.map(year_of),
|
||||
);
|
||||
match years {
|
||||
(Some(first), Some(last)) if first != last => meta.push_str(&format!(" · {first}–{last}")),
|
||||
(Some(year), _) | (None, Some(year)) => meta.push_str(&format!(" · {year}")),
|
||||
(None, None) => {}
|
||||
}
|
||||
meta
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Episodes
|
||||
|
||||
fn draw_episodes(frame: &mut Frame, area: Rect, state: &AppState) {
|
||||
let tab = &state.podcasts;
|
||||
let Some(opened) = &tab.opened else {
|
||||
return;
|
||||
};
|
||||
let podcast = tab.current_podcast();
|
||||
let mut title = podcast.map_or("…".to_string(), |row| row.title().to_string());
|
||||
if opened.season_cursor.is_some() {
|
||||
let season = match opened.season {
|
||||
Some(furumi_library::podcasts::SeasonFilter::Season(number)) => {
|
||||
format!("Season {number}")
|
||||
}
|
||||
Some(furumi_library::podcasts::SeasonFilter::NoSeason) => {
|
||||
"Without a season".to_string()
|
||||
}
|
||||
_ => "All episodes".to_string(),
|
||||
};
|
||||
title = format!("{title} ▸ {season}");
|
||||
}
|
||||
let inner = bordered(frame, area, state, format!(" Podcasts ▸ {title} "));
|
||||
let Some(list) = tab.opened_episodes() else {
|
||||
return centered_line(frame, inner, loading_line(state, "loading episodes…"));
|
||||
};
|
||||
|
||||
let (main, side) = split_side_panel(inner);
|
||||
if let Some(side) = side {
|
||||
draw_side_panel(frame, side, state, podcast);
|
||||
}
|
||||
let notes_height = if main.height >= 18 { 6 } else { 0 };
|
||||
let [summary_area, list_area, notes_area] = Layout::vertical([
|
||||
Constraint::Length(1),
|
||||
Constraint::Min(1),
|
||||
Constraint::Length(notes_height),
|
||||
])
|
||||
.areas(main);
|
||||
|
||||
let mut summary = vec![Span::styled(
|
||||
episode_summary(state, list.items.len(), list.complete),
|
||||
theme::dim(),
|
||||
)];
|
||||
if list.loading || list.loading_more {
|
||||
summary.push(Span::raw(" "));
|
||||
summary.extend(loading_line(state, "loading…").spans);
|
||||
} else if podcast.is_some_and(|row| tab.refreshing.contains(row.id())) {
|
||||
summary.push(Span::raw(" "));
|
||||
summary.extend(loading_line(state, "checking for new episodes…").spans);
|
||||
}
|
||||
frame.render_widget(Paragraph::new(Line::from(summary)), summary_area);
|
||||
|
||||
if let Some(error) = &list.error {
|
||||
return centered_line(frame, list_area, Line::styled(error.clone(), error_style()));
|
||||
}
|
||||
if list.items.is_empty() {
|
||||
if list.loading {
|
||||
return;
|
||||
}
|
||||
let message = if tab.search.trim().is_empty()
|
||||
&& tab.played == furumi_library::podcasts::PlayedFilter::Any
|
||||
{
|
||||
"this feed has no playable episodes yet"
|
||||
} else {
|
||||
"no episode matches the search or filter"
|
||||
};
|
||||
return centered_line(frame, list_area, Line::styled(message, theme::dim()));
|
||||
}
|
||||
|
||||
let cursor = opened.episode_cursor.min(list.items.len() - 1);
|
||||
let visible = usize::from(list_area.height.max(1));
|
||||
let first = first_visible(cursor, list.items.len(), visible);
|
||||
for (index, item) in list.items.iter().enumerate().skip(first).take(visible) {
|
||||
draw_episode_row(
|
||||
frame,
|
||||
row_area(list_area, index - first),
|
||||
state,
|
||||
item,
|
||||
index == cursor,
|
||||
);
|
||||
}
|
||||
if notes_height > 0
|
||||
&& let Some(item) = list.items.get(cursor)
|
||||
{
|
||||
draw_notes(frame, notes_area, state, item);
|
||||
}
|
||||
}
|
||||
|
||||
fn episode_summary(state: &AppState, loaded: usize, complete: bool) -> String {
|
||||
let tab = &state.podcasts;
|
||||
let count = if complete {
|
||||
format!("{loaded} episodes")
|
||||
} else {
|
||||
format!("{loaded}+ episodes")
|
||||
};
|
||||
let mut parts = vec![
|
||||
count,
|
||||
played_label(tab.played).to_string(),
|
||||
sort_label(tab.sort).to_string(),
|
||||
];
|
||||
let search = tab.search.trim();
|
||||
if !search.is_empty() {
|
||||
parts.push(format!("search: \"{search}\""));
|
||||
}
|
||||
parts.join(" · ")
|
||||
}
|
||||
|
||||
/// Marker and right-hand text for an episode's listening state.
|
||||
pub(crate) fn episode_state(item: &EpisodeListItem) -> (&'static str, String) {
|
||||
let duration = item.episode.duration_ms.filter(|ms| *ms > 0);
|
||||
match item.status() {
|
||||
ProgressStatus::Completed => (
|
||||
"✓",
|
||||
match duration {
|
||||
Some(ms) => format!("played · {}", format_duration_ms(ms)),
|
||||
None => "played".to_string(),
|
||||
},
|
||||
),
|
||||
ProgressStatus::InProgress => {
|
||||
let position = format_duration_ms(item.resume_position_ms());
|
||||
(
|
||||
"◐",
|
||||
match duration {
|
||||
Some(ms) => format!("{position} / {}", format_duration_ms(ms)),
|
||||
None => format!("at {position}"),
|
||||
},
|
||||
)
|
||||
}
|
||||
ProgressStatus::NotStarted => ("●", duration.map(format_duration_ms).unwrap_or_default()),
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_episode_row(
|
||||
frame: &mut Frame,
|
||||
area: Rect,
|
||||
state: &AppState,
|
||||
item: &EpisodeListItem,
|
||||
selected: bool,
|
||||
) {
|
||||
let episode = &item.episode;
|
||||
let date = episode
|
||||
.published_at_ms
|
||||
.map(|ms| format_date(ms.div_euclid(1000)))
|
||||
.unwrap_or_else(|| " ".repeat(10));
|
||||
let (marker, right) = episode_state(item);
|
||||
let marker_style = match item.status() {
|
||||
ProgressStatus::NotStarted => theme::accent_for(state),
|
||||
_ => theme::dim(),
|
||||
};
|
||||
let number = match (episode.season, episode.episode_number) {
|
||||
(Some(season), Some(number)) => format!("S{season}E{number} "),
|
||||
(None, Some(number)) => format!("#{number} "),
|
||||
_ => String::new(),
|
||||
};
|
||||
let mut title = episode.title.clone();
|
||||
if !item.in_feed {
|
||||
title.push_str(" (no longer in the feed)");
|
||||
}
|
||||
let title_width =
|
||||
usize::from(area.width).saturating_sub(2 + 10 + 2 + number.width() + right.width() + 2);
|
||||
let title_style = match item.status() {
|
||||
ProgressStatus::Completed => theme::dim(),
|
||||
_ => Style::default(),
|
||||
};
|
||||
let line = Line::from(vec![
|
||||
Span::styled(format!("{marker} "), marker_style),
|
||||
Span::styled(date, theme::dim()),
|
||||
Span::raw(" "),
|
||||
Span::styled(number, theme::dim()),
|
||||
Span::styled(fit_width(&title, title_width), title_style),
|
||||
]);
|
||||
draw_row(
|
||||
frame,
|
||||
area,
|
||||
state,
|
||||
line,
|
||||
(!right.is_empty()).then_some(right),
|
||||
selected,
|
||||
);
|
||||
}
|
||||
|
||||
/// Rows the lines need when wrapped to `width` (a lower bound: word wrapping
|
||||
/// may need one more row per line, which the paragraph area leaves room for).
|
||||
fn estimated_rows(lines: &[Line], width: u16) -> u16 {
|
||||
let width = usize::from(width.max(1));
|
||||
let rows: usize = lines
|
||||
.iter()
|
||||
.map(|line| line.width().div_ceil(width).max(1))
|
||||
.sum();
|
||||
u16::try_from(rows).unwrap_or(u16::MAX)
|
||||
fn draw_notes(frame: &mut Frame, area: Rect, state: &AppState, item: &EpisodeListItem) {
|
||||
let block = Block::default()
|
||||
.borders(Borders::TOP)
|
||||
.border_style(theme::border_for(state))
|
||||
.title(Span::styled(" Episode notes ", theme::dim()));
|
||||
let inner = block.inner(area);
|
||||
frame.render_widget(block, area);
|
||||
let mut text = item
|
||||
.episode
|
||||
.description
|
||||
.clone()
|
||||
.unwrap_or_else(|| "No description in the feed.".to_string());
|
||||
if item.completed_count > 1 {
|
||||
text = format!("Listened {} times. {text}", item.completed_count);
|
||||
}
|
||||
frame.render_widget(
|
||||
Paragraph::new(Line::raw(text)).wrap(Wrap { trim: true }),
|
||||
inner,
|
||||
);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Formatting
|
||||
|
||||
/// Truncate to a display width, adding an ellipsis when cut.
|
||||
pub(crate) fn fit_width(text: &str, width: usize) -> String {
|
||||
if UnicodeWidthStr::width(text) <= width {
|
||||
return text.to_string();
|
||||
}
|
||||
if width == 0 {
|
||||
return String::new();
|
||||
}
|
||||
let mut out = String::new();
|
||||
let mut used = 0;
|
||||
for ch in text.chars() {
|
||||
let w = unicode_width::UnicodeWidthChar::width(ch).unwrap_or(0);
|
||||
if used + w + 1 > width {
|
||||
break;
|
||||
}
|
||||
out.push(ch);
|
||||
used += w;
|
||||
}
|
||||
out.push('…');
|
||||
out
|
||||
}
|
||||
|
||||
pub(crate) fn format_duration_ms(ms: i64) -> String {
|
||||
let total = (ms.max(0) + 500) / 1000;
|
||||
let (hours, minutes, secs) = (total / 3600, total / 60 % 60, total % 60);
|
||||
if hours > 0 {
|
||||
format!("{hours}:{minutes:02}:{secs:02}")
|
||||
} else {
|
||||
format!("{minutes}:{secs:02}")
|
||||
}
|
||||
}
|
||||
|
||||
/// `YYYY-MM-DD` (UTC) for unix seconds: Howard Hinnant's days-to-civil.
|
||||
pub(crate) fn format_date(unix_seconds: i64) -> String {
|
||||
let (year, month, day) = civil(unix_seconds);
|
||||
format!("{year:04}-{month:02}-{day:02}")
|
||||
}
|
||||
|
||||
fn year_of(unix_ms: i64) -> i64 {
|
||||
civil(unix_ms.div_euclid(1000)).0
|
||||
}
|
||||
|
||||
fn civil(unix_seconds: i64) -> (i64, i64, i64) {
|
||||
let days = unix_seconds.div_euclid(86_400);
|
||||
let z = days + 719_468;
|
||||
let era = z.div_euclid(146_097);
|
||||
let doe = z.rem_euclid(146_097);
|
||||
let yoe = (doe - doe / 1_460 + doe / 36_524 - doe / 146_096) / 365;
|
||||
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
|
||||
let mp = (5 * doy + 2) / 153;
|
||||
let day = doy - (153 * mp + 2) / 5 + 1;
|
||||
let month = if mp < 10 { mp + 3 } else { mp - 9 };
|
||||
let year = yoe + era * 400 + i64::from(month <= 2);
|
||||
(year, month, day)
|
||||
}
|
||||
|
||||
/// "5 min ago", "in 2 h", or a date for anything further away.
|
||||
fn relative_time(at_ms: i64) -> String {
|
||||
let now = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map(|d| d.as_millis() as i64)
|
||||
.unwrap_or(0);
|
||||
relative_to(at_ms, now)
|
||||
}
|
||||
|
||||
pub(crate) fn relative_to(at_ms: i64, now_ms: i64) -> String {
|
||||
let delta = (now_ms - at_ms) / 1000;
|
||||
let (amount, future) = (delta.unsigned_abs(), delta < 0);
|
||||
let text = match amount {
|
||||
0..60 => {
|
||||
return if future {
|
||||
"soon".into()
|
||||
} else {
|
||||
"just now".into()
|
||||
};
|
||||
}
|
||||
60..3_600 => format!("{} min", amount / 60),
|
||||
3_600..86_400 => format!("{} h", amount / 3_600),
|
||||
_ => return format_date(at_ms.div_euclid(1000)),
|
||||
};
|
||||
if future {
|
||||
format!("in {text}")
|
||||
} else {
|
||||
format!("{text} ago")
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn formats_dates_durations_and_relative_times() {
|
||||
assert_eq!(format_date(0), "1970-01-01");
|
||||
assert_eq!(format_date(951_782_400), "2000-02-29");
|
||||
assert_eq!(format_date(-86_400), "1969-12-31");
|
||||
assert_eq!(format_duration_ms(59_600), "1:00");
|
||||
assert_eq!(format_duration_ms(754_000), "12:34");
|
||||
assert_eq!(format_duration_ms(3_723_000), "1:02:03");
|
||||
let now = 1_700_000_000_000;
|
||||
assert_eq!(relative_to(now - 30_000, now), "just now");
|
||||
assert_eq!(relative_to(now - 5 * 60_000, now), "5 min ago");
|
||||
assert_eq!(relative_to(now + 2 * 3_600_000, now), "in 2 h");
|
||||
assert_eq!(relative_to(now - 3 * 86_400_000, now), "2023-11-11");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fits_display_width_with_ellipsis() {
|
||||
assert_eq!(fit_width("Выпуск 650", 20), "Выпуск 650");
|
||||
assert_eq!(fit_width("Выпуск 650", 5), "Выпу…");
|
||||
assert_eq!(fit_width("abc", 0), "");
|
||||
}
|
||||
}
|
||||
+55
-3
@@ -25,6 +25,9 @@ pub fn draw(frame: &mut Frame, state: &AppState) {
|
||||
..
|
||||
}) => draw_edit(frame, state, title, fields, *focus, error.as_deref()),
|
||||
Some(Popup::ConfirmDelete { label, .. }) => draw_confirm_delete(frame, state, label),
|
||||
Some(Popup::ConfirmPodcastUnsubscribe { title, .. }) => {
|
||||
draw_confirm_podcast_unsubscribe(frame, state, title)
|
||||
}
|
||||
Some(Popup::SimilarityPrivacyConsent { .. }) => {
|
||||
draw_similarity_privacy_consent(frame, state)
|
||||
}
|
||||
@@ -43,7 +46,12 @@ pub fn draw(frame: &mut Frame, state: &AppState) {
|
||||
}) => draw_track_artists(frame, state, tracks, *cursor, *selected),
|
||||
Some(Popup::LogDetail(entry)) => draw_log_detail(frame, state, entry),
|
||||
Some(Popup::FedInput { field, input }) => {
|
||||
draw_fed_input(frame, state, field.title(), field.help(), input)
|
||||
let keys = if *field == crate::app::state::FedInputField::PodcastSearch {
|
||||
"enter: keep · esc: clear"
|
||||
} else {
|
||||
"enter: apply · esc: cancel"
|
||||
};
|
||||
draw_fed_input(frame, state, field.title(), field.help(), keys, input)
|
||||
}
|
||||
Some(Popup::FedText { title, text }) => draw_fed_text(frame, state, title, text),
|
||||
Some(Popup::FedCopyText {
|
||||
@@ -891,6 +899,7 @@ fn draw_fed_input(
|
||||
state: &AppState,
|
||||
title: &str,
|
||||
help: &str,
|
||||
keys: &str,
|
||||
input: &crate::app::input::LineEdit,
|
||||
) {
|
||||
let area = centered(frame.area(), 72, 8);
|
||||
@@ -916,8 +925,7 @@ fn draw_fed_input(
|
||||
let spans = super::line_edit_spans(input, usize::from(entry_area.width.saturating_sub(1)));
|
||||
frame.render_widget(Paragraph::new(Line::from(spans)), entry_area);
|
||||
frame.render_widget(
|
||||
Paragraph::new(Line::styled("enter: apply · esc: cancel", theme::dim()))
|
||||
.alignment(Alignment::Center),
|
||||
Paragraph::new(Line::styled(keys.to_string(), theme::dim())).alignment(Alignment::Center),
|
||||
hint_area,
|
||||
);
|
||||
}
|
||||
@@ -1266,6 +1274,50 @@ fn draw_confirm_delete(frame: &mut Frame, state: &AppState, label: &str) {
|
||||
);
|
||||
}
|
||||
|
||||
/// Text of the unsubscribe confirmation: unsubscribing keeps progress and
|
||||
/// listening history, so subscribing again continues where the user left off.
|
||||
pub(crate) const PODCAST_UNSUBSCRIBE_NOTE: &str = "Listening progress and history are kept: \
|
||||
subscribe again later to continue where you left off.";
|
||||
|
||||
fn draw_confirm_podcast_unsubscribe(frame: &mut Frame, state: &AppState, title: &str) {
|
||||
let width = 66.min(frame.area().width.saturating_sub(4)).max(30);
|
||||
let area = centered(frame.area(), width, 9);
|
||||
let block = Block::bordered()
|
||||
.title(" Unsubscribe? ")
|
||||
.title_style(theme::header_for(state))
|
||||
.border_style(theme::strong_border_for(state));
|
||||
let inner = block.inner(area);
|
||||
frame.render_widget(Clear, area);
|
||||
frame.render_widget(block, area);
|
||||
|
||||
let [question, note, footer] = Layout::vertical([
|
||||
Constraint::Length(2),
|
||||
Constraint::Min(1),
|
||||
Constraint::Length(1),
|
||||
])
|
||||
.areas(inner);
|
||||
frame.render_widget(
|
||||
Paragraph::new(format!("Unsubscribe from \"{title}\"?"))
|
||||
.wrap(Wrap { trim: false })
|
||||
.alignment(Alignment::Center),
|
||||
question,
|
||||
);
|
||||
frame.render_widget(
|
||||
Paragraph::new(Line::styled(PODCAST_UNSUBSCRIBE_NOTE, theme::dim()))
|
||||
.wrap(Wrap { trim: true })
|
||||
.alignment(Alignment::Center),
|
||||
note,
|
||||
);
|
||||
frame.render_widget(
|
||||
Paragraph::new(Line::styled(
|
||||
"enter/y unsubscribe · esc/n cancel",
|
||||
theme::dim(),
|
||||
))
|
||||
.alignment(Alignment::Center),
|
||||
footer,
|
||||
);
|
||||
}
|
||||
|
||||
fn draw_log_detail(frame: &mut Frame, state: &AppState, entry: &crate::config::logging::LogEntry) {
|
||||
let width = 90.min(frame.area().width.saturating_sub(4)).max(40);
|
||||
let height = 18.min(frame.area().height.saturating_sub(2)).max(7);
|
||||
|
||||
Reference in new issue
Block a user