Simplify Editor::open

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

View File

@ -58,34 +58,27 @@ 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 || {
match &mut self {
Editor::VSCode => { Editor::VSCode => {
run_cmd(Command::new("code").arg(exercise_path))?; run_cmd(Command::new("code").arg(exercise_path))?;
Ok(Self::VSCode)
} }
Editor::Cmd(program, args) => { Editor::Cmd(program, args) => {
run_cmd(Command::new(&program).args(&args).arg(exercise_path))?; run_cmd(Command::new(program).args(args).arg(exercise_path))?;
Ok(Self::Cmd(program, args))
} }
Editor::Zellij(open_pane) => { Editor::Zellij(open_pane) => {
if let Some((pane_id_str, pane_id, open_exercise_ind)) = open_pane { if let Some((pane_id_str, pane_id, open_exercise_ind)) = open_pane {
if open_exercise_ind == exercise_ind { if *open_exercise_ind == exercise_ind {
if zellij::pane_open(pane_id)? { if zellij::pane_open(*pane_id)? {
return Ok(Self::Zellij(Some(( return Ok(self);
pane_id_str,
pane_id,
exercise_ind,
))));
} }
} else { } else {
zellij::close_pane(&pane_id_str)?; zellij::close_pane(pane_id_str)?;
} }
} }
@ -99,8 +92,11 @@ impl Editor {
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")?;