src/config/commands.rs

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