Added new patterns.

Description:
- Added patterns for more generic command substitution.

Signed-off-by: Pavel Kirilin <win10@list.ru>
This commit is contained in:
2020-05-06 01:38:40 +04:00
parent 9d81cf5dc6
commit 496a65f60a
2 changed files with 23 additions and 5 deletions

View File

@ -3,9 +3,9 @@ use crate::CONFIG_PATH;
use std::io::{Write, Read}; use std::io::{Write, Read};
use crate::tty_stuff::{choose_pattern, choose_command, choose_episode}; use crate::tty_stuff::{choose_pattern, choose_command, choose_episode};
#[derive(Serialize, Default, Deserialize)] #[derive(Serialize, Default, Clone, Deserialize)]
pub struct Config { pub struct Config {
current_episode_count: usize, pub current_episode_count: usize,
pattern: String, pattern: String,
#[serde(default = "default_command")] #[serde(default = "default_command")]
pub command: String, pub command: String,

View File

@ -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<()> { pub fn play() -> AppResult<()> {
let conf = Config::read()?; let mut conf = Config::read()?;
let mut episode = conf.get_current_episode()?; let mut episode = conf.get_current_episode()?;
while !episode.is_empty() { while !episode.is_empty() {
let mut child = Command::new("sh") let mut child = Command::new("sh")
.arg("-c") .arg("-c")
.arg(conf.command.replace("{}", episode.as_str())) .arg(prepare_command(conf.clone())?)
.spawn()?; .spawn()?;
child.wait()?; child.wait()?;
update_episode(next_episode)?; update_episode(next_episode)?;
let conf = Config::read()?; conf = Config::read()?;
episode = conf.get_current_episode()?; episode = conf.get_current_episode()?;
} }
Ok(()) Ok(())