// yt - A fully featured command line YouTube client // // Copyright (C) 2024 Benedikt Peetz // 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 . use crate::{ app::App, cli::{SelectCommand, SharedSelectionCommandArgs}, storage::video_database::{ getters::get_video_by_hash, setters::{set_video_options, set_video_status}, VideoOptions, VideoStatus, }, }; use anyhow::{Context, Result}; pub async fn handle_select_cmd( app: &App, cmd: SelectCommand, line_number: Option, ) -> Result<()> { match cmd { SelectCommand::Pick { shared } => { handle_status_change(app, shared, line_number, VideoStatus::Pick).await?; } SelectCommand::Drop { shared } => { handle_status_change(app, shared, line_number, VideoStatus::Drop).await?; } SelectCommand::Watched { shared } => { handle_status_change(app, shared, line_number, VideoStatus::Watched).await?; } SelectCommand::Watch { shared } => { let hash = shared.hash.clone().realize(app).await?; let video = get_video_by_hash(app, &hash).await?; if video.cache_path.is_some() { handle_status_change(app, shared, line_number, VideoStatus::Cached).await?; } else { handle_status_change(app, shared, line_number, VideoStatus::Watch).await?; } } SelectCommand::Url { shared } => { let mut firefox = std::process::Command::new("firefox"); firefox.args(["-P", "timesinks.youtube"]); firefox.arg(shared.url.as_str()); let _handle = firefox.spawn().context("Failed to run firefox")?; } SelectCommand::File { .. } => unreachable!("This should have been filtered out"), } Ok(()) } async fn handle_status_change( app: &App, shared: SharedSelectionCommandArgs, line_number: Option, new_status: VideoStatus, ) -> Result<()> { let hash = shared.hash.realize(app).await?; let video_options = VideoOptions::new( shared .subtitle_langs .unwrap_or(app.config.select.subtitle_langs.clone()), shared.speed.unwrap_or(app.config.select.playback_speed), ); let priority = compute_priority(line_number, shared.priority); set_video_status(app, &hash, new_status, priority).await?; set_video_options(app, &hash, &video_options).await?; Ok(()) } fn compute_priority(line_number: Option, priority: Option) -> Option { if let Some(pri) = priority { Some(pri) } else { line_number } }