src/config/commands.rs
154 lines · 5222 bytes
1use std::collections::HashMap;
2
3use serde::{Deserialize, Serialize};
4use typemap::Key;
5
6use crate::global::traits::ConfigTrait;
7
8/// Stores combinations of label and commands
9#[derive(Clone)]
10pub struct CommandsConfig {
11 pub launch_command: String,
12 pub video: Vec<(String, String)>,
13 pub playlist: Vec<(String, String)>,
14}
15
16impl Key for CommandsConfig {
17 type Value = Self;
18}
19
20impl From<CommandsConfigSerde> for CommandsConfig {
21 fn from(original: CommandsConfigSerde) -> Self {
22 Self {
23 launch_command: original.launch_command,
24 video: original
25 .video
26 .into_iter()
27 .map(|hashmap| hashmap.into_iter().last().unwrap())
28 .collect(),
29 playlist: original
30 .playlist
31 .into_iter()
32 .map(|hashmap| hashmap.into_iter().last().unwrap())
33 .collect(),
34 }
35 }
36}
37
38/// Hashmaps are better formatted in YAML, impls `Into<CommandsConfig>`
39// uses vector to keep the ordering of the commands, and hashmap to have that key - value pair look
40#[derive(Serialize, Deserialize, Clone)]
41pub struct CommandsConfigSerde {
42 #[serde(default = "launch_command_default")]
43 pub launch_command: String,
44 #[serde(default = "video_default")]
45 pub video: Vec<HashMap<String, String>>,
46 #[serde(default = "playlist_default")]
47 pub playlist: Vec<HashMap<String, String>>,
48}
49
50impl ConfigTrait for CommandsConfigSerde {
51 const LABEL: &'static str = "commands";
52}
53
54impl Default for CommandsConfigSerde {
55 fn default() -> Self {
56 Self {
57 launch_command: launch_command_default(),
58 video: video_default(),
59 playlist: playlist_default(),
60 }
61 }
62}
63
64// `${label}` will be replaced by the values set in `main.yml` in `env`
65// Different pages may contain different `env`s (for example `url` is different in each page)
66
67fn launch_command_default() -> String {
68 String::from("loadpage popular ;; flush ;; history clear")
69}
70
71fn video_default() -> Vec<HashMap<String, String>> {
72 vec![
73 HashMap::from([(
74 String::from("Play video"),
75 String::from("run ${video-player} '${embed-url}'"),
76 )]),
77 HashMap::from([(
78 String::from("Play audio"),
79 String::from("run ${terminal-emulator} ${video-player} '${embed-url}' --no-video"),
80 )]),
81 HashMap::from([(
82 String::from("Play audio (loop)"),
83 String::from(
84 "run ${terminal-emulator} ${video-player} '${embed-url}' --no-video --loop-file=inf",
85 ),
86 )]),
87 HashMap::from([(
88 String::from("View channel"),
89 String::from("channel ${channel-id}"),
90 )]),
91 HashMap::from([(
92 String::from("Open in browser"),
93 String::from("run ${browser} '${url}'"),
94 )]),
95 HashMap::from([(
96 String::from("Download video (webm)"),
97 String::from(
98 "run ${terminal-emulator} ${youtube-downloader} -o ${download-path} '${embed-url}'",
99 ),
100 )]),
101 HashMap::from([(
102 String::from("Download audio (opus)"),
103 String::from(
104 "run ${terminal-emulator} ${youtube-downloader} -o ${download-path} '${embed-url}' -x",
105 ),
106 )]),
107 HashMap::from([(
108 String::from("Mode: ${provider}"),
109 String::from("switchprovider"),
110 )]),
111 ]
112}
113
114fn playlist_default() -> Vec<HashMap<String, String>> {
115 vec![
116 HashMap::from([(String::from("Switch view"), String::from("%switch-view%"))]),
117 HashMap::from([(
118 String::from("Play all videos"),
119 String::from("run ${video-player} ${all-videos}"),
120 )]),
121 HashMap::from([(
122 String::from("Play all audio"),
123 String::from("run ${terminal-emulator} ${video-player} ${all-videos} --no-video"),
124 )]),
125 HashMap::from([(
126 String::from("Shuffle play all audio"),
127 String::from("run ${terminal-emulator} ${video-player} ${all-videos} --no-video --shuffle"),
128 )]),
129 HashMap::from([(
130 String::from("Shuffle play all audio (loop)"),
131 String::from("run ${terminal-emulator} ${video-player} ${all-videos} --no-video --shuffle --loop-playlist=inf"),
132 )]),
133 HashMap::from([(
134 String::from("View channel"),
135 String::from("channel ${channel-id}"),
136 )]),
137 HashMap::from([(
138 String::from("Open in browser"),
139 String::from("run ${browser} '${url}'"),
140 )]),
141 HashMap::from([(
142 String::from("Download all video (webm)"),
143 String::from("run ${terminal-emulator} ${youtube-downloader} -o ${download-path} ${all-videos}")
144 )]),
145 HashMap::from([(
146 String::from("Download all audio (opus)"),
147 String::from("run ${terminal-emulator} ${youtube-downloader} -o ${download-path} ${all-videos} -x")
148 )]),
149 HashMap::from([(
150 String::from("Mode: ${provider}"),
151 String::from("switchprovider"),
152 )]),
153 ]
154}