From 496a65f60a6630a9d02e8f1c9b019a73e5f28136 Mon Sep 17 00:00:00 2001 From: Pavel Kirilin <win10@list.ru> Date: Wed, 6 May 2020 01:38:40 +0400 Subject: [PATCH] Added new patterns. Description: - Added patterns for more generic command substitution. Signed-off-by: Pavel Kirilin <win10@list.ru> --- src/config.rs | 4 ++-- src/run_modes.rs | 24 +++++++++++++++++++++--- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/config.rs b/src/config.rs index d5c32b0..8cccdf7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,9 +3,9 @@ use crate::CONFIG_PATH; use std::io::{Write, Read}; use crate::tty_stuff::{choose_pattern, choose_command, choose_episode}; -#[derive(Serialize, Default, Deserialize)] +#[derive(Serialize, Default, Clone, Deserialize)] pub struct Config { - current_episode_count: usize, + pub current_episode_count: usize, pattern: String, #[serde(default = "default_command")] pub command: String, diff --git a/src/run_modes.rs b/src/run_modes.rs index a4acc82..a2d910a 100644 --- a/src/run_modes.rs +++ b/src/run_modes.rs @@ -48,17 +48,35 @@ pub fn next_episode(current: usize) -> AppResult<usize> { } } +fn add_leading_zero(n: usize) -> String { + if n < 10 { + format!("0{}", n) + } else { + format!("{}", n) + } +} + +fn prepare_command(conf: Config) -> AppResult<String> { + let index = conf.current_episode_count; + Ok(conf.command + .replace("{}", conf.get_current_episode()?.as_str()) + .replace("{n}", format!("{}", index).as_str()) + .replace("{n+}", format!("{}", index + 1).as_str()) + .replace("{zn}", add_leading_zero(index).as_str()) + .replace("{zn+}", add_leading_zero(index).as_str())) +} + pub fn play() -> AppResult<()> { - let conf = Config::read()?; + let mut conf = Config::read()?; let mut episode = conf.get_current_episode()?; while !episode.is_empty() { let mut child = Command::new("sh") .arg("-c") - .arg(conf.command.replace("{}", episode.as_str())) + .arg(prepare_command(conf.clone())?) .spawn()?; child.wait()?; update_episode(next_episode)?; - let conf = Config::read()?; + conf = Config::read()?; episode = conf.get_current_episode()?; } Ok(()) -- GitLab