11use std:: {
2+ borrow:: Cow ,
23 env,
34 process:: { Command , Stdio } ,
45 thread:: { self , JoinHandle } ,
@@ -28,16 +29,29 @@ fn run_cmd(cmd: &mut Command) -> Result<Vec<u8>> {
2829 Ok ( output. stdout )
2930}
3031
32+ fn program_exists ( program : & str ) -> bool {
33+ Command :: new ( program)
34+ . arg ( "--version" )
35+ . stdin ( Stdio :: null ( ) )
36+ . stdout ( Stdio :: null ( ) )
37+ . stderr ( Stdio :: null ( ) )
38+ . status ( )
39+ . is_ok_and ( |status| status. success ( ) )
40+ }
41+
3142pub enum Editor {
32- VSCode ,
33- Cmd ( String , Vec < String > ) ,
43+ Cmd ( Cow < ' static , str > , Vec < String > ) ,
3444 Zellij ( Option < ( String , u32 , usize ) > ) ,
3545}
3646
3747impl Editor {
38- pub fn new ( cmd : Option < String > ) -> Result < Option < Self > > {
39- if env:: var_os ( "TERM_PROGRAM" ) . is_some_and ( |v| v == "vscode" ) {
40- return Ok ( Some ( Self :: VSCode ) ) ;
48+ pub fn new ( cmd : Option < String > , vs_code_term : bool ) -> Result < Option < Self > > {
49+ if vs_code_term {
50+ for program in [ "code" , "codium" ] {
51+ if program_exists ( program) {
52+ return Ok ( Some ( Self :: Cmd ( Cow :: Borrowed ( program) , Vec :: new ( ) ) ) ) ;
53+ }
54+ }
4155 }
4256
4357 if let Some ( cmd) = cmd {
@@ -47,10 +61,10 @@ impl Editor {
4761 if shlex. had_error {
4862 bail ! ( "Failed to parse the command in `--edit-cmd`" ) ;
4963 }
50- return Ok ( Some ( Self :: Cmd ( program, args) ) ) ;
64+ return Ok ( Some ( Self :: Cmd ( Cow :: Owned ( program) , args) ) ) ;
5165 }
5266
53- if env:: var_os ( "ZELLIJ" ) . is_some ( ) {
67+ if env:: var_os ( "ZELLIJ" ) . is_some ( ) && program_exists ( "zellij" ) {
5468 return Ok ( Some ( Self :: Zellij ( None ) ) ) ;
5569 }
5670
@@ -65,11 +79,8 @@ impl Editor {
6579 let handle = thread:: Builder :: new ( )
6680 . spawn ( move || {
6781 match & mut self {
68- Editor :: VSCode => {
69- run_cmd ( Command :: new ( "code" ) . arg ( exercise_path) ) ?;
70- }
7182 Editor :: Cmd ( program, args) => {
72- run_cmd ( Command :: new ( program) . args ( args) . arg ( exercise_path) ) ?;
83+ run_cmd ( Command :: new ( & * * program) . args ( args) . arg ( exercise_path) ) ?;
7384 }
7485 Editor :: Zellij ( open_pane) => {
7586 if let Some ( ( pane_id_str, pane_id, open_exercise_ind) ) = open_pane {
@@ -105,7 +116,7 @@ impl Editor {
105116
106117 pub fn close ( & mut self ) -> Result < ( ) > {
107118 match self {
108- Editor :: VSCode | Editor :: Cmd ( _, _) => ( ) ,
119+ Editor :: Cmd ( _, _) => ( ) ,
109120 Editor :: Zellij ( open_pane) => {
110121 if let Some ( ( pane_id_str, _, _) ) = open_pane. take ( ) {
111122 zellij:: close_pane ( & pane_id_str) ?;
0 commit comments