Simplify Editor::open

This commit is contained in:
mo8it 2026-04-06 17:27:35 +02:00
parent b0dc014040
commit bc0b4e9f9a

View File

@ -58,49 +58,45 @@ impl Editor {
} }
pub fn open( pub fn open(
self, mut self,
exercise_ind: usize, exercise_ind: usize,
exercise_path: &'static str, exercise_path: &'static str,
) -> Result<EditorJoinHandle> { ) -> Result<EditorJoinHandle> {
let handle = thread::Builder::new() let handle = thread::Builder::new()
.spawn(move || match self { .spawn(move || {
Editor::VSCode => { match &mut self {
run_cmd(Command::new("code").arg(exercise_path))?; 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)?;
}
} }
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( let stdout = run_cmd(
Command::new("zellij") Command::new("zellij")
.arg("action") .arg("action")
.arg("edit") .arg("edit")
.arg(exercise_path), .arg(exercise_path),
)?; )?;
let (pane_id_str, pane_id) = zellij::parse_pane_id(&stdout) let (pane_id_str, pane_id) = zellij::parse_pane_id(&stdout)
.context("Failed to parse the ID of the new Zellij pane")?; .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")?; .context("Failed to spawn a thread to open the editor")?;