ayan_player_cli/
player.rs1use std::{
2 io, process::{Command, Output},
3 fmt::Display
4};
5
6use serde::{Deserialize, Serialize};
7
8use crate::config::Configs;
9
10
11
12#[derive(Debug, Serialize, Deserialize)]
13pub enum PlayerType {
14 MPV,
15 VLC,
16 Other,
17}
18
19impl Display for PlayerType {
20 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21 write!(f, "{}", match self {
22 PlayerType::MPV => "mpv",
23 PlayerType::VLC => "vlc",
24 PlayerType::Other => "other",
25 })
26 }
27}
28
29
30impl PlayerType {
31 pub fn build_command(&self, file: &str, config: &Configs) -> Command {
32 let mut cmd = Command::new(match self {
33 PlayerType::MPV => format!("mpv"),
34 PlayerType::VLC => format!("vlc"),
35 PlayerType::Other => format!("xdg-open"),
36 });
37 cmd.arg(file);
38 self.build_volume(&config, &mut cmd);
39 self.build_speed(&config, &mut cmd);
40 cmd
41 }
42
43 fn build_volume(&self, config: &Configs, command: &mut Command) {
44 match self {
45 PlayerType::MPV => command.arg(format!("--volume={}", config.get_volume())),
46 PlayerType::VLC => command.arg(format!(
47 "--volume {}",
48 (config.get_volume() / 100.0) * 256.0
49 )),
50 PlayerType::Other => command,
51 };
52 }
53 fn build_speed(&self, config: &Configs, command: &mut Command) {
54 match self {
55 PlayerType::MPV => command.arg(format!("--speed={}", config.get_speed())),
56 PlayerType::VLC => command.arg(format!("--rate {}", config.get_speed())),
57 PlayerType::Other => command,
58 };
59 }
60}
61
62pub fn get_player_from_str(config: &str) -> PlayerType {
63 let player_str = config.to_lowercase();
64 if player_str == "mpv" {
65 return PlayerType::MPV;
66 } else if player_str == "vlc" {
67 return PlayerType::VLC;
68 } else {
69 return PlayerType::Other;
70 }
71}
72
73pub fn play(file: &str, config: &Configs) -> Result<Output, io::Error> {
74 let mut cmd = config.player.build_command(file, &config);
75 println!("Command used: {cmd:?}");
76 let result = cmd.output();
77 result
78}