zellij_utils/input/
command.rs1use crate::data::{Direction, OriginatingPlugin};
3use serde::{Deserialize, Serialize};
4use std::path::PathBuf;
5
6#[derive(Debug, Clone)]
7pub enum TerminalAction {
8 OpenFile(OpenFilePayload),
9 RunCommand(RunCommand),
10}
11
12impl TerminalAction {
13 pub fn change_cwd(&mut self, new_cwd: PathBuf) {
14 match self {
15 TerminalAction::OpenFile(open_file_payload) => {
16 open_file_payload.cwd = Some(new_cwd);
17 },
18 TerminalAction::RunCommand(run_command) => {
19 run_command.cwd = Some(new_cwd);
20 },
21 }
22 }
23}
24
25#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
26pub struct OpenFilePayload {
27 pub path: PathBuf,
28 pub line_number: Option<usize>,
29 pub cwd: Option<PathBuf>,
30 pub originating_plugin: Option<OriginatingPlugin>,
31}
32
33impl Default for OpenFilePayload {
34 fn default() -> Self {
35 OpenFilePayload {
36 path: PathBuf::new(),
37 line_number: None,
38 cwd: None,
39 originating_plugin: None,
40 }
41 }
42}
43
44impl OpenFilePayload {
45 pub fn new(path: PathBuf, line_number: Option<usize>, cwd: Option<PathBuf>) -> Self {
46 OpenFilePayload {
47 path,
48 line_number,
49 cwd,
50 originating_plugin: None,
51 }
52 }
53 pub fn with_originating_plugin(mut self, originating_plugin: OriginatingPlugin) -> Self {
54 self.originating_plugin = Some(originating_plugin);
55 self
56 }
57}
58
59#[derive(Clone, Debug, Deserialize, Default, Serialize, PartialEq, Eq)]
60pub struct RunCommand {
61 #[serde(alias = "cmd")]
62 pub command: PathBuf,
63 #[serde(default)]
64 pub args: Vec<String>,
65 #[serde(default)]
66 pub cwd: Option<PathBuf>,
67 #[serde(default)]
68 pub hold_on_close: bool,
69 #[serde(default)]
70 pub hold_on_start: bool,
71 #[serde(default)]
72 pub originating_plugin: Option<OriginatingPlugin>,
73 #[serde(default)]
74 pub use_terminal_title: bool,
75}
76
77impl std::fmt::Display for RunCommand {
78 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
79 let mut command: String = self
80 .command
81 .as_path()
82 .as_os_str()
83 .to_string_lossy()
84 .to_string();
85 for arg in &self.args {
86 command.push(' ');
87 command.push_str(arg);
88 }
89 write!(f, "{}", command)
90 }
91}
92
93#[derive(Clone, Debug, Deserialize, Default, Serialize, PartialEq, Eq)]
95pub struct RunCommandAction {
96 #[serde(rename = "cmd")]
97 pub command: PathBuf,
98 #[serde(default)]
99 pub args: Vec<String>,
100 #[serde(default)]
101 pub cwd: Option<PathBuf>,
102 #[serde(default)]
103 pub direction: Option<Direction>,
104 #[serde(default)]
105 pub hold_on_close: bool,
106 #[serde(default)]
107 pub hold_on_start: bool,
108 #[serde(default)]
109 pub originating_plugin: Option<OriginatingPlugin>,
110 #[serde(default)]
111 pub use_terminal_title: bool,
112}
113
114impl From<RunCommandAction> for RunCommand {
115 fn from(action: RunCommandAction) -> Self {
116 RunCommand {
117 command: action.command,
118 args: action.args,
119 cwd: action.cwd,
120 hold_on_close: action.hold_on_close,
121 hold_on_start: action.hold_on_start,
122 originating_plugin: action.originating_plugin,
123 use_terminal_title: action.use_terminal_title,
124 }
125 }
126}
127
128impl From<RunCommand> for RunCommandAction {
129 fn from(run_command: RunCommand) -> Self {
130 RunCommandAction {
131 command: run_command.command,
132 args: run_command.args,
133 cwd: run_command.cwd,
134 direction: None,
135 hold_on_close: run_command.hold_on_close,
136 hold_on_start: run_command.hold_on_start,
137 originating_plugin: run_command.originating_plugin,
138 use_terminal_title: run_command.use_terminal_title,
139 }
140 }
141}
142
143impl RunCommandAction {
144 pub fn new(mut command: Vec<String>) -> Self {
145 if command.is_empty() {
146 Default::default()
147 } else {
148 RunCommandAction {
149 command: PathBuf::from(command.remove(0)),
150 args: command,
151 ..Default::default()
152 }
153 }
154 }
155 pub fn populate_originating_plugin(&mut self, originating_plugin: OriginatingPlugin) {
156 self.originating_plugin = Some(originating_plugin);
157 }
158}
159
160impl RunCommand {
161 pub fn new(command: PathBuf) -> Self {
162 RunCommand {
163 command,
164 ..Default::default()
165 }
166 }
167 pub fn with_cwd(mut self, cwd: PathBuf) -> Self {
168 self.cwd = Some(cwd);
169 self
170 }
171}