From bc0b4e9f9a716e577e80667b2b1521b55724d71b Mon Sep 17 00:00:00 2001 From: mo8it Date: Mon, 6 Apr 2026 17:27:35 +0200 Subject: [PATCH] Simplify Editor::open --- src/editor.rs | 64 ++++++++++++++++++++++++--------------------------- 1 file changed, 30 insertions(+), 34 deletions(-) diff --git a/src/editor.rs b/src/editor.rs index 6e712605..3f36e266 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -58,49 +58,45 @@ impl Editor { } pub fn open( - self, + mut self, exercise_ind: usize, exercise_path: &'static str, ) -> Result { 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")?;