@@ -8,6 +8,25 @@ use anyhow::{Context, Result, bail};
88
99mod 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+
1130pub 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) ) ) )
0 commit comments