1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
//! Trigger a command
use crate::data::Direction;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
#[derive(Debug, Clone)]
pub enum TerminalAction {
OpenFile(PathBuf, Option<usize>, Option<PathBuf>), // path to file (should be absolute), optional line_number and an
// optional cwd
RunCommand(RunCommand),
}
#[derive(Clone, Debug, Deserialize, Default, Serialize, PartialEq, Eq)]
pub struct RunCommand {
#[serde(alias = "cmd")]
pub command: PathBuf,
#[serde(default)]
pub args: Vec<String>,
#[serde(default)]
pub cwd: Option<PathBuf>,
#[serde(default)]
pub hold_on_close: bool,
#[serde(default)]
pub hold_on_start: bool,
}
impl std::fmt::Display for RunCommand {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut command: String = self
.command
.as_path()
.as_os_str()
.to_string_lossy()
.to_string();
for arg in &self.args {
command.push(' ');
command.push_str(arg);
}
write!(f, "{}", command)
}
}
/// Intermediate representation
#[derive(Clone, Debug, Deserialize, Default, Serialize, PartialEq, Eq)]
pub struct RunCommandAction {
#[serde(rename = "cmd")]
pub command: PathBuf,
#[serde(default)]
pub args: Vec<String>,
#[serde(default)]
pub cwd: Option<PathBuf>,
#[serde(default)]
pub direction: Option<Direction>,
#[serde(default)]
pub hold_on_close: bool,
#[serde(default)]
pub hold_on_start: bool,
}
impl From<RunCommandAction> for RunCommand {
fn from(action: RunCommandAction) -> Self {
RunCommand {
command: action.command,
args: action.args,
cwd: action.cwd,
hold_on_close: action.hold_on_close,
hold_on_start: action.hold_on_start,
}
}
}
impl RunCommand {
pub fn new(command: PathBuf) -> Self {
RunCommand {
command,
..Default::default()
}
}
pub fn with_cwd(mut self, cwd: PathBuf) -> Self {
self.cwd = Some(cwd);
self
}
}
|