ad_editor/exec/
runner.rs

1use crate::system::{DefaultSystem, System};
2use std::{io, path::PathBuf};
3
4pub trait Runner {
5    fn run_shell_command(&mut self, cmd: &str, input: Option<&str>) -> io::Result<String>;
6}
7
8pub struct EditorRunner<'a, S>
9where
10    S: System,
11{
12    pub(crate) system: &'a mut S,
13    pub(crate) dir: PathBuf,
14    pub(crate) bufid: usize,
15}
16
17impl<'a, S> Runner for EditorRunner<'a, S>
18where
19    S: System,
20{
21    fn run_shell_command(&mut self, cmd: &str, input: Option<&str>) -> io::Result<String> {
22        match input {
23            Some(input) => self
24                .system
25                .pipe_through_command(cmd, input, &self.dir, self.bufid),
26            None => self.system.run_command_blocking(cmd, &self.dir, self.bufid),
27        }
28    }
29}
30
31#[derive(Debug)]
32pub struct SystemRunner {
33    system: DefaultSystem,
34    dir: PathBuf,
35}
36
37impl SystemRunner {
38    pub fn new(dir: PathBuf) -> Self {
39        Self {
40            system: DefaultSystem::without_clipboard_provider(),
41            dir,
42        }
43    }
44
45    pub fn set_dir(&mut self, dir: impl Into<PathBuf>) {
46        self.dir = dir.into();
47    }
48}
49
50impl Runner for SystemRunner {
51    fn run_shell_command(&mut self, cmd: &str, input: Option<&str>) -> io::Result<String> {
52        match input {
53            Some(input) => self.system.pipe_through_command(cmd, input, &self.dir, 0),
54            None => self.system.run_command_blocking(cmd, &self.dir, 0),
55        }
56    }
57}