src/app/config/commands.rs
118 lines · 3625 bytes
1use serde::{Deserialize, Serialize};
2
3use crate::functions::insert_vec;
4
5use super::{Config, EnvVar};
6
7#[derive(Clone, Debug, Serialize, Deserialize)]
8pub struct CommandsConfig {
9 pub video_player: CommandConfig,
10 pub audio_player: CommandConfig,
11 pub image_viewer: CommandConfig,
12 pub video_downloader: CommandConfig,
13 pub audio_downloader: CommandConfig,
14 pub terminal: CommandConfig,
15}
16
17#[derive(Clone, Debug, Serialize, Deserialize)]
18pub struct CommandConfig {
19 pub command: String,
20 pub open_in_console: bool,
21 pub args: Vec<String>,
22}
23
24impl CommandConfig {
25 pub fn as_vec(&self) -> Vec<String> {
26 let mut args = self.args.clone();
27 args.insert(0, self.command.clone());
28 args
29 }
30
31 pub fn as_command_vec(self, env: EnvVar, config: &Config) -> Result<Vec<String>, ()> {
32 let mut out;
33 if self.open_in_console {
34 out = config.commands.terminal.as_vec();
35 let index = match out.iter().position(|x| x == "{command}") {
36 Some(index) => {
37 out.swap_remove(index);
38 index
39 }
40 None => out.len(),
41 };
42
43 insert_vec(&mut out, self.as_vec(), index);
44 } else {
45 out = self.as_vec();
46 }
47
48 for x in out.iter_mut() {
49 match x.as_str() {
50 "{url}" => {
51 if let Some(url) = &env.url {
52 *x = url.clone();
53 } else {
54 return Err(());
55 }
56 }
57
58 "{video_save_location}" => {
59 *x = config.main.yt_dl.video_path.clone();
60 }
61
62 "{audio_save_location}" => {
63 *x = config.main.yt_dl.audio_path.clone();
64 }
65
66 _ => {}
67 }
68 }
69
70 Ok(out)
71 }
72}
73
74impl Default for CommandsConfig {
75 fn default() -> Self {
76 Self {
77 video_player: CommandConfig {
78 command: String::from("mpv"),
79 open_in_console: false,
80 args: vec![String::from("--no-terminal"), String::from("{url}")],
81 },
82 audio_player: CommandConfig {
83 command: String::from("mpv"),
84 open_in_console: true,
85 args: vec![String::from("--no-video"), String::from("{url}")],
86 },
87 image_viewer: CommandConfig {
88 command: String::from("mpv"),
89 open_in_console: false,
90 args: vec![String::from("{url}"), String::from("--no-terminal")],
91 },
92 video_downloader: CommandConfig {
93 command: String::from("yt-dlp"),
94 open_in_console: true,
95 args: vec![
96 String::from("{url}"),
97 String::from("-o"),
98 String::from("{video_save_location}"),
99 ],
100 },
101 audio_downloader: CommandConfig {
102 command: String::from("yt-dlp"),
103 open_in_console: true,
104 args: vec![
105 String::from("{url}"),
106 String::from("-o"),
107 String::from("{audio_save_location}"),
108 String::from("-x"),
109 ],
110 },
111 terminal: CommandConfig {
112 command: String::from("konsole"),
113 open_in_console: false,
114 args: vec![String::from("-e"), String::from("{command}")],
115 },
116 }
117 }
118}