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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
//! Trigger a command
use crate::data::{Direction, OriginatingPlugin};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
#[derive(Debug, Clone)]
pub enum TerminalAction {
OpenFile(OpenFilePayload),
RunCommand(RunCommand),
}
impl TerminalAction {
pub fn change_cwd(&mut self, new_cwd: PathBuf) {
match self {
TerminalAction::OpenFile(open_file_payload) => {
open_file_payload.cwd = Some(new_cwd);
},
TerminalAction::RunCommand(run_command) => {
run_command.cwd = Some(new_cwd);
},
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct OpenFilePayload {
pub path: PathBuf,
pub line_number: Option<usize>,
pub cwd: Option<PathBuf>,
pub originating_plugin: Option<OriginatingPlugin>,
}
impl Default for OpenFilePayload {
fn default() -> Self {
OpenFilePayload {
path: PathBuf::new(),
line_number: None,
cwd: None,
originating_plugin: None,
}
}
}
impl OpenFilePayload {
pub fn new(path: PathBuf, line_number: Option<usize>, cwd: Option<PathBuf>) -> Self {
OpenFilePayload {
path,
line_number,
cwd,
originating_plugin: None,
}
}
pub fn with_originating_plugin(mut self, originating_plugin: OriginatingPlugin) -> Self {
self.originating_plugin = Some(originating_plugin);
self
}
}
#[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,
#[serde(default)]
pub originating_plugin: Option<OriginatingPlugin>,
#[serde(default)]
pub use_terminal_title: 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,
#[serde(default)]
pub originating_plugin: Option<OriginatingPlugin>,
#[serde(default)]
pub use_terminal_title: 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,
originating_plugin: action.originating_plugin,
use_terminal_title: action.use_terminal_title,
}
}
}
impl From<RunCommand> for RunCommandAction {
fn from(run_command: RunCommand) -> Self {
RunCommandAction {
command: run_command.command,
args: run_command.args,
cwd: run_command.cwd,
direction: None,
hold_on_close: run_command.hold_on_close,
hold_on_start: run_command.hold_on_start,
originating_plugin: run_command.originating_plugin,
use_terminal_title: run_command.use_terminal_title,
}
}
}
impl RunCommandAction {
pub fn new(mut command: Vec<String>) -> Self {
if command.is_empty() {
Default::default()
} else {
RunCommandAction {
command: PathBuf::from(command.remove(0)),
args: command,
..Default::default()
}
}
}
pub fn populate_originating_plugin(&mut self, originating_plugin: OriginatingPlugin) {
self.originating_plugin = Some(originating_plugin);
}
}
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
}
}
|