forked from rust-lang/rustlings
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.rs
More file actions
144 lines (122 loc) · 4.19 KB
/
Copy patheditor.rs
File metadata and controls
144 lines (122 loc) · 4.19 KB
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
use std::{
borrow::Cow,
env,
process::{Command, Stdio},
thread::{self, JoinHandle},
};
use anyhow::{Context, Result, bail};
use shlex::Shlex;
mod zellij;
fn run_cmd(cmd: &mut Command) -> Result<Vec<u8>> {
let output = cmd
.stdin(Stdio::null())
.output()
.with_context(|| format!("Failed to run the command {cmd:?}"))?;
if !output.status.success() {
bail!(
"The command {cmd:?} didn't run successfully\n\n\
stdout:\n{}\n\n\
stderr:\n{}",
str::from_utf8(&output.stdout).unwrap_or_default(),
str::from_utf8(&output.stderr).unwrap_or_default(),
);
}
Ok(output.stdout)
}
fn program_exists(program: &str) -> bool {
Command::new(program)
.arg("--version")
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.is_ok_and(|status| status.success())
}
pub enum Editor {
Cmd(Cow<'static, str>, Vec<String>),
Zellij(Option<(String, u32, usize)>),
}
impl Editor {
pub fn new(cmd: Option<String>, vs_code_term: bool) -> Result<Option<Self>> {
if vs_code_term {
for program in ["code", "codium"] {
if program_exists(program) {
return Ok(Some(Self::Cmd(Cow::Borrowed(program), Vec::new())));
}
}
}
if let Some(cmd) = cmd {
let shlex = &mut Shlex::new(&cmd);
let program = shlex.next().context("Program missing in `--edit-cmd`")?;
let args = shlex.collect();
if shlex.had_error {
bail!("Failed to parse the command in `--edit-cmd`");
}
return Ok(Some(Self::Cmd(Cow::Owned(program), args)));
}
if env::var_os("ZELLIJ").is_some() && program_exists("zellij") {
return Ok(Some(Self::Zellij(None)));
}
Ok(None)
}
pub fn open(
mut self,
exercise_ind: usize,
exercise_path: &'static str,
) -> Result<EditorJoinHandle> {
let handle = thread::Builder::new()
.spawn(move || {
match &mut self {
Editor::Cmd(program, args) => {
run_cmd(Command::new(&**program).args(args).arg(exercise_path))?;
}
Editor::Zellij(open_pane) => {
if let Some((pane_id_str, pane_id, open_exercise_ind)) = open_pane {
if *open_exercise_ind == exercise_ind {
if zellij::pane_open(*pane_id)? {
return Ok(self);
}
} else {
zellij::close_pane(pane_id_str)?;
}
}
let stdout = run_cmd(
Command::new("zellij")
.arg("action")
.arg("edit")
.arg(exercise_path),
)?;
let (pane_id_str, pane_id) = zellij::parse_pane_id(&stdout)
.context("Failed to parse the ID of the new Zellij pane")?;
*open_pane = Some((pane_id_str, pane_id, exercise_ind));
}
}
Ok(self)
})
.context("Failed to spawn a thread to open the editor")?;
Ok(EditorJoinHandle(Some(handle)))
}
pub fn close(&mut self) -> Result<()> {
match self {
Editor::Cmd(_, _) => (),
Editor::Zellij(open_pane) => {
if let Some((pane_id_str, _, _)) = open_pane.take() {
zellij::close_pane(&pane_id_str)?;
}
}
}
Ok(())
}
}
#[must_use]
#[derive(Default)]
pub struct EditorJoinHandle(Option<JoinHandle<Result<Editor>>>);
impl EditorJoinHandle {
pub fn join(self) -> Result<Option<Editor>> {
if let Some(handle) = self.0 {
let editor = handle.join().unwrap()?;
return Ok(Some(editor));
}
Ok(None)
}
}