Added new patterns.
Description: - Added patterns for more generic command substitution. Signed-off-by: Pavel Kirilin <win10@list.ru>
This commit is contained in:
@ -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,
|
||||
|
@ -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(())
|
||||
|
Reference in New Issue
Block a user