Skip to content

Commit b486630

Browse files
committed
Add shlex
1 parent dace3e3 commit b486630

4 files changed

Lines changed: 22 additions & 6 deletions

File tree

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ notify = "8"
5252
rustlings-macros = { path = "rustlings-macros", version = "=6.5.0" }
5353
serde_json = "1"
5454
serde.workspace = true
55+
shlex = "1"
5556
toml.workspace = true
5657

5758
[target.'cfg(not(windows))'.dependencies]

src/editor.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::{
55
};
66

77
use anyhow::{Context, Result, bail};
8+
use shlex::Shlex;
89

910
mod zellij;
1011

@@ -34,20 +35,26 @@ pub enum Editor {
3435
}
3536

3637
impl Editor {
37-
pub fn new(cmd: Option<String>) -> Option<Self> {
38+
pub fn new(cmd: Option<String>) -> Result<Option<Self>> {
3839
if env::var_os("TERM_PROGRAM").is_some_and(|v| v == "vscode") {
39-
return Some(Self::VSCode);
40+
return Ok(Some(Self::VSCode));
4041
}
4142

4243
if let Some(cmd) = cmd {
43-
todo!()
44+
let shlex = &mut Shlex::new(&cmd);
45+
let program = shlex.next().context("Program missing in `--edit-cmd`")?;
46+
let args = shlex.collect();
47+
if shlex.had_error {
48+
bail!("Failed to parse the command in `--edit-cmd`");
49+
}
50+
return Ok(Some(Self::Cmd(program, args)));
4451
}
4552

4653
if env::var_os("ZELLIJ").is_some() {
47-
return Some(Self::Zellij(None));
54+
return Ok(Some(Self::Zellij(None)));
4855
}
4956

50-
None
57+
Ok(None)
5158
}
5259

5360
pub fn open(

src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,11 @@ fn main() -> Result<ExitCode> {
6060
bail!(FORMAT_VERSION_HIGHER_ERR);
6161
}
6262

63+
let editor = Editor::new(args.edit_cmd)?;
6364
let (mut app_state, state_file_status) = AppState::new(
6465
info_file.exercises,
6566
info_file.final_message.unwrap_or_default(),
66-
Editor::new(args.edit_cmd),
67+
editor,
6768
)?;
6869

6970
// Show the welcome message if the state file doesn't exist yet.

0 commit comments

Comments
 (0)