Initial commit: Emby TUI client with libmpv playback
A ratatui-based terminal browser for an Emby server with mpv as the player
backend. Authenticates via /Users/AuthenticateByName, lists libraries and
items, streams via /Videos/{id}/stream, and reports playback progress so
"Continue Watching" stays in sync across clients.
Credentials are read in priority order: CLI flags, env vars
(EMBY_SERVER/EMBY_USER/EMBY_PASSWORD), ./config.toml or
$XDG_CONFIG_HOME/emby-client/config.toml, then an in-TUI login prompt with
an option to persist to config.toml.
Modules: config (CLI + TOML loader), login (TUI prompt), emby (HTTP client
+ progress Reporter), player (libmpv2 wrapper, blocks until file ends),
app (browse state, Continue Watching virtual entry), ui (ratatui frontend).
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
This commit is contained in:
commit
af43b21f5d
11 files changed
+3712
No files matched your search
+261
@@ -0,0 +1,261 @@
|
||||
use anyhow::Result;
|
||||
use ratatui::widgets::ListState;
|
||||
|
||||
use crate::emby::{Client, Item};
|
||||
use crate::player::Player;
|
||||
|
||||
pub const RESUME_VIRTUAL_ID: &str = "__resume__";
|
||||
pub const RESUME_TITLE: &str = "Continue Watching";
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||
pub enum View {
|
||||
Libraries,
|
||||
Items,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Action {
|
||||
Play {
|
||||
id: String,
|
||||
/// Display title (e.g. "Series — S01E02 — Episode Title").
|
||||
title: String,
|
||||
/// Resume position from server-side UserData, in Emby ticks (100 ns units).
|
||||
start_ticks: i64,
|
||||
},
|
||||
}
|
||||
|
||||
pub struct App {
|
||||
pub client: Client,
|
||||
pub view: View,
|
||||
pub libraries: Vec<Item>,
|
||||
pub resume: Vec<Item>,
|
||||
/// Libraries view list = optional virtual "Continue Watching" entry + libraries.
|
||||
pub home_entries: Vec<Item>,
|
||||
pub items: Vec<Item>,
|
||||
pub list_state: ListState,
|
||||
pub current_title: Option<String>,
|
||||
pub breadcrumbs: Vec<(String, String)>, // (id, name)
|
||||
pub status: String,
|
||||
}
|
||||
|
||||
impl App {
|
||||
pub async fn new(client: Client) -> Result<Self> {
|
||||
let libraries = client.views().await?;
|
||||
// Best-effort: don't fail startup if the resume endpoint hiccups.
|
||||
let resume = client.resume_items(20).await.unwrap_or_default();
|
||||
|
||||
let home_entries = build_home_entries(&libraries, &resume);
|
||||
|
||||
let mut list_state = ListState::default();
|
||||
if !home_entries.is_empty() {
|
||||
list_state.select(Some(0));
|
||||
}
|
||||
let status = format!(
|
||||
"Connected as {} · {} libraries · {} in progress",
|
||||
client.user_name,
|
||||
libraries.len(),
|
||||
resume.len()
|
||||
);
|
||||
Ok(Self {
|
||||
client,
|
||||
view: View::Libraries,
|
||||
libraries,
|
||||
resume,
|
||||
home_entries,
|
||||
items: vec![],
|
||||
list_state,
|
||||
current_title: None,
|
||||
breadcrumbs: vec![],
|
||||
status,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn current_list(&self) -> &[Item] {
|
||||
match self.view {
|
||||
View::Libraries => &self.home_entries,
|
||||
View::Items => &self.items,
|
||||
}
|
||||
}
|
||||
|
||||
/// Refresh the Continue Watching list (after playback ends, for example).
|
||||
pub async fn refresh_resume(&mut self) {
|
||||
if let Ok(resume) = self.client.resume_items(20).await {
|
||||
self.resume = resume;
|
||||
self.home_entries = build_home_entries(&self.libraries, &self.resume);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn move_down(&mut self) {
|
||||
let len = self.current_list().len();
|
||||
if len == 0 {
|
||||
return;
|
||||
}
|
||||
let i = self.list_state.selected().unwrap_or(0);
|
||||
self.list_state.select(Some((i + 1).min(len - 1)));
|
||||
}
|
||||
|
||||
pub fn move_up(&mut self) {
|
||||
let i = self.list_state.selected().unwrap_or(0);
|
||||
self.list_state.select(Some(i.saturating_sub(1)));
|
||||
}
|
||||
|
||||
pub fn page_down(&mut self) {
|
||||
let len = self.current_list().len();
|
||||
if len == 0 {
|
||||
return;
|
||||
}
|
||||
let i = self.list_state.selected().unwrap_or(0);
|
||||
self.list_state.select(Some((i + 10).min(len - 1)));
|
||||
}
|
||||
|
||||
pub fn page_up(&mut self) {
|
||||
let i = self.list_state.selected().unwrap_or(0);
|
||||
self.list_state.select(Some(i.saturating_sub(10)));
|
||||
}
|
||||
|
||||
pub async fn activate(&mut self) -> Result<Option<Action>> {
|
||||
let Some(i) = self.list_state.selected() else {
|
||||
return Ok(None);
|
||||
};
|
||||
match self.view {
|
||||
View::Libraries => {
|
||||
let Some(entry) = self.home_entries.get(i).cloned() else {
|
||||
return Ok(None);
|
||||
};
|
||||
if entry.id == RESUME_VIRTUAL_ID {
|
||||
self.items = self.resume.clone();
|
||||
self.view = View::Items;
|
||||
self.breadcrumbs.clear();
|
||||
self.breadcrumbs
|
||||
.push((RESUME_VIRTUAL_ID.into(), RESUME_TITLE.into()));
|
||||
self.current_title = Some(RESUME_TITLE.into());
|
||||
self.list_state
|
||||
.select(if self.items.is_empty() { None } else { Some(0) });
|
||||
self.status = format!("{}: {} items", RESUME_TITLE, self.items.len());
|
||||
return Ok(None);
|
||||
}
|
||||
self.status = format!("Loading {}...", entry.name);
|
||||
self.items = self.client.items(&entry.id).await?;
|
||||
self.view = View::Items;
|
||||
self.breadcrumbs.clear();
|
||||
self.breadcrumbs
|
||||
.push((entry.id.clone(), entry.name.clone()));
|
||||
self.current_title = Some(entry.name.clone());
|
||||
self.list_state
|
||||
.select(if self.items.is_empty() { None } else { Some(0) });
|
||||
self.status = format!("{}: {} items", entry.name, self.items.len());
|
||||
Ok(None)
|
||||
}
|
||||
View::Items => {
|
||||
let Some(item) = self.items.get(i).cloned() else {
|
||||
return Ok(None);
|
||||
};
|
||||
if item.is_folder {
|
||||
self.status = format!("Loading {}...", item.name);
|
||||
self.items = self.client.items(&item.id).await?;
|
||||
self.breadcrumbs.push((item.id.clone(), item.name.clone()));
|
||||
self.current_title = Some(item.name.clone());
|
||||
self.list_state
|
||||
.select(if self.items.is_empty() { None } else { Some(0) });
|
||||
self.status = format!("{}: {} items", item.name, self.items.len());
|
||||
Ok(None)
|
||||
} else {
|
||||
let start_ticks = item
|
||||
.user_data
|
||||
.as_ref()
|
||||
.map(|ud| ud.position_ticks)
|
||||
.unwrap_or(0);
|
||||
let title = item.display_title();
|
||||
Ok(Some(Action::Play {
|
||||
id: item.id,
|
||||
title,
|
||||
start_ticks,
|
||||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn back(&mut self) -> Result<()> {
|
||||
if self.view != View::Items {
|
||||
return Ok(());
|
||||
}
|
||||
// Pop the current breadcrumb (current folder)
|
||||
self.breadcrumbs.pop();
|
||||
if let Some((parent_id, parent_name)) = self.breadcrumbs.last().cloned() {
|
||||
if parent_id == RESUME_VIRTUAL_ID {
|
||||
self.items = self.resume.clone();
|
||||
} else {
|
||||
self.items = self.client.items(&parent_id).await?;
|
||||
}
|
||||
self.current_title = Some(parent_name.clone());
|
||||
self.list_state
|
||||
.select(if self.items.is_empty() { None } else { Some(0) });
|
||||
self.status = format!("{}: {} items", parent_name, self.items.len());
|
||||
} else {
|
||||
self.view = View::Libraries;
|
||||
self.current_title = None;
|
||||
self.items.clear();
|
||||
self.list_state.select(if self.home_entries.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(0)
|
||||
});
|
||||
self.status = format!(
|
||||
"{} libraries · {} in progress",
|
||||
self.libraries.len(),
|
||||
self.resume.len()
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn play(&mut self, action: &Action) -> Result<()> {
|
||||
match action {
|
||||
Action::Play {
|
||||
id,
|
||||
title,
|
||||
start_ticks,
|
||||
} => {
|
||||
let url = self.client.stream_url(id);
|
||||
self.status = format!("Playing {title}...");
|
||||
let start_seconds = (*start_ticks as f64) / 10_000_000.0;
|
||||
let reporter = self.client.reporter(id.clone());
|
||||
let mut player = Player::new()?;
|
||||
let r = player.play_blocking(&url, title, start_seconds, Some(&reporter));
|
||||
self.status = match &r {
|
||||
Ok(_) => format!("Finished: {title}"),
|
||||
Err(e) => format!("Playback error: {e}"),
|
||||
};
|
||||
r
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn breadcrumb_path(&self) -> String {
|
||||
if self.breadcrumbs.is_empty() {
|
||||
"Libraries".into()
|
||||
} else {
|
||||
self.breadcrumbs
|
||||
.iter()
|
||||
.map(|(_, n)| n.as_str())
|
||||
.collect::<Vec<_>>()
|
||||
.join(" / ")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn build_home_entries(libraries: &[Item], resume: &[Item]) -> Vec<Item> {
|
||||
let mut entries: Vec<Item> = Vec::with_capacity(libraries.len() + 1);
|
||||
if !resume.is_empty() {
|
||||
entries.push(Item {
|
||||
id: RESUME_VIRTUAL_ID.into(),
|
||||
name: format!("{RESUME_TITLE} ({})", resume.len()),
|
||||
item_type: "Collection".into(),
|
||||
is_folder: true,
|
||||
..Item::default()
|
||||
});
|
||||
}
|
||||
entries.extend(libraries.iter().cloned());
|
||||
entries
|
||||
}
|
||||
Reference in new issue
Block a user