mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-05-15 09:48:45 +00:00
Simplify Editor::open
This commit is contained in:
parent
b0dc014040
commit
bc0b4e9f9a
@ -58,49 +58,45 @@ impl Editor {
|
||||
}
|
||||
|
||||
pub fn open(
|
||||
self,
|
||||
mut self,
|
||||
exercise_ind: usize,
|
||||
exercise_path: &'static str,
|
||||
) -> Result<EditorJoinHandle> {
|
||||
let handle = thread::Builder::new()
|
||||
.spawn(move || match self {
|
||||
Editor::VSCode => {
|
||||
run_cmd(Command::new("code").arg(exercise_path))?;
|
||||
|
||||
Ok(Self::VSCode)
|
||||
}
|
||||
Editor::Cmd(program, args) => {
|
||||
run_cmd(Command::new(&program).args(&args).arg(exercise_path))?;
|
||||
|
||||
Ok(Self::Cmd(program, args))
|
||||
}
|
||||
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::Zellij(Some((
|
||||
pane_id_str,
|
||||
pane_id,
|
||||
exercise_ind,
|
||||
))));
|
||||
}
|
||||
} else {
|
||||
zellij::close_pane(&pane_id_str)?;
|
||||
}
|
||||
.spawn(move || {
|
||||
match &mut self {
|
||||
Editor::VSCode => {
|
||||
run_cmd(Command::new("code").arg(exercise_path))?;
|
||||
}
|
||||
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 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")?;
|
||||
let (pane_id_str, pane_id) = zellij::parse_pane_id(&stdout)
|
||||
.context("Failed to parse the ID of the new Zellij pane")?;
|
||||
|
||||
Ok(Self::Zellij(Some((pane_id_str, pane_id, exercise_ind))))
|
||||
*open_pane = Some((pane_id_str, pane_id, exercise_ind));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(self)
|
||||
})
|
||||
.context("Failed to spawn a thread to open the editor")?;
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user