about summary refs log tree commit diff stats
path: root/src/select/cmds.rs
blob: c480bd923f591ea221daca79e75769cacda9a97c (plain) (blame)
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
// 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 crate::{
    app::App,
    cli::SelectCommand,
    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<i64>,
) -> Result<()> {
    fn compute_priority(line_number: Option<i64>, priority: Option<i64>) -> Option<i64> {
        if let Some(pri) = priority {
            Some(pri)
        } else if let Some(pri) = line_number {
            Some(pri)
        } else {
            None
        }
    }

    match cmd {
        SelectCommand::Pick { shared } => {
            let priority = compute_priority(line_number, shared.priority);
            set_video_status(
                app,
                &shared.hash.realize(app).await?,
                VideoStatus::Pick,
                priority,
            )
            .await?
        }
        SelectCommand::Drop { shared } => {
            let priority = compute_priority(line_number, shared.priority);
            set_video_status(
                app,
                &shared.hash.realize(app).await?,
                VideoStatus::Drop,
                priority,
            )
            .await?
        }
        SelectCommand::Watch {
            shared,
            subtitle_langs,
            speed,
        } => {
            let hash = shared.hash.realize(&app).await?;
            let video = get_video_by_hash(app, &hash).await?;
            let video_options = VideoOptions::new(
                subtitle_langs.unwrap_or(app.config.select.subtitle_langs.clone()),
                speed.unwrap_or(app.config.select.playback_speed),
            );
            let priority = compute_priority(line_number, shared.priority);

            if let Some(_) = video.cache_path {
                set_video_status(app, &hash, VideoStatus::Cached, priority).await?;
            } else {
                set_video_status(app, &hash, VideoStatus::Watch, priority).await?;
            }

            set_video_options(app, hash, &video_options).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(())
}