src/config/commands.rs

137 lines · 4572 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("Open in browser"),
 80            String::from("${browser} ${url}"),
 81        )]),
 82        HashMap::from([(
 83            String::from("Download video (webm)"),
 84            String::from(
 85                "${terminal-emulator} ${youtube-downloader} -o ${download-path} ${embed-url}",
 86            ),
 87        )]),
 88        HashMap::from([(
 89            String::from("Download audio (opus)"),
 90            String::from(
 91                "${terminal-emulator} ${youtube-downloader} -o ${download-path} ${embed-url} -x",
 92            ),
 93        )]),
 94        HashMap::from([(
 95            String::from("Mode: ${provider}"),
 96            String::from("%switch-provider%"),
 97        )]),
 98    ]
 99}
100
101fn playlist_default() -> Vec<HashMap<String, String>> {
102    vec![
103        HashMap::from([(String::from("Switch view"), String::from("%switch-view%"))]),
104        HashMap::from([(
105            String::from("Play all videos"),
106            String::from("${video-player} ${all-videos}"),
107        )]),
108        HashMap::from([(
109            String::from("Play all audio"),
110            String::from("${terminal-emulator} ${video-player} ${all-videos} --no-video"),
111        )]),
112        HashMap::from([(
113            String::from("Shuffle play all audio"),
114            String::from("${terminal-emulator} ${video-player} ${all-videos} --no-video --shuffle"),
115        )]),
116        HashMap::from([(
117            String::from("Shuffle play all audio (loop)"),
118            String::from("${terminal-emulator} ${video-player} ${all-videos} --no-video --shuffle --loop-playlist=inf"),
119        )]),
120        HashMap::from([(
121            String::from("Open in browser"),
122            String::from("${browser} ${url}"),
123        )]),
124        HashMap::from([(
125            String::from("Download all video (webm)"),
126            String::from("${terminal-emulator} ${youtube-downloader} -o ${download-path} ${all-videos}")
127        )]),
128        HashMap::from([(
129            String::from("Download all audio (opus)"),
130            String::from("${terminal-emulator} ${youtube-downloader} -o ${download-path} ${all-videos} -x")
131        )]),
132        HashMap::from([(
133            String::from("Mode: ${provider}"),
134            String::from("%switch-provider%"),
135        )]),
136    ]
137}