Skip to content

Commit dace3e3

Browse files
committed
Add run_cmd
1 parent c9ccedc commit dace3e3

2 files changed

Lines changed: 47 additions & 61 deletions

File tree

src/editor.rs

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,25 @@ use anyhow::{Context, Result, bail};
88

99
mod zellij;
1010

11+
fn run_cmd(cmd: &mut Command) -> Result<Vec<u8>> {
12+
let output = cmd
13+
.stdin(Stdio::null())
14+
.output()
15+
.with_context(|| format!("Failed to run the command {cmd:?}"))?;
16+
17+
if !output.status.success() {
18+
bail!(
19+
"The command {cmd:?} didn't run successfully\n\n\
20+
stdout:\n{}\n\n\
21+
stderr:\n{}",
22+
str::from_utf8(&output.stdout).unwrap_or_default(),
23+
str::from_utf8(&output.stderr).unwrap_or_default(),
24+
);
25+
}
26+
27+
Ok(output.stdout)
28+
}
29+
1130
pub enum Editor {
1231
VSCode,
1332
Cmd(String, Vec<String>),
@@ -39,32 +58,12 @@ impl Editor {
3958
let handle = thread::Builder::new()
4059
.spawn(move || match self {
4160
Editor::VSCode => {
42-
if !Command::new("code")
43-
.arg(exercise_path)
44-
.stdin(Stdio::null())
45-
.stdout(Stdio::null())
46-
.stderr(Stdio::null())
47-
.status()
48-
.context("Failed to run `code` to open the current exercise file")?
49-
.success()
50-
{
51-
bail!("Failed to run `code PATH` to open the current exercise file");
52-
}
61+
run_cmd(Command::new("code").arg(exercise_path))?;
5362

5463
Ok(Self::VSCode)
5564
}
5665
Editor::Cmd(program, args) => {
57-
if !Command::new("code")
58-
.arg(exercise_path)
59-
.stdin(Stdio::null())
60-
.stdout(Stdio::null())
61-
.stderr(Stdio::null())
62-
.status()
63-
.context("Failed to run the command from `--edit-cmd`")
64-
.is_ok_and(|status| status.success())
65-
{
66-
bail!("Failed to run the command from `--edit-cmd`");
67-
}
66+
run_cmd(Command::new(&program).args(&args).arg(exercise_path))?;
6867

6968
Ok(Self::Cmd(program, args))
7069
}
@@ -83,20 +82,14 @@ impl Editor {
8382
}
8483
}
8584

86-
let output = Command::new("zellij")
87-
.arg("action")
88-
.arg("edit")
89-
.arg(exercise_path)
90-
.stdin(Stdio::null())
91-
.stderr(Stdio::null())
92-
.output()
93-
.context("Failed to run `zellij`")?;
94-
95-
if !output.status.success() {
96-
bail!("Failed to open a new Zellij editor pane");
97-
}
85+
let stdout = run_cmd(
86+
Command::new("zellij")
87+
.arg("action")
88+
.arg("edit")
89+
.arg(exercise_path),
90+
)?;
9891

99-
let (pane_id_str, pane_id) = zellij::parse_pane_id(&output.stdout)
92+
let (pane_id_str, pane_id) = zellij::parse_pane_id(&stdout)
10093
.context("Failed to parse the ID of the new Zellij pane")?;
10194

10295
Ok(Self::Zellij(Some((pane_id_str, pane_id, exercise_ind))))

src/editor/zellij.rs

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
use std::process::{Command, Stdio};
1+
use std::process::Command;
22

3-
use anyhow::{Context, Result, bail};
3+
use anyhow::{Context, Result};
44
use serde::Deserialize;
55

6+
use crate::editor::run_cmd;
7+
68
#[derive(Deserialize)]
79
struct Pane {
810
id: u32,
@@ -24,39 +26,30 @@ pub fn parse_pane_id(b: &[u8]) -> Option<(String, u32)> {
2426
}
2527

2628
pub fn pane_open(pane_id: u32) -> Result<bool> {
27-
let mut output = Command::new("zellij")
28-
.arg("action")
29-
.arg("list-panes")
30-
.arg("-j")
31-
.stdin(Stdio::null())
32-
.stderr(Stdio::null())
33-
.output()
34-
.context("Failed to run `zellij action list-panes -j`")?;
35-
36-
if !output.status.success() {
37-
bail!("`zellij action list-panes -j` didn't exit successfully");
38-
}
29+
let mut stdout = run_cmd(
30+
Command::new("zellij")
31+
.arg("action")
32+
.arg("list-panes")
33+
.arg("-j"),
34+
)?;
3935

4036
// Remove newline
41-
output.stdout.pop();
37+
stdout.pop();
4238

43-
let panes = serde_json::de::from_slice::<Vec<Pane>>(&output.stdout)
39+
let panes = serde_json::de::from_slice::<Vec<Pane>>(&stdout)
4440
.context("Failed to parse the output of `zellij action list-panes -j`")?;
4541

4642
Ok(panes.iter().any(|pane| pane.id == pane_id))
4743
}
4844

4945
pub fn close_pane(pane_id: &str) -> Result<()> {
50-
Command::new("zellij")
51-
.arg("action")
52-
.arg("close-pane")
53-
.arg("-p")
54-
.arg(pane_id)
55-
.stdin(Stdio::null())
56-
.stdout(Stdio::null())
57-
.stderr(Stdio::null())
58-
.status()
59-
.context("Failed to run `zellij action close-pane -p ID`")?;
46+
run_cmd(
47+
Command::new("zellij")
48+
.arg("action")
49+
.arg("close-pane")
50+
.arg("-p")
51+
.arg(pane_id),
52+
)?;
6053

6154
Ok(())
6255
}

0 commit comments

Comments
 (0)